]>
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 // include src files provided by psc-package or Spago
46 function requestDependencySources(packagerCommand
, srcPath
, loaderOptions
) {
47 const packagerArgs
= ['sources'];
49 const loaderSrc
= loaderOptions
.src
|| [
53 debug('%s %o', packagerCommand
, packagerArgs
);
55 const cmd
= spawn(packagerCommand
, packagerArgs
);
58 throw new Error(cmd
.error
);
60 else if (cmd
.status
!== 0) {
61 const error
= cmd
.stdout
.toString();
63 throw new Error(error
);
66 const result
= cmd
.stdout
.toString().split(eol
).filter(v
=> v
!= '').concat(loaderSrc
);
68 debug('%s result: %o', packagerCommand
, result
);
70 CACHE_VAR
.srcOption
= result
;
76 module
.exports
= function purescriptLoader(source
, map
) {
77 this.cacheable
&& this.cacheable();
79 const webpackContext
= (this.options
&& this.options
.context
) || this.rootContext
;
81 const callback
= this.async();
83 const loaderOptions
= loaderUtils
.getOptions(this) || {};
85 const srcOption
= ((pscPackage
, spago
) => {
86 const srcPath
= path
.join('src', '**', '*.purs');
88 const bowerPath
= path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
90 if (CACHE_VAR
.srcOption
.length
> 0) {
91 return CACHE_VAR
.srcOption
;
93 else if (pscPackage
) {
94 return requestDependencySources('psc-package', srcPath
, loaderOptions
)
97 return requestDependencySources('spago', srcPath
, loaderOptions
)
100 const result
= loaderOptions
.src
|| [
105 CACHE_VAR
.srcOption
= result
;
109 })(loaderOptions
.pscPackage
, loaderOptions
.spago
);
111 const options
= Object
.assign({
112 context: webpackContext
,
118 pscIdeClientArgs: {},
120 pscIdeServerArgs: {},
121 pscIdeRebuildArgs: {},
123 pscIdeColors: loaderOptions
.psc
=== 'psa',
126 bundleOutput: 'output/bundle.js',
127 bundleNamespace: 'PS',
137 if (!CACHE_VAR
.installed
) {
138 debugVerbose('installing purs-loader with options: %O', options
);
140 CACHE_VAR
.installed
= true;
142 const invalidCb
= () => {
143 debugVerbose('invalidating loader CACHE_VAR');
146 rebuild: options
.pscIde
,
149 ideServer: CACHE_VAR
.ideServer
,
150 psModuleMap: CACHE_VAR
.psModuleMap
,
153 compilationStarted: false,
154 compilationFinished: false,
155 installed: CACHE_VAR
.installed
,
160 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
161 if(this._compiler
.hooks
){
162 this._compiler
.hooks
.invalid
.tap('purs-loader', invalidCb
);
164 this._compiler
.plugin('invalid', invalidCb
);
167 const afterCompileCb
= (compilation
, callback
) => {
168 CACHE_VAR
.warnings
.forEach(warning
=> {
169 compilation
.warnings
.push(warning
);
172 CACHE_VAR
.errors
.forEach(error
=> {
173 compilation
.errors
.push(error
);
179 // add psc warnings to webpack compilation warnings
180 if(this._compiler
.hooks
) {
181 this._compiler
.hooks
.afterCompile
.tapAsync('purs-loader', afterCompileCb
);
183 this._compiler
.plugin('after-compile', afterCompileCb
);
187 const psModuleName
= PsModuleMap
.matchModule(source
);
192 load: ({js
, map
}) => callback(null, js
, map
),
193 reject: error
=> callback(error
),
194 srcPath: this.resourcePath
,
195 remainingRequest: loaderUtils
.getRemainingRequest(this),
196 srcDir: path
.dirname(this.resourcePath
),
197 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
200 emitWarning: warning
=> {
201 if (options
.warnings
&& warning
.length
) {
202 CACHE_VAR
.warnings
.push(warning
);
205 emitError: error
=> {
207 CACHE_VAR
.errors
.push(error
);
212 debug('loading %s', psModule
.name
);
214 if (options
.bundle
) {
215 CACHE_VAR
.bundleModules
.push(psModule
.name
);
218 if (CACHE_VAR
.rebuild
) {
219 const connect
= () => {
220 if (!CACHE_VAR
.ideServer
) {
221 CACHE_VAR
.ideServer
= true;
223 return ide
.connect(psModule
)
225 CACHE_VAR
.ideServer
= ideServer
;
228 .then(ide
.loadWithRetry
)
230 if (CACHE_VAR
.ideServer
.kill
) {
231 debug('ide failed to initially load modules, stopping the ide server process');
233 CACHE_VAR
.ideServer
.kill();
236 CACHE_VAR
.ideServer
= null;
238 return Promise
.reject(error
);
243 return Promise
.resolve(psModule
);
247 const rebuild
= () =>
248 ide
.rebuild(psModule
)
250 toJavaScript(psModule
)
251 .then(js
=> sourceMaps(psModule
, js
))
253 .catch(psModule
.reject
)
256 if (error
instanceof ide
.UnknownModuleError
) {
257 // Store the modules that trigger a recompile due to an
258 // unknown module error. We need to wait until compilation is
259 // done before loading these files.
261 CACHE_VAR
.deferred
.push(psModule
);
263 if (!CACHE_VAR
.compilationStarted
) {
264 CACHE_VAR
.compilationStarted
= true;
266 return compile(psModule
)
268 CACHE_VAR
.compilationFinished
= true;
271 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
273 .then(() => toJavaScript(psModule
))
274 .then(js
=> sourceMaps(psModule
, js
))
279 CACHE_VAR
.deferred
[0].reject(error
);
281 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
282 psModule
.reject(new Error('purs-loader failed'));
288 // The compilation has started. We must wait until it is
289 // done in order to ensure the module map contains all of
290 // the unknown modules.
294 debug('ide rebuild failed due to an unhandled error: %o', error
);
296 psModule
.reject(error
);
301 connect().then(rebuild
);
303 else if (CACHE_VAR
.compilationFinished
) {
304 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
306 toJavaScript(psModule
)
307 .then(js
=> sourceMaps(psModule
, js
))
309 .catch(psModule
.reject
);
312 // The compilation has not finished yet. We need to wait for
313 // compilation to finish before the loaders run so that references
314 // to compiled output are valid. Push the modules into the CACHE_VAR to
315 // be loaded once the complation is complete.
317 CACHE_VAR
.deferred
.push(psModule
);
319 if (!CACHE_VAR
.compilationStarted
) {
320 CACHE_VAR
.compilationStarted
= true;
324 CACHE_VAR
.compilationFinished
= true;
327 if (options
.bundle
) {
328 return bundle(options
, CACHE_VAR
.bundleModules
);
332 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
333 toJavaScript(psModule
)
334 .then(js
=> sourceMaps(psModule
, js
))
339 CACHE_VAR
.deferred
[0].reject(error
);
341 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
342 psModule
.reject(new Error('purs-loader failed'));
348 // The complation has started. Nothing to do but wait until it is
349 // done before loading all of the modules.