]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/index.js
11b06eb8653b168f5bcbfda32cbd673fd01e4967
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 pscIdeColors: loaderOptions
.psc
=== 'psa',
115 bundleOutput: 'output/bundle.js',
116 bundleNamespace: 'PS',
126 if (!cache
.installed
) {
127 debugVerbose('installing purs-loader with options: %O', options
);
129 cache
.installed
= true;
131 // invalidate loader cache when bundle is marked as invalid (in watch mode)
132 this._compiler
.plugin('invalid', () => {
133 debugVerbose('invalidating loader cache');
135 cache
= webpackConfig
.purescriptLoaderCache
= {
136 rebuild: options
.pscIde
,
139 ideServer: cache
.ideServer
,
140 psModuleMap: cache
.psModuleMap
,
143 compilationStarted: false,
144 compilationFinished: false,
145 installed: cache
.installed
,
150 // add psc warnings to webpack compilation warnings
151 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
152 cache
.warnings
.forEach(warning
=> {
153 compilation
.warnings
.push(warning
);
156 cache
.errors
.forEach(error
=> {
157 compilation
.errors
.push(error
);
164 const psModuleName
= PsModuleMap
.matchModule(source
);
168 load: js
=> callback(null, js
),
169 reject: error
=> callback(error
),
170 srcPath: this.resourcePath
,
171 srcDir: path
.dirname(this.resourcePath
),
172 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
175 emitWarning: warning
=> {
176 if (options
.warnings
&& warning
.length
) {
177 cache
.warnings
.push(warning
);
180 emitError: error
=> {
182 cache
.errors
.push(error
);
187 debug('loading %s', psModule
.name
);
189 if (options
.bundle
) {
190 cache
.bundleModules
.push(psModule
.name
);
194 const connect
= () => {
195 if (!cache
.ideServer
) {
196 cache
.ideServer
= true;
198 return ide
.connect(psModule
)
200 cache
.ideServer
= ideServer
;
203 .then(ide
.loadWithRetry
)
205 if (cache
.ideServer
.kill
) {
206 debug('ide failed to initially load modules, stopping the ide server process');
208 cache
.ideServer
.kill();
211 cache
.ideServer
= null;
213 return Promise
.reject(error
);
218 return Promise
.resolve(psModule
);
222 const rebuild
= () =>
223 ide
.rebuild(psModule
)
225 toJavaScript(psModule
)
227 .catch(psModule
.reject
)
230 if (error
instanceof ide
.UnknownModuleError
) {
231 // Store the modules that trigger a recompile due to an
232 // unknown module error. We need to wait until compilation is
233 // done before loading these files.
235 cache
.deferred
.push(psModule
);
237 if (!cache
.compilationStarted
) {
238 cache
.compilationStarted
= true;
240 return compile(psModule
)
242 cache
.compilationFinished
= true;
245 PsModuleMap
.makeMap(options
.src
).then(map
=> {
246 debug('rebuilt module map after unknown module forced a recompilation');
248 cache
.psModuleMap
= map
;
252 Promise
.map(cache
.deferred
, psModule
=>
254 .then(() => toJavaScript(psModule
))
259 cache
.deferred
[0].reject(error
);
261 cache
.deferred
.slice(1).forEach(psModule
=> {
262 psModule
.reject(new Error('purs-loader failed'));
268 // The compilation has started. We must wait until it is
269 // done in order to ensure the module map contains all of
270 // the unknown modules.
274 debug('ide rebuild failed due to an unhandled error: %o', error
);
276 psModule
.reject(error
);
281 connect().then(rebuild
);
283 else if (cache
.compilationFinished
) {
284 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
286 toJavaScript(psModule
)
288 .catch(psModule
.reject
);
291 // The compilation has not finished yet. We need to wait for
292 // compilation to finish before the loaders run so that references
293 // to compiled output are valid. Push the modules into the cache to
294 // be loaded once the complation is complete.
296 cache
.deferred
.push(psModule
);
298 if (!cache
.compilationStarted
) {
299 cache
.compilationStarted
= true;
303 cache
.compilationFinished
= true;
306 if (options
.bundle
) {
307 return bundle(options
, cache
.bundleModules
);
311 PsModuleMap
.makeMap(options
.src
).then(map
=> {
312 debug('rebuilt module map after compilation');
314 cache
.psModuleMap
= map
;
318 Promise
.map(cache
.deferred
, psModule
=>
319 toJavaScript(psModule
)
324 cache
.deferred
[0].reject(error
);
326 cache
.deferred
.slice(1).forEach(psModule
=> {
327 psModule
.reject(new Error('purs-loader failed'));
333 // The complation has started. Nothing to do but wait until it is
334 // done before loading all of the modules.