]>
Commit | Line | Data |
---|---|---|
7de41f10 AM |
1 | 'use strict' |
2 | ||
7f0547d4 | 3 | const debug_ = require('debug'); |
4 | ||
5 | const debug = debug_('purs-loader'); | |
6 | ||
7 | const debugVerbose = debug_('purs-loader:verbose'); | |
1c12889c | 8 | |
7de41f10 | 9 | const loaderUtils = require('loader-utils') |
1c12889c | 10 | |
7de41f10 | 11 | const Promise = require('bluebird') |
1c12889c | 12 | |
7de41f10 | 13 | const path = require('path') |
1c12889c | 14 | |
15 | const PsModuleMap = require('./purs-module-map'); | |
16 | ||
17 | const compile = require('./compile'); | |
18 | ||
19 | const bundle = require('./bundle'); | |
20 | ||
21 | const ide = require('./ide'); | |
22 | ||
8e21ab0a | 23 | const toJavaScript = require('./to-javascript'); |
1c12889c | 24 | |
466c0068 | 25 | const sourceMaps = require('./source-maps'); |
26 | ||
531c751f | 27 | const dargs = require('./dargs'); |
1c12889c | 28 | |
86e2b3d4 | 29 | const spawn = require('cross-spawn').sync |
1c12889c | 30 | |
86e2b3d4 | 31 | const eol = require('os').EOL |
7de41f10 | 32 | |
7de41f10 | 33 | module.exports = function purescriptLoader(source, map) { |
7f0547d4 | 34 | this.cacheable && this.cacheable(); |
35 | ||
7f0547d4 | 36 | const webpackConfig = this.options; |
37 | ||
e1719658 | 38 | var cache = webpackConfig.purescriptLoaderCache = webpackConfig.purescriptLoaderCache || { |
39 | rebuild: false, | |
40 | deferred: [], | |
41 | bundleModules: [], | |
42 | ideServer: null, | |
43 | psModuleMap: null, | |
44 | warnings: [], | |
45 | errors: [], | |
46 | compilationStarted: false, | |
47 | compilationFinished: false, | |
48 | installed: false, | |
49 | srcOption: [] | |
50 | }; | |
51 | ||
52 | const callback = this.async(); | |
53 | ||
7f0547d4 | 54 | const loaderOptions = loaderUtils.getOptions(this) || {}; |
7de41f10 | 55 | |
7f0547d4 | 56 | const srcOption = (pscPackage => { |
e1719658 | 57 | const srcPath = path.join('src', '**', '*.purs'); |
58 | ||
59 | const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs'); | |
60 | ||
61 | if (cache.srcOption.length > 0) { | |
62 | return cache.srcOption; | |
63 | } | |
64 | else if (pscPackage) { | |
7f0547d4 | 65 | const pscPackageCommand = 'psc-package'; |
86e2b3d4 | 66 | |
7f0547d4 | 67 | const pscPackageArgs = ['sources']; |
68 | ||
e1719658 | 69 | const loaderSrc = loaderOptions.src || [ |
70 | srcPath | |
71 | ]; | |
72 | ||
7f0547d4 | 73 | debug('psc-package %s %o', pscPackageCommand, pscPackageArgs); |
74 | ||
e1719658 | 75 | const cmd = spawn(pscPackageCommand, pscPackageArgs); |
76 | ||
6ccb09a5 | 77 | if (cmd.error) { |
78 | throw new Error(cmd.error); | |
79 | } | |
80 | else if (cmd.status !== 0) { | |
e1719658 | 81 | const error = cmd.stdout.toString(); |
82 | ||
83 | throw new Error(error); | |
84 | } | |
85 | else { | |
86 | const result = cmd.stdout.toString().split(eol).filter(v => v != '').concat(loaderSrc); | |
87 | ||
88 | debug('psc-package result: %o', result); | |
89 | ||
90 | cache.srcOption = result; | |
91 | ||
92 | return result; | |
93 | } | |
86e2b3d4 AD |
94 | } |
95 | else { | |
e1719658 | 96 | const result = loaderOptions.src || [ |
97 | bowerPath, | |
98 | srcPath | |
7f0547d4 | 99 | ]; |
e1719658 | 100 | |
101 | cache.srcOption = result; | |
102 | ||
103 | return result; | |
86e2b3d4 | 104 | } |
7f0547d4 | 105 | })(loaderOptions.pscPackage); |
86e2b3d4 | 106 | |
7f0547d4 | 107 | const options = Object.assign({ |
108 | context: webpackConfig.context, | |
1c12889c | 109 | psc: null, |
7de41f10 | 110 | pscArgs: {}, |
1c12889c | 111 | pscBundle: null, |
7de41f10 | 112 | pscBundleArgs: {}, |
71a96808 | 113 | pscIdeClient: null, |
114 | pscIdeClientArgs: {}, | |
115 | pscIdeServer: null, | |
116 | pscIdeServerArgs: {}, | |
5163b5a2 | 117 | pscIde: false, |
7f0547d4 | 118 | pscIdeColors: loaderOptions.psc === 'psa', |
86e2b3d4 | 119 | pscPackage: false, |
7de41f10 AM |
120 | bundleOutput: 'output/bundle.js', |
121 | bundleNamespace: 'PS', | |
122 | bundle: false, | |
123 | warnings: true, | |
df8798fa | 124 | watch: false, |
7de41f10 | 125 | output: 'output', |
7f0547d4 | 126 | src: [] |
127 | }, loaderOptions, { | |
128 | src: srcOption | |
129 | }); | |
7de41f10 | 130 | |
e1719658 | 131 | if (!cache.installed) { |
7f0547d4 | 132 | debugVerbose('installing purs-loader with options: %O', options); |
86e2b3d4 | 133 | |
e1719658 | 134 | cache.installed = true; |
7de41f10 AM |
135 | |
136 | // invalidate loader cache when bundle is marked as invalid (in watch mode) | |
137 | this._compiler.plugin('invalid', () => { | |
7f0547d4 | 138 | debugVerbose('invalidating loader cache'); |
531c751f | 139 | |
7f0547d4 | 140 | cache = webpackConfig.purescriptLoaderCache = { |
5163b5a2 | 141 | rebuild: options.pscIde, |
7de41f10 | 142 | deferred: [], |
531c751f | 143 | bundleModules: [], |
144 | ideServer: cache.ideServer, | |
b683b0b1 | 145 | psModuleMap: cache.psModuleMap, |
146 | warnings: [], | |
e1719658 | 147 | errors: [], |
78e2b0d9 | 148 | compilationStarted: false, |
149 | compilationFinished: false, | |
e1719658 | 150 | installed: cache.installed, |
6ccb09a5 | 151 | srcOption: [] |
7f0547d4 | 152 | }; |
b683b0b1 | 153 | }); |
154 | ||
155 | // add psc warnings to webpack compilation warnings | |
156 | this._compiler.plugin('after-compile', (compilation, callback) => { | |
157 | cache.warnings.forEach(warning => { | |
158 | compilation.warnings.push(warning); | |
159 | }); | |
160 | ||
161 | cache.errors.forEach(error => { | |
162 | compilation.errors.push(error); | |
163 | }); | |
164 | ||
165 | callback() | |
166 | }); | |
7de41f10 AM |
167 | } |
168 | ||
7f0547d4 | 169 | const psModuleName = PsModuleMap.matchModule(source); |
170 | ||
7de41f10 AM |
171 | const psModule = { |
172 | name: psModuleName, | |
466c0068 | 173 | source: source, |
174 | load: ({js, map}) => callback(null, js, map), | |
7de41f10 AM |
175 | reject: error => callback(error), |
176 | srcPath: this.resourcePath, | |
466c0068 | 177 | remainingRequest: loaderUtils.getRemainingRequest(this), |
7de41f10 AM |
178 | srcDir: path.dirname(this.resourcePath), |
179 | jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), | |
180 | options: options, | |
181 | cache: cache, | |
b683b0b1 | 182 | emitWarning: warning => { |
183 | if (options.warnings && warning.length) { | |
184 | cache.warnings.push(warning); | |
185 | } | |
186 | }, | |
187 | emitError: error => { | |
188 | if (error.length) { | |
189 | cache.errors.push(error); | |
190 | } | |
191 | } | |
7de41f10 AM |
192 | } |
193 | ||
7f0547d4 | 194 | debug('loading %s', psModule.name); |
67888496 | 195 | |
7de41f10 | 196 | if (options.bundle) { |
7f0547d4 | 197 | cache.bundleModules.push(psModule.name); |
7de41f10 AM |
198 | } |
199 | ||
200 | if (cache.rebuild) { | |
7f0547d4 | 201 | const connect = () => { |
202 | if (!cache.ideServer) { | |
203 | cache.ideServer = true; | |
204 | ||
205 | return ide.connect(psModule) | |
206 | .then(ideServer => { | |
207 | cache.ideServer = ideServer; | |
208 | return psModule; | |
209 | }) | |
210 | .then(ide.loadWithRetry) | |
211 | .catch(error => { | |
212 | if (cache.ideServer.kill) { | |
213 | debug('ide failed to initially load modules, stopping the ide server process'); | |
214 | ||
215 | cache.ideServer.kill(); | |
216 | } | |
217 | ||
218 | cache.ideServer = null; | |
219 | ||
220 | return Promise.reject(error); | |
221 | }) | |
222 | ; | |
223 | } | |
224 | else { | |
225 | return Promise.resolve(psModule); | |
226 | } | |
227 | }; | |
228 | ||
229 | const rebuild = () => | |
78e2b0d9 | 230 | ide.rebuild(psModule) |
231 | .then(() => | |
232 | toJavaScript(psModule) | |
466c0068 | 233 | .then(js => sourceMaps(psModule, js)) |
78e2b0d9 | 234 | .then(psModule.load) |
235 | .catch(psModule.reject) | |
236 | ) | |
237 | .catch(error => { | |
7f0547d4 | 238 | if (error instanceof ide.UnknownModuleError) { |
78e2b0d9 | 239 | // Store the modules that trigger a recompile due to an |
240 | // unknown module error. We need to wait until compilation is | |
241 | // done before loading these files. | |
242 | ||
243 | cache.deferred.push(psModule); | |
244 | ||
7f0547d4 | 245 | if (!cache.compilationStarted) { |
246 | cache.compilationStarted = true; | |
247 | ||
248 | return compile(psModule) | |
249 | .then(() => { | |
250 | cache.compilationFinished = true; | |
251 | }) | |
252 | .then(() => | |
253 | PsModuleMap.makeMap(options.src).then(map => { | |
254 | debug('rebuilt module map after unknown module forced a recompilation'); | |
255 | ||
256 | cache.psModuleMap = map; | |
257 | }) | |
258 | ) | |
78e2b0d9 | 259 | .then(() => |
260 | Promise.map(cache.deferred, psModule => | |
261 | ide.load(psModule) | |
262 | .then(() => toJavaScript(psModule)) | |
466c0068 | 263 | .then(js => sourceMaps(psModule, js)) |
78e2b0d9 | 264 | .then(psModule.load) |
265 | ) | |
266 | ) | |
267 | .catch(error => { | |
268 | cache.deferred[0].reject(error); | |
269 | ||
270 | cache.deferred.slice(1).forEach(psModule => { | |
271 | psModule.reject(new Error('purs-loader failed')); | |
272 | }) | |
273 | }) | |
7f0547d4 | 274 | ; |
275 | } | |
276 | else { | |
78e2b0d9 | 277 | // The compilation has started. We must wait until it is |
278 | // done in order to ensure the module map contains all of | |
279 | // the unknown modules. | |
7f0547d4 | 280 | } |
281 | } | |
282 | else { | |
283 | debug('ide rebuild failed due to an unhandled error: %o', error); | |
284 | ||
78e2b0d9 | 285 | psModule.reject(error); |
7f0547d4 | 286 | } |
287 | }) | |
288 | ; | |
289 | ||
78e2b0d9 | 290 | connect().then(rebuild); |
7de41f10 | 291 | } |
7f0547d4 | 292 | else if (cache.compilationFinished) { |
293 | debugVerbose('compilation is already finished, loading module %s', psModule.name); | |
7de41f10 | 294 | |
7f0547d4 | 295 | toJavaScript(psModule) |
466c0068 | 296 | .then(js => sourceMaps(psModule, js)) |
7f0547d4 | 297 | .then(psModule.load) |
298 | .catch(psModule.reject); | |
7de41f10 | 299 | } |
7f0547d4 | 300 | else { |
301 | // The compilation has not finished yet. We need to wait for | |
302 | // compilation to finish before the loaders run so that references | |
303 | // to compiled output are valid. Push the modules into the cache to | |
304 | // be loaded once the complation is complete. | |
305 | ||
306 | cache.deferred.push(psModule); | |
307 | ||
308 | if (!cache.compilationStarted) { | |
309 | cache.compilationStarted = true; | |
310 | ||
311 | compile(psModule) | |
312 | .then(() => { | |
313 | cache.compilationFinished = true; | |
314 | }) | |
315 | .then(() => { | |
316 | if (options.bundle) { | |
317 | return bundle(options, cache.bundleModules); | |
318 | } | |
319 | }) | |
320 | .then(() => | |
1c12889c | 321 | PsModuleMap.makeMap(options.src).then(map => { |
7f0547d4 | 322 | debug('rebuilt module map after compilation'); |
323 | ||
1c12889c | 324 | cache.psModuleMap = map; |
325 | }) | |
7f0547d4 | 326 | ) |
327 | .then(() => | |
328 | Promise.map(cache.deferred, psModule => | |
78e2b0d9 | 329 | toJavaScript(psModule) |
466c0068 | 330 | .then(js => sourceMaps(psModule, js)) |
78e2b0d9 | 331 | .then(psModule.load) |
7f0547d4 | 332 | ) |
333 | ) | |
334 | .catch(error => { | |
335 | cache.deferred[0].reject(error); | |
336 | ||
337 | cache.deferred.slice(1).forEach(psModule => { | |
338 | psModule.reject(new Error('purs-loader failed')); | |
339 | }) | |
340 | }) | |
341 | ; | |
342 | } | |
343 | else { | |
344 | // The complation has started. Nothing to do but wait until it is | |
345 | // done before loading all of the modules. | |
346 | } | |
7de41f10 AM |
347 | } |
348 | } |