Skip to content

Commit c403add

Browse files
committed
revert: feat: be smart on onRequire stub's module space
This reverts commit 3851939.
1 parent 31f5ab9 commit c403add

2 files changed

Lines changed: 25 additions & 101 deletions

File tree

spec/bundler.spec.js

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ test('Bundler replaces deps when onRequire returns array', t => {
427427
.then(t.end);
428428
});
429429

430-
test('Bundler supports user space implementation returned by onRequire', t => {
430+
test('Bundler supports implementation returned by onRequire', t => {
431431
const fakeFs = {
432432
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',
433433
'node_modules/loo/package.json': '{"name":"loo","main":"loo"}',
@@ -469,51 +469,6 @@ test('Bundler supports user space implementation returned by onRequire', t => {
469469
.then(t.end);
470470
});
471471

472-
test('Bundler supports package space implementation returned by onRequire', t => {
473-
const fakeFs = {
474-
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',
475-
'node_modules/loo/package.json': '{"name":"loo","main":"loo"}',
476-
'node_modules/loo/loo.js': '',
477-
'node_modules/bar/package.json': '{"name":"bar","main":"bar"}',
478-
'node_modules/bar/bar.js': 'foo',
479-
};
480-
const bundler = createBundler(fakeFs, {
481-
onRequire(moduleId) {
482-
// onRequire can return a Promise to resolve to false, array, or string.
483-
if (moduleId === 'foo') return Promise.resolve("loo"); // "loo" will be processed by mockTrace
484-
}
485-
});
486-
487-
Promise.resolve()
488-
.then(() => bundler.capture({path: 'src/app.js', contents: 'bar', moduleId: 'app'}))
489-
.then(() => bundler.resolve())
490-
.then(() => bundler.bundle())
491-
.then(
492-
bundleMap => {
493-
t.deepEqual(bundleMap, {
494-
'entry-bundle': {
495-
files: [
496-
{contents: 'dumber-module-loader;'},
497-
{contents: 'define.switchToUserSpace();'},
498-
{path: 'src/app.js', contents: "define('app',[\"bar\"],1);", sourceMap: undefined},
499-
{contents: 'define.switchToPackageSpace();'},
500-
{path: 'node_modules/bar/bar.js', contents: "define('bar/bar',[\"foo\"],1);define('bar',['bar/bar'],function(m){return m;});", sourceMap: undefined},
501-
{path: '__on_require__/foo.js', contents: "define('foo',[\"loo\"],1);", sourceMap: undefined},
502-
{path: 'node_modules/loo/loo.js', contents: "define('loo/loo',[],1);define('loo',['loo/loo'],function(m){return m;});", sourceMap: undefined},
503-
{contents: 'define.switchToUserSpace();'},
504-
],
505-
config: {
506-
baseUrl: '/dist',
507-
bundles: {}
508-
}
509-
}
510-
})
511-
},
512-
err => t.fail(err.stack)
513-
)
514-
.then(t.end);
515-
});
516-
517472
test('Bundler swallows onRequire exception', t => {
518473
const fakeFs = {
519474
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',

src/index.js

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ export default class Bundler {
4343
// turn on injection of css (inject onto html head)
4444
if (opts.injectCss || opts.injectCSS) this._injectCss = true;
4545

46-
this._unitsMap = Object.create(null);
47-
this._user_modules_done = new Set();
48-
this._package_modules_done = new Set();
49-
this._moduleIds_todo = Object.create(null);
50-
this._readersMap = Object.create(null);
46+
this._unitsMap = {};
47+
this._moduleId_done = new Set();
48+
this._moduleIds_todo = new Set();
49+
this._readersMap = {};
5150
this._locator = opts.packageLocator || defaultPackageLocator;
5251

5352
// baseUrl default to "dist"
@@ -81,7 +80,7 @@ export default class Bundler {
8180
}
8281

8382
packageReaderFor(packageConfig) {
84-
if (this._readersMap[packageConfig.name]) {
83+
if (this._readersMap.hasOwnProperty(packageConfig.name)) {
8584
return Promise.resolve(this._readersMap[packageConfig.name]);
8685
}
8786

@@ -125,45 +124,26 @@ export default class Bundler {
125124
);
126125
}
127126

128-
_didUserModule(id) {
127+
_addToDone(id) {
129128
if (typeof id === 'string') {
130-
this._user_modules_done.add(id);
129+
this._moduleId_done.add(id);
131130
} else if (Array.isArray(id)) {
132-
id.forEach(d => this._user_modules_done.add(d));
133-
}
134-
}
135-
136-
_didPackageModule(id) {
137-
if (typeof id === 'string') {
138-
this._package_modules_done.add(id);
139-
} else if (Array.isArray(id)) {
140-
id.forEach(d => this._package_modules_done.add(d));
141-
}
142-
}
143-
144-
_addTodo(d, isPackageSpace) {
145-
if (this._moduleIds_todo[d]) {
146-
if (isPackageSpace) this._moduleIds_todo[d] = 2;
147-
} else {
148-
this._moduleIds_todo[d] = isPackageSpace ? 2 : 1
131+
id.forEach(d => this._moduleId_done.add(d));
149132
}
150133
}
151134

152135
_capture(tracedUnit) {
153136
this._unitsMap[tracedUnit.path] = tracedUnit;
154137

155138
// mark as done.
156-
if (tracedUnit.packageName) {
157-
this._didPackageModule(tracedUnit.moduleId);
158-
this._didPackageModule(tracedUnit.defined);
159-
} else {
160-
this._didUserModule(tracedUnit.moduleId);
161-
this._didUserModule(tracedUnit.defined);
162-
}
139+
this._addToDone(tracedUnit.moduleId);
140+
this._addToDone(tracedUnit.defined);
163141

164-
// mark todo. beware we didn't check whether the id is in _user/package_modules_done.
142+
// mark todo. beware we didn't check whether the id is in _moduleId_done.
165143
// they will be checked during resolve phase.
166-
tracedUnit.deps.forEach(d => this._addTodo(d, tracedUnit.packageName));
144+
145+
// console.log('_capture ' + tracedUnit.moduleId + ' deps ' + tracedUnit.deps);
146+
tracedUnit.deps.forEach(d => this._moduleIds_todo.add(d));
167147

168148
const bundle = this.bundleOf(tracedUnit);
169149
// mark related bundle dirty
@@ -218,7 +198,7 @@ export default class Bundler {
218198
// to some browser replacement.
219199
// e.g. readable-stream/readable -> readable-stream/readable-browser
220200
_ensureNpmAlias(tracedUnit, id) {
221-
if (this._package_modules_done.has(id)) return;
201+
if (this._moduleId_done.has(id)) return;
222202

223203
const defined = tracedUnit.defined;
224204
let toId;
@@ -237,7 +217,7 @@ export default class Bundler {
237217
if (toId !== id && toId !== tracedUnit.packageName) {
238218
const aliasResult = alias(id, toId);
239219
mergeTransformed(tracedUnit, aliasResult);
240-
this._didPackageModule(aliasResult.defined);
220+
this._addToDone(aliasResult.defined);
241221
}
242222
}
243223

@@ -253,31 +233,21 @@ export default class Bundler {
253233
}
254234

255235
resolve() {
256-
// todo is always to be resolved in package space.
257236
let todo = [];
258-
259237
return this._supportInjectCssIfNeeded()
260238
.then(() => this._resolvePrependsAndAppends())
261239
.then(() => this._resolveExplicitDepsIfNeeded())
262240
.then(() => {
263241
const consults = [];
264-
const rawTodo = JSON.parse(JSON.stringify(this._moduleIds_todo));
265-
this._moduleIds_todo = Object.create(null);
242+
const rawTodo = Array.from(this._moduleIds_todo);
243+
this._moduleIds_todo.clear();
244+
266245

267-
Object.keys(rawTodo).forEach(id => {
268-
const isPackageSpace = rawTodo[id] === 2;
246+
// console.log('_moduleIds_todo', this._moduleIds_todo);
247+
rawTodo.forEach(id => {
269248
const parsedId = parse(id);
270249
const possibleIds = nodejsIds(parsedId.bareId);
271-
272-
const isAvailabe = possibleIds.some(id => {
273-
if (isPackageSpace) {
274-
return this._package_modules_done.has(id);
275-
} else {
276-
return this._user_modules_done.has(id) || this._package_modules_done.has(id);
277-
}
278-
});
279-
280-
if (isAvailabe) return;
250+
if (possibleIds.some(id => this._moduleId_done.has(id))) return;
281251

282252
const j = new Promise(resolve => {
283253
resolve(this._onRequire && this._onRequire(parsedId.bareId, parsedId));
@@ -297,8 +267,7 @@ export default class Bundler {
297267
return this.capture({
298268
path: '__on_require__/' + parsedId.bareId + (parsedId.ext ? '' : '.js'),
299269
contents: result,
300-
moduleId: parsedId.bareId,
301-
packageName: isPackageSpace ? parsedId.parts[0] : ''
270+
moduleId: parsedId.bareId
302271
});
303272
}
304273

@@ -356,7 +325,7 @@ export default class Bundler {
356325
return p;
357326
})
358327
.then(() => {
359-
if (Object.keys(this._moduleIds_todo).length) {
328+
if (this._moduleIds_todo.size) {
360329
return this.resolve();
361330
}
362331
});

0 commit comments

Comments
 (0)