diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 635efaa459d76f..30cc2f6399a01d 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -487,6 +487,23 @@ const patternRegEx = /\*/g; function resolvePackageTargetString( target, subpath, match, packageJSONUrl, base, pattern, internal, conditions) { + + const composeResult = (resolved) => { + let format; + try { + format = getPackageType(resolved); + } catch (err) { + if (err.code === 'ERR_INVALID_FILE_URL_PATH') { + const invalidModuleErr = new ERR_INVALID_MODULE_SPECIFIER( + resolved, 'must not include encoded "/" or "\\" characters', base); + invalidModuleErr.cause = err; + throw invalidModuleErr; + } + throw err; + } + return { resolved, ...(format !== 'none') && { format } }; + }; + if (subpath !== '' && !pattern && target[target.length - 1] !== '/') throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); @@ -502,7 +519,8 @@ function resolvePackageTargetString( const exportTarget = pattern ? RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) : target + subpath; - return packageResolve(exportTarget, packageJSONUrl, conditions); + return packageResolve( + exportTarget, packageJSONUrl, conditions); } } throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); @@ -518,15 +536,18 @@ function resolvePackageTargetString( if (!StringPrototypeStartsWith(resolvedPath, packagePath)) throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base); - if (subpath === '') return resolved; + if (subpath === '') return composeResult(resolved); if (RegExpPrototypeTest(invalidSegmentRegEx, subpath)) throwInvalidSubpath(match + subpath, packageJSONUrl, internal, base); - if (pattern) - return new URL(RegExpPrototypeSymbolReplace(patternRegEx, resolved.href, - () => subpath)); - return new URL(subpath, resolved); + if (pattern) { + return composeResult(new URL(RegExpPrototypeSymbolReplace(patternRegEx, + resolved.href, + () => subpath))); + } + + return composeResult(new URL(subpath, resolved)); } /** @@ -552,9 +573,9 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, let lastException; for (let i = 0; i < target.length; i++) { const targetItem = target[i]; - let resolved; + let resolveResult; try { - resolved = resolvePackageTarget( + resolveResult = resolvePackageTarget( packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern, internal, conditions); } catch (e) { @@ -563,13 +584,13 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, continue; throw e; } - if (resolved === undefined) + if (resolveResult === undefined) continue; - if (resolved === null) { + if (resolveResult === null) { lastException = null; continue; } - return resolved; + return resolveResult; } if (lastException === undefined || lastException === null) return lastException; @@ -588,12 +609,12 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, const key = keys[i]; if (key === 'default' || conditions.has(key)) { const conditionalTarget = target[key]; - const resolved = resolvePackageTarget( + const resolveResult = resolvePackageTarget( packageJSONUrl, conditionalTarget, subpath, packageSubpath, base, pattern, internal, conditions); - if (resolved === undefined) + if (resolveResult === undefined) continue; - return resolved; + return resolveResult; } } return undefined; @@ -652,12 +673,15 @@ function packageExportsResolve( !StringPrototypeIncludes(packageSubpath, '*') && !StringPrototypeEndsWith(packageSubpath, '/')) { const target = exports[packageSubpath]; - const resolved = resolvePackageTarget( + const resolveResult = resolvePackageTarget( packageJSONUrl, target, '', packageSubpath, base, false, false, conditions ); - if (resolved === null || resolved === undefined) + + if (resolveResult == null) { throwExportsNotFound(packageSubpath, packageJSONUrl, base); - return { resolved, exact: true }; + } + + return { ...resolveResult, exact: true }; } let bestMatch = ''; @@ -693,14 +717,25 @@ function packageExportsResolve( if (bestMatch) { const target = exports[bestMatch]; const pattern = StringPrototypeIncludes(bestMatch, '*'); - const resolved = resolvePackageTarget(packageJSONUrl, target, - bestMatchSubpath, bestMatch, base, - pattern, false, conditions); - if (resolved === null || resolved === undefined) + const resolveResult = resolvePackageTarget( + packageJSONUrl, + target, + bestMatchSubpath, + bestMatch, + base, + pattern, + false, + conditions); + + if (resolveResult == null) { throwExportsNotFound(packageSubpath, packageJSONUrl, base); - if (!pattern) + } + + if (!pattern) { emitFolderMapDeprecation(bestMatch, packageJSONUrl, true, base); - return { resolved, exact: pattern }; + } + + return { ...resolveResult, exact: pattern }; } throwExportsNotFound(packageSubpath, packageJSONUrl, base); @@ -740,11 +775,12 @@ function packageImportsResolve(name, base, conditions) { if (ObjectPrototypeHasOwnProperty(imports, name) && !StringPrototypeIncludes(name, '*') && !StringPrototypeEndsWith(name, '/')) { - const resolved = resolvePackageTarget( + const resolveResult = resolvePackageTarget( packageJSONUrl, imports[name], '', name, base, false, true, conditions ); - if (resolved !== null) - return { resolved, exact: true }; + if (resolveResult != null) { + return { resolved: resolveResult.resolved, exact: true }; + } } else { let bestMatch = ''; let bestMatchSubpath; @@ -776,14 +812,15 @@ function packageImportsResolve(name, base, conditions) { if (bestMatch) { const target = imports[bestMatch]; const pattern = StringPrototypeIncludes(bestMatch, '*'); - const resolved = resolvePackageTarget(packageJSONUrl, target, - bestMatchSubpath, bestMatch, - base, pattern, true, - conditions); - if (resolved !== null) { + const resolveResult = resolvePackageTarget( + packageJSONUrl, target, + bestMatchSubpath, bestMatch, + base, pattern, true, + conditions); + if (resolveResult !== null) { if (!pattern) emitFolderMapDeprecation(bestMatch, packageJSONUrl, false, base); - return { resolved, exact: pattern }; + return { resolved: resolveResult.resolved, exact: pattern }; } } } @@ -843,7 +880,7 @@ function parsePackageName(specifier, base) { * @param {string} specifier * @param {string | URL | undefined} base * @param {Set} conditions - * @returns {URL} + * @returns {resolved: URL, format? : string} */ function packageResolve(specifier, base, conditions) { if (NativeModule.canBeRequiredByUsers(specifier)) @@ -859,8 +896,7 @@ function packageResolve(specifier, base, conditions) { if (packageConfig.name === packageName && packageConfig.exports !== undefined && packageConfig.exports !== null) { return packageExportsResolve( - packageJSONUrl, packageSubpath, packageConfig, base, conditions - ).resolved; + packageJSONUrl, packageSubpath, packageConfig, base, conditions); } } @@ -882,13 +918,26 @@ function packageResolve(specifier, base, conditions) { // Package match. const packageConfig = getPackageConfig(packageJSONPath, specifier, base); - if (packageConfig.exports !== undefined && packageConfig.exports !== null) + if (packageConfig.exports !== undefined && packageConfig.exports !== null) { return packageExportsResolve( - packageJSONUrl, packageSubpath, packageConfig, base, conditions - ).resolved; - if (packageSubpath === '.') - return legacyMainResolve(packageJSONUrl, packageConfig, base); - return new URL(packageSubpath, packageJSONUrl); + packageJSONUrl, packageSubpath, packageConfig, base, conditions); + } + + if (packageSubpath === '.') { + return { + resolved: legacyMainResolve( + packageJSONUrl, + packageConfig, + base), + ...(packageConfig.type !== 'none') && { format: packageConfig.type } + }; + } + + return { + resolved: new URL(packageSubpath, packageJSONUrl), + ...(packageConfig.type !== 'none') && { format: packageConfig.type } + }; + // Cross-platform root check. } while (packageJSONPath.length !== lastPath.length); @@ -932,6 +981,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { // Order swapped from spec for minor perf gain. // Ok since relative URLs cannot parse as URLs. let resolved; + let format; if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { resolved = new URL(specifier, base); } else if (specifier[0] === '#') { @@ -940,12 +990,15 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { try { resolved = new URL(specifier); } catch { - resolved = packageResolve(specifier, base, conditions); + ({ resolved, format } = packageResolve(specifier, base, conditions)); } } if (resolved.protocol !== 'file:') return resolved; - return finalizeResolution(resolved, base, preserveSymlinks); + return { + url: finalizeResolution(resolved, base, preserveSymlinks), + ...(format != null) && { format } + }; } /** @@ -1034,9 +1087,15 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) { conditions = getConditionsSet(conditions); let url; + let format; try { - url = moduleResolve(specifier, parentURL, conditions, - isMain ? preserveSymlinksMain : preserveSymlinks); + ({ url, format } = + moduleResolve( + specifier, + parentURL, + conditions, + isMain ? preserveSymlinksMain : preserveSymlinks + )); } catch (error) { // Try to give the user a hint of what would have been the // resolved CommonJS module @@ -1064,7 +1123,10 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) { url.protocol !== 'node:') throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url); - return { url: `${url}` }; + return { + url: `${url}`, + ...(format != null) && { format } + }; } module.exports = { diff --git a/test/es-module/test-esm-loader-resolve-type.mjs b/test/es-module/test-esm-loader-resolve-type.mjs new file mode 100644 index 00000000000000..f4bab3723d1f46 --- /dev/null +++ b/test/es-module/test-esm-loader-resolve-type.mjs @@ -0,0 +1,41 @@ +// Flags: --loader ./test/fixtures/es-module-loaders/hook-resolve-type.mjs +import { allowGlobals } from '../common/index.mjs'; +import * as fixtures from '../common/fixtures.mjs'; +import { strict as assert } from 'assert'; +import * as fs from 'fs'; + +allowGlobals(global.getModuleTypeStats); + +const basePath = + new URL('./node_modules/', import.meta.url); + +const rel = (file) => new URL(file, basePath); +const createDir = (path) => { + if (!fs.existsSync(path)) { + fs.mkdirSync(path); + } +}; + +const moduleName = 'module-counter-by-type'; + +const moduleDir = rel(`${moduleName}`); +createDir(basePath); +createDir(moduleDir); +fs.cpSync( + fixtures.path('es-modules', moduleName), + moduleDir, + { recursive: true } +); + +const { importedESM: importedESMBefore, + importedCJS: importedCJSBefore } = global.getModuleTypeStats(); + +import(`${moduleName}`).finally(() => { + fs.rmSync(basePath, { recursive: true, force: true }); +}); + +const { importedESM: importedESMAfter, + importedCJS: importedCJSAfter } = global.getModuleTypeStats(); + +assert.strictEqual(importedESMBefore + 1, importedESMAfter); +assert.strictEqual(importedCJSBefore, importedCJSAfter); diff --git a/test/es-module/test-esm-resolve-type.js b/test/es-module/test-esm-resolve-type.js new file mode 100644 index 00000000000000..3cc484188ad513 --- /dev/null +++ b/test/es-module/test-esm-resolve-type.js @@ -0,0 +1,184 @@ +'use strict'; +// Flags: --expose-internals + +/** + * This test ensures defaultResolve returns the found module format in the + * return object in the form: + * { url: , format: <'module'|'commonjs'|undefined> }; + */ + +const common = require('../common'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const path = require('path'); +const fs = require('fs'); +const url = require('url'); + +if (!common.isMainThread) { + common.skip( + 'test-esm-resolve-type.js: process.chdir is not available in Workers' + ); +} + +const assert = require('assert'); +const { + defaultResolve: resolve +} = require('internal/modules/esm/resolve'); + +const rel = (file) => path.join(tmpdir.path, file); +const previousCwd = process.cwd(); +const nmDir = rel('node_modules'); +try { + tmpdir.refresh(); + process.chdir(tmpdir.path); + /** + * ensure that resolving by full path does not return the format + * with the defaultResolver + */ + [ [ '/es-modules/package-type-module/index.js', undefined ], + [ '/es-modules/package-type-commonjs/index.js', undefined ], + [ '/es-modules/package-without-type/index.js', undefined ], + [ '/es-modules/package-without-pjson/index.js', undefined ], + ].forEach((testVariant) => { + const [ testScript, expectedType ] = testVariant; + const resolvedPath = path.resolve(fixtures.path(testScript)); + const resolveResult = resolve(url.pathToFileURL(resolvedPath)); + assert.strictEqual(resolveResult.format, expectedType); + }); + + /** + * create a test module and try to resolve it by module name. + * check the result is as expected + */ + + [ [ 'test-module-mainjs', 'js', 'module', 'module'], + [ 'test-module-mainmjs', 'mjs', 'module', 'module'], + [ 'test-module-cjs', 'js', 'commonjs', 'commonjs'], + [ 'test-module-ne', 'js', undefined, undefined], + ].forEach((testVariant) => { + const [ moduleName, + moduleExtenstion, + moduleType, + expectedResolvedType ] = testVariant; + process.chdir(previousCwd); + tmpdir.refresh(); + process.chdir(tmpdir.path); + const createDir = (path) => { + if (!fs.existsSync(path)) { + fs.mkdirSync(path); + } + }; + + const mDir = rel(`node_modules/${moduleName}`); + const subDir = rel(`node_modules/${moduleName}/subdir`); + const pkg = rel(`node_modules/${moduleName}/package.json`); + const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtenstion}`); + + createDir(nmDir); + createDir(mDir); + createDir(subDir); + const pkgJsonContent = { + ...(moduleType !== undefined) && { type: moduleType }, + main: `subdir/mainfile.${moduleExtenstion}` + }; + fs.writeFileSync(pkg, JSON.stringify(pkgJsonContent)); + fs.writeFileSync(script, + 'export function esm-resolve-tester() {return 42}'); + + const resolveResult = resolve(`${moduleName}`); + assert.strictEqual(resolveResult.format, expectedResolvedType); + + fs.rmSync(nmDir, { recursive: true, force: true }); + }); + + // Helpers + const createDir = (path) => { + if (!fs.existsSync(path)) { + fs.mkdirSync(path); + } + }; + + // Create a dummy dual package + // + /** + * this creates following directory structure: + * + * ./node_modules: + * |-> my-dual-package + * |-> es + * |-> index.js + * |-> package.json [2] + * |-> lib + * |-> index.js + * |->package.json [1] + * + * [1] - main package.json of the package + * - it contains: + * - type: 'commonjs' + * - main: 'lib/mainfile.js' + * - conditional exports for 'require' (lib/index.js) and + * 'import' (es/index.js) + * [2] - package.json add-on for the import case + * - it only contains: + * - type: 'module' + * + * in case the package is consumed as an ESM by importing it: + * import * as my-package from 'my-dual-package' + * it will cause the resolve method to return: + * { + * url: '/node_modules/my-dual-package/es/index.js', + * format: 'module' + * } + * + * following testcase ensures that resolve works correctly in this case + * returning the information as specified above. Source for 'url' value + * is [1], source for 'format' value is [2] + */ + + const moduleName = 'my-dual-package'; + + const mDir = rel(`node_modules/${moduleName}`); + const esSubDir = rel(`node_modules/${moduleName}/es`); + const cjsSubDir = rel(`node_modules/${moduleName}/lib`); + const pkg = rel(`node_modules/${moduleName}/package.json`); + const esmPkg = rel(`node_modules/${moduleName}/es/package.json`); + const esScript = rel(`node_modules/${moduleName}/es/index.js`); + const cjsScript = rel(`node_modules/${moduleName}/lib/index.js`); + + createDir(nmDir); + createDir(mDir); + createDir(esSubDir); + createDir(cjsSubDir); + + const mainPkgJsonContent = { + type: 'commonjs', + main: 'lib/index.js', + exports: { + '.': { + 'require': './lib/index.js', + 'import': './es/index.js' + }, + './package.json': './package.json', + } + }; + const esmPkgJsonContent = { + type: 'module' + }; + + fs.writeFileSync(pkg, JSON.stringify(mainPkgJsonContent)); + fs.writeFileSync(esmPkg, JSON.stringify(esmPkgJsonContent)); + fs.writeFileSync(esScript, + 'export function esm-resolve-tester() {return 42}'); + fs.writeFileSync(cjsScript, + `module.exports = { + esm-resolve-tester: () => {return 42}}` + ); + + // test the resolve + const resolveResult = resolve(`${moduleName}`); + assert.strictEqual(resolveResult.format, 'module'); + assert.ok(resolveResult.url.includes('my-dual-package/es/index.js')); +} finally { + process.chdir(previousCwd); + fs.rmSync(nmDir, { recursive: true, force: true }); +} diff --git a/test/fixtures/es-module-loaders/hook-resolve-type.mjs b/test/fixtures/es-module-loaders/hook-resolve-type.mjs new file mode 100644 index 00000000000000..48692ba4eec544 --- /dev/null +++ b/test/fixtures/es-module-loaders/hook-resolve-type.mjs @@ -0,0 +1,21 @@ +let importedESM = 0; +let importedCJS = 0; +global.getModuleTypeStats = () => { return {importedESM, importedCJS} }; + +export function load(url, context, next) { + return next(url, context, next); +} + +export function resolve(specifier, context, next) { + const nextResult = next(specifier, context); + const { format } = nextResult; + + if (format === 'module' || specifier.endsWith('.mjs')) { + importedESM++; + } else if (format == null || format === 'commonjs') { + importedCJS++; + } + + return nextResult; +} + diff --git a/test/fixtures/es-modules/module-counter-by-type/index.js b/test/fixtures/es-modules/module-counter-by-type/index.js new file mode 100644 index 00000000000000..901fd8dae0a419 --- /dev/null +++ b/test/fixtures/es-modules/module-counter-by-type/index.js @@ -0,0 +1,3 @@ +let dummy = 42; + +export {dummy}; diff --git a/test/fixtures/es-modules/module-counter-by-type/package.json b/test/fixtures/es-modules/module-counter-by-type/package.json new file mode 100644 index 00000000000000..c60bc2b004572a --- /dev/null +++ b/test/fixtures/es-modules/module-counter-by-type/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "main": "index.js" +} diff --git a/test/fixtures/es-modules/package-without-pjson/index.js b/test/fixtures/es-modules/package-without-pjson/index.js new file mode 100644 index 00000000000000..29560bd838d029 --- /dev/null +++ b/test/fixtures/es-modules/package-without-pjson/index.js @@ -0,0 +1,7 @@ +const identifier = 'package-without-pjson'; + +const common = require('../common'); +common.requireNoPackageJSONAbove(); + +console.log(identifier); +module.exports = identifier;