Skip to content

Commit

Permalink
add 'id' to the module variable, #619 (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey authored and devongovett committed Jan 29, 2018
1 parent c2022b3 commit 8d51fdc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/builtins/hmr-runtime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var global = (1, eval)('this');
var OldModule = module.bundle.Module;
function Module() {
OldModule.call(this);
function Module(moduleName) {
OldModule.call(this, moduleName);
this.hot = {
accept: function (fn) {
this._acceptCallback = fn || function () {};
Expand Down
5 changes: 3 additions & 2 deletions src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require = (function (modules, cache, entry) {

localRequire.resolve = resolve;

var module = cache[name] = new newRequire.Module;
var module = cache[name] = new newRequire.Module(name);

modules[name][0].call(module.exports, localRequire, module, module.exports);
}
Expand All @@ -53,7 +53,8 @@ require = (function (modules, cache, entry) {
}
}

function Module() {
function Module(moduleName) {
this.id = moduleName;
this.bundle = newRequire;
this.exports = {};
}
Expand Down
12 changes: 11 additions & 1 deletion test/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ describe('hmr', function() {
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
let bundle = await b.bundle();
let outputs = [];
let moduleId = '';

run(bundle, {
reportModuleId(id) {
moduleId = id;
},
output(o) {
outputs.push(o);
}
Expand All @@ -195,7 +199,13 @@ describe('hmr', function() {
);

await nextEvent(b, 'bundled');
assert.deepEqual(outputs, [3, 'dispose', 10, 'accept']);
assert.notEqual(moduleId, undefined);
assert.deepEqual(outputs, [
3,
'dispose-' + moduleId,
10,
'accept-' + moduleId
]);
});

it('should work across bundles', async function() {
Expand Down
7 changes: 5 additions & 2 deletions test/integration/hmr-callbacks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ function run() {

run();

// eslint-disable-next-line no-undef
reportModuleId(module.id);

module.hot.dispose(function () {
output('dispose');
output('dispose-' + module.id);
});

module.hot.accept(function () {
output('accept');
output('accept-' + module.id);
});

0 comments on commit 8d51fdc

Please sign in to comment.