Skip to content

Commit

Permalink
fixup! fixup! fixup! loader: return package format from defaultResolv…
Browse files Browse the repository at this point in the history
…e if known
  • Loading branch information
dygabo committed Dec 1, 2021
1 parent 5935065 commit 022a2eb
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion test/es-module/test-esm-resolve-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<base_path>/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');
Expand All @@ -114,7 +150,7 @@ try {

const mainPkgJsonContent = {
type: 'commonjs',
main: 'lib/mainfile.js',
main: 'lib/index.js',
exports: {
'.': {
'require': './lib/index.js',
Expand All @@ -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);
Expand Down

0 comments on commit 022a2eb

Please sign in to comment.