1 | import path from 'path'
|
2 |
|
3 | import write_assets from './write assets'
|
4 | import notify_stats from './notify stats'
|
5 |
|
6 | import Log from './../tools/log'
|
7 |
|
8 | import { exists, clone, convert_from_camel_case, alias_properties_with_camel_case } from './../helpers'
|
9 |
|
10 | import { default_webpack_assets, normalize_options, verbosity_levels } from './../common'
|
11 |
|
12 |
|
13 | export default function Webpack_isomorphic_tools_plugin(options)
|
14 | {
|
15 |
|
16 | this.options = convert_from_camel_case(clone(options))
|
17 |
|
18 |
|
19 | normalize_options(this.options)
|
20 |
|
21 |
|
22 | this.log = new Log('webpack-isomorphic-tools/plugin', { debug: this.options.debug })
|
23 |
|
24 |
|
25 |
|
26 | this.regular_expressions = {}
|
27 |
|
28 |
|
29 | for (let asset_type of Object.keys(this.options.assets))
|
30 | {
|
31 | const description = this.options.assets[asset_type]
|
32 |
|
33 |
|
34 | this.regular_expressions[asset_type] = description.regular_expression || Webpack_isomorphic_tools_plugin.regular_expression(description.extensions)
|
35 | }
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 | Webpack_isomorphic_tools_plugin.prototype.start_dev_server = function()
|
41 | {
|
42 | const express = require('express')
|
43 | const app = express()
|
44 |
|
45 | app.get('/', (request, response) =>
|
46 | {
|
47 | if (!this.assets)
|
48 | {
|
49 | return response.status(404).send('Webpack assets not generated yet')
|
50 | }
|
51 |
|
52 | response.send(this.assets)
|
53 | })
|
54 |
|
55 | app.listen(this.options.port, () =>
|
56 | {
|
57 | this.log.info(`HTTP service listening on port ${this.options.port}`)
|
58 | })
|
59 | }
|
60 |
|
61 |
|
62 | Webpack_isomorphic_tools_plugin.prototype.regular_expression = function(asset_type)
|
63 | {
|
64 | if (!exists(this.regular_expressions[asset_type]))
|
65 | {
|
66 | throw new Error(`There's no asset type "${asset_type}" defined in webpack-isomorphic-tools configuration. Perhaps you didn't spell it correctly.`)
|
67 | }
|
68 |
|
69 | return this.regular_expressions[asset_type]
|
70 | }
|
71 |
|
72 |
|
73 | Webpack_isomorphic_tools_plugin.prototype.regexp = Webpack_isomorphic_tools_plugin.prototype.regular_expression
|
74 |
|
75 |
|
76 | Webpack_isomorphic_tools_plugin.regular_expression = function(extensions)
|
77 | {
|
78 | if (!Array.isArray(extensions))
|
79 | {
|
80 | throw new Error(`You were expected to pass a list of extensions (an array). Instead got: ${extensions}. Maybe you were looking for the instance method istead of the class method of this plugin?`)
|
81 | }
|
82 |
|
83 | let matcher
|
84 | if (extensions.length > 1)
|
85 | {
|
86 | matcher = `(${extensions.join('|')})`
|
87 | }
|
88 | else
|
89 | {
|
90 | matcher = extensions
|
91 | }
|
92 |
|
93 | return new RegExp(`\\.${matcher}$`)
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 | Webpack_isomorphic_tools_plugin.prototype.development = function(flag)
|
99 | {
|
100 |
|
101 | this.options.development = exists(flag) ? flag : true
|
102 |
|
103 | if (this.options.development)
|
104 | {
|
105 | this.log.debug('entering development mode')
|
106 | }
|
107 | else
|
108 | {
|
109 | this.log.debug('entering production mode')
|
110 | }
|
111 |
|
112 |
|
113 | return this
|
114 | }
|
115 |
|
116 |
|
117 | Webpack_isomorphic_tools_plugin.prototype.apply = function(compiler)
|
118 | {
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | if (this.options.development && this.options.port)
|
125 | {
|
126 | this.start_dev_server()
|
127 | }
|
128 |
|
129 |
|
130 | const webpack_configuration = compiler.options
|
131 |
|
132 |
|
133 | if (!webpack_configuration.context)
|
134 | {
|
135 | throw new Error('You must specify ".context" in your webpack configuration')
|
136 | }
|
137 |
|
138 |
|
139 | this.options.project_path = webpack_configuration.context
|
140 |
|
141 |
|
142 | const webpack_assets_path = path.resolve(this.options.project_path, this.options.webpack_assets_file_path)
|
143 |
|
144 |
|
145 | const webpack_stats_path = path.resolve(this.options.project_path, this.options.webpack_stats_file_path)
|
146 |
|
147 |
|
148 | const plugin = this
|
149 |
|
150 |
|
151 |
|
152 | compiler.plugin('done', function(stats)
|
153 | {
|
154 | plugin.log.debug('------------------- Started -------------------')
|
155 |
|
156 | const json = stats.toJson
|
157 | ({
|
158 | context: webpack_configuration.context
|
159 | })
|
160 |
|
161 |
|
162 | if (plugin.options.development && plugin.options.verbosity !== verbosity_levels.no_webpack_stats)
|
163 | {
|
164 |
|
165 |
|
166 | notify_stats(stats, json, plugin.options.verbosity === verbosity_levels.webpack_stats_for_each_build)
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | const assets_base_url = (webpack_configuration.devServer && webpack_configuration.devServer.publicPath) ? webpack_configuration.devServer.publicPath : json.publicPath
|
179 |
|
180 |
|
181 | const serve_assets_from_memory = plugin.options.development && plugin.options.port
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | plugin.assets = write_assets(json,
|
187 | {
|
188 | development : plugin.options.development,
|
189 | debug : plugin.options.debug,
|
190 | assets : plugin.options.assets,
|
191 | alias : plugin.options.alias,
|
192 | project_path : plugin.options.project_path,
|
193 | assets_base_url,
|
194 | webpack_assets_path : webpack_assets_path,
|
195 | webpack_stats_path : webpack_stats_path,
|
196 | output : default_webpack_assets(),
|
197 | output_to_a_file : !serve_assets_from_memory,
|
198 | regular_expressions : plugin.regular_expressions
|
199 | },
|
200 | plugin.log)
|
201 |
|
202 | plugin.log.debug('------------------- Finished -------------------')
|
203 | })
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 | Webpack_isomorphic_tools_plugin.url_loader_parser = function(module, options, log)
|
209 | {
|
210 | return module.source
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 | Webpack_isomorphic_tools_plugin.css_loader_parser = function(module, options, log)
|
216 | {
|
217 | return module.source + '\n module.exports = module.exports.toString();'
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 | Webpack_isomorphic_tools_plugin.css_modules_loader_parser = function(module, options, log)
|
223 | {
|
224 | return module.source + '\n module.exports = exports.locals || {}; module.exports._style = exports.toString();'
|
225 | }
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | Webpack_isomorphic_tools_plugin.style_loader_filter = function(module, regular_expression, options, log)
|
234 | {
|
235 | const css_loader = module.name.split('!')[0]
|
236 | return regular_expression.test(module.name) &&
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | (css_loader.indexOf('/~/css-loader') > 0 ||
|
255 | css_loader.indexOf('/~/.npminstall/css-loader') > 0 ||
|
256 | css_loader.indexOf('/~/.store/css-loader') > 0)
|
257 | }
|
258 |
|
259 |
|
260 | Webpack_isomorphic_tools_plugin.style_loader_path_extractor = function(module, options, log)
|
261 | {
|
262 | return module.name.slice(module.name.lastIndexOf('!') + 1)
|
263 | }
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | Webpack_isomorphic_tools_plugin.prototype.regularExpression = Webpack_isomorphic_tools_plugin.prototype.regular_expression
|
273 |
|
274 | Webpack_isomorphic_tools_plugin.urlLoaderParser = Webpack_isomorphic_tools_plugin.url_loader_parser
|
275 | Webpack_isomorphic_tools_plugin.cssLoaderParser = Webpack_isomorphic_tools_plugin.css_loader_parser
|
276 | Webpack_isomorphic_tools_plugin.cssModulesLoaderParser = Webpack_isomorphic_tools_plugin.css_modules_loader_parser
|
277 | Webpack_isomorphic_tools_plugin.styleLoaderFilter = Webpack_isomorphic_tools_plugin.style_loader_filter
|
278 | Webpack_isomorphic_tools_plugin.styleLoaderPathExtractor = Webpack_isomorphic_tools_plugin.style_loader_path_extractor |
\ | No newline at end of file |