minified browser bundle

master
Evgeny Poberezkin 2015-12-27 16:43:56 +00:00
parent c6ebc3c26f
commit 2eef59dede
24 changed files with 91 additions and 69 deletions

3
.gitignore vendored
View File

@ -35,4 +35,5 @@ lib/dotjs/*.js
.browser
# Ajv bundle
ajv.bundle.js
ajv.bundle.*
ajv.min.*

View File

@ -100,14 +100,14 @@ __Please note__: every time validation function or `ajv.validate` are called `er
You can require ajv directly from the code you browserify - in this case ajv will be a part of your bundle.
If you need to use ajv in several bundles you can create a separate browserified bundle using `npm run bundle` script (thanks to [siddo420](https://github.com/siddo420)).
If you need to use ajv in several bundles you can create a separate UMD bundle using `npm run bundle` script (thanks to [siddo420](https://github.com/siddo420)).
Then you need to load ajv in the browser:
```
<script src="ajv.bundle.js"></script>
<script src="ajv.min.js"></script>
```
Now you can use it as shown above - `require` will be global and you can `require('ajv')`.
This bundle can be used with different module systems or creates global `Ajv` if no module system is found.
Ajv was tested with these browsers:

View File

@ -15,7 +15,8 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'.browser/ajv.beautify.js',
'ajv.min.js',
'node_modules/chai/chai.js',
'.browser/*.spec.js'
],

View File

@ -102,7 +102,8 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'.browser/ajv.beautify.js',
'ajv.min.js',
'node_modules/chai/chai.js',
'.browser/*.spec.js'
],

View File

@ -5,7 +5,7 @@ var resolve = require('./resolve')
, equal = require('./equal')
, stableStringify = require('json-stable-stringify');
try { var beautify = require('' + 'js-beautify').js_beautify; } catch(e) {}
var beautify = (function() { try { return require('' + 'js-beautify').js_beautify; } catch(e) {} })();
var validateGenerator = require('../dotjs/validate');

View File

@ -1,16 +1,16 @@
{{## def.setupKeyword:
{{
var $lvl = it.level
, $dataLvl = it.dataLevel
, $schema = it.schema[$keyword]
, $schemaPath = it.schemaPath + '.' + $keyword
, $errSchemaPath = it.errSchemaPath + '/' + $keyword
, $breakOnError = !it.opts.allErrors
, $errorKeyword;
var $lvl = it.level;
var $dataLvl = it.dataLevel;
var $schema = it.schema[$keyword];
var $schemaPath = it.schemaPath + '.' + $keyword;
var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors;
var $errorKeyword;
var $data = 'data' + ($dataLvl || '')
, $valid = 'valid' + $lvl
, $errs = 'errs__' + $lvl;
var $data = 'data' + ($dataLvl || '');
var $valid = 'valid' + $lvl;
var $errs = 'errs__' + $lvl;
}}
#}}
@ -30,8 +30,8 @@
{{## def.setupNextLevel:
{{
var $it = it.util.copy(it)
, $closingBraces = '';
var $it = it.util.copy(it);
var $closingBraces = '';
$it.level++;
}}
#}}

View File

@ -1,7 +1,5 @@
'use strict';
var util = require('./compile/util');
var IDENTIFIER = /^[a-z_$][a-z0-9_$]*$/i;
/**

View File

@ -14,9 +14,9 @@
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "mocha spec/*.spec.js --debug-brk -R spec",
"test-cov": "istanbul cover -x '**/spec/**' node_modules/mocha/bin/_mocha -- spec/*.spec.js -R spec",
"bundle": "browserify -r ./lib/ajv.js:ajv -o ajv.bundle.js",
"bundle": "browserify -r ./lib/ajv.js:ajv -o ajv.bundle.js -s Ajv && uglifyjs ajv.bundle.js -o ajv.min.js -c pure_getters -m --source-map ajv.min.js.map -r Ajv --preamble '/* Ajv JSON-schema validator */'",
"build": "node scripts/compile-dots.js",
"test-browser": "scripts/prepare-tests && karma start --single-run --browsers PhantomJS",
"test-browser": "npm run bundle && scripts/prepare-tests && karma start --single-run --browsers PhantomJS",
"test": "npm run jshint && npm run build && npm run test-cov && npm run test-browser",
"prepublish": "npm run build",
"watch": "watch 'npm run build' ./lib/dot"
@ -60,6 +60,7 @@
"phantomjs": "^1.9.18",
"pre-commit": "^1.1.1",
"require-globify": "^1.3.0",
"uglify-js": "^2.6.1",
"watch": "^0.16.0"
}
}

View File

