]>
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
);
75 if (cmd
.status
!== 0) {
76 const error
= cmd
.stdout
.toString();
78 throw new Error(error
);
81 const result
= cmd
.stdout
.toString().split(eol
).filter(v
=> v
!= '').concat(loaderSrc
);
83 debug('psc-package result: %o', result
);
85 cache
.srcOption
= result
;
91 const result
= loaderOptions
.src
|| [
96 cache
.srcOption
= result
;
100 })(loaderOptions
.pscPackage
);
102 const options
= Object
.assign({
103 context: webpackConfig
.context
,
109 pscIdeColors: loaderOptions
.psc
=== 'psa',
112 bundleOutput: 'output/bundle.js',
113 bundleNamespace: 'PS',
123 if (!cache
.installed
) {
124 debugVerbose('installing purs-loader with options: %O', options
);
126 cache
.installed
= true;
128 // invalidate loader cache when bundle is marked as invalid (in watch mode)
129 this._compiler
.plugin('invalid', () => {
130 debugVerbose('invalidating loader cache');
132 cache
= webpackConfig
.purescriptLoaderCache
= {
133 rebuild: options
.pscIde
,
136 ideServer: cache
.ideServer
,
137 psModuleMap: cache
.psModuleMap
,
140 compilationStarted: false,
141 compilationFinished: false,
142 installed: cache
.installed
,
143 srcOption: cache
.srcOption
147 // add psc warnings to webpack compilation warnings
148 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
149 cache
.warnings
.forEach(warning
=> {
150 compilation
.warnings
.push(warning
);
153 cache
.errors
.forEach(error
=> {
154 compilation
.errors
.push(error
);
161 const psModuleName
= PsModuleMap
.matchModule(source
);
165 load: js
=> callback(null, js
),
166 reject: error
=> callback(error
),
167 srcPath: this.resourcePath
,
168 srcDir: path
.dirname(this.resourcePath
),
169 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
172 emitWarning: warning
=> {
173 if (options
.warnings
&& warning
.length
) {
174 cache
.warnings
.push(warning
);
177 emitError: error
=> {
179 cache
.errors
.push(error
);
184 debug('loading %s', psModule
.name
);
186 if (options
.bundle
) {
187 cache
.bundleModules
.push(psModule
.name
);
191 const connect
= () => {
192 if (!cache
.ideServer
) {
193 cache
.ideServer
= true;
195 return ide
.connect(psModule
)
197 cache
.ideServer
= ideServer
;
200 .then(ide
.loadWithRetry
)
202 if (cache
.ideServer
.kill
) {
203 debug('ide failed to initially load modules, stopping the ide server process');
205 cache
.ideServer
.kill();
208 cache
.ideServer
= null;
210 return Promise
.reject(error
);
215 return Promise
.resolve(psModule
);
219 const rebuild
= () =>
220 ide
.rebuild(psModule
)
222 toJavaScript(psModule
)
224 .catch(psModule
.reject
)
227 if (error
instanceof ide
.UnknownModuleError
) {
228 // Store the modules that trigger a recompile due to an
229 // unknown module error. We need to wait until compilation is
230 // done before loading these files.
232 cache
.deferred
.push(psModule
);
234 if (!cache
.compilationStarted
) {
235 cache
.compilationStarted
= true;
237 return compile(psModule
)
239 cache
.compilationFinished
= true;
242 PsModuleMap
.makeMap(options
.src
).then(map
=> {
243 debug('rebuilt module map after unknown module forced a recompilation');
245 cache
.psModuleMap
= map
;
249 Promise
.map(cache
.deferred
, psModule
=>
251 .then(() => toJavaScript(psModule
))
256 cache
.deferred
[0].reject(error
);
258 cache
.deferred
.slice(1).forEach(psModule
=> {
259 psModule
.reject(new Error('purs-loader failed'));
265 // The compilation has started. We must wait until it is
266 // done in order to ensure the module map contains all of
267 // the unknown modules.
271 debug('ide rebuild failed due to an unhandled error: %o', error
);
273 psModule
.reject(error
);
278 connect().then(rebuild
);
280 else if (cache
.compilationFinished
) {
281 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
283 toJavaScript(psModule
)
285 .catch(psModule
.reject
);
288 // The compilation has not finished yet. We need to wait for
289 // compilation to finish before the loaders run so that references
290 // to compiled output are valid. Push the modules into the cache to
291 // be loaded once the complation is complete.
293 cache
.deferred
.push(psModule
);
295 if (!cache
.compilationStarted
) {
296 cache
.compilationStarted
= true;
300 cache
.compilationFinished
= true;
303 if (options
.bundle
) {
304 return bundle(options
, cache
.bundleModules
);
308 PsModuleMap
.makeMap(options
.src
).then(map
=> {
309 debug('rebuilt module map after compilation');
311 cache
.psModuleMap
= map
;
315 Promise
.map(cache
.deferred
, psModule
=>
316 toJavaScript(psModule
)
321 cache
.deferred
[0].reject(error
);
323 cache
.deferred
.slice(1).forEach(psModule
=> {
324 psModule
.reject(new Error('purs-loader failed'));
330 // The complation has started. Nothing to do but wait until it is
331 // done before loading all of the modules.