Skip to content

Commit efe776f

Browse files
committed
fix: fix false error on stub module
1 parent c8ceff2 commit efe776f

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

spec/bundler.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ test('Bundler traces files, sorts shim', t => {
328328
{path: 'node_modules/jquery/dist/jquery.js', contents: 'define("jquery",[],1);', sourceMap: undefined},
329329
{path: 'node_modules/bootstrap/dist/bootstrap.js', contents: "define('bootstrap/dist/bootstrap',[\"jquery\"],function(){return jQuery;});define('bootstrap',['bootstrap/dist/bootstrap'],function(m){return m;});", sourceMap: undefined},
330330
// mockTrace didn't touch fs stub, it is different in real usage
331-
{path: '__stub__/fs', contents: "define(function(){return {};});", sourceMap: undefined},
331+
{path: '__stub__/fs.js', contents: "define(function(){return {};});", sourceMap: undefined},
332332
{contents: 'define.switchToUserSpace();'},
333333
],
334334
config: {

spec/trace.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@ test('trace rejects not-matching packageName and moduleId', t => {
1414
})
1515
});
1616

17+
test('trace does not reject moduleId which is same as packageName', t => {
18+
const unit = {
19+
path: '__stub__/fs.js',
20+
contents: "define(function(){});",
21+
moduleId: 'fs',
22+
packageName: 'fs'
23+
}
24+
trace(unit).then(
25+
traced => {
26+
t.deepEqual(traced, {
27+
path: '__stub__/fs.js',
28+
contents: "define('fs',function(){});",
29+
sourceMap: undefined,
30+
moduleId: 'fs',
31+
defined: 'fs',
32+
deps: [],
33+
packageName: 'fs',
34+
shimed: undefined
35+
});
36+
t.end();
37+
},
38+
err => {
39+
console.log(err.stack);
40+
t.fail(err);
41+
t.end();
42+
}
43+
);
44+
});
45+
1746
test('trace traces js', t => {
1847
const unit = {
1948
path: 'src/foo/bar.js',

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export default class Bundler {
279279
if (typeof stub === 'string') {
280280
p = p.then(() => this.capture({
281281
// not a real file path
282-
path:'__stub__/' + bareId,
282+
path:'__stub__/' + bareId + '.js',
283283
contents: stub,
284284
moduleId: bareId,
285285
packageName

src/trace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function (unit, opts = {}) {
2222
let shim = unit.shim;
2323
let shimed;
2424

25-
if (packageName && !moduleId.startsWith(packageName + '/')) {
25+
if (packageName && (moduleId !== packageName && !moduleId.startsWith(packageName + '/'))) {
2626
return Promise.reject(new Error('module "' + moduleId + '" is not in package "' + packageName + '"'));
2727
}
2828

@@ -117,7 +117,7 @@ export default function (unit, opts = {}) {
117117
const {parts, cleanId} = parse(unit.moduleId);
118118
let toSkip = 0;
119119

120-
if (DIST_FOLDERS.indexOf(parts[1].toLowerCase()) !== -1) {
120+
if (parts.length > 2 && DIST_FOLDERS.indexOf(parts[1].toLowerCase()) !== -1) {
121121
toSkip = 1;
122122
if (parts.length > 3 && DIST_FAVORS.indexOf(parts[2].toLowerCase()) !== -1) {
123123
toSkip = 2;

0 commit comments

Comments
 (0)