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 matchErrModuleName
= /in module ((?:\w
+\.)*\w
+)/;
221 var _ref4
= matchErrModuleName
.exec(error
) || [],
222 _ref5
= _slicedToArray(_ref4
, 2),
223 baseModuleName
= _ref5
[1];
225 if (!baseModuleName
) continue;
227 var matchMissingModuleName
= /Module ((?:\w
+\.)*\w
+) was not found
/;
228 var matchMissingImportFromModuleName
= /Cannot
import value
\w
+ from module ((?:\w
+\.)*\w
+)/;
229 var _arr
= [matchMissingModuleName
, matchMissingImportFromModuleName
];
230 for (var _i
= 0; _i
< _arr
.length
; _i
++) {
232 var _ref6
= re
.exec(error
) || [],
233 _ref7
= _slicedToArray(_ref6
, 2),
234 targetModuleName
= _ref7
[1];
236 if (targetModuleName
) {
237 var resolved
= utils
.resolvePursModule({
238 baseModulePath: baseModulePath
,
239 baseModuleName: baseModuleName
,
240 targetModuleName: targetModuleName
242 _this
.addDependency(resolved
);
247 name: baseModuleName
,
248 filename: baseModulePath
251 if (typeof _this
.describePscError
=== 'function') {
252 var _describePscError
= _this
.describePscError(error
, desc
),
253 _describePscError
$dep
= _describePscError
.dependencies
,
254 dependencies
= _describePscError
$dep
=== undefined ? [] : _describePscError
$dep
,
255 details
= _describePscError
.details
;
257 var _iteratorNormalCompletion2
= true;
258 var _didIteratorError2
= false;
259 var _iteratorError2
= undefined;
263 for (var _iterator2
= dependencies
[Symbol
.iterator
](), _step2
; !(_iteratorNormalCompletion2
= (_step2
= _iterator2
.next()).done
); _iteratorNormalCompletion2
= true) {
264 var dep
= _step2
.value
;
266 _this
.addDependency(dep
);
269 _didIteratorError2
= true;
270 _iteratorError2
= err
;
273 if (!_iteratorNormalCompletion2
&& _iterator2
.return) {
277 if (_didIteratorError2
) {
278 throw _iteratorError2
;
283 Object
.assign(desc
, details
);
289 _didIteratorError
= true;
290 _iteratorError
= err
;
293 if (!_iteratorNormalCompletion
&& _iterator
.return) {
297 if (_didIteratorError
) {
298 throw _iteratorError
;
303 CACHE_VAR
.errors
.push(new utils
.PscError(pscMessage
, modules
));
308 debug('loading %s', psModule
.name
);
310 if (options
.bundle
) {
311 CACHE_VAR
.bundleModules
.push(psModule
.name
);
314 if (CACHE_VAR
.rebuild
) {
315 var connect
= function connect() {
316 if (!CACHE_VAR
.ideServer
) {
317 CACHE_VAR
.ideServer
= true;
319 return ide
.connect(psModule
).then(function (ideServer
) {
320 CACHE_VAR
.ideServer
= ideServer
;
322 }).then(ide
.loadWithRetry
).catch(function (error
) {
323 if (CACHE_VAR
.ideServer
.kill
) {
324 debug('ide failed to initially load modules, stopping the ide server process');
326 CACHE_VAR
.ideServer
.kill();
329 CACHE_VAR
.ideServer
= null;
331 return Promise
.reject(error
);
334 return Promise
.resolve(psModule
);
338 var rebuild
= function rebuild() {
339 return ide
.rebuild(psModule
).then(function () {
340 return toJavaScript(psModule
).then(function (js
) {
341 return sourceMaps(psModule
, js
);
342 }).then(psModule
.load
).catch(psModule
.reject
);
343 }).catch(function (error
) {
344 if (error
instanceof ide
.UnknownModuleError
) {
345 // Store the modules that trigger a recompile due to an
346 // unknown module error. We need to wait until compilation is
347 // done before loading these files.
349 CACHE_VAR
.deferred
.push(psModule
);
351 if (!CACHE_VAR
.compilationStarted
) {
352 CACHE_VAR
.compilationStarted
= true;
354 return compile(psModule
).then(function () {
355 CACHE_VAR
.compilationFinished
= true;
356 }).then(function () {
357 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
358 return ide
.load(psModule
).then(function () {
359 return toJavaScript(psModule
);
360 }).then(function (js
) {
361 return sourceMaps(psModule
, js
);
362 }).then(psModule
.load
);
364 }).catch(function (error
) {
365 CACHE_VAR
.compilationFailed
= true;
367 CACHE_VAR
.deferred
[0].reject(error
);
369 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
370 psModule
.reject(new Error('purs-loader failed'));
373 } else if (CACHE_VAR
.compilationFailed
) {
374 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
376 // The compilation has started. We must wait until it is
377 // done in order to ensure the module map contains all of
378 // the unknown modules.
381 debug('ide rebuild failed due to an unhandled error: %o', error
);
383 psModule
.reject(error
);
388 connect().then(rebuild
);
389 } else if (CACHE_VAR
.compilationFinished
) {
390 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
392 toJavaScript(psModule
).then(function (js
) {
393 return sourceMaps(psModule
, js
);
394 }).then(psModule
.load
).catch(psModule
.reject
);
396 // The compilation has not finished yet. We need to wait for
397 // compilation to finish before the loaders run so that references
398 // to compiled output are valid. Push the modules into the CACHE_VAR to
399 // be loaded once the complation is complete.
401 CACHE_VAR
.deferred
.push(psModule
);
403 if (!CACHE_VAR
.compilationStarted
) {
404 CACHE_VAR
.compilationStarted
= true;
406 compile(psModule
).then(function () {
407 CACHE_VAR
.compilationFinished
= true;
408 }).then(function () {
409 if (options
.bundle
) {
410 return bundle(options
, CACHE_VAR
.bundleModules
);
412 }).then(function () {
413 return Promise
.map(CACHE_VAR
.deferred
, function (psModule
) {
414 return toJavaScript(psModule
).then(function (js
) {
415 return sourceMaps(psModule
, js
);
416 }).then(psModule
.load
);
418 }).catch(function (error
) {
419 CACHE_VAR
.compilationFailed
= true;
421 CACHE_VAR
.deferred
[0].reject(error
);
423 CACHE_VAR
.deferred
.slice(1).forEach(function (psModule
) {
424 psModule
.reject(new Error('purs-loader failed'));
427 } else if (CACHE_VAR
.compilationFailed
) {
428 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
430 // The complation has started. Nothing to do but wait until it is
431 // done before loading all of the modules.