Skip to content

Commit

Permalink
fix: support unresolved loader resolve calls (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgoddard authored Jul 2, 2018
1 parent 872e7ba commit 66086aa
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/TransformNormalModuleFactoryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ class NormalModuleFactoryPlugin {
context,
request,
(err, result) => {
module.__hardSource_resolved[
JSON.stringify({ context, request })
] = {
resource: result,
resolveOptions: module.resolveOptions,
};
callback(err, result);
if (err) {
callback(err, result);
} else {
module.__hardSource_resolved[
JSON.stringify({ context, request })
] = {
resource: result,
resolveOptions: module.resolveOptions,
};
callback(err, result);
}
},
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 2 : 0);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./loader!./fib');

console.log(fib(3));
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function(source) {
this.cacheable && this.cacheable();
const done = this.async();
new Promise(
(f, e) => this.resolve(this.context, './fab', (er, v) => (er ? e(er) : f(v)))
)
.then(v => JSON.stringify(v))
.then(v => done(null, v), done);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var HardSourceWebpackPlugin = require('../../..');

module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: __dirname + '/tmp',
filename: 'main.js',
},
plugins: [
new HardSourceWebpackPlugin({
cacheDirectory: 'cache',
environmentHash: {
root: __dirname + '/../../..',
},
}),
],
};
3 changes: 3 additions & 0 deletions tests/fixtures/loader-custom-resolve-request-missing/fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 2 : 0);
};
3 changes: 3 additions & 0 deletions tests/fixtures/loader-custom-resolve-request-missing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./loader!./fib');

console.log(fib(3));
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function(source) {
this.cacheable && this.cacheable();
const done = this.async();
new Promise(
(f, e) => this.resolve(this.context, './fab', (er, v) => (er ? f('unresolved') : f(v)))
)
.then(v => JSON.stringify(v))
.then(v => done(null, v), done);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var HardSourceWebpackPlugin = require('../../..');

module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: __dirname + '/tmp',
filename: 'main.js',
},
plugins: [
new HardSourceWebpackPlugin({
cacheDirectory: 'cache',
environmentHash: {
root: __dirname + '/../../..',
},
}),
],
};
11 changes: 11 additions & 0 deletions tests/loaders-webpack-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describeWP(3)('loader webpack 3 use', function() {

itCompilesTwice('loader-worker-1dep');
itCompilesTwice('loader-worker-1dep', {exportStats: true});
itCompilesTwice('loader-custom-resolve-request-missing');
itCompilesTwice('loader-custom-resolve-request-missing', {exportStats: true});

});

Expand All @@ -32,5 +34,14 @@ describeWP(3)('loader webpack 3 use - builds changes', function() {
expect(output.run2['main.js'].toString()).to.match(/fab(\/|\\\\|\\\\\\\\)index\.js/);
});

itCompilesChange('loader-custom-resolve-request-missing-change', {
'fab.js': null,
}, {
'fab.js': 'bar',
}, function(output) {
expect(output.run1['main.js'].toString()).to.not.match(/fab\.js/);
expect(output.run2['main.js'].toString()).to.match(/fab\.js/);
});

});

0 comments on commit 66086aa

Please sign in to comment.