This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
apply.js
150 lines (137 loc) · 5.92 KB
/
apply.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* apply.js
*
* This is the meat of our webpack plugin, it's what does all the component
* finding and compiling
*/
import fs from 'fs';
import path from 'path';
import includes from 'lodash/includes';
import ExtraEntryWebpackPlugin from 'extra-entry-webpack-plugin';
import readMultipleFiles from 'read-multiple-files';
import emitAssets from './utils/emitAssets';
import registerPlugins from './registerPlugins';
import registerDefaultPlugins from './registerDefaultPlugins';
import createHtml from './utils/createHtml';
import getCommonsChunkFilename from './utils/getCommonsChunkFilename';
import getBasePath from './utils/getBasePath';
import inProd from './utils/inProd';
function apply(compiler) {
// Warn if users are in production
if (inProd(compiler)) {
// eslint-disable-next-line no-console
console.warn(
'[CarteBlanche] Running CarteBlanche in production is not recommended! It could make your ' +
'application run slower and eat up unnecessary resources, also giving access to your ' +
'servers file system from the outside.'
);
}
const basePath = getBasePath(compiler, this.options);
const filter = this.options.filter;
// Register either the plugins or the default plugins
if (this.options.plugins && this.options.plugins.length > 0) {
registerPlugins(compiler, this.options.plugins);
} else {
registerDefaultPlugins(compiler);
}
// Compile the client
const userBundleFileName = path.posix.join(this.options.dest, 'user-bundle.js');
const userEntries = compiler.options.entry;
const devServerOptions = compiler.options.devServer;
const commonsChunkFilename = getCommonsChunkFilename(compiler.options.plugins);
// Load the dynamic resolve loader with a placeholder file
const extraEntries = [
`!!${require.resolve('./dynamic-resolve.js')}?${
JSON.stringify({
filter: filter.toString(),
componentRoot: this.options.componentRoot,
context: compiler.context,
basePath,
commonsChunkFilename,
})}!${require.resolve('./assets/placeholder.js')}`,
];
// Find out if we need to include the webpack-dev-server client
// TODO Test automatically if the user has any variant (middlware, devserver,...) of HMR enabled
const devServerWithHMR = includes(userEntries, 'webpack-dev-server/client') &&
devServerOptions &&
devServerOptions.hot;
const middlwareWithHMR = includes(userEntries, 'webpack-hot-middleware/client');
if (this.options.hot !== false && (this.options.hot === true || devServerWithHMR)) {
if (includes(userEntries, 'webpack/hot/only-dev-server')) {
extraEntries.unshift('webpack/hot/only-dev-server');
}
extraEntries.unshift(`webpack-dev-server/client?http://${devServerOptions.host}:${devServerOptions.port}`);
} else if (this.options.hot !== false && (this.options.hot === true || middlwareWithHMR)) {
if (includes(userEntries, 'webpack/hot/only-dev-server')) {
extraEntries.unshift('webpack/hot/only-dev-server');
}
extraEntries.unshift('webpack-hot-middleware/client');
}
// Apply the ExtraEntry plugin with our entries above, a unique entryName
// and ouput everything to userBundleFileName
compiler.apply(new ExtraEntryWebpackPlugin({
entry: extraEntries,
entryName: `CarteBlanche_${this.id}`,
outputName: userBundleFileName,
}));
// The client assets, default the HTML to only include the client bundles and the
// user bundle
const clientAssets = {
'index.html': createHtml({ basePath, commonsChunkFilename }),
'client-bundle.js': fs.readFileSync(path.resolve(__dirname, './assets/client-bundle.js')),
'client-bundle.css': fs.readFileSync(path.resolve(__dirname, './assets/client.css')),
'iframe-client-bundle.js': fs.readFileSync(
path.resolve(__dirname, './assets/iframe-client-bundle.js')
),
};
compiler.plugin('emit', (compilation, callback) => {
// If some custom files were passed by the user, default to them
const assets = this.options.files || [];
// Allow plugin developers to add assets to the client
compilation.applyPlugins('carte-blanche-plugin-assets-processing', assets);
// If any custom assets were passed in, read the files from the filesystem
if (assets.length > 0) {
readMultipleFiles(assets, (err, contents) => {
if (err) {
throw err;
}
const scripts = [];
const styles = [];
// Depending on the asset type that was passed add them to a script
// or style tag
assets.forEach((assetFilename, index) => {
switch (assetFilename.substr(-3)) {
case '.js': scripts.push(contents[index]); break;
case 'css': styles.push(contents[index]); break;
default: break;
}
});
// Put together the HTML file based on the assets we got
clientAssets['index.html'] = createHtml({
basePath,
extraScripts: scripts,
extraStyles: styles,
});
emitAssets(compilation, clientAssets, this.options.dest, callback);
});
} else {
// If not custom assets were passed in by neither the user nor any plugins
// emit the defaults straight away
emitAssets(compilation, clientAssets, this.options.dest, callback);
}
});
// Don't add the carte-blanche chunk to html files
compiler.plugin('compilation', (compilation) =>
compilation.plugin('html-webpack-plugin-alter-chunks', (chunks) =>
chunks.filter((chunk) => chunk.files.indexOf(userBundleFileName) === -1)
));
// Log out that CarteBlanche has started
if (devServerOptions && devServerOptions.host && devServerOptions.port) {
// eslint-disable-next-line no-console
console.log(`CarteBlanche started at http://${devServerOptions.host}:${devServerOptions.port}/${basePath}`);
} else {
// eslint-disable-next-line no-console
console.log(`CarteBlanche started at /${basePath}`);
}
}
export default apply;