3 var _slicedToArray = function () { function sliceIterator(arr
, i
) { var _arr
= []; var _n
= true; var _d
= false; var _e
= undefined; try { for (var _i
= arr
[Symbol
.iterator
](), _s
; !(_n
= (_s
= _i
.next()).done
); _n
= true) { _arr
.push(_s
.value
); if (i
&& _arr
.length
=== i
) break; } } catch (err
) { _d
= true; _e
= err
; } finally { try { if (!_n
&& _i
["return"]) _i
["return"](); } finally { if (_d
) throw _e
; } } return _arr
; } return function (arr
, i
) { if (Array
.isArray(arr
)) { return arr
; } else if (Symbol
.iterator
in Object(arr
)) { return sliceIterator(arr
, i
); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
5 var debug_
= require('debug');
7 var debug
= debug_('purs-loader');
9 var debugVerbose
= debug_('purs-loader:verbose');
11 var loaderUtils
= require('loader-utils');
13 var Promise
= require('bluebird');
15 var path
= require('path');
17 var PsModuleMap
= require('./purs-module-map');
19 var compile
= require('./compile');
21 var bundle
= require('./bundle');
23 var ide
= require('./ide');
25 var toJavaScript
= require('./to-javascript');
27 var sourceMaps
= require('./source-maps');
29 var dargs
= require('./dargs');
31 var utils
= require('./utils');
33 var spawn
= require('cross-spawn').sync
;
35 var eol
= require('os').EOL
;
45 compilationStarted: false,
46 compilationFinished: false,
47 compilationFailed: false,
52 module
.exports
= function purescriptLoader(source
, map
) {
55 this.cacheable
&& this.cacheable();
57 var webpackContext
= this.options
&& this.options
.context
|| this.rootContext
;
59 var callback
= this.async();
61 var loaderOptions
= loaderUtils
.getOptions(this) || {};
63 var srcOption = function (pscPackage
) {
64 var srcPath
= path
.join('src', '**', '*.purs');
66 var bowerPath
= path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
68 if (CACHE_VAR
.srcOption
.length
> 0) {
69 return CACHE_VAR
.srcOption
;
70 } else if (pscPackage
) {
71 var pscPackageCommand
= 'psc-package';
73 var pscPackageArgs
= ['sources'];
75 var loaderSrc
= loaderOptions
.src
|| [srcPath
];
77 debug('psc-package %s %o', pscPackageCommand
, pscPackageArgs
);
79 var cmd
= spawn(pscPackageCommand
, pscPackageArgs
);
82 throw new Error(cmd
.error
);
83 } else if (cmd
.status
!== 0) {
84 var error
= cmd
.stdout
.toString();
86 throw new Error(error
);
88 var result
= cmd
.stdout
.toString().split(eol
).filter(function (v
) {
92 debug('psc-package result: %o', result
);
94 CACHE_VAR
.srcOption
= result
;
99 var _result
= loaderOptions
.src
|| [bowerPath
, srcPath
];
101 CACHE_VAR
.srcOption
= _result
;
105 }(loaderOptions
.pscPackage
);
107 var options
= Object
.assign({
108 context: webpackContext
,
114 pscIdeClientArgs: {},
116 pscIdeServerArgs: {},
118 pscIdeColors: loaderOptions
.psc
=== 'psa',
120 bundleOutput: 'output/bundle.js',
121 bundleNamespace: 'PS',
132 if (!CACHE_VAR
.installed
) {
133 debugVerbose('installing purs-loader with options: %O', options
);
135 CACHE_VAR
.installed
= true;
137 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
138 this._compiler
.plugin('invalid', function () {
139 debugVerbose('invalidating loader CACHE_VAR');
142 rebuild: options
.pscIde
,
145 ideServer: CACHE_VAR
.ideServer
,
146 psModuleMap: CACHE_VAR
.psModuleMap
,
149 compilationStarted: false,
150 compilationFinished: false,
151 compilationFailed: false,
152 installed: CACHE_VAR
.installed
,
157 // add psc warnings to webpack compilation warnings
158 this._compiler
.plugin('after-compile', function (compilation
, callback
) {
159 CACHE_VAR
.warnings
.forEach(function (warning
) {
160 compilation
.warnings
.push(warning
);
163 CACHE_VAR
.errors
.forEach(function (error
) {
164 compilation
.errors
.push(error
);
171 var psModuleName
= PsModuleMap
.matchModule(source
);
176 load: function load(_ref
) {
179 return callback(null, js
, map
);
181 reject: function reject(error
) {
182 return callback(error
);
184 srcPath: this.resourcePath
,
185 remainingRequest: loaderUtils
.getRemainingRequest(this),
186 srcDir: path
.dirname(this.resourcePath
),
187 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
190 emitWarning: function emitWarning(warning
) {
191 if (options
.warnings
&& warning
.length
) {
192 CACHE_VAR
.warnings
.push(warning
);
195 emitError: function emitError(pscMessage
) {
196 if (pscMessage
.length
) {
199 var matchErrorsSeparator
= /\n(?=Error)/;
200 var errors
= pscMessage
.split(matchErrorsSeparator
);
201 var _iteratorNormalCompletion
= true;
202 var _didIteratorError
= false;
203 var _iteratorError
= undefined;
206 for (var _iterator
= errors
[Symbol
.iterator
](), _step
; !(_iteratorNormalCompletion
= (_step
= _iterator
.next()).done
); _iteratorNormalCompletion
= true) {
207 var error
= _step
.value
;
209 var matchErrLocation
= /at (.+\.purs
):(\d
+):(\d
+) - (\d
+):(\d
+) \(line
\2, column
\3 - line
\4, column
\5\)/;
211 var _ref2
= matchErrLocation
.exec(error
) || [],
212 _ref3
= _slicedToArray(_ref2
, 2),
215 if (!filename
) continue;
217 var baseModulePath
= path
.join(_this
.rootContext
, filename
);
218 _this
.addDependency(baseModulePath
);
220 var foreignModulesErrorCodes
= ['ErrorParsingFFIModule', 'MissingFFIImplementations', 'UnusedFFIImplementations', 'MissingFFIModule'];
221 var _iteratorNormalCompletion2
= true;
222 var _didIteratorError2
= false;
223 var _iteratorError2
= undefined;
226 for (var _iterator2
= foreignModulesErrorCodes
[Symbol
.iterator
](), _step2
; !(_iteratorNormalCompletion2
= (_step2
= _iterator2
.next()).done
); _iteratorNormalCompletion2
= true) {
227 var code
= _step2
.value
;
229 if (error
.includes(code
)) {
230 var resolved
= utils
.resolveForeignModule(baseModulePath
);
231 _this
.addDependency(resolved
);
235 _didIteratorError2
= true;
236 _iteratorError2
= err
;
239 if (!_iteratorNormalCompletion2
&& _iterator2
.return) {
243 if (_didIteratorError2
) {
244 throw _iteratorError2
;
249 var matchErrModuleName
= /in module ((?:\w
+\.)*\w
+)/;
251 var _ref4
= matchErrModuleName
.exec(error
) || [],
252 _ref5
= _slicedToArray(_ref4
, 2),
253 baseModuleName
= _ref5
[1];
255 if (!baseModuleName
) continue;
257 var matchMissingModuleName
= /Module ((?:\w
+\.)*\w
+) was not found
/;
258 var matchMissingImportFromModuleName
= /Cannot
import value
\w
+ from module ((?:\w
+\.)*\w
+)/;
259 var _arr
= [matchMissingModuleName
, matchMissingImportFromModuleName
];
260 for (var _i
= 0; _i
< _arr
.length
; _i
++) {
262 var _ref6
= re
.exec(error
) || [],
263 _ref7
= _slicedToArray(_ref6
, 2),
264 targetModuleName
= _ref7
[1];
266 if (targetModuleName
) {
267 var _resolved
= utils
.resolvePursModule({
268 baseModulePath: baseModulePath
,
269 baseModuleName: baseModuleName
,
270 rewriteRules: options
.rewriteRules
,
271 targetModuleName: targetModuleName
273 _this
.addDependency(_resolved
);
278 name: baseModuleName
,
279 filename: baseModulePath
282 if (typeof _this
.describePscError
=== 'function') {
283 var _describePscError
= _this
.describePscError(error
, desc
),
284 _describePscError
$dep
= _describePscError
.dependencies
,
285 dependencies
= _describePscError
$dep
=== undefined ? [] : _describePscError
$dep
,
286 details
= _describePscError
.details
;
288 var _iteratorNormalCompletion3
= true;
289 var _didIteratorError3
= false;
290 var _iteratorError3
= undefined;
294 for (var _iterator3
= dependencies
[Symbol
.iterator
](), _step3
; !(_iteratorNormalCompletion3
= (_step3
= _iterator3
.next()).done
); _iteratorNormalCompletion3
= true) {
295 var dep
= _step3
.value
;
297 _this
.addDependency(dep
);
300 _didIteratorError3
= true;
301 _iteratorError3
= err
;
304 if (!_iteratorNormalCompletion3
&& _iterator3
.return) {
308 if (_didIteratorError3
) {
309 throw _iteratorError3
;
314 Object
.assign(desc
, details
);
320 _didIteratorError
= true;
321 _iteratorError
= err
;
324 if (!_iteratorNormalCompletion
&& _iterator
.return) {
328 if (_didIteratorError
) {
329 throw _iteratorError
;
334 CACHE_VAR
.errors
.push(new utils
.PscError(pscMessage
, modules
));
339 debug('loading %s', psModule
.name
);
341 if (options
.bundle
) {
342 CACHE_VAR
.bundleModules
.push(psModule
.name
);
345 if (CACHE_VAR
.rebuild
) {
346 var connect
= function connect() {
347 if (!CACHE_VAR
.ideServer
) {
348 CACHE_VAR
.ideServer
= true;
350 return ide
.connect(psModule
).then(function (ideServer
) {
351 CACHE_VAR
.ideServer
= ideServer
;
353 }).then(ide
.loadWithRetry
).catch(function (error
) {
354 if (CACHE_VAR
.ideServer
.kill
) {
355 debug('ide failed to initially load modules, stopping the ide server process');
357 CACHE_VAR
.ideServer
.kill();
360 CACHE_VAR
.ideServer
= null;
362 return Promise
.reject(error
);
365 return Promise
.resolve(psModule
);
369 var rebuild
= function rebuild() {
370 return ide
.rebuild(psModule
).then(function () {
371 return toJavaScript(psModule
).then(function (js
) {
372 return sourceMaps(psModule
, js
);
373 }).then(psModule
.load
).catch(psModule
.reject
);
374 }).catch(function (error
) {
375 if (error
instanceof ide
.UnknownModuleError
) {
376 // Store the modules that trigger a recompile due to an
377 // unknown module error. We need to wait until compilation is
378 // done before loading these files.
380 CACHE_VAR
.deferred
.push(psModule
);
382 if (!CACHE_VAR
.compilationStarted
) {
383 CACHE_VAR
.compilationStarted
= true;
385 return compile(psModule
).then(function () {
386 CACHE_VAR
.compilationFinished
= true;
387 }).then(function () {
388 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
389 return ide
.load(psModule
).then(function () {
390 return toJavaScript(psModule
);
391 }).then(function (js
) {
392 return sourceMaps(psModule
, js
);
393 }).then(psModule
.load
);
395 }).catch(function (error
) {
396 CACHE_VAR
.compilationFailed
= true;
398 CACHE_VAR
.deferred
[0].reject(error
);
400 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
401 psModule
.reject(new Error('purs-loader failed'));
404 } else if (CACHE_VAR
.compilationFailed
) {
405 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
407 // The compilation has started. We must wait until it is
408 // done in order to ensure the module map contains all of
409 // the unknown modules.
412 debug('ide rebuild failed due to an unhandled error: %o', error
);
414 psModule
.reject(error
);
419 connect().then(rebuild
);
420 } else if (CACHE_VAR
.compilationFinished
) {
421 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
423 toJavaScript(psModule
).then(function (js
) {
424 return sourceMaps(psModule
, js
);
425 }).then(psModule
.load
).catch(psModule
.reject
);
427 // The compilation has not finished yet. We need to wait for
428 // compilation to finish before the loaders run so that references
429 // to compiled output are valid. Push the modules into the CACHE_VAR to
430 // be loaded once the complation is complete.
432 CACHE_VAR
.deferred
.push(psModule
);
434 if (!CACHE_VAR
.compilationStarted
) {
435 CACHE_VAR
.compilationStarted
= true;
437 compile(psModule
).then(function () {
438 CACHE_VAR
.compilationFinished
= true;
439 }).then(function () {
440 if (options
.bundle
) {
441 return bundle(options
, CACHE_VAR
.bundleModules
);
443 }).then(function () {
444 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
445 return toJavaScript(psModule
).then(function (js
) {
446 return sourceMaps(psModule
, js
);
447 }).then(psModule
.load
);
449 }).catch(function (error
) {
450 CACHE_VAR
.compilationFailed
= true;
452 CACHE_VAR
.deferred
[0].reject(error
);
454 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
455 psModule
.reject(new Error('purs-loader failed'));
458 } else if (CACHE_VAR
.compilationFailed
) {
459 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
461 // The complation has started. Nothing to do but wait until it is
462 // done before loading all of the modules.