Skip to content

Commit 17c010d

Browse files
committed
Fix chunks ordering in case of manifest + vendor files
1 parent badba13 commit 17c010d

File tree

7 files changed

+87
-17
lines changed

7 files changed

+87
-17
lines changed

index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var RawSource = require('webpack-sources/lib/RawSource');
22
var evaluate = require('eval');
33
var path = require('path');
44
var cheerio = require('cheerio');
5+
var JSDOM = require('jsdom').JSDOM;
56
var url = require('url');
67
var Promise = require('bluebird');
78
var vm = require('vm');
@@ -127,31 +128,27 @@ function merge (a, b) {
127128
* Function to handle commonschunk plugin. Currently only supports a manifest file and single external
128129
* library file name vendor.
129130
*/
130-
var loadChunkAssetsToScope = function(scope, compilation, webpackStatsJson) {
131-
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName)
132-
var chunkValues = chunkNames.map(function(chunk) {
133-
return findAsset(chunk, compilation, webpackStatsJson);
134-
});
131+
var loadChunkAssetsToScope = function(globals, compilation, webpackStatsJson) {
132+
var dom = new JSDOM('', { runScripts: 'outside-only' });
133+
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName);
135134

136-
if(!scope) {
137-
scope = {};
135+
// CommonChunksPlugin will place webpackJsonP manifest loading in last bundle
136+
if (chunkNames[chunkNames.length - 1] === 'manifest') {
137+
chunkNames = ['manifest'].concat(chunkNames.slice(0, -1));
138138
}
139139

140-
if (!scope.window) {
141-
scope.window = {};
142-
}
140+
var chunkValues = chunkNames.map(function(chunk) {
141+
return findAsset(chunk, compilation, webpackStatsJson);
142+
});
143143

144-
var sandbox = {};
145-
merge(sandbox, scope);
144+
merge(dom.window, globals);
146145

147-
// CommonChunksPlugin will place webpackJsonP manifest loading in last bundle
148-
chunkValues.reverse().map(function(chunk) {
146+
chunkValues.map(function(chunk) {
149147
var script = new vm.Script(chunk.source());
150-
script.runInNewContext(sandbox, {});
151-
merge(sandbox, sandbox.window);
148+
dom.runVMScript(script);
152149
});
153150

154-
return sandbox;
151+
return dom.window;
155152
}
156153

157154
var findAsset = function(src, compilation, webpackStatsJson) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>/foo/bar</h1>
4+
</body>
5+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>/foo</h1>
4+
</body>
5+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>/</h1>
4+
</body>
5+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var bluebird = require('bluebird');
2+
3+
module.exports = function(locals, callback) {
4+
setTimeout(function() {
5+
callback(null, locals.template({ html: '<h1>' + locals.path + '</h1>' }));
6+
}, 10);
7+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<%- html %>
4+
</body>
5+
</html>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var StaticSiteGeneratorPlugin = require('../../../');
2+
var StatsWriterPlugin = require("webpack-stats-plugin").StatsWriterPlugin;
3+
var webpack = require('webpack');
4+
var ejs = require('ejs');
5+
var fs = require('fs');
6+
7+
var template = ejs.compile(fs.readFileSync(__dirname + '/template.ejs', 'utf-8'))
8+
9+
var paths = [
10+
'/',
11+
'/foo',
12+
'/foo/bar'
13+
];
14+
15+
module.exports = {
16+
entry: {
17+
index: __dirname + '/index.js',
18+
},
19+
20+
output: {
21+
filename: '[name].js',
22+
path: __dirname + '/actual-output',
23+
libraryTarget: 'umd'
24+
},
25+
26+
plugins: [
27+
new webpack.optimize.CommonsChunkPlugin({
28+
name: 'vendor',
29+
minChunks: function(module) {
30+
return module.context && module.context.indexOf('node_modules') !== -1;
31+
}
32+
}),
33+
new webpack.optimize.CommonsChunkPlugin({
34+
name: 'manifest',
35+
minChunks: Infinity
36+
}),
37+
new StaticSiteGeneratorPlugin({
38+
entry: 'index',
39+
paths: paths,
40+
locals: {
41+
template: template
42+
}
43+
}),
44+
new StatsWriterPlugin() // Causes the asset's `size` method to be called
45+
]
46+
};

0 commit comments

Comments
 (0)