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

Handle commonschunkplugin assets #64

Open
wants to merge 4 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
50 changes: 48 additions & 2 deletions index.js
Expand Up @@ -2,6 +2,7 @@ var RawSource = require('webpack-sources/lib/RawSource');
var evaluate = require('eval');
var path = require('path');
var Promise = require('bluebird');
var vm = require('vm');

function StaticSiteGeneratorWebpackPlugin(renderSrc, outputPaths, locals, scope) {
this.renderSrc = renderSrc;
Expand All @@ -27,11 +28,12 @@ StaticSiteGeneratorWebpackPlugin.prototype.apply = function(compiler) {
throw new Error('Source file not found: "' + self.renderSrc + '"');
}

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

var assets = getAssetsFromCompilation(compilation, webpackStatsJson);

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

var render = evaluate(source, /* filename: */ self.renderSrc, /* scope: */ scope, /* includeGlobals: */ true);
if (render.hasOwnProperty('default')) {
render = render['default'];
}
Expand Down Expand Up @@ -81,6 +83,50 @@ StaticSiteGeneratorWebpackPlugin.prototype.apply = function(compiler) {
});
};

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 manifest = findAsset('manifest', compilation, webpackStatsJson);
var vendor = findAsset('vendor', compilation, webpackStatsJson);

if (!manifest || !vendor) {
return scope;
}

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

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

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

var manifestScript = new vm.Script(manifest.source());
manifestScript.runInNewContext(sandbox, {});

merge(sandbox, sandbox.window)

var vendorScript = new vm.Script(vendor.source());
vendorScript.runInNewContext(sandbox, {});

return sandbox.window;
}

var findAsset = function(src, compilation, webpackStatsJson) {
var asset = compilation.assets[src];

Expand Down
@@ -0,0 +1,5 @@
<html>
<body>
<h1>/foo/bar</h1>
</body>
</html>
@@ -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
@@ -0,0 +1,5 @@
<html>
<body>
<h1>/</h1>
</body>
</html>
5 changes: 5 additions & 0 deletions test/success-cases/commonschunk/index.js
@@ -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
@@ -0,0 +1,5 @@
<html>
<body>
<%- html %>
</body>
</html>
35 changes: 35 additions & 0 deletions test/success-cases/commonschunk/webpack.config.js
@@ -0,0 +1,35 @@
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('index.js', paths, { template: template }),
new StatsWriterPlugin() // Causes the asset's `size` method to be called
]
};