Skip to content

Commit

Permalink
Add support for configuring module resolver based on environment
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Aug 29, 2024
1 parent ae8fe4c commit 1553e61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased:
chores:
- Add support for configuring module resolver based on environment

5.1.1:
date: 2024-08-01
fixed bugs:
Expand Down
28 changes: 25 additions & 3 deletions lib/bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ class Bundle {
*/
this.compress = options.compress;

this.preferBrowserResolver = options.preferBrowserResolver;

// process any list of modules externally required and also accommodate the use of built-ins if needed
_.forEach(options.require, (options, resolve) => {
// allow resolution override where the required module is resolved
// from a different name than the one provided in options
options.resolve && (resolve = options.resolve);
if (this.preferBrowserResolver && options.resolveBrowser) {
resolve = options.resolveBrowser;
}
else if (options.resolve) {
resolve = options.resolve;
}

// set the name using which the module is exported to the one marked as the module name (only in case when
// one is not explicitly provided in options.)
Expand All @@ -88,10 +95,25 @@ class Bundle {
this.bundler.bundle((err, bundle) => {
if (err) { return done(err); }

// Expose only the required node built-ins to be used in vendor modules
// These will be cleared out by sandbox when the bundle is loaded
const safeExposeBuiltIns = `
if (typeof require === 'function') {
globalThis._nodeRequires = {
buffer: require('buffer')
};
// prevent access to node's require function
require = null;
}
`,

bundleString = safeExposeBuiltIns + bundle.toString();

// bail out if compression is disabled
if (!this.compress) { return done(null, bundle.toString()); }
if (!this.compress) { return done(null, bundleString); }

minify(bundle.toString(), {
minify(bundleString, {
compress: {
drop_console: true // discard calls to console.* functions
},
Expand Down
4 changes: 3 additions & 1 deletion npm/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createBundle (options, file, done) {
// @note: we are appending "require=null;" here to avoid access to
// node's require function when running sandbox in worker_threads.
// This does not affect the require function injected by browserify.
fs.writeFile(file, `module.exports=c=>c(null,${JSON.stringify('require=null;' + codeString)})`, next);
fs.writeFile(file, `module.exports=c=>c(null,${JSON.stringify(codeString)})`, next);
},

function (next) {
Expand All @@ -48,10 +48,12 @@ module.exports = function (exit) {
async.parallel([
async.apply(createBundle, _.merge({
compress: true,
preferBrowserResolver: false,
bundler: { browserField: false }
}, options), './.cache/bootcode.js'),
async.apply(createBundle, _.merge({
compress: true,
preferBrowserResolver: true,
bundler: { browserField: true }
}, options), './.cache/bootcode.browser.js')
], function (err) {
Expand Down

0 comments on commit 1553e61

Please sign in to comment.