Skip to content

Commit

Permalink
browser bundle script (6 tests fail in browser), do not browserify co…
Browse files Browse the repository at this point in the history
…de in schema tests
  • Loading branch information
epoberezkin committed Aug 31, 2020
1 parent 6939f90 commit 888029f
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 253 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ node_modules
# Browserified tests
.browser

# bundles
# compiled typescript
dist/

# browser bundles
bundle/

package-lock.json
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ajv",
"description": "Another JSON Schema Validator",
"main": "dist/ajv.min.js",
"main": "bundle/ajv.min.js",
"authors": ["Evgeny Poberezkin"],
"license": "MIT",
"keywords": ["JSON", "schema", "validator"],
Expand Down
31 changes: 0 additions & 31 deletions circle.yml

This file was deleted.

2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function (config) {

// list of files / patterns to load in the browser
files: [
"dist/ajv.min.js",
"bundle/ajv.min.js",
"node_modules/chai/chai.js",
"node_modules/ajv-async/dist/ajv-async.min.js",
"node_modules/bluebird/js/browser/bluebird.core.min.js",
Expand Down
122 changes: 0 additions & 122 deletions karma.sauce.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/compile/validate/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ function funcKeywordCode(cxt: KeywordContext, def: FuncKeywordDefinition) {
return validateErrs
}

function assignValid(await: Code = def.async ? _`await ` : nil): void {
function assignValid(_await: Code = def.async ? _`await ` : nil): void {
const passCxt = it.opts.passContext ? N.this : N.self
const passSchema = !(("compile" in def && !$data) || def.schema === false)
gen.assign(valid, _`${await}${callValidateCode(cxt, validateRef, passCxt, passSchema)}`)
gen.assign(valid, _`${_await}${callValidateCode(cxt, validateRef, passCxt, passSchema)}`)
}

// TODO maybe refactor to gen.ifNot(def.valid ?? valid, repErrs) once dead branches are removed
Expand Down
1 change: 0 additions & 1 deletion lib/vocabularies/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function callValidateCode(
const dataAndSchema = passSchema
? _`${schemaCode}, ${data}, ${it.topSchemaRef}${it.schemaPath}`
: data
// const appendErrPath = it.errorPath.toString() === '""' ? nil : _` + ${it.errorPath}`
const dataPath = _`(${N.dataPath} || '') + ${it.errorPath}` // TODO refactor other places
const args = _`${dataAndSchema}, ${dataPath}, ${it.parentData}, ${it.parentDataProperty}, ${N.rootData}`
return context !== nil ? _`${func}.call(${context}, ${args})` : _`${func}(${args})`
Expand Down
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
],
"scripts": {
"eslint": "eslint lib/{**/,}*.ts spec/{**/,}*.js scripts --ignore-pattern spec/JSON-Schema-Test-Suite",
"lint": "npm run eslint",
"prettier:write": "prettier --write './**/*.{md,json,yaml,js,ts}'",
"prettier:check": "prettier --list-different './**/*.{md,json,yaml,js,ts}'",
"test-spec": "mocha spec/{**/,}*.spec.js -R dot",
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "npm run test-spec -- --inspect-brk",
"test-cov": "nyc npm run test-spec",
"test-ts": "tsc --target ES5 --noImplicitAny --noEmit spec/typescript/index.ts",
"bundle": "del-cli dist && node ./scripts/bundle.js . Ajv pure_getters",
"bundle-beautify": "node ./scripts/bundle.js js-beautify",
"build": "del-cli dist && tsc && cp -r lib/refs dist/refs",
"bundle": "rm -rf bundle && node ./scripts/bundle.js",
"build": "rm -rf dist && tsc && cp -r lib/refs dist/refs",
"test-karma": "karma start",
"test-browser": "del-cli .browser && npm run bundle && scripts/prepare-tests && npm run test-karma",
"test-browser": "rm -rf .browser && npm run bundle && scripts/prepare-tests && npm run test-karma",
"test-all": "npm run test-cov && if-node-version 12 npm run test-browser",
"test": "npm run lint && npm run build && npm run test-cov",
"test": "npm run eslint && npm run build && npm run test-cov",
"prepublish": "npm run build",
"watch": "watch \"npm run build\" ./lib"
},
Expand Down Expand Up @@ -80,7 +77,6 @@
"browserify": "^16.2.0",
"chai": "^4.0.1",
"coveralls": "^3.0.1",
"del-cli": "^3.0.0",
"eslint": "^7.3.1",
"eslint-config-prettier": "^6.11.0",
"gh-pages-generator": "^0.2.3",
Expand All @@ -92,14 +88,13 @@
"karma": "^5.0.0",
"karma-chrome-launcher": "^3.0.0",
"karma-mocha": "^2.0.0",
"karma-sauce-launcher": "^4.1.3",
"lint-staged": "^10.2.11",
"mocha": "^8.0.1",
"nyc": "^15.0.0",
"prettier": "^2.0.5",
"require-globify": "^1.3.0",
"terser": "^5.2.1",
"typescript": "^4.0.0",
"uglify-js": "^3.6.9",
"watch": "^1.0.0"
},
"collective": {
Expand Down
111 changes: 44 additions & 67 deletions scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,46 @@
"use strict"

var fs = require("fs"),
path = require("path"),
browserify = require("browserify"),
uglify = require("uglify-js")

var pkg = process.argv[2],
standalone = process.argv[3],
compress = process.argv[4]

var packageDir = path.join(__dirname, "..")
if (pkg !== ".") packageDir = path.join(packageDir, "node_modules", pkg)

var json = require(path.join(packageDir, "package.json"))

var distDir = path.join(__dirname, "..", "dist")
if (!fs.existsSync(distDir)) fs.mkdirSync(distDir)

var bOpts = {}
if (standalone) bOpts.standalone = standalone

browserify(bOpts)
.require(path.join(packageDir, json.main), {expose: json.name})
.bundle((err, buf) => {
if (err) {
console.error("browserify error:", err)
process.exit(1)
}

var outputFile = path.join(distDir, json.name)
var uglifyOpts = {
warnings: true,
compress: {},
output: {
preamble:
"/* " +
json.name +
" " +
json.version +
": " +
json.description +
" */",
},
}
if (compress) {
var compressOpts = compress.split(",")
for (var i = 0, il = compressOpts.length; i < il; ++i) {
var pair = compressOpts[i].split("=")
uglifyOpts.compress[pair[0]] = pair.length < 1 || pair[1] !== "false"
}
}
if (standalone) {
uglifyOpts.sourceMap = {
filename: json.name + ".min.js",
url: json.name + ".min.js.map",
}
}

var result = uglify.minify(buf.toString(), uglifyOpts)
fs.writeFileSync(outputFile + ".min.js", result.code)
if (result.map) fs.writeFileSync(outputFile + ".min.js.map", result.map)
if (standalone) fs.writeFileSync(outputFile + ".bundle.js", buf)
if (result.warnings) {
for (var j = 0, jl = result.warnings.length; j < jl; ++j) {
console.warn("UglifyJS warning:", result.warnings[j])
}
}
})
const fs = require("fs")
const path = require("path")
const browserify = require("browserify")
const {minify} = require("terser")

const json = require(path.join(__dirname, "..", "package.json"))
const bundleDir = path.join(__dirname, "..", "bundle")
if (!fs.existsSync(bundleDir)) fs.mkdirSync(bundleDir)

browserify({standalone: "Ajv"})
.require(path.join(__dirname, "..", json.main), {expose: json.name})
.bundle(saveAndMinify)

async function saveAndMinify(err, buf) {
if (err) {
console.error("browserify error:", err)
process.exit(1)
}

const bundlePath = path.join(bundleDir, json.name)
const opts = {
ecma: 2018,
warnings: true,
compress: {
pure_getters: true,
keep_infinity: true,
unsafe_methods: true,
},
format: {
preamble: `/* ${json.name} ${json.version}: ${json.description} */`,
},
sourceMap: {
filename: json.name + ".min.js",
url: json.name + ".min.js.map",
},
}

const result = await minify(buf.toString(), opts)

fs.writeFileSync(bundlePath + ".bundle.js", buf)
fs.writeFileSync(bundlePath + ".min.js", result.code)
fs.writeFileSync(bundlePath + ".min.js.map", result.map)
if (result.warnings) result.warnings.forEach((msg) => console.warn("terser.minify warning:", msg))
}
10 changes: 0 additions & 10 deletions scripts/info

This file was deleted.

6 changes: 3 additions & 3 deletions spec/ajv_instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ function getAjvInstances(options, extraOpts) {
function _getAjvInstances(opts, useOpts) {
var optNames = Object.keys(opts)
if (optNames.length) {
opts = {...opts}
var useOpts1 = {...useOpts},
optName = optNames[0]
opts = Object.assign({}, opts)
var useOpts1 = Object.assign({}, useOpts)
var optName = optNames[0]
useOpts1[optName] = opts[optName]
delete opts[optName]
var instances = _getAjvInstances(opts, useOpts),
Expand Down
Loading

0 comments on commit 888029f

Please sign in to comment.