@@ -181,69 +181,49 @@ try {
181
181
assert . ok ( resolveResult . url . includes ( 'my-dual-package/es/index.js' ) ) ;
182
182
}
183
183
184
- function testDualPackageWithMjsMainScriptAndCJSType ( ) {
184
+ testDualPackageWithJsMainScriptAndModuleType ( ) ;
185
185
186
- // Additional test for following scenario
187
- /**
188
- * this creates following directory structure:
189
- *
190
- * ./node_modules:
191
- * |-> dual-mjs-pjson
192
- * |-> subdir
193
- * |-> index.mjs [3]
194
- * |-> package.json [2]
195
- * |-> index.js
196
- * |->package.json [1]
197
- *
198
- * [1] - main package.json of the package
199
- * - it contains:
200
- * - type: 'commonjs'
201
- * - main: 'subdir/index.js'
202
- * - conditional exports for 'require' (subdir/index.js) and
203
- * 'import' (subdir/index.mjs)
204
- * [2] - package.json add-on for the import case
205
- * - it only contains:
206
- * - type: 'commonjs'
207
- * [3] - main script for the `import` case
208
- *
209
- * in case the package is consumed as an ESM by importing it:
210
- * import * as my-package from 'dual-mjs-pjson'
211
- * it will cause the resolve method to return:
212
- * {
213
- * url: '<base_path>/node_modules/dual-mjs-pjson/subdir/index.mjs',
214
- * format: 'module'
215
- * }
216
- *
217
- * following testcase ensures that resolve works correctly in this case
218
- * returning the information as specified above. Source for 'url' value
219
- * is [1], source for 'format' value is the file extension of [3]
220
- */
221
- const moduleName = 'dual-mjs-pjson' ;
186
+ // TestParameters are ModuleName, mainRequireScript, mainImportScript,
187
+ // mainPackageType, subdirPkgJsonType, expectedResolvedFormat
188
+ [ [ 'mjs-mod-mod' , 'index.js' , 'index.mjs' , 'module' , 'module' , 'module' ] ,
189
+ [ 'mjs-com-com' , 'idx.js' , 'idx.mjs' , 'commonjs' , 'commonjs' , 'module' ] ,
190
+ [ 'mjs-mod-com' , 'index.js' , 'imp.mjs' , 'module' , 'commonjs' , 'module' ] ,
191
+ [ 'js-com-com' , 'index.js' , 'imp.js' , 'commonjs' , 'commonjs' , 'commonjs' ] ,
192
+ [ 'js-com-mod' , 'index.js' , 'imp.js' , 'commonjs' , 'module' , 'module' ] ,
193
+ [ 'ts-mod-com' , 'index.js' , 'imp.ts' , 'module' , 'commonjs' , undefined ] ,
194
+ ] . forEach ( ( testVariant ) => {
195
+ const [
196
+ moduleName ,
197
+ mainRequireScript ,
198
+ mainImportScript ,
199
+ mainPackageType ,
200
+ subdirPackageType ,
201
+ expectedResolvedFormat ] = testVariant ;
222
202
223
203
const mDir = rel ( `node_modules/${ moduleName } ` ) ;
224
204
const subDir = rel ( `node_modules/${ moduleName } /subdir` ) ;
225
205
const pkg = rel ( `node_modules/${ moduleName } /package.json` ) ;
226
206
const subdirPkg = rel ( `node_modules/${ moduleName } /subdir/package.json` ) ;
227
- const esScript = rel ( `node_modules/${ moduleName } /subdir/index.mjs ` ) ;
228
- const cjsScript = rel ( `node_modules/${ moduleName } /subdir/index.js ` ) ;
207
+ const esScript = rel ( `node_modules/${ moduleName } /subdir/${ mainImportScript } ` ) ;
208
+ const cjsScript = rel ( `node_modules/${ moduleName } /subdir/${ mainRequireScript } ` ) ;
229
209
230
210
createDir ( nmDir ) ;
231
211
createDir ( mDir ) ;
232
212
createDir ( subDir ) ;
233
213
234
214
const mainPkgJsonContent = {
235
- type : 'commonjs' ,
236
- main : 'lib/index.js' ,
215
+ type : mainPackageType ,
216
+ main : `./subdir/ ${ mainRequireScript } ` ,
237
217
exports : {
238
218
'.' : {
239
- 'require' : ' ./subdir/index.js' ,
240
- 'import' : ' ./subdir/index.mjs'
219
+ 'require' : ` ./subdir/${ mainRequireScript } ` ,
220
+ 'import' : ` ./subdir/${ mainImportScript } `
241
221
} ,
242
222
'./package.json' : './package.json' ,
243
223
}
244
224
} ;
245
225
const subdirPkgJsonContent = {
246
- type : 'commonjs'
226
+ type : ` ${ subdirPackageType } `
247
227
} ;
248
228
249
229
fs . writeFileSync ( pkg , JSON . stringify ( mainPkgJsonContent ) ) ;
@@ -257,12 +237,9 @@ try {
257
237
258
238
// test the resolve
259
239
const resolveResult = resolve ( `${ moduleName } ` ) ;
260
- assert . strictEqual ( resolveResult . format , 'module' ) ;
261
- assert . ok ( resolveResult . url . includes ( `${ moduleName } /subdir/index.mjs` ) ) ;
262
- }
263
-
264
- testDualPackageWithJsMainScriptAndModuleType ( ) ;
265
- testDualPackageWithMjsMainScriptAndCJSType ( ) ;
240
+ assert . strictEqual ( resolveResult . format , expectedResolvedFormat ) ;
241
+ assert . ok ( resolveResult . url . includes ( `${ moduleName } /subdir/${ mainImportScript } ` ) ) ;
242
+ } ) ;
266
243
267
244
} finally {
268
245
process . chdir ( previousCwd ) ;
0 commit comments