Skip to content

Commit

Permalink
Merge c39fcdc into 09001fe
Browse files Browse the repository at this point in the history
  • Loading branch information
HorizonXP committed Mar 23, 2017
2 parents 09001fe + c39fcdc commit 4063854
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 2 deletions.
47 changes: 45 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var cheerio = require('cheerio');
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 +37,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 +113,47 @@ 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(scope, compilation, webpackStatsJson) {
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName)
var chunkValues = chunkNames.map(function(chunk) {
return findAsset(chunk, compilation, webpackStatsJson);
});

if(!scope) {
scope = {};
}

if (!scope.window) {
scope.window = {};
}

var sandbox = {};
merge(sandbox, scope);

// CommonChunksPlugin will place webpackJsonP manifest loading in last bundle
chunkValues.reverse().map(function(chunk) {
var script = new vm.Script(chunk.source());
script.runInNewContext(sandbox, {});
merge(sandbox, sandbox.window);
});

return sandbox;
}

var findAsset = function(src, compilation, webpackStatsJson) {
if (!src) {
var chunkNames = Object.keys(webpackStatsJson.assetsByChunkName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>/foo/bar</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>/foo</h1>
</body>
</html>
5 changes: 5 additions & 0 deletions test/success-cases/commonschunk/expected-output/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>/</h1>
</body>
</html>
5 changes: 5 additions & 0 deletions test/success-cases/commonschunk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(locals, callback) {
setTimeout(function() {
callback(null, locals.template({ html: '<h1>' + locals.path + '</h1>' }));
}, 10);
};
5 changes: 5 additions & 0 deletions test/success-cases/commonschunk/template.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%- html %>
</body>
</html>
41 changes: 41 additions & 0 deletions test/success-cases/commonschunk/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var StaticSiteGeneratorPlugin = require('../../../');
var StatsWriterPlugin = require("webpack-stats-plugin").StatsWriterPlugin;
var webpack = require('webpack');
var ejs = require('ejs');
var fs = require('fs');

var template = ejs.compile(fs.readFileSync(__dirname + '/template.ejs', 'utf-8'))

var paths = [
'/',
'/foo',
'/foo/bar'
];

module.exports = {
entry: {
index: __dirname + '/index.js',
vendor: 'bluebird'
},

output: {
filename: '[name].js',
path: __dirname + '/actual-output',
libraryTarget: 'umd'
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest'],
minChunks: Infinity
}),
new StaticSiteGeneratorPlugin({
entry: 'index',
paths: paths,
locals: {
template: template
}
}),
new StatsWriterPlugin() // Causes the asset's `size` method to be called
]
};

0 comments on commit 4063854

Please sign in to comment.