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