| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9eb1fa1 commit 6ca8fb5
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,6 +105,7 @@ const { | |||
| 105 | 105 | } = require('internal/constants'); | |
| 106 | 106 | ||
| 107 | 107 | const asyncESM = require('internal/process/esm_loader'); | |
| 108 | + const { enrichCJSError } = require('internal/modules/esm/translators'); | ||
| 108 | 109 | const { kEvaluated } = internalBinding('module_wrap'); | |
| 109 | 110 | const { | |
| 110 | 111 | encodedSepRegEx, | |
@@ -119,31 +120,6 @@ const relativeResolveCache = ObjectCreate(null); | |||
| 119 | 120 | let requireDepth = 0; | |
| 120 | 121 | let statCache = null; | |
| 121 | 122 | ||
| 122 | - function enrichCJSError(err) { | ||
| 123 | - const stack = err.stack.split('\n'); | ||
| 124 | - | ||
| 125 | - const lineWithErr = stack[1]; | ||
| 126 | - | ||
| 127 | - /* | ||
| 128 | - The regular expression below targets the most common import statement | ||
| 129 | - usage. However, some cases are not matching, cases like import statement | ||
| 130 | - after a comment block and/or after a variable definition. | ||
| 131 | - */ | ||
| 132 | - if (err.message.startsWith('Unexpected token \'export\'') || | ||
| 133 | - (RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, lineWithErr))) { | ||
| 134 | - // Emit the warning synchronously because we are in the middle of handling | ||
| 135 | - // a SyntaxError that will throw and likely terminate the process before an | ||
| 136 | - // asynchronous warning would be emitted. | ||
| 137 | - process.emitWarning( | ||
| 138 | - 'To load an ES module, set "type": "module" in the package.json or use ' + | ||
| 139 | - 'the .mjs extension.', | ||
| 140 | - undefined, | ||
| 141 | - undefined, | ||
| 142 | - undefined, | ||
| 143 | - true); | ||
| 144 | - } | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | 123 | function stat(filename) { | |
| 148 | 124 | filename = path.toNamespacedPath(filename); | |
| 149 | 125 | if (statCache !== null) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,9 +9,12 @@ const { | |||
| 9 | 9 | ObjectKeys, | |
| 10 | 10 | PromisePrototypeCatch, | |
| 11 | 11 | PromiseReject, | |
| 12 | + RegExpPrototypeTest, | ||
| 12 | 13 | SafeMap, | |
| 13 | 14 | SafeSet, | |
| 14 | 15 | StringPrototypeReplace, | |
| 16 | + StringPrototypeSplit, | ||
| 17 | + StringPrototypeStartsWith, | ||
| 15 | 18 | } = primordials; | |
| 16 | 19 | ||
| 17 | 20 | let _TYPES = null; | |
@@ -57,6 +60,7 @@ const cjsParse = require('internal/deps/cjs-module-lexer/lexer'); | |||
| 57 | 60 | ||
| 58 | 61 | const translators = new SafeMap(); | |
| 59 | 62 | exports.translators = translators; | |
| 63 | + exports.enrichCJSError = enrichCJSError; | ||
| 60 | 64 | ||
| 61 | 65 | let DECODER = null; | |
| 62 | 66 | function assertBufferSource(body, allowString, hookName) { | |
@@ -130,6 +134,29 @@ translators.set('module', async function moduleStrategy(url) { | |||
| 130 | 134 | return module; | |
| 131 | 135 | }); | |
| 132 | 136 | ||
| 137 | + | ||
| 138 | + function enrichCJSError(err) { | ||
| 139 | + const stack = StringPrototypeSplit(err.stack, '\n'); | ||
| 140 | + /* | ||
| 141 | + The regular expression below targets the most common import statement | ||
| 142 | + usage. However, some cases are not matching, cases like import statement | ||
| 143 | + after a comment block and/or after a variable definition. | ||
| 144 | + */ | ||
| 145 | + if (StringPrototypeStartsWith(err.message, 'Unexpected token \'export\'') || | ||
| 146 | + (RegExpPrototypeTest(/^\s*import(?=[ {'"*])\s*(?![ (])/, stack[1]))) { | ||
| 147 | + // Emit the warning synchronously because we are in the middle of handling | ||
| 148 | + // a SyntaxError that will throw and likely terminate the process before an | ||
| 149 | + // asynchronous warning would be emitted. | ||
| 150 | + process.emitWarning( | ||
| 151 | + 'To load an ES module, set "type": "module" in the package.json or use ' + | ||
| 152 | + 'the .mjs extension.', | ||
| 153 | + undefined, | ||
| 154 | + undefined, | ||
| 155 | + undefined, | ||
| 156 | + true); | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 133 | 160 | // Strategy for loading a node-style CommonJS module | |
| 134 | 161 | const isWindows = process.platform === 'win32'; | |
| 135 | 162 | const winSepRegEx = /\//g; | |
@@ -152,7 +179,12 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) { | |||
| 152 | 179 | exports = asyncESM.ESMLoader.cjsCache.get(module); | |
| 153 | 180 | asyncESM.ESMLoader.cjsCache.delete(module); | |
| 154 | 181 | } else { | |
| 155 | - exports = CJSModule._load(filename, undefined, isMain); | ||
| 182 | + try { | ||
| 183 | + exports = CJSModule._load(filename, undefined, isMain); | ||
| 184 | + } catch (err) { | ||
| 185 | + enrichCJSError(err); | ||
| 186 | + throw err; | ||
| 187 | + } | ||
| 156 | 188 | } | |
| 157 | 189 | ||
| 158 | 190 | for (const exportName of exportNames) { | |
@@ -190,7 +222,13 @@ function cjsPreparseModuleExports(filename) { | |||
| 190 | 222 | source = readFileSync(filename, 'utf8'); | |
| 191 | 223 | } catch {} | |
| 192 | 224 | ||
| 193 | - const { exports, reexports } = cjsParse(source || ''); | ||
| 225 | + let exports, reexports; | ||
| 226 | + try { | ||
| 227 | + ({ exports, reexports } = cjsParse(source || '')); | ||
| 228 | + } catch { | ||
| 229 | + exports = []; | ||
| 230 | + reexports = []; | ||
| 231 | + } | ||
| 194 | 232 | ||
| 195 | 233 | const exportNames = new SafeSet(exports); | |
| 196 | 234 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ const assert = require('assert'); | |||
| 7 | 7 | ||
| 8 | 8 | const entry = fixtures.path('/es-modules/cjs-exports.mjs'); | |
| 9 | 9 | ||
| 10 | - const child = spawn(process.execPath, [entry]); | ||
| 10 | + let child = spawn(process.execPath, [entry]); | ||
| 11 | 11 | child.stderr.setEncoding('utf8'); | |
| 12 | 12 | let stdout = ''; | |
| 13 | 13 | child.stdout.setEncoding('utf8'); | |
@@ -19,3 +19,17 @@ child.on('close', common.mustCall((code, signal) => { | |||
| 19 | 19 | assert.strictEqual(signal, null); | |
| 20 | 20 | assert.strictEqual(stdout, 'ok\n'); | |
| 21 | 21 | })); | |
| 22 | + | ||
| 23 | + const entryInvalid = fixtures.path('/es-modules/cjs-exports-invalid.mjs'); | ||
| 24 | + child = spawn(process.execPath, [entryInvalid]); | ||
| 25 | + let stderr = ''; | ||
| 26 | + child.stderr.setEncoding('utf8'); | ||
| 27 | + child.stderr.on('data', (data) => { | ||
| 28 | + stderr += data; | ||
| 29 | + }); | ||
| 30 | + child.on('close', common.mustCall((code, signal) => { | ||
| 31 | + assert.strictEqual(code, 1); | ||
| 32 | + assert.strictEqual(signal, null); | ||
| 33 | + assert.ok(stderr.includes('Warning: To load an ES module')); | ||
| 34 | + assert.ok(stderr.includes('Unexpected token \'export\'')); | ||
| 35 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + import cjs from './invalid-cjs.js'; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + export var name = 5; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments