Skip to content

Commit

Permalink
Allow to override strict mode setting for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
XmiliaH committed Jul 5, 2022
1 parent 6fcb707 commit 67c3fd4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/resolver-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ function makeExternalMatcher(obj) {

class LegacyResolver extends DefaultResolver {

constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, externals, allowTransitive) {
super(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler);
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict, externals, allowTransitive) {
super(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict);
this.externals = externals;
this.currMod = undefined;
this.trustedMods = new WeakMap();
Expand Down Expand Up @@ -282,7 +282,8 @@ function resolverFromOptions(vm, options, override, compiler) {
root: rootPaths,
resolve: customResolver,
customRequire: hostRequire = defaultRequire,
context = 'host'
context = 'host',
strict = true,
} = options;

const builtins = genBuiltinsFromOptions(vm, builtinOpt, mockOpt, override);
Expand Down Expand Up @@ -325,7 +326,7 @@ function resolverFromOptions(vm, options, override, compiler) {
}

if (typeof externalOpt !== 'object') {
return new DefaultResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler);
return new DefaultResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, strict);
}

let transitive = false;
Expand All @@ -336,7 +337,7 @@ function resolverFromOptions(vm, options, override, compiler) {
transitive = context === 'sandbox' && externalOpt.transitive;
}
externals = external.map(makeExternalMatcher);
return new LegacyResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, externals, transitive);
return new LegacyResolver(builtins, checkPath, [], () => context, newCustomResolver, hostRequire, compiler, strict, externals, transitive);
}

exports.resolverFromOptions = resolverFromOptions;
5 changes: 3 additions & 2 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ class Resolver {

class DefaultResolver extends Resolver {

constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler) {
constructor(builtinModules, checkPath, globalPaths, pathContext, customResolver, hostRequire, compiler, strict) {
super(builtinModules, globalPaths, hostRequire);
this.checkPath = checkPath;
this.pathContext = pathContext;
this.customResolver = customResolver;
this.compiler = compiler;
this.strict = strict;
this.packageCache = {__proto__: null};
this.scriptCache = {__proto__: null};
}
Expand Down Expand Up @@ -200,7 +201,7 @@ class DefaultResolver extends Resolver {
this.checkAccess(mod, filename);
if (this.pathContext(filename, 'js') === 'sandbox') {
const script = this.readScript(filename);
vm.run(script, {filename, strict: true, module: mod, wrapper: 'none', dirname: mod.path});
vm.run(script, {filename, strict: this.strict, module: mod, wrapper: 'none', dirname: mod.path});
} else {
const m = this.hostRequire(filename);
mod.exports = vm.readonly(m);
Expand Down

0 comments on commit 67c3fd4

Please sign in to comment.