forked from nodejs/node
/
test-esm-resolve-type.js
270 lines (242 loc) · 8.81 KB
/
test-esm-resolve-type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
// Flags: --expose-internals
/**
* This test ensures defaultResolve returns the found module format in the
* return object in the form:
* { url: <url_value>, 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);
}
};
function testDualPackageWithJsMainScriptAndModuleType() {
// 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 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'));
}
function testDualPackageWithMjsMainScriptAndCJSType() {
// Additional test for following scenario
/**
* this creates following directory structure:
*
* ./node_modules:
* |-> dual-mjs-pjson
* |-> subdir
* |-> index.mjs [3]
* |-> package.json [2]
* |-> index.js
* |->package.json [1]
*
* [1] - main package.json of the package
* - it contains:
* - type: 'commonjs'
* - main: 'subdir/index.js'
* - conditional exports for 'require' (subdir/index.js) and
* 'import' (subdir/index.mjs)
* [2] - package.json add-on for the import case
* - it only contains:
* - type: 'commonjs'
* [3] - main script for the `import` case
*
* in case the package is consumed as an ESM by importing it:
* import * as my-package from 'dual-mjs-pjson'
* it will cause the resolve method to return:
* {
* url: '<base_path>/node_modules/dual-mjs-pjson/subdir/index.mjs',
* 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 the file extension of [3]
*/
const moduleName = 'dual-mjs-pjson';
const mDir = rel(`node_modules/${moduleName}`);
const subDir = rel(`node_modules/${moduleName}/subdir`);
const pkg = rel(`node_modules/${moduleName}/package.json`);
const subdirPkg = rel(`node_modules/${moduleName}/subdir/package.json`);
const esScript = rel(`node_modules/${moduleName}/subdir/index.mjs`);
const cjsScript = rel(`node_modules/${moduleName}/subdir/index.js`);
createDir(nmDir);
createDir(mDir);
createDir(subDir);
const mainPkgJsonContent = {
type: 'commonjs',
main: 'lib/index.js',
exports: {
'.': {
'require': './subdir/index.js',
'import': './subdir/index.mjs'
},
'./package.json': './package.json',
}
};
const subdirPkgJsonContent = {
type: 'commonjs'
};
fs.writeFileSync(pkg, JSON.stringify(mainPkgJsonContent));
fs.writeFileSync(subdirPkg, JSON.stringify(subdirPkgJsonContent));
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(`${moduleName}/subdir/index.mjs`));
}
testDualPackageWithJsMainScriptAndModuleType();
testDualPackageWithMjsMainScriptAndCJSType();
} finally {
process.chdir(previousCwd);
fs.rmSync(nmDir, { recursive: true, force: true });
}