| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,18 +4,21 @@ const { | |||
| 4 | 4 | ArrayPrototypeJoin, | |
| 5 | 5 | ArrayPrototypeMap, | |
| 6 | 6 | ArrayPrototypePush, | |
| 7 | + ArrayPrototypeSome, | ||
| 7 | 8 | FunctionPrototype, | |
| 8 | 9 | ObjectSetPrototypeOf, | |
| 9 | 10 | PromiseAll, | |
| 10 | 11 | PromiseResolve, | |
| 11 | 12 | PromisePrototypeCatch, | |
| 12 | 13 | ReflectApply, | |
| 14 | + RegExpPrototypeTest, | ||
| 13 | 15 | SafeArrayIterator, | |
| 14 | 16 | SafeSet, | |
| 15 | 17 | StringPrototypeIncludes, | |
| 16 | 18 | StringPrototypeMatch, | |
| 17 | 19 | StringPrototypeReplace, | |
| 18 | 20 | StringPrototypeSplit, | |
| 21 | + StringPrototypeStartsWith, | ||
| 19 | 22 | } = primordials; | |
| 20 | 23 | ||
| 21 | 24 | const { ModuleWrap } = internalBinding('module_wrap'); | |
@@ -28,6 +31,19 @@ const noop = FunctionPrototype; | |||
| 28 | 31 | ||
| 29 | 32 | let hasPausedEntry = false; | |
| 30 | 33 | ||
| 34 | + const CJSGlobalLike = [ | ||
| 35 | + 'require', | ||
| 36 | + 'module', | ||
| 37 | + 'exports', | ||
| 38 | + '__filename', | ||
| 39 | + '__dirname', | ||
| 40 | + ]; | ||
| 41 | + const isCommonJSGlobalLikeNotDefinedError = (errorMessage) => | ||
| 42 | + ArrayPrototypeSome( | ||
| 43 | + CJSGlobalLike, | ||
| 44 | + (globalLike) => errorMessage === `${globalLike} is not defined` | ||
| 45 | + ); | ||
| 46 | + | ||
| 31 | 47 | /* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of | |
| 32 | 48 | * its dependencies, over time. */ | |
| 33 | 49 | class ModuleJob { | |
@@ -155,7 +171,32 @@ class ModuleJob { | |||
| 155 | 171 | await this.instantiate(); | |
| 156 | 172 | const timeout = -1; | |
| 157 | 173 | const breakOnSigint = false; | |
| 158 | - await this.module.evaluate(timeout, breakOnSigint); | ||
| 174 | + try { | ||
| 175 | + await this.module.evaluate(timeout, breakOnSigint); | ||
| 176 | + } catch (e) { | ||
| 177 | + if (e?.name === 'ReferenceError' && | ||
| 178 | + isCommonJSGlobalLikeNotDefinedError(e.message)) { | ||
| 179 | + e.message += ' in ES module scope'; | ||
| 180 | + | ||
| 181 | + if (StringPrototypeStartsWith(e.message, 'require ')) { | ||
| 182 | + e.message += ', you can use import instead'; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + const packageConfig = | ||
| 186 | + StringPrototypeStartsWith(this.module.url, 'file://') && | ||
| 187 | + RegExpPrototypeTest(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) && | ||
| 188 | + require('internal/modules/esm/resolve') | ||
| 189 | + .getPackageScopeConfig(this.module.url); | ||
| 190 | + if (packageConfig.type === 'module') { | ||
| 191 | + e.message += | ||
| 192 | + '\nThis file is being treated as an ES module because it has a ' + | ||
| 193 | + `'.js' file extension and '${packageConfig.pjsonPath}' contains ` + | ||
| 194 | + '"type": "module". To treat it as a CommonJS script, rename it ' + | ||
| 195 | + 'to use the \'.cjs\' file extension.'; | ||
| 196 | + } | ||
| 197 | + } | ||
| 198 | + throw e; | ||
| 199 | + } | ||
| 159 | 200 | return { module: this.module }; | |
| 160 | 201 | } | |
| 161 | 202 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -893,6 +893,7 @@ module.exports = { | |||
| 893 | 893 | DEFAULT_CONDITIONS, | |
| 894 | 894 | defaultResolve, | |
| 895 | 895 | encodedSepRegEx, | |
| 896 | + getPackageScopeConfig, | ||
| 896 | 897 | getPackageType, | |
| 897 | 898 | packageExportsResolve, | |
| 898 | 899 | packageImportsResolve | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const fixtures = require('../common/fixtures'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { pathToFileURL } = require('url'); | ||
| 6 | + | ||
| 7 | + assert.rejects( | ||
| 8 | + import('data:text/javascript,require;'), | ||
| 9 | + /require is not defined in ES module scope, you can use import instead$/ | ||
| 10 | + ).then(common.mustCall()); | ||
| 11 | + assert.rejects( | ||
| 12 | + import('data:text/javascript,exports={};'), | ||
| 13 | + /exports is not defined in ES module scope$/ | ||
| 14 | + ).then(common.mustCall()); | ||
| 15 | + | ||
| 16 | + assert.rejects( | ||
| 17 | + import('data:text/javascript,require_custom;'), | ||
| 18 | + /^(?!in ES module scope)(?!use import instead).*$/ | ||
| 19 | + ).then(common.mustCall()); | ||
| 20 | + | ||
| 21 | + const pkgUrl = pathToFileURL(fixtures.path('/es-modules/package-type-module/')); | ||
| 22 | + assert.rejects( | ||
| 23 | + import(new URL('./cjs.js', pkgUrl)), | ||
| 24 | + /use the '\.cjs' file extension/ | ||
| 25 | + ).then(common.mustCall()); | ||
| 26 | + assert.rejects( | ||
| 27 | + import(new URL('./cjs.js#target', pkgUrl)), | ||
| 28 | + /use the '\.cjs' file extension/ | ||
| 29 | + ).then(common.mustCall()); | ||
| 30 | + assert.rejects( | ||
| 31 | + import(new URL('./cjs.js?foo=bar', pkgUrl)), | ||
| 32 | + /use the '\.cjs' file extension/ | ||
| 33 | + ).then(common.mustCall()); | ||
| 34 | + assert.rejects( | ||
| 35 | + import(new URL('./cjs.js?foo=bar#target', pkgUrl)), | ||
| 36 | + /use the '\.cjs' file extension/ | ||
| 37 | + ).then(common.mustCall()); | ||
| 38 | + | ||
| 39 | + assert.rejects( | ||
| 40 | + import('data:text/javascript,require;//.js'), | ||
| 41 | + /^(?!use the '\.cjs' file extension).*$/ | ||
| 42 | + ).then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments