]>
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 sourceMaps
= require('./source-maps');
27 const spawn
= require('cross-spawn').sync
29 const eol
= require('os').EOL
39 compilationStarted: false,
40 compilationFinished: false,
45 module
.exports
= function purescriptLoader(source
, map
) {
46 this.cacheable
&& this.cacheable();
48 const webpackContext
= (this.options
&& this.options
.context
) || this.rootContext
;
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_VAR
.srcOption
.length
> 0) {
60 return CACHE_VAR
.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_VAR
.srcOption
= result
;
94 const result
= loaderOptions
.src
|| [
99 CACHE_VAR
.srcOption
= result
;
103 })(loaderOptions
.pscPackage
);
105 const options
= Object
.assign({
106 context: webpackContext
,
112 pscIdeClientArgs: {},
114 pscIdeServerArgs: {},
115 pscIdeRebuildArgs: {},
117 pscIdeColors: loaderOptions
.psc
=== 'psa',
119 bundleOutput: 'output/bundle.js',
120 bundleNamespace: 'PS',
130 if (!CACHE_VAR
.installed
) {
131 debugVerbose('installing purs-loader with options: %O', options
);
133 CACHE_VAR
.installed
= true;
135 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
136 this._compiler
.plugin('invalid', () => {
137 debugVerbose('invalidating loader CACHE_VAR');
140 rebuild: options
.pscIde
,
143 ideServer: CACHE_VAR
.ideServer
,
144 psModuleMap: CACHE_VAR
.psModuleMap
,
147 compilationStarted: false,
148 compilationFinished: false,
149 installed: CACHE_VAR
.installed
,
154 // add psc warnings to webpack compilation warnings
155 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
156 CACHE_VAR
.warnings
.forEach(warning
=> {
157 compilation
.warnings
.push(warning
);
160 CACHE_VAR
.errors
.forEach(error
=> {
161 compilation
.errors
.push(error
);
168 const psModuleName
= PsModuleMap
.matchModule(source
);
173 load: ({js
, map
}) => callback(null, js
, map
),
174 reject: error
=> callback(error
),
175 srcPath: this.resourcePath
,
176 remainingRequest: loaderUtils
.getRemainingRequest(this),
177 srcDir: path
.dirname(this.resourcePath
),
178 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
181 emitWarning: warning
=> {
182 if (options
.warnings
&& warning
.length
) {
183 CACHE_VAR
.warnings
.push(warning
);
186 emitError: error
=> {
188 CACHE_VAR
.errors
.push(error
);
193 debug('loading %s', psModule
.name
);
195 if (options
.bundle
) {
196 CACHE_VAR
.bundleModules
.push(psModule
.name
);
199 if (CACHE_VAR
.rebuild
) {
200 const connect
= () => {
201 if (!CACHE_VAR
.ideServer
) {
202 CACHE_VAR
.ideServer
= true;
204 return ide
.connect(psModule
)
206 CACHE_VAR
.ideServer
= ideServer
;
209 .then(ide
.loadWithRetry
)
211 if (CACHE_VAR
.ideServer
.kill
) {
212 debug('ide failed to initially load modules, stopping the ide server process');
214 CACHE_VAR
.ideServer
.kill();
217 CACHE_VAR
.ideServer
= null;
219 return Promise
.reject(error
);
224 return Promise
.resolve(psModule
);
228 const rebuild
= () =>
229 ide
.rebuild(psModule
)
231 toJavaScript(psModule
)
232 .then(js
=> sourceMaps(psModule
, js
))
234 .catch(psModule
.reject
)
237 if (error
instanceof ide
.UnknownModuleError
) {
238 // Store the modules that trigger a recompile due to an
239 // unknown module error. We need to wait until compilation is
240 // done before loading these files.
242 CACHE_VAR
.deferred
.push(psModule
);
244 if (!CACHE_VAR
.compilationStarted
) {
245 CACHE_VAR
.compilationStarted
= true;
247 return compile(psModule
)
249 CACHE_VAR
.compilationFinished
= true;
252 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
254 .then(() => toJavaScript(psModule
))
255 .then(js
=> sourceMaps(psModule
, js
))
260 CACHE_VAR
.deferred
[0].reject(error
);
262 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
263 psModule
.reject(new Error('purs-loader failed'));
269 // The compilation has started. We must wait until it is
270 // done in order to ensure the module map contains all of
271 // the unknown modules.
275 debug('ide rebuild failed due to an unhandled error: %o', error
);
277 psModule
.reject(error
);
282 connect().then(rebuild
);
284 else if (CACHE_VAR
.compilationFinished
) {
285 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
287 toJavaScript(psModule
)
288 .then(js
=> sourceMaps(psModule
, js
))
290 .catch(psModule
.reject
);
293 // The compilation has not finished yet. We need to wait for
294 // compilation to finish before the loaders run so that references
295 // to compiled output are valid. Push the modules into the CACHE_VAR to
296 // be loaded once the complation is complete.
298 CACHE_VAR
.deferred
.push(psModule
);
300 if (!CACHE_VAR
.compilationStarted
) {
301 CACHE_VAR
.compilationStarted
= true;
305 CACHE_VAR
.compilationFinished
= true;
308 if (options
.bundle
) {
309 return bundle(options
, CACHE_VAR
.bundleModules
);
313 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
314 toJavaScript(psModule
)
315 .then(js
=> sourceMaps(psModule
, js
))
320 CACHE_VAR
.deferred
[0].reject(error
);
322 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
323 psModule
.reject(new Error('purs-loader failed'));
329 // The complation has started. Nothing to do but wait until it is
330 // done before loading all of the modules.