From 022a2eb0990762096b38fb87b05dbd0fdb368bf3 Mon Sep 17 00:00:00 2001 From: Gabriel Bota Date: Wed, 1 Dec 2021 09:21:29 +0100 Subject: [PATCH] fixup! fixup! fixup! loader: return package format from defaultResolve if known --- test/es-module/test-esm-resolve-type.js | 39 ++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/test/es-module/test-esm-resolve-type.js b/test/es-module/test-esm-resolve-type.js index c95ef610ea61c0..a7b9f39f3065d6 100644 --- a/test/es-module/test-esm-resolve-type.js +++ b/test/es-module/test-esm-resolve-type.js @@ -96,6 +96,42 @@ try { }; // 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 nmDir = rel('node_modules'); @@ -114,7 +150,7 @@ try { const mainPkgJsonContent = { type: 'commonjs', - main: 'lib/mainfile.js', + main: 'lib/index.js', exports: { '.': { 'require': './lib/index.js', @@ -140,6 +176,7 @@ try { // test the resolve const resolveResult = resolve(`${moduleName}`); assert.strictEqual(resolveResult.format, 'module'); + assert.ok(resolveResult.url.includes('my-dual-package/es/index.js')); // cleanup fs.unlinkSync(esScript);