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