| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9916458 commit d1331fc
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,19 @@ function underNodeModules(url) { | |||
| 95 | 95 | } | |
| 96 | 96 | ||
| 97 | 97 | let typelessPackageJsonFilesWarnedAbout; | |
| 98 | + function warnTypelessPackageJsonFile(pjsonPath, url) { | ||
| 99 | + typelessPackageJsonFilesWarnedAbout ??= new SafeSet(); | ||
| 100 | + if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) { | ||
| 101 | + const warning = `Module type of ${url} is not specified and it doesn't parse as CommonJS.\n` + | ||
| 102 | + 'Reparsing as ES module because module syntax was detected. This incurs a performance overhead.\n' + | ||
| 103 | + `To eliminate this warning, add "type": "module" to ${pjsonPath}.`; | ||
| 104 | + process.emitWarning(warning, { | ||
| 105 | + code: 'MODULE_TYPELESS_PACKAGE_JSON', | ||
| 106 | + }); | ||
| 107 | + typelessPackageJsonFilesWarnedAbout.add(pjsonPath); | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + | ||
| 98 | 111 | /** | |
| 99 | 112 | * @param {URL} url | |
| 100 | 113 | * @param {{parentURL: string; source?: Buffer}} context | |
@@ -106,7 +119,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 106 | 119 | const ext = extname(url); | |
| 107 | 120 | ||
| 108 | 121 | if (ext === '.js') { | |
| 109 | - const { type: packageType, pjsonPath } = getPackageScopeConfig(url); | ||
| 122 | + const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url); | ||
| 110 | 123 | if (packageType !== 'none') { | |
| 111 | 124 | return packageType; | |
| 112 | 125 | } | |
@@ -127,18 +140,10 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 127 | 140 | // For ambiguous files (no type field, .js extension) we return | |
| 128 | 141 | // undefined from `resolve` and re-run the check in `load`. | |
| 129 | 142 | const format = detectModuleFormat(source, url); | |
| 130 | - if (format === 'module') { | ||
| 143 | + if (format === 'module' && foundPackageJson) { | ||
| 131 | 144 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. | |
| 132 | 145 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. | |
| 133 | - typelessPackageJsonFilesWarnedAbout ??= new SafeSet(); | ||
| 134 | - if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) { | ||
| 135 | - const warning = `${url} parsed as an ES module because module syntax was detected;` + | ||
| 136 | - ` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`; | ||
| 137 | - process.emitWarning(warning, { | ||
| 138 | - code: 'MODULE_TYPELESS_PACKAGE_JSON', | ||
| 139 | - }); | ||
| 140 | - typelessPackageJsonFilesWarnedAbout.add(pjsonPath); | ||
| 141 | - } | ||
| 146 | + warnTypelessPackageJsonFile(pjsonPath, url); | ||
| 142 | 147 | } | |
| 143 | 148 | return format; | |
| 144 | 149 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -352,6 +352,18 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |||
| 352 | 352 | }); | |
| 353 | 353 | } | |
| 354 | 354 | ||
| 355 | + it('does not warn when there are no package.json', async () => { | ||
| 356 | + const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ | ||
| 357 | + fixtures.path('es-modules/loose.js'), | ||
| 358 | + ]); | ||
| 359 | + | ||
| 360 | + strictEqual(stderr, ''); | ||
| 361 | + strictEqual(stdout, 'executed\n'); | ||
| 362 | + strictEqual(code, 0); | ||
| 363 | + strictEqual(signal, null); | ||
| 364 | + }); | ||
| 365 | + | ||
| 366 | + | ||
| 355 | 367 | it('warns only once for a package.json that affects multiple files', async () => { | |
| 356 | 368 | const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ | |
| 357 | 369 | fixtures.path('es-modules/package-without-type/detected-as-esm.js'), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments