]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/index.js
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 dargs
= require('./dargs');
27 const spawn
= require('cross-spawn').sync
29 const eol
= require('os').EOL
31 module
.exports
= function purescriptLoader(source
, map
) {
32 this.cacheable
&& this.cacheable();
34 const webpackConfig
= this.options
;
36 var cache
= webpackConfig
.purescriptLoaderCache
= webpackConfig
.purescriptLoaderCache
|| {
44 compilationStarted: false,
45 compilationFinished: false,
50 const callback
= this.async();
52 const loaderOptions
= loaderUtils
.getOptions(this) || {};
54 const srcOption
= (pscPackage
=> {
55 const srcPath
= path
.join('src', '**', '*.purs');
57 const bowerPath
= path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
59 if (cache
.srcOption
.length
> 0) {
60 return cache
.srcOption
;
62 else if (pscPackage
) {
63 const pscPackageCommand
= 'psc-package';
65 const pscPackageArgs
= ['sources'];
67 const loaderSrc
= loaderOptions
.src
|| [
71 debug('psc-package %s %o', pscPackageCommand
, pscPackageArgs
);
73 const cmd
= spawn(pscPackageCommand
, pscPackageArgs
);
76 throw new Error(cmd
.error
);
78 else if (cmd
.status
!== 0) {
79 const error
= cmd
.stdout
.toString();
81 throw new Error(error
);
84 const result
= cmd
.stdout
.toString().split(eol
).filter(v
=> v
!= '').concat(loaderSrc
);
86 debug('psc-package result: %o', result
);
88 cache
.srcOption
= result
;
94 const result
= loaderOptions
.src
|| [
99 cache
.srcOption
= result
;
103 })(loaderOptions
.pscPackage
);
105 const options
= Object
.assign({
106 context: webpackConfig
.context
,
112 pscIdeClientArgs: {},
114 pscIdeServerArgs: {},
116 pscIdeColors: loaderOptions
.psc
=== 'psa',
118 bundleOutput: 'output/bundle.js',
119 bundleNamespace: 'PS',
129 if (!cache
.installed
) {
130 debugVerbose('installing purs-loader with options: %O', options
);
132 cache
.installed
= true;
134 // invalidate loader cache when bundle is marked as invalid (in watch mode)
135 this._compiler
.plugin('invalid', () => {
136 debugVerbose('invalidating loader cache');
138 cache
= webpackConfig
.purescriptLoaderCache
= {
139 rebuild: options
.pscIde
,
142 ideServer: cache
.ideServer
,
143 psModuleMap: cache
.psModuleMap
,
146 compilationStarted: false,
147 compilationFinished: false,
148 installed: cache
.installed
,
153 // add psc warnings to webpack compilation warnings
154 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
155 cache
.warnings
.forEach(warning
=> {
156 compilation
.warnings
.push(warning
);
159 cache
.errors
.forEach(error
=> {
160 compilation
.errors
.push(error
);
167 const psModuleName
= PsModuleMap
.matchModule(source
);
171 load: js
=> callback(null, js
),
172 reject: error
=> callback(error
),
173 srcPath: this.resourcePath
,
174 srcDir: path
.dirname(this.resourcePath
),
175 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
178 emitWarning: warning
=> {
179 if (options
.warnings
&& warning
.length
) {
180 cache
.warnings
.push(warning
);
183 emitError: error
=> {
185 cache
.errors
.push(error
);
190 debug('loading %s', psModule
.name
);
192 if (options
.bundle
) {
193 cache
.bundleModules
.push(psModule
.name
);
197 const connect
= () => {
198 if (!cache
.ideServer
) {
199 cache
.ideServer
= true;
201 return ide
.connect(psModule
)
203 cache
.ideServer
= ideServer
;
206 .then(ide
.loadWithRetry
)
208 if (cache
.ideServer
.kill
) {
209 debug('ide failed to initially load modules, stopping the ide server process');
211 cache
.ideServer
.kill();
214 cache
.ideServer
= null;
216 return Promise
.reject(error
);
221 return Promise
.resolve(psModule
);
225 const rebuild
= () =>
226 ide
.rebuild(psModule
)
228 toJavaScript(psModule
)
230 .catch(psModule
.reject
)
233 if (error
instanceof ide
.UnknownModuleError
) {
234 // Store the modules that trigger a recompile due to an
235 // unknown module error. We need to wait until compilation is
236 // done before loading these files.
238 cache
.deferred
.push(psModule
);
240 if (!cache
.compilationStarted
) {
241 cache
.compilationStarted
= true;
243 return compile(psModule
)
245 cache
.compilationFinished
= true;
248 PsModuleMap
.makeMap(options
.src
).then(map
=> {
249 debug('rebuilt module map after unknown module forced a recompilation');
251 cache
.psModuleMap
= map
;
255 Promise
.map(cache
.deferred
, psModule
=>
257 .then(() => toJavaScript(psModule
))
262 cache
.deferred
[0].reject(error
);
264 cache
.deferred
.slice(1).forEach(psModule
=> {
265 psModule
.reject(new Error('purs-loader failed'));
271 // The compilation has started. We must wait until it is
272 // done in order to ensure the module map contains all of
273 // the unknown modules.
277 debug('ide rebuild failed due to an unhandled error: %o', error
);
279 psModule
.reject(error
);
284 connect().then(rebuild
);
286 else if (cache
.compilationFinished
) {
287 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
289 toJavaScript(psModule
)
291 .catch(psModule
.reject
);
294 // The compilation has not finished yet. We need to wait for
295 // compilation to finish before the loaders run so that references
296 // to compiled output are valid. Push the modules into the cache to
297 // be loaded once the complation is complete.
299 cache
.deferred
.push(psModule
);
301 if (!cache
.compilationStarted
) {
302 cache
.compilationStarted
= true;
306 cache
.compilationFinished
= true;
309 if (options
.bundle
) {
310 return bundle(options
, cache
.bundleModules
);
314 PsModuleMap
.makeMap(options
.src
).then(map
=> {
315 debug('rebuilt module map after compilation');
317 cache
.psModuleMap
= map
;
321 Promise
.map(cache
.deferred
, psModule
=>
322 toJavaScript(psModule
)
327 cache
.deferred
[0].reject(error
);
329 cache
.deferred
.slice(1).forEach(psModule
=> {
330 psModule
.reject(new Error('purs-loader failed'));
336 // The complation has started. Nothing to do but wait until it is
337 // done before loading all of the modules.