@ -20,6 +20,8 @@ console.log('\n\nCompiling:');
var FUNCTION_NAME = /function\s+anonymous\s*\(it[^)]*\)\s*{/;
var OUT_EMPTY_STRING = /out\s*\+=\s*'\s*';/g;
var ISTANBUL = /\'(istanbul[^']+)\';/g;
var VARS = ['$errs', '$valid', '$lvl', '$data', '$dataLvl',
'$errorKeyword', '$closingBraces', '$schemaPath'];
files.forEach(function (f) {
var keyword = path.basename(f, '.jst');
@ -30,8 +32,19 @@ files.forEach(function (f) {
.replace(OUT_EMPTY_STRING, '')
.replace(FUNCTION_NAME, 'function generate_' + keyword + '(it, $keyword) {')
.replace(ISTANBUL, '/* $1 */');
VARS.forEach(removeUnusedVar);
code = "'use strict';\nmodule.exports = " + code;
code = beautify(code, { indent_size: 2 }) + '\n';
fs.writeFileSync(targetPath, code);
console.log('compiled', keyword);
function removeUnusedVar(v) {
v = v.replace(/\$/g, '\\$$');
var regexp = new RegExp(v + '[^A-Za-z0-9_$]', 'g');
var count = (code.match(regexp) || []).length;
if (count == 1) {
regexp = new RegExp('var\\s+' + v + '\\s*=[^;]+;|var\\s+' + v + ';');
code = code.replace(regexp, '');
}
}
});

View File

@ -4,8 +4,6 @@ set -e
mkdir -p .browser
browserify -r js-beautify -r ./lib/ajv.js:ajv -o .browser/ajv.beautify.js
find spec -type f -name '*.spec.js' | \
xargs -I {} sh -c \
'export f="{}"; browserify $f -t require-globify -t brfs -x ajv -o $(echo $f | sed -e "s/spec/.browser/");'
'export f="{}"; browserify $f -t require-globify -t brfs -x ajv -u buffer -o $(echo $f | sed -e "s/spec/.browser/");'

1
spec/ajv.js Normal file
View File

@ -0,0 +1 @@
module.exports = typeof window == 'object' ? window.Ajv : require('' + '../lib/ajv');

View File

@ -1,8 +1,8 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should()
var Ajv = require('./ajv')
, should = require('./chai').should()
, stableStringify = require('json-stable-stringify');

View File

@ -1,7 +1,6 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, util = require('../lib/compile/util');
var Ajv = require('./ajv');
module.exports = getAjvInstances;
@ -13,8 +12,8 @@ function getAjvInstances(options, extraOpts) {
function _getAjvInstances(opts, useOpts) {
var optNames = Object.keys(opts);
if (optNames.length) {
opts = util.copy(opts);
var useOpts1 = util.copy(useOpts)
opts = copy(opts);
var useOpts1 = copy(useOpts)
, optName = optNames[0];
useOpts1[optName] = opts[optName];
delete opts[optName];
@ -23,3 +22,10 @@ function _getAjvInstances(opts, useOpts) {
return instances.concat(instances1);
} else return [ Ajv(useOpts) ];
}
function copy(o, to) {
to = to || {};
for (var key in o) to[key] = o[key];
return to;
}

18
spec/ajv_options.js Normal file
View File

@ -0,0 +1,18 @@
'use strict';
var isBrowser = typeof window == 'object';
var fullTest = isBrowser || !process.env.AJV_FAST_TEST;
var options = fullTest
? {
allErrors: true,
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true,
}
: { allErrors: true };
if (fullTest && !isBrowser) options.beautify = true;
module.exports = options;

View File

@ -1,9 +1,8 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should()
, stableStringify = require('json-stable-stringify');
var Ajv = require('./ajv')
, should = require('./chai').should();
describe('compileAsync method', function() {

1
spec/chai.js Normal file
View File

@ -0,0 +1 @@
module.exports = typeof window == 'object' ? window.chai : require('' + 'chai');

View File

@ -1,7 +1,7 @@
'use strict';
var getAjvInstances = require('./ajv_instances')
, should = require('chai').should()
, should = require('./chai').should()
, equal = require('../lib/compile/equal')
, customRules = require('./custom_rules');

View File

@ -1,7 +1,7 @@
'use strict';
var equal = require('../lib/compile/equal')
, should = require('chai').should();
, should = require('./chai').should();
describe('equal', function() {

View File

@ -1,8 +1,8 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should();
var Ajv = require('./ajv')
, should = require('./chai').should();
describe('Validation errors', function () {

View File

@ -1,7 +1,7 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should();
var Ajv = require('./ajv')
, should = require('./chai').should();
describe('issue #50: references with "definitions"', function () {

View File

@ -1,19 +1,10 @@
'use strict';
var jsonSchemaTest = require('json-schema-test')
, getAjvInstances = require('./ajv_instances');
, getAjvInstances = require('./ajv_instances')
, options = require('./ajv_options');
var isBrowser = typeof window == 'object';
var fullTest = isBrowser || !process.env.AJV_FAST_TEST;
var instances = getAjvInstances(fullTest ? {
beautify: true,
allErrors: true,
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true,
} : { allErrors: true });
var instances = getAjvInstances(options);
var remoteRefs = {
// for JSON-Schema-Test-Suite
@ -55,6 +46,7 @@ jsonSchemaTest(instances, {
skip: [
'optional/zeroTerminatedFloats'
],
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator.opts);
},

View File

@ -1,8 +1,8 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should();
var Ajv = require('./ajv')
, should = require('./chai').should()
describe('Ajv Options', function () {

View File

@ -1,8 +1,8 @@
'use strict';
var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')
, should = require('chai').should()
var Ajv = require('./ajv')
, should = require('./chai').should()
, getAjvInstances = require('./ajv_instances');

View File

@ -1,24 +1,16 @@
'use strict';
var jsonSchemaTest = require('json-schema-test')
, getAjvInstances = require('./ajv_instances');
, getAjvInstances = require('./ajv_instances')
, options = require('./ajv_options');
var isBrowser = typeof window == 'object';
var fullTest = isBrowser || !process.env.AJV_FAST_TEST;
var instances = getAjvInstances(fullTest ? {
beautify: true,
allErrors: true,
verbose: true,
format: 'full',
inlineRefs: false,
jsonPointers: true,
} : { allErrors: true }, { v5: true });
var instances = getAjvInstances(options, { v5: true });
jsonSchemaTest(instances, {
description: 'v5 schemas tests of ' + instances.length + ' ajv instances with different options',
suites: testSuites(),
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator.opts);
},