Skip to content

Commit

Permalink
change exports to fix bundles (#1812)
Browse files Browse the repository at this point in the history
* change exports to fix bundles

* use browserify for bundling

* revert exports

* use browserify in bundle script
  • Loading branch information
epoberezkin committed Nov 13, 2021
1 parent 8fccddb commit f68ef8f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"test-codegen": "nyc cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register 'spec/codegen.spec.ts' -R spec",
"test-debug": "npm run test-spec -- --inspect-brk",
"test-cov": "nyc npm run test-spec",
"bundle": "rm -rf bundle && rollup -c",
"rollup": "rm -rf bundle && rollup -c",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv7 ajv7 && node ./scripts/bundle.js 2019 ajv2019 ajv2019 && node ./scripts/bundle.js 2020 ajv2020 ajv2020 && node ./scripts/bundle.js jtd ajvJTD ajvJTD",
"build": "rm -rf dist && tsc && cp -r lib/refs dist && rm dist/refs/json-schema-2019-09/index.ts && rm dist/refs/json-schema-2020-12/index.ts && rm dist/refs/jtd-schema.ts",
"json-tests": "rm -rf spec/_json/*.js && node scripts/jsontests",
"test-karma": "karma start",
Expand Down Expand Up @@ -75,6 +76,7 @@
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^3.8.0",
"ajv-formats": "^3.0.0-rc.0",
"browserify": "^17.0.0",
"chai": "^4.0.1",
"cross-env": "^7.0.2",
"dayjs": "^1.10.4",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function createBundleConfig(sourceFile, outFile, globalName) {
}

export default [
createBundleConfig("ajv", "ajv7", "av7"),
createBundleConfig("ajv", "ajv7", "ajv7"),
createBundleConfig("2019", "ajv2019", "ajv2019"),
createBundleConfig("2020", "ajv2020", "ajv2020"),
createBundleConfig("jtd", "ajvJTD", "ajvJTD"),
Expand Down
48 changes: 48 additions & 0 deletions scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict"

const fs = require("fs")
const path = require("path")
const browserify = require("browserify")
const {minify} = require("terser")

const [sourceFile, outFile, globalName] = process.argv.slice(2)

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

browserify({standalone: globalName})
.require(path.join(__dirname, "../dist", sourceFile), {expose: sourceFile})
.bundle(saveAndMinify)

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

const bundlePath = path.join(bundleDir, outFile)
const opts = {
ecma: 2018,
warnings: true,
compress: {
pure_getters: true,
keep_infinity: true,
unsafe_methods: true,
},
format: {
preamble: `/* ${json.name} ${json.version} (${globalName}): ${json.description} */`,
},
sourceMap: {
filename: outFile + ".min.js",
url: outFile + ".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))
}

0 comments on commit f68ef8f

Please sign in to comment.