From db43fc499d84e4b856c38216ac9e57aec4fe440c Mon Sep 17 00:00:00 2001 From: Sean Lang-Brown Date: Fri, 5 Feb 2021 15:44:23 -0800 Subject: [PATCH] remove eval from inquire and update documentation --- lib/inquire/README.md | 6 ++++++ lib/inquire/index.js | 13 ++++++++++++- lib/inquire/package.json | 4 ++-- package.json | 8 ++++++++ scripts/browserify.json | 6 ++++++ scripts/bundle.js | 3 ++- 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 scripts/browserify.json diff --git a/lib/inquire/README.md b/lib/inquire/README.md index 3eabd864f..67a06c732 100644 --- a/lib/inquire/README.md +++ b/lib/inquire/README.md @@ -10,4 +10,10 @@ API * **inquire(moduleName: `string`): `?Object`**
Requires a module only if available. +Browser Bundler Compatibility +--- +Inquire can be used in browser modules if these configurations are set: + 1) If a bundler is used, all modules loaded with inquire() must be added to the bundle config field "externals". This is supported by [webpack](https://webpack.js.org/configuration/externals/), [browserify](https://github.com/browserify/browserify#usage) ([gulp](https://benclinkinbeard.com/posts/external-bundles-for-faster-browserify-builds/)), [rollup](https://rollupjs.org/guide/en/#inputoptions-object), and possibly others. + 2) When used in node/npm packages for distribution (including protobufjs) all modules loaded with inquire() must be [ignored in the package.json browser option](https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module). This is also [supported by webpack, browserify and rollup](https://github.com/webpack/webpack/issues/8826#issuecomment-491081733). + **License:** [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause) diff --git a/lib/inquire/index.js b/lib/inquire/index.js index 259011b17..699bcba77 100644 --- a/lib/inquire/index.js +++ b/lib/inquire/index.js @@ -3,13 +3,24 @@ module.exports = inquire; /** * Requires a module only if available. + * All modules requested with inquire must be set as externals in browserify.json, e.g. + * "externals": [ + * "long", + * "buffer" + * ] + * and added ignored in the package.json browser field, e.g. + * "browser": { + * "long": false, + * "buffer": false + * } + * * @memberof util * @param {string} moduleName Module to require * @returns {?Object} Required module if available and not empty, otherwise `null` */ function inquire(moduleName) { try { - var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval + var mod = require(moduleName); if (mod && (mod.length || Object.keys(mod).length)) return mod; } catch (e) {} // eslint-disable-line no-empty diff --git a/lib/inquire/package.json b/lib/inquire/package.json index 564a99bad..f2095999e 100644 --- a/lib/inquire/package.json +++ b/lib/inquire/package.json @@ -1,6 +1,6 @@ { "name": "@protobufjs/inquire", - "description": "Requires a module only if available and hides the require call from bundlers.", + "description": "Requires a module only if available.", "version": "1.1.0", "author": "Daniel Wirtz ", "repository": { @@ -18,4 +18,4 @@ "test": "tape tests/*.js", "coverage": "istanbul cover node_modules/tape/bin/tape tests/*.js" } -} \ No newline at end of file +} diff --git a/package.json b/package.json index e6f945212..7e96e600e 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,14 @@ ], "main": "index.js", "types": "index.d.ts", + "browser": { + "long": false, + "buffer": false + }, + "bin": { + "pbjs": "bin/pbjs", + "pbts": "bin/pbts" + }, "scripts": { "bench": "node bench", "build": "npm run build:bundle && npm run build:types", diff --git a/scripts/browserify.json b/scripts/browserify.json new file mode 100644 index 000000000..93a326382 --- /dev/null +++ b/scripts/browserify.json @@ -0,0 +1,6 @@ +{ + "externals": [ + "long", + "buffer" + ] +} diff --git a/scripts/bundle.js b/scripts/bundle.js index 42b7d5123..d7d4ad0c5 100644 --- a/scripts/bundle.js +++ b/scripts/bundle.js @@ -16,6 +16,7 @@ var vinylfs = require("vinyl-fs"); var source = require("vinyl-source-stream"); var pkg = require(path.join(__dirname, "..", "package.json")); +var { externals } = require("./browserify.json"); /*eslint-disable no-template-curly-in-string*/ var license = [ @@ -50,7 +51,7 @@ function bundle(options) { prelude: prelude, preludePath: "./lib/prelude.js" }) - .external("long"); + .external(externals); if (options.exclude) options.exclude.forEach(bundler.exclude, bundler); return bundler