Skip to content

Commit 5dca486

Browse files
Jack Zhaomastilver
Jack Zhao
authored andcommitted
Fix scope hoisting (#152)
Issues: #151 facebook/create-react-app#4492 Same as #117 with a test case. ----- fix #151 closes #117
1 parent 95d94ef commit 5dca486

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

lib/plugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ ManifestPlugin.prototype.apply = function(compiler) {
4040
var outputName = path.relative(outputFolder, outputFile);
4141

4242
var moduleAsset = function (module, file) {
43-
moduleAssets[file] = path.join(
44-
path.dirname(file),
45-
path.basename(module.userRequest)
46-
);
43+
if (module.userRequest) {
44+
moduleAssets[file] = path.join(
45+
path.dirname(file),
46+
path.basename(module.userRequest)
47+
);
48+
}
4749
};
4850

4951
var emit = function(compilation, compileCallback) {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
"file-loader": "^1.1.11",
2323
"jest": "^22.4.3",
2424
"memory-fs": "^0.2.0",
25+
"react": "^16.3.2",
2526
"rimraf": "^2.6.1",
2627
"style-loader": "^0.8.3",
28+
"svgr": "^1.9.2",
2729
"webpack": "^3.5.2"
2830
},
2931
"files": [

spec/plugin.integration.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,48 @@ describe('ManifestPlugin with memory-fs', function() {
436436
});
437437
});
438438
});
439+
440+
describe('scoped hoisting', function() {
441+
beforeAll(function () {
442+
fse.outputFileSync(path.join(__dirname, 'output/scoped-hoisting/index.js'), 'import { ReactComponent } from "./logo.svg";');
443+
fse.outputFileSync(path.join(__dirname, 'output/scoped-hoisting/logo.svg'), '<svg />');
444+
});
445+
446+
it('outputs a manifest', function(done) {
447+
let plugins;
448+
if (webpack.optimize.ModuleConcatenationPlugin) {
449+
// ModuleConcatenationPlugin works with webpack 3, 4.
450+
plugins = [
451+
new webpack.optimize.ModuleConcatenationPlugin(),
452+
new ManifestPlugin(),
453+
];
454+
} else {
455+
plugins = [
456+
new ManifestPlugin(),
457+
];
458+
}
459+
compiler = webpackCompile({
460+
context: __dirname,
461+
entry: './output/scoped-hoisting/index.js',
462+
module: {
463+
rules: [
464+
{
465+
test: /\.svg$/,
466+
use: ['svgr/webpack', 'file-loader']
467+
},
468+
],
469+
},
470+
output: {
471+
filename: '[name].[hash].js',
472+
path: path.join(__dirname, 'output/scoped-hoisting')
473+
},
474+
plugins,
475+
}, {}, function(stats) {
476+
var manifest = JSON.parse(fse.readFileSync(path.join(__dirname, 'output/scoped-hoisting/manifest.json')))
477+
478+
expect(manifest).toBeDefined();
479+
expect(manifest['main.js']).toEqual('main.'+ stats.hash +'.js');
480+
return done();
481+
});
482+
});
483+
});

0 commit comments

Comments
 (0)