Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chunks ordering in case of manifest + vendor files #115

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "4.1"
- "6"
after_script:
- npm run coveralls
44 changes: 42 additions & 2 deletions index.js
Expand Up @@ -2,8 +2,10 @@ var RawSource = require('webpack-sources/lib/RawSource');
var evaluate = require('eval');
var path = require('path');
var cheerio = require('cheerio');
var JSDOM = require('jsdom').JSDOM;
var url = require('url');
var Promise = require('bluebird');
var vm = require('vm');

function StaticSiteGeneratorWebpackPlugin(options) {
if (arguments.length > 1) {
Expand Down Expand Up @@ -36,11 +38,12 @@ StaticSiteGeneratorWebpackPlugin.prototype.apply = function(compiler) {
throw new Error('Source file not found: "' + self.entry + '"');
}

var scope = loadChunkAssetsToScope(self.globals, compilation, webpackStatsJson);

var assets = getAssetsFromCompilation(compilation, webpackStatsJson);

var source = asset.source();
var render = evaluate(source, /* filename: */ self.entry, /* scope: */ self.globals, /* includeGlobals: */ true);

var render = evaluate(source, /* filename: */ self.entry, /* scope: */ scope, /* includeGlobals: */ true);
if (render.hasOwnProperty('default')) {
render = render['default'];
}
Expand Down Expand Up @@ -111,6 +114,43 @@ function renderPaths(crawl, userLocals, paths, render, assets, webpackStats, com
return Promise.all(renderPromises);
}

function merge (a, b) {
if (!a || !b) return a
var keys = Object.keys(b)
for (var k, i = 0, n = keys.length; i < n; i++) {
k = keys[i]
a[k] = b[k]
}
return a
}

/*
* Function to handle commonschunk plugin. Currently only supports a manifest file and single external
* library file name vendor.
*/
var loadChunkAssetsToScope = function(globals, compilation, webpackStatsJson) {
var dom = new JSDOM('', { runScripts: 'outside-only' });
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName);

// CommonChunksPlugin will place webpackJsonP manifest loading in last bundle
if (chunkNames[chunkNames.length - 1] === 'manifest') {
chunkNames = ['manifest'].concat(chunkNames.slice(0, -1));
}

var chunkValues = chunkNames.map(function(chunk) {
return findAsset(chunk, compilation, webpackStatsJson);
});

merge(dom.window, globals);

chunkValues.map(function(chunk) {
var script = new vm.Script(chunk.source());
dom.runVMScript(script);
});

return dom.window;
}

var findAsset = function(src, compilation, webpackStatsJson) {
if (!src) {
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName);
Expand Down