From bb89b499e803d911dfc6e1948e69988d509affe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rouven=20We=C3=9Fling?= Date: Fri, 27 Nov 2020 17:16:12 +0100 Subject: [PATCH] Generate CommonJS build for Node.js --- package.json | 2 ++ rollup.config.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f9d12041..f36bf782 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "0.0.0-semantically-released", "description": "Validator of HTTP transactions (JavaScript implementation)", "main": "build/index.js", + "unpkg": "build/index.umd.js", + "jsdelivr": "build/index.umd.js", "typings": "typings.d.ts", "engines": { "node": ">= 10.18" diff --git a/rollup.config.js b/rollup.config.js index 3782963c..63e48ea1 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,10 +5,12 @@ const { terser } = require('rollup-plugin-terser'); const packageJson = require('./package.json'); +const dependencies = Object.keys(packageJson.dependencies); + const buildUmd = { input: 'lib/index.js', output: { - file: packageJson.main, + file: packageJson.unpkg, format: 'umd', name: 'gavel', exports: 'named', @@ -28,4 +30,41 @@ const buildUmd = { ] }; -module.exports = [buildUmd]; +const buildCjs = { + input: 'lib/index.js', + output: { + file: packageJson.main, + format: 'cjs', + exports: 'named' + }, + external: (id) => { + if (dependencies.includes(id)) { + return true; + } + + // url is a built-in module and should not be bundled either + if (id === 'url') { + return true; + } + + // There are some deep imports of ajv files + if (id.startsWith('ajv/')) { + return true; + } + + return false; + }, + plugins: [ + resolve({ + browser: false, + + // Forbid bundling of NodeJS built-ins (i.e. "fs", "path"). + // Throw when such modules are present in the bundle. + preferBuiltins: false + }), + json(), + commonjs() + ] +}; + +module.exports = [buildUmd, buildCjs];