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',
131 if (!CACHE_VAR
.installed
) {
132 debugVerbose('installing purs-loader with options: %O', options
);
134 CACHE_VAR
.installed
= true;
136 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
137 this._compiler
.plugin('invalid', function () {
138 debugVerbose('invalidating loader CACHE_VAR');
141 rebuild: options
.pscIde
,
144 ideServer: CACHE_VAR
.ideServer
,
145 psModuleMap: CACHE_VAR
.psModuleMap
,
148 compilationStarted: false,
149 compilationFinished: false,
150 compilationFailed: false,
151 installed: CACHE_VAR
.installed
,
156 // add psc warnings to webpack compilation warnings
157 this._compiler
.plugin('after-compile', function (compilation
, callback
) {
158 CACHE_VAR
.warnings
.forEach(function (warning
) {
159 compilation
.warnings
.push(warning
);
162 CACHE_VAR
.errors
.forEach(function (error
) {
163 compilation
.errors
.push(error
);
170 var psModuleName
= PsModuleMap
.matchModule(source
);
175 load: function load(_ref
) {
178 return callback(null, js
, map
);
180 reject: function reject(error
) {
181 return callback(error
);
183 srcPath: this.resourcePath
,
184 remainingRequest: loaderUtils
.getRemainingRequest(this),
185 srcDir: path
.dirname(this.resourcePath
),
186 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
189 emitWarning: function emitWarning(warning
) {
190 if (options
.warnings
&& warning
.length
) {
191 CACHE_VAR
.warnings
.push(warning
);
194 emitError: function emitError(pscMessage
) {
195 if (pscMessage
.length
) {
198 var matchErrorsSeparator
= /\n(?=Error)/;
199 var errors
= pscMessage
.split(matchErrorsSeparator
);
200 var _iteratorNormalCompletion
= true;
201 var _didIteratorError
= false;
202 var _iteratorError
= undefined;
205 for (var _iterator
= errors
[Symbol
.iterator
](), _step
; !(_iteratorNormalCompletion
= (_step
= _iterator
.next()).done
); _iteratorNormalCompletion
= true) {
206 var error
= _step
.value
;
208 var matchErrLocation
= /at (.+\.purs
):(\d
+):(\d
+) - (\d
+):(\d
+) \(line
\2, column
\3 - line
\4, column
\5\)/;
210 var _ref2
= matchErrLocation
.exec(error
) || [],
211 _ref3
= _slicedToArray(_ref2
, 2),
214 if (!filename
) continue;
216 var baseModulePath
= path
.join(_this
.rootContext
, filename
);
217 _this
.addDependency(baseModulePath
);
219 var foreignModulesErrorCodes
= ['ErrorParsingFFIModule', 'MissingFFIImplementations', 'UnusedFFIImplementations', 'MissingFFIModule'];
220 var _iteratorNormalCompletion2
= true;
221 var _didIteratorError2
= false;
222 var _iteratorError2
= undefined;
225 for (var _iterator2
= foreignModulesErrorCodes
[Symbol
.iterator
](), _step2
; !(_iteratorNormalCompletion2
= (_step2
= _iterator2
.next()).done
); _iteratorNormalCompletion2
= true) {
226 var code
= _step2
.value
;
228 if (error
.includes(code
)) {
229 var resolved
= utils
.resolveForeignModule(baseModulePath
);
230 _this
.addDependency(resolved
);
234 _didIteratorError2
= true;
235 _iteratorError2
= err
;
238 if (!_iteratorNormalCompletion2
&& _iterator2
.return) {
242 if (_didIteratorError2
) {
243 throw _iteratorError2
;
248 var matchErrModuleName
= /in module ((?:\w
+\.)*\w
+)/;
250 var _ref4
= matchErrModuleName
.exec(error
) || [],
251 _ref5
= _slicedToArray(_ref4
, 2),
252 baseModuleName
= _ref5
[1];
254 if (!baseModuleName
) continue;
256 var matchMissingModuleName
= /Module ((?:\w
+\.)*\w
+) was not found
/;
257 var matchMissingImportFromModuleName
= /Cannot
import value
\w
+ from module ((?:\w
+\.)*\w
+)/;
258 var _arr
= [matchMissingModuleName
, matchMissingImportFromModuleName
];
259 for (var _i
= 0; _i
< _arr
.length
; _i
++) {
261 var _ref6
= re
.exec(error
) || [],
262 _ref7
= _slicedToArray(_ref6
, 2),
263 targetModuleName
= _ref7
[1];
265 if (targetModuleName
) {
266 var _resolved
= utils
.resolvePursModule({
267 baseModulePath: baseModulePath
,
268 baseModuleName: baseModuleName
,
269 targetModuleName: targetModuleName
271 _this
.addDependency(_resolved
);
276 name: baseModuleName
,
277 filename: baseModulePath
280 if (typeof _this
.describePscError
=== 'function') {
281 var _describePscError
= _this
.describePscError(error
, desc
),
282 _describePscError
$dep
= _describePscError
.dependencies
,
283 dependencies
= _describePscError
$dep
=== undefined ? [] : _describePscError
$dep
,
284 details
= _describePscError
.details
;
286 var _iteratorNormalCompletion3
= true;
287 var _didIteratorError3
= false;
288 var _iteratorError3
= undefined;
292 for (var _iterator3
= dependencies
[Symbol
.iterator
](), _step3
; !(_iteratorNormalCompletion3
= (_step3
= _iterator3
.next()).done
); _iteratorNormalCompletion3
= true) {
293 var dep
= _step3
.value
;
295 _this
.addDependency(dep
);
298 _didIteratorError3
= true;
299 _iteratorError3
= err
;
302 if (!_iteratorNormalCompletion3
&& _iterator3
.return) {
306 if (_didIteratorError3
) {
307 throw _iteratorError3
;
312 Object
.assign(desc
, details
);
318 _didIteratorError
= true;
319 _iteratorError
= err
;
322 if (!_iteratorNormalCompletion
&& _iterator
.return) {
326 if (_didIteratorError
) {
327 throw _iteratorError
;
332 CACHE_VAR
.errors
.push(new utils
.PscError(pscMessage
, modules
));
337 debug('loading %s', psModule
.name
);
339 if (options
.bundle
) {
340 CACHE_VAR
.bundleModules
.push(psModule
.name
);
343 if (CACHE_VAR
.rebuild
) {
344 var connect
= function connect() {
345 if (!CACHE_VAR
.ideServer
) {
346 CACHE_VAR
.ideServer
= true;
348 return ide
.connect(psModule
).then(function (ideServer
) {
349 CACHE_VAR
.ideServer
= ideServer
;
351 }).then(ide
.loadWithRetry
).catch(function (error
) {
352 if (CACHE_VAR
.ideServer
.kill
) {
353 debug('ide failed to initially load modules, stopping the ide server process');
355 CACHE_VAR
.ideServer
.kill();
358 CACHE_VAR
.ideServer
= null;
360 return Promise
.reject(error
);
363 return Promise
.resolve(psModule
);
367 var rebuild
= function rebuild() {
368 return ide
.rebuild(psModule
).then(function () {
369 return toJavaScript(psModule
).then(function (js
) {
370 return sourceMaps(psModule
, js
);
371 }).then(psModule
.load
).catch(psModule
.reject
);
372 }).catch(function (error
) {
373 if (error
instanceof ide
.UnknownModuleError
) {
374 // Store the modules that trigger a recompile due to an
375 // unknown module error. We need to wait until compilation is
376 // done before loading these files.
378 CACHE_VAR
.deferred
.push(psModule
);
380 if (!CACHE_VAR
.compilationStarted
) {
381 CACHE_VAR
.compilationStarted
= true;
383 return compile(psModule
).then(function () {
384 CACHE_VAR
.compilationFinished
= true;
385 }).then(function () {
386 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
387 return ide
.load(psModule
).then(function () {
388 return toJavaScript(psModule
);
389 }).then(function (js
) {
390 return sourceMaps(psModule
, js
);
391 }).then(psModule
.load
);
393 }).catch(function (error
) {
394 CACHE_VAR
.compilationFailed
= true;
396 CACHE_VAR
.deferred
[0].reject(error
);
398 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
399 psModule
.reject(new Error('purs-loader failed'));
402 } else if (CACHE_VAR
.compilationFailed
) {
403 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
405 // The compilation has started. We must wait until it is
406 // done in order to ensure the module map contains all of
407 // the unknown modules.
410 debug('ide rebuild failed due to an unhandled error: %o', error
);
412 psModule
.reject(error
);
417 connect().then(rebuild
);
418 } else if (CACHE_VAR
.compilationFinished
) {
419 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
421 toJavaScript(psModule
).then(function (js
) {
422 return sourceMaps(psModule
, js
);
423 }).then(psModule
.load
).catch(psModule
.reject
);
425 // The compilation has not finished yet. We need to wait for
426 // compilation to finish before the loaders run so that references
427 // to compiled output are valid. Push the modules into the CACHE_VAR to
428 // be loaded once the complation is complete.
430 CACHE_VAR
.deferred
.push(psModule
);
432 if (!CACHE_VAR
.compilationStarted
) {
433 CACHE_VAR
.compilationStarted
= true;
435 compile(psModule
).then(function () {
436 CACHE_VAR
.compilationFinished
= true;
437 }).then(function () {
438 if (options
.bundle
) {
439 return bundle(options
, CACHE_VAR
.bundleModules
);
441 }).then(function () {
442 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
443 return toJavaScript(psModule
).then(function (js
) {
444 return sourceMaps(psModule
, js
);
445 }).then(psModule
.load
);
447 }).catch(function (error
) {
448 CACHE_VAR
.compilationFailed
= true;
450 CACHE_VAR
.deferred
[0].reject(error
);
452 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
453 psModule
.reject(new Error('purs-loader failed'));
456 } else if (CACHE_VAR
.compilationFailed
) {
457 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
459 // The complation has started. Nothing to do but wait until it is
460 // done before loading all of the modules.