-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
99 lines (81 loc) · 2.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var React = require('react');
var evaluate = require('eval');
// src can be either a filename or a chunk name
function ReactToHtmlWebpackPlugin(destPath, src, options) {
this.src = src;
this.destPath = destPath;
this.options = typeof options === 'object' ? options : {};
}
ReactToHtmlWebpackPlugin.prototype.apply = function(compiler) {
compiler.plugin('emit', function(compiler, done) {
var webpackStatsJson = compiler.getStats().toJson();
try {
var asset = findAsset(this.src, compiler, webpackStatsJson);
if (!asset) {
throw new Error('Output file not found: "' + this.src + '"');
}
var source = asset.source();
var Component = evaluate(source, /* filename: */ undefined, /* scope: */ undefined, /* includeGlobals: */ true);
var renderMethod = this.options.static ? 'renderToStaticMarkup' : 'renderToString';
var html = React[renderMethod](React.createElement(Component));
var template = this.options.template;
if (template != null && typeof template !== 'function') {
throw new Error('Template must be a function');
}
var output = typeof template === 'function' ?
template({
html: html,
assets: getAssetsFromCompiler(compiler, webpackStatsJson)
}) :
html;
compiler.assets[this.destPath] = createAssetFromContents(output);
} catch (err) {
return done(err);
}
done();
}.bind(this));
};
var findAsset = function(src, compiler, webpackStatsJson) {
var asset = compiler.assets[src];
if (asset) {
return asset;
}
var chunkValue = webpackStatsJson.assetsByChunkName[src];
if (!chunkValue) {
return null;
}
// Webpack outputs an array for each chunk when using sourcemaps
if (chunkValue instanceof Array) {
// Is the main bundle always the first element?
chunkValue = chunkValue[0];
}
return compiler.assets[chunkValue];
};
// Shamelessly stolen from html-webpack-plugin - Thanks @ampedandwired :)
var getAssetsFromCompiler = function(compiler, webpackStatsJson) {
var assets = {};
for (var chunk in webpackStatsJson.assetsByChunkName) {
var chunkValue = webpackStatsJson.assetsByChunkName[chunk];
// Webpack outputs an array for each chunk when using sourcemaps
if (chunkValue instanceof Array) {
// Is the main bundle always the first element?
chunkValue = chunkValue[0];
}
if (compiler.options.output.publicPath) {
chunkValue = compiler.options.output.publicPath + chunkValue;
}
assets[chunk] = chunkValue;
}
return assets;
};
var createAssetFromContents = function(contents) {
return {
source: function() {
return contents;
},
size: function() {
return contents.length;
}
};
};
module.exports = ReactToHtmlWebpackPlugin;