UNPKG

6.77 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _fs = require('fs');
8
9var _fs2 = _interopRequireDefault(_fs);
10
11var _path = require('path');
12
13var _path2 = _interopRequireDefault(_path);
14
15var _includes = require('lodash/includes');
16
17var _includes2 = _interopRequireDefault(_includes);
18
19var _extraEntryWebpackPlugin = require('extra-entry-webpack-plugin');
20
21var _extraEntryWebpackPlugin2 = _interopRequireDefault(_extraEntryWebpackPlugin);
22
23var _readMultipleFiles = require('read-multiple-files');
24
25var _readMultipleFiles2 = _interopRequireDefault(_readMultipleFiles);
26
27var _emitAssets = require('./utils/emitAssets');
28
29var _emitAssets2 = _interopRequireDefault(_emitAssets);
30
31var _registerPlugins = require('./registerPlugins');
32
33var _registerPlugins2 = _interopRequireDefault(_registerPlugins);
34
35var _registerDefaultPlugins = require('./registerDefaultPlugins');
36
37var _registerDefaultPlugins2 = _interopRequireDefault(_registerDefaultPlugins);
38
39var _createHTML = require('./utils/createHTML');
40
41var _createHTML2 = _interopRequireDefault(_createHTML);
42
43var _getCommonsChunkFilename = require('./utils/getCommonsChunkFilename');
44
45var _getCommonsChunkFilename2 = _interopRequireDefault(_getCommonsChunkFilename);
46
47function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
48
49/**
50 * apply.js
51 *
52 * This is the meat of our webpack plugin, it's what does all the component
53 * finding and compiling
54 */
55
56function apply(compiler) {
57 var _this = this;
58
59 var dest = this.options.dest;
60 var filter = this.options.filter;
61
62 // Register either the plugins or the default plugins
63 if (this.options.plugins && this.options.plugins.length > 0) {
64 (0, _registerPlugins2.default)(compiler, this.options.plugins);
65 } else {
66 (0, _registerDefaultPlugins2.default)(compiler);
67 }
68
69 // Compile the client
70 var userBundleFileName = _path2.default.join(dest, 'user-bundle.js');
71 var userEntries = compiler.options.entry;
72 var devServerOptions = compiler.options.devServer;
73 var commonsChunkFilename = (0, _getCommonsChunkFilename2.default)(compiler.options.plugins);
74
75 // Load the dynamic resolve loader with a placeholder file
76 var extraEntries = ['!!' + require.resolve('./dynamic-resolve.js') + '?' + JSON.stringify({
77 filter: filter.toString(),
78 componentRoot: this.options.componentRoot,
79 context: compiler.context,
80 dest: this.options.dest,
81 commonsChunkFilename: commonsChunkFilename
82 }) + '!' + require.resolve('./assets/placeholder.js')];
83 // Find out if we need to include the webpack-dev-server client
84 // TODO Test automatically if the user has any variant (middlware, devserver,...) of HMR enabled
85 var devServerWithHMR = (0, _includes2.default)(userEntries, 'webpack-dev-server/client') && devServerOptions && devServerOptions.hot;
86 var middlwareWithHMR = (0, _includes2.default)(userEntries, 'webpack-hot-middleware/client');
87
88 if (this.options.hot !== false && (this.options.hot === true || devServerWithHMR)) {
89 if ((0, _includes2.default)(userEntries, 'webpack/hot/only-dev-server')) {
90 extraEntries.unshift('webpack/hot/only-dev-server');
91 }
92 extraEntries.unshift('webpack-dev-server/client?http://' + devServerOptions.host + ':' + devServerOptions.port);
93 } else if (this.options.hot !== false && (this.options.hot === true || middlwareWithHMR)) {
94 if ((0, _includes2.default)(userEntries, 'webpack/hot/only-dev-server')) {
95 extraEntries.unshift('webpack/hot/only-dev-server');
96 }
97 extraEntries.unshift('webpack-hot-middleware/client');
98 }
99 // Apply the ExtraEntry plugin with our entries above, a unique entryName
100 // and ouput everything to userBundleFileName
101 compiler.apply(new _extraEntryWebpackPlugin2.default({
102 entry: extraEntries,
103 entryName: 'CarteBlanche [' + this.id + ']',
104 outputName: userBundleFileName
105 }));
106
107 // The client assets, default the HTML to only include the client bundles and the
108 // user bundle
109 var clientAssets = {
110 'index.html': (0, _createHTML2.default)({ dest: dest, commonsChunkFilename: commonsChunkFilename }),
111 'client-bundle.js': _fs2.default.readFileSync(_path2.default.resolve(__dirname, './assets/client-bundle.js')),
112 'client-bundle.css': _fs2.default.readFileSync(_path2.default.resolve(__dirname, './assets/client.css')),
113 'iframe-client-bundle.js': _fs2.default.readFileSync(_path2.default.resolve(__dirname, './assets/iframe-client-bundle.js'))
114 };
115
116 compiler.plugin('emit', function (compilation, callback) {
117 // If some custom files were passed by the user, default to them
118 var assets = _this.options.files || [];
119 // Allow plugin developers to add assets to the client
120 compilation.applyPlugins('carte-blanche-plugin-assets-processing', assets);
121 // If any custom assets were passed in, read the files from the filesystem
122 if (assets.length > 0) {
123 (0, _readMultipleFiles2.default)(assets, function (err, contents) {
124 if (err) {
125 throw err;
126 }
127 var scripts = [];
128 var styles = [];
129 // Depending on the asset type that was passed add them to a script
130 // or style tag
131 assets.forEach(function (assetFilename, index) {
132 switch (assetFilename.substr(-3)) {
133 case '.js':
134 scripts.push(contents[index]);break;
135 case 'css':
136 styles.push(contents[index]);break;
137 default:
138 break;
139 }
140 });
141 // Put together the HTML file based on the assets we got
142 clientAssets['index.html'] = (0, _createHTML2.default)({
143 dest: dest,
144 extraScripts: scripts,
145 extraStyles: styles
146 });
147 (0, _emitAssets2.default)(compilation, clientAssets, dest, callback);
148 });
149 } else {
150 // If not custom assets were passed in by neither the user nor any plugins
151 // emit the defaults straight away
152 (0, _emitAssets2.default)(compilation, clientAssets, dest, callback);
153 }
154 });
155
156 // Don't add the carte-blanche chunk to html files
157 compiler.plugin('compilation', function (compilation) {
158 return compilation.plugin('html-webpack-plugin-alter-chunks', function (chunks) {
159 return chunks.filter(function (chunk) {
160 return chunk.files.indexOf(userBundleFileName) === -1;
161 });
162 });
163 });
164
165 // Log out that CarteBlanche has started
166 if (devServerOptions && devServerOptions.host && devServerOptions.port) {
167 // eslint-disable-next-line no-console
168 console.log('CarteBlanche started at http://' + devServerOptions.host + ':' + devServerOptions.port + '/' + dest + '!');
169 } else {
170 // eslint-disable-next-line no-console
171 console.log('CarteBlanche started at /' + dest);
172 }
173}
174
175exports.default = apply;
\No newline at end of file