Skip to content

Commit 99b4bba

Browse files
committed
fix: avoid conflict in conventional alias, only apply for package with main with same path prefix
1 parent 8033a56 commit 99b4bba

6 files changed

Lines changed: 171 additions & 14 deletions

File tree

lib/package-reader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ module.exports = class PackageReader {
130130
contents: stripSourceMappingUrl(file.contents),
131131
moduleId,
132132
packageName: this.name,
133+
packageMainPath: this.mainPath,
133134
sourceMap: getSourceMap(file.contents, file.path)
134135
};
135136

lib/transformers/conventional-alias.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ const DIST_FAVORS = ['amd', 'cjs', 'commonjs', 'native-module', 'native-modules'
99

1010
// only for npm file and defined a single module
1111
module.exports = function(unit) {
12-
const {defined, packageName} = unit;
12+
const {defined, packageName, packageMainPath} = unit;
1313
if (!packageName) return;
1414
if (!defined || !defined.length) return;
1515
if (defined.length > 1) return;
1616

1717
const {parts} = parse(defined[0]);
1818
let toSkip = 0;
1919

20-
if (parts.length > 2 && DIST_FOLDERS.indexOf(parts[1].toLowerCase()) !== -1) {
20+
if (parts.length > 2 &&
21+
DIST_FOLDERS.indexOf(parts[1].toLowerCase()) !== -1 &&
22+
packageMainPath.startsWith(parts[1])) {
2123
toSkip = 1;
22-
if (parts.length > 3 && DIST_FAVORS.indexOf(parts[2].toLowerCase()) !== -1) {
24+
if (parts.length > 3 &&
25+
DIST_FAVORS.indexOf(parts[2].toLowerCase()) !== -1 &&
26+
packageMainPath.slice(parts[1].length + 1).startsWith(parts[2])) {
2327
toSkip = 2;
2428
}
2529
}

test/bundler.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ test('Bundler supports inject css by default', t => {
11471147
},
11481148
{
11491149
"path": "node_modules/dumber/lib/inject-css.js",
1150-
"contents": ";\ndefine('dumber/lib/inject-css',function(){});\n\n;define.alias('dumber/inject-css','dumber/lib/inject-css');"
1150+
"contents": ";\ndefine('dumber/lib/inject-css',function(){});\n"
11511151
},
11521152
{
11531153
"contents": "define.switchToUserSpace();"
@@ -1209,7 +1209,7 @@ test('Bundler supports inject css (relative path) by default', t => {
12091209
},
12101210
{
12111211
"path": "node_modules/dumber/lib/inject-css.js",
1212-
"contents": ";\ndefine('dumber/lib/inject-css',function(){});\n\n;define.alias('dumber/inject-css','dumber/lib/inject-css');"
1212+
"contents": ";\ndefine('dumber/lib/inject-css',function(){});\n"
12131213
},
12141214
{
12151215
"contents": "define.switchToUserSpace();"

test/package-reader.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test('packageReader falls back to main:index when package.json is missing', t =>
2121
moduleId: 'foo/index',
2222
alias: 'foo',
2323
packageName: 'foo',
24+
packageMainPath: 'index.js',
2425
sourceMap: undefined
2526
});
2627

@@ -64,6 +65,7 @@ test('packageReader can still read resource when main is missing', t => {
6465
contents: 'lorem',
6566
moduleId: 'foo/bar',
6667
packageName: 'foo',
68+
packageMainPath: 'index.js',
6769
sourceMap: undefined
6870
});
6971

@@ -91,6 +93,7 @@ test('packageReader uses default index.js as main path if main file is missing b
9193
contents: 'lorem',
9294
moduleId: 'foo/bar',
9395
packageName: 'foo',
96+
packageMainPath: 'index.js',
9497
sourceMap: undefined
9598
});
9699

@@ -118,6 +121,7 @@ test('packageReader reads main file', t => {
118121
contents: 'lorem',
119122
moduleId: 'foo/index',
120123
packageName: 'foo',
124+
packageMainPath: 'index.js',
121125
alias: 'foo',
122126
sourceMap: undefined
123127
});
@@ -161,6 +165,7 @@ test('packageReader use default main index.js', t => {
161165
contents: 'lorem',
162166
moduleId: 'foo/index',
163167
packageName: 'foo',
168+
packageMainPath: 'index.js',
164169
alias: 'foo',
165170
sourceMap: undefined
166171
});
@@ -189,6 +194,7 @@ test('packageReader reads module over main field', t => {
189194
contents: 'es',
190195
moduleId: 'foo/es',
191196
packageName: 'foo',
197+
packageMainPath: 'es.js',
192198
alias: 'foo',
193199
sourceMap: undefined
194200
});
@@ -218,6 +224,7 @@ test('packageReader reads browser over main/module field', t => {
218224
contents: 'br',
219225
moduleId: 'foo/br',
220226
packageName: 'foo',
227+
packageMainPath: 'br.js',
221228
alias: 'foo',
222229
sourceMap: undefined
223230
});
@@ -245,6 +252,7 @@ test('packageReader reads main file with explicit ext', t => {
245252
contents: 'lorem',
246253
moduleId: 'foo.js/main',
247254
packageName: 'foo.js',
255+
packageMainPath: 'main.js',
248256
alias: 'foo.js',
249257
sourceMap: undefined
250258
});
@@ -272,6 +280,7 @@ test('packageReader reads main file with non-js file', t => {
272280
contents: 'lorem',
273281
moduleId: 'foo/main.css',
274282
packageName: 'foo',
283+
packageMainPath: 'main.css',
275284
sourceMap: undefined
276285
});
277286

@@ -298,6 +307,7 @@ test('packageReader reads implicit main file', t => {
298307
contents: 'lorem',
299308
moduleId: 'foo/lib/index',
300309
packageName: 'foo',
310+
packageMainPath: 'lib/index.js',
301311
alias: 'foo',
302312
sourceMap: undefined
303313
});
@@ -344,6 +354,7 @@ test('packageReader reads resource', t => {
344354
contents: 'lorem2',
345355
moduleId: 'foo/lib/bar',
346356
packageName: 'foo',
357+
packageMainPath: 'lib/main.js',
347358
sourceMap: undefined
348359
});
349360

@@ -371,6 +382,7 @@ test('packageReader reads relative resource', t => {
371382
contents: 'lorem2',
372383
moduleId: 'foo/lib/bar',
373384
packageName: 'foo',
385+
packageMainPath: 'lib/main.js',
374386
alias: 'foo/bar',
375387
sourceMap: undefined
376388
});
@@ -399,6 +411,7 @@ test('packageReader reads deep relative resource', t => {
399411
contents: 'lorem2',
400412
moduleId: 'foo/dist/cjs/foo/bar',
401413
packageName: 'foo',
414+
packageMainPath: 'dist/cjs/main.js',
402415
alias: 'foo/foo/bar',
403416
sourceMap: undefined
404417
});
@@ -427,6 +440,7 @@ test('packageReader reads json resouce', t => {
427440
contents: '{"a":1}',
428441
moduleId: 'foo/dist/cjs/foo/bar.json',
429442
packageName: 'foo',
443+
packageMainPath: 'dist/cjs/main.js',
430444
alias: 'foo/foo/bar',
431445
sourceMap: undefined
432446
});
@@ -455,6 +469,7 @@ test('packageReader reads directory index.js', t => {
455469
contents: 'lorem2',
456470
moduleId: 'foo/lib/index',
457471
packageName: 'foo',
472+
packageMainPath: 'index.js',
458473
alias: 'foo/lib',
459474
sourceMap: undefined
460475
});
@@ -483,6 +498,7 @@ test('packageReader reads directory index.json', t => {
483498
contents: '{"a":1}',
484499
moduleId: 'foo/lib/index.json',
485500
packageName: 'foo',
501+
packageMainPath: 'index.js',
486502
alias: 'foo/lib',
487503
sourceMap: undefined
488504
});
@@ -513,6 +529,7 @@ test('packageReader reads directory package.json', t => {
513529
contents: 'es',
514530
moduleId: 'foo/lib/es/index',
515531
packageName: 'foo',
532+
packageMainPath: 'index.js',
516533
alias: 'foo/lib',
517534
sourceMap: undefined
518535
});
@@ -567,6 +584,7 @@ test('packageReader reads browser replacement in package.json', t => {
567584
contents: 'lorem',
568585
moduleId: 'foo/index',
569586
packageName: 'foo',
587+
packageMainPath: 'index.js',
570588
alias: 'foo',
571589
replacement: {
572590
'module-a': '__ignore__',
@@ -611,6 +629,7 @@ test('packageReader uses browser replacement in package.json to normalize replac
611629
contents: "require('module-a');require('module-b.js');require('module-c');",
612630
moduleId: 'foo/shims/client-only',
613631
packageName: 'foo',
632+
packageMainPath: 'index.js',
614633
replacement: {
615634
'module-a': '__ignore__',
616635
'module-b.js': './module/b',
@@ -649,6 +668,7 @@ test('packageReader uses browser replacement in package.json to normalize main r
649668
contents: "browser",
650669
moduleId: 'foo/browser',
651670
packageName: 'foo',
671+
packageMainPath: 'browser.js',
652672
alias: 'foo',
653673
replacement: { './index': './browser' },
654674
sourceMap: undefined
@@ -684,6 +704,7 @@ test('packageReader uses browser replacement in package.json to normalize main r
684704
contents: "require('module-a');require('module-b.js');require('module-c');require('./server/only.js');",
685705
moduleId: 'foo/index',
686706
packageName: 'foo',
707+
packageMainPath: 'index.js',
687708
alias: 'foo',
688709
replacement: {
689710
'module-a': '__ignore__',
@@ -724,6 +745,7 @@ test('packageReader uses browser replacement in package.json to normalize replac
724745
contents: "require('module-a');require('module-b.js');require('module-c');require('./only.js');",
725746
moduleId: 'foo/server/bar',
726747
packageName: 'foo',
748+
packageMainPath: 'index.js',
727749
replacement: {
728750
'module-a': '__ignore__',
729751
'module-b.js': '../shims/module/b',
@@ -763,6 +785,7 @@ test('packageReader uses browser replacement in package.json to normalize replac
763785
contents: "require('module-a');require('module-b.js');require('module-c');require('../server/only.js');",
764786
moduleId: 'foo/lib/bar',
765787
packageName: 'foo',
788+
packageMainPath: 'index.js',
766789
replacement: {
767790
'module-a': '__ignore__',
768791
'module-b.js': '../shims/module/b',
@@ -794,6 +817,7 @@ test('packageReader reads main file for package alias', t => {
794817
contents: 'lorem',
795818
moduleId: 'bar/index',
796819
packageName: 'bar',
820+
packageMainPath: 'index.js',
797821
alias: 'bar',
798822
sourceMap: undefined
799823
});
@@ -823,6 +847,7 @@ test('packageReader reads resource file for package alias', t => {
823847
contents: 'lorem2',
824848
moduleId: 'bar/lo',
825849
packageName: 'bar',
850+
packageMainPath: 'index.js',
826851
sourceMap: undefined
827852
});
828853

test/trace.spec.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ test('trace rejects not-matching packageName and moduleId', t => {
1414
path: 'node_module/foo/bar.js',
1515
contents: "lorem",
1616
moduleId: 'x/bar',
17-
packageName: 'foo'
17+
packageName: 'foo',
18+
packageMainPath: 'index.js'
1819
};
1920
trace(unit).catch(err => {
2021
t.ok(err);
@@ -27,7 +28,8 @@ test('trace does not reject moduleId which is same as packageName', t => {
2728
path: '__stub__/fs.js',
2829
contents: "define(function(){});",
2930
moduleId: 'fs',
30-
packageName: 'fs'
31+
packageName: 'fs',
32+
packageMainPath: 'index.js'
3133
};
3234
trace(unit).then(
3335
traced => {
@@ -45,7 +47,8 @@ test('trace does not reject moduleId which is same as packageName', t => {
4547
moduleId: 'fs',
4648
defined: ['fs'],
4749
deps: [],
48-
packageName: 'fs'
50+
packageName: 'fs',
51+
packageMainPath: 'index.js'
4952
});
5053
t.end();
5154
},
@@ -134,6 +137,7 @@ test('trace traces shimed js and update sourceMap', t => {
134137
},
135138
moduleId: 'bar/bar',
136139
packageName: 'bar',
140+
packageMainPath: 'index.js',
137141
shim: { deps: ['foo'], 'exports': 'Bar', wrapShim: true}
138142
};
139143

@@ -158,6 +162,7 @@ test('trace traces shimed js and update sourceMap', t => {
158162
defined: ['bar/bar'],
159163
deps: ['foo'],
160164
packageName: 'bar',
165+
packageMainPath: 'index.js',
161166
shim: { deps: ['foo'], 'exports': 'Bar', wrapShim: true},
162167
shimed: true
163168
});
@@ -178,7 +183,8 @@ test('trace forces shim on old js and update sourceMap', t => {
178183
sourcesContent: ["var Bar = 1;"]
179184
},
180185
moduleId: 'bar/bar',
181-
packageName: 'bar'
186+
packageName: 'bar',
187+
packageMainPath: 'index.js'
182188
};
183189

184190
trace(unit).then(traced => {
@@ -198,6 +204,7 @@ test('trace forces shim on old js and update sourceMap', t => {
198204
defined: ['bar/bar'],
199205
deps: [],
200206
packageName: 'bar',
207+
packageMainPath: 'index.js',
201208
shimed: true
202209
});
203210
t.end();
@@ -420,7 +427,8 @@ test('trace traces npm js with dist alias', t => {
420427
path: 'node_modules/foo/dist/bar.js',
421428
contents: "define(['a','text!./b.css'],function() {});",
422429
moduleId: 'foo/dist/bar',
423-
packageName: 'foo'
430+
packageName: 'foo',
431+
packageMainPath: 'dist/index.js'
424432
};
425433

426434
trace(unit).then(traced => {
@@ -439,6 +447,7 @@ test('trace traces npm js with dist alias', t => {
439447
defined: ['foo/dist/bar', 'foo/bar'],
440448
deps: ['a', 'text!./b.css'],
441449
packageName: 'foo',
450+
packageMainPath: 'dist/index.js',
442451
alias: null
443452
});
444453
t.end();
@@ -450,7 +459,8 @@ test('trace traces npm html with dist alias', t => {
450459
path: 'node_modules/foo/dist/cjs/bar.html',
451460
contents: "<p></p>",
452461
moduleId: 'foo/dist/cjs/bar.html',
453-
packageName: 'foo'
462+
packageName: 'foo',
463+
packageMainPath: 'dist/cjs/index.js'
454464
};
455465

456466
trace(unit).then(traced => {
@@ -469,6 +479,7 @@ test('trace traces npm html with dist alias', t => {
469479
defined: ['text!foo/dist/cjs/bar.html', 'text!foo/bar.html'],
470480
deps: [],
471481
packageName: 'foo',
482+
packageMainPath: 'dist/cjs/index.js',
472483
alias: null
473484
});
474485
t.end();
@@ -497,7 +508,8 @@ test('trace patches momentjs to expose global var "moment"', t => {
497508
contents: moment,
498509
sourceMap: undefined,
499510
moduleId: 'moment/moment',
500-
packageName: 'moment'
511+
packageName: 'moment',
512+
packageMainPath: 'moment.js'
501513
};
502514

503515
trace(unit).then(traced => {
@@ -508,6 +520,7 @@ test('trace patches momentjs to expose global var "moment"', t => {
508520
defined: ['moment/moment'],
509521
deps: [],
510522
packageName: 'moment',
523+
packageMainPath: 'moment.js',
511524
sourceMap: {
512525
version: 3,
513526
sources: [ 'node_modules/moment/moment.js' ],
@@ -541,6 +554,7 @@ ${patchedProcessFile}
541554
sourceMap: undefined,
542555
moduleId: 'process/browser',
543556
packageName: 'process',
557+
packageMainPath: 'browser.js',
544558
alias: 'process',
545559
};
546560

@@ -552,6 +566,7 @@ ${patchedProcessFile}
552566
defined: ['process/browser', 'process'],
553567
deps: [],
554568
packageName: 'process',
569+
packageMainPath: 'browser.js',
555570
alias: null,
556571
sourceMap: {
557572
version: 3,

0 commit comments

Comments
 (0)