3 const debug_
= require('debug');
5 const debug
= debug_('purs-loader');
7 const debugVerbose
= debug_('purs-loader:verbose');
9 const loaderUtils
= require('loader-utils')
11 const Promise
= require('bluebird')
13 const path
= require('path')
15 const PsModuleMap
= require('./purs-module-map');
17 const compile
= require('./compile');
19 const bundle
= require('./bundle');
21 const ide
= require('./ide');
23 const toJavaScript
= require('./to-javascript');
25 const sourceMaps
= require('./source-maps');
27 const dargs
= require('./dargs');
29 const utils
= require('./utils');
31 const spawn
= require('cross-spawn').sync
33 const eol
= require('os').EOL
43 compilationStarted: false,
44 compilationFinished: false,
45 compilationFailed: false,
50 module
.exports
= function purescriptLoader(source
, map
) {
51 this.cacheable
&& this.cacheable();
53 const webpackContext
= (this.options
&& this.options
.context
) || this.rootContext
;
55 const callback
= this.async();
57 const loaderOptions
= loaderUtils
.getOptions(this) || {};
59 const srcOption
= (pscPackage
=> {
60 const srcPath
= path
.join('src', '**', '*.purs');
62 const bowerPath
= path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
64 if (CACHE_VAR
.srcOption
.length
> 0) {
65 return CACHE_VAR
.srcOption
;
67 else if (pscPackage
) {
68 const pscPackageCommand
= 'psc-package';
70 const pscPackageArgs
= ['sources'];
72 const loaderSrc
= loaderOptions
.src
|| [
76 debug('psc-package %s %o', pscPackageCommand
, pscPackageArgs
);
78 const cmd
= spawn(pscPackageCommand
, pscPackageArgs
);
81 throw new Error(cmd
.error
);
83 else if (cmd
.status
!== 0) {
84 const error
= cmd
.stdout
.toString();
86 throw new Error(error
);
89 const result
= cmd
.stdout
.toString().split(eol
).filter(v
=> v
!= '').concat(loaderSrc
);
91 debug('psc-package result: %o', result
);
93 CACHE_VAR
.srcOption
= result
;
99 const result
= loaderOptions
.src
|| [
104 CACHE_VAR
.srcOption
= result
;
108 })(loaderOptions
.pscPackage
);
110 const options
= Object
.assign({
111 context: webpackContext
,
117 pscIdeClientArgs: {},
119 pscIdeServerArgs: {},
121 pscIdeColors: loaderOptions
.psc
=== 'psa',
123 bundleOutput: 'output/bundle.js',
124 bundleNamespace: 'PS',
134 if (!CACHE_VAR
.installed
) {
135 debugVerbose('installing purs-loader with options: %O', options
);
137 CACHE_VAR
.installed
= true;
139 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
140 this._compiler
.plugin('invalid', () => {
141 debugVerbose('invalidating loader CACHE_VAR');
144 rebuild: options
.pscIde
,
147 ideServer: CACHE_VAR
.ideServer
,
148 psModuleMap: CACHE_VAR
.psModuleMap
,
151 compilationStarted: false,
152 compilationFinished: false,
153 compilationFailed: false,
154 installed: CACHE_VAR
.installed
,
159 // add psc warnings to webpack compilation warnings
160 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
161 CACHE_VAR
.warnings
.forEach(warning
=> {
162 compilation
.warnings
.push(warning
);
165 CACHE_VAR
.errors
.forEach(error
=> {
166 compilation
.errors
.push(error
);
173 const psModuleName
= PsModuleMap
.matchModule(source
);
178 load: ({js
, map
}) => callback(null, js
, map
),
179 reject: error
=> callback(error
),
180 srcPath: this.resourcePath
,
181 remainingRequest: loaderUtils
.getRemainingRequest(this),
182 srcDir: path
.dirname(this.resourcePath
),
183 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
186 emitWarning: warning
=> {
187 if (options
.warnings
&& warning
.length
) {
188 CACHE_VAR
.warnings
.push(warning
);
191 emitError: pscMessage
=> {
192 if (pscMessage
.length
) {
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;
202 const baseModulePath
= path
.join(this.rootContext
, filename
);
203 this.addDependency(baseModulePath
);
205 const matchErrModuleName
= /in module ((?:\w
+\.)*\w
+)/;
206 const [, baseModuleName
] = matchErrModuleName
.exec(error
) || [];
207 if (!baseModuleName
) continue;
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({
219 this.addDependency(resolved
);
224 name: baseModuleName
,
225 filename: baseModulePath
228 if (typeof this.extractPursDependenciesFromError
=== 'function') {
229 const dependencies
= this.extractPursDependenciesFromError(error
) || [];
231 for (const dep
of dependencies
) {
232 this.addDependency(dep
);
239 CACHE_VAR
.errors
.push(Object
.assign(new Error(pscMessage
), { modules
}));
244 debug('loading %s', psModule
.name
);
246 if (options
.bundle
) {
247 CACHE_VAR
.bundleModules
.push(psModule
.name
);
250 if (CACHE_VAR
.rebuild
) {
251 const connect
= () => {
252 if (!CACHE_VAR
.ideServer
) {
253 CACHE_VAR
.ideServer
= true;
255 return ide
.connect(psModule
)
257 CACHE_VAR
.ideServer
= ideServer
;
260 .then(ide
.loadWithRetry
)
262 if (CACHE_VAR
.ideServer
.kill
) {
263 debug('ide failed to initially load modules, stopping the ide server process');
265 CACHE_VAR
.ideServer
.kill();
268 CACHE_VAR
.ideServer
= null;
270 return Promise
.reject(error
);
275 return Promise
.resolve(psModule
);
279 const rebuild
= () =>
280 ide
.rebuild(psModule
)
282 toJavaScript(psModule
)
283 .then(js
=> sourceMaps(psModule
, js
))
285 .catch(psModule
.reject
)
288 if (error
instanceof ide
.UnknownModuleError
) {
289 // Store the modules that trigger a recompile due to an
290 // unknown module error. We need to wait until compilation is
291 // done before loading these files.
293 CACHE_VAR
.deferred
.push(psModule
);
295 if (!CACHE_VAR
.compilationStarted
) {
296 CACHE_VAR
.compilationStarted
= true;
298 return compile(psModule
)
300 CACHE_VAR
.compilationFinished
= true;
303 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
305 .then(() => toJavaScript(psModule
))
306 .then(js
=> sourceMaps(psModule
, js
))
311 CACHE_VAR
.compilationFailed
= true;
313 CACHE_VAR
.deferred
[0].reject(error
);
315 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
316 psModule
.reject(new Error('purs-loader failed'));
320 } else if (CACHE_VAR
.compilationFailed
) {
321 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
323 // The compilation has started. We must wait until it is
324 // done in order to ensure the module map contains all of
325 // the unknown modules.
329 debug('ide rebuild failed due to an unhandled error: %o', error
);
331 psModule
.reject(error
);
336 connect().then(rebuild
);
338 else if (CACHE_VAR
.compilationFinished
) {
339 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
341 toJavaScript(psModule
)
342 .then(js
=> sourceMaps(psModule
, js
))
344 .catch(psModule
.reject
);
347 // The compilation has not finished yet. We need to wait for
348 // compilation to finish before the loaders run so that references
349 // to compiled output are valid. Push the modules into the CACHE_VAR to
350 // be loaded once the complation is complete.
352 CACHE_VAR
.deferred
.push(psModule
);
354 if (!CACHE_VAR
.compilationStarted
) {
355 CACHE_VAR
.compilationStarted
= true;
359 CACHE_VAR
.compilationFinished
= true;
362 if (options
.bundle
) {
363 return bundle(options
, CACHE_VAR
.bundleModules
);
367 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
368 toJavaScript(psModule
)
369 .then(js
=> sourceMaps(psModule
, js
))
374 CACHE_VAR
.compilationFailed
= true;
376 CACHE_VAR
.deferred
[0].reject(error
);
378 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
379 psModule
.reject(new Error('purs-loader failed'));
383 } else if (CACHE_VAR
.compilationFailed
) {
384 CACHE_VAR
.deferred
.pop().reject(new Error('purs-loader failed'));
386 // The complation has started. Nothing to do but wait until it is
387 // done before loading all of the modules.