]>
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 const invalidCb
= () => {
136 debugVerbose('invalidating loader CACHE_VAR');
139 rebuild: options
.pscIde
,
142 ideServer: CACHE_VAR
.ideServer
,
143 psModuleMap: CACHE_VAR
.psModuleMap
,
146 compilationStarted: false,
147 compilationFinished: false,
148 installed: CACHE_VAR
.installed
,
153 // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
154 if(this._compiler
.hooks
){
155 this._compiler
.hooks
.invalid
.tap('purs-loader', invalidCb
);
157 this._compiler
.plugin('invalid', invalidCb
);
160 const afterCompileCb
= (compilation
, callback
) => {
161 CACHE_VAR
.warnings
.forEach(warning
=> {
162 compilation
.warnings
.push(warning
);
165 CACHE_VAR
.errors
.forEach(error
=> {
166 compilation
.errors
.push(error
);
172 // add psc warnings to webpack compilation warnings
173 if(this._compiler
.hooks
) {
174 this._compiler
.hooks
.afterCompile
.tapAsync('purs-loader', afterCompileCb
);
176 this._compiler
.plugin('after-compile', afterCompileCb
);
180 const psModuleName
= PsModuleMap
.matchModule(source
);
185 load: ({js
, map
}) => callback(null, js
, map
),
186 reject: error
=> callback(error
),
187 srcPath: this.resourcePath
,
188 remainingRequest: loaderUtils
.getRemainingRequest(this),
189 srcDir: path
.dirname(this.resourcePath
),
190 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
193 emitWarning: warning
=> {
194 if (options
.warnings
&& warning
.length
) {
195 CACHE_VAR
.warnings
.push(warning
);
198 emitError: error
=> {
200 CACHE_VAR
.errors
.push(error
);
205 debug('loading %s', psModule
.name
);
207 if (options
.bundle
) {
208 CACHE_VAR
.bundleModules
.push(psModule
.name
);
211 if (CACHE_VAR
.rebuild
) {
212 const connect
= () => {
213 if (!CACHE_VAR
.ideServer
) {
214 CACHE_VAR
.ideServer
= true;
216 return ide
.connect(psModule
)
218 CACHE_VAR
.ideServer
= ideServer
;
221 .then(ide
.loadWithRetry
)
223 if (CACHE_VAR
.ideServer
.kill
) {
224 debug('ide failed to initially load modules, stopping the ide server process');
226 CACHE_VAR
.ideServer
.kill();
229 CACHE_VAR
.ideServer
= null;
231 return Promise
.reject(error
);
236 return Promise
.resolve(psModule
);
240 const rebuild
= () =>
241 ide
.rebuild(psModule
)
243 toJavaScript(psModule
)
244 .then(js
=> sourceMaps(psModule
, js
))
246 .catch(psModule
.reject
)
249 if (error
instanceof ide
.UnknownModuleError
) {
250 // Store the modules that trigger a recompile due to an
251 // unknown module error. We need to wait until compilation is
252 // done before loading these files.
254 CACHE_VAR
.deferred
.push(psModule
);
256 if (!CACHE_VAR
.compilationStarted
) {
257 CACHE_VAR
.compilationStarted
= true;
259 return compile(psModule
)
261 CACHE_VAR
.compilationFinished
= true;
264 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
266 .then(() => toJavaScript(psModule
))
267 .then(js
=> sourceMaps(psModule
, js
))
272 CACHE_VAR
.deferred
[0].reject(error
);
274 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
275 psModule
.reject(new Error('purs-loader failed'));
281 // The compilation has started. We must wait until it is
282 // done in order to ensure the module map contains all of
283 // the unknown modules.
287 debug('ide rebuild failed due to an unhandled error: %o', error
);
289 psModule
.reject(error
);
294 connect().then(rebuild
);
296 else if (CACHE_VAR
.compilationFinished
) {
297 debugVerbose('compilation is already finished, loading module %s', psModule
.name
);
299 toJavaScript(psModule
)
300 .then(js
=> sourceMaps(psModule
, js
))
302 .catch(psModule
.reject
);
305 // The compilation has not finished yet. We need to wait for
306 // compilation to finish before the loaders run so that references
307 // to compiled output are valid. Push the modules into the CACHE_VAR to
308 // be loaded once the complation is complete.
310 CACHE_VAR
.deferred
.push(psModule
);
312 if (!CACHE_VAR
.compilationStarted
) {
313 CACHE_VAR
.compilationStarted
= true;
317 CACHE_VAR
.compilationFinished
= true;
320 if (options
.bundle
) {
321 return bundle(options
, CACHE_VAR
.bundleModules
);
325 Promise
.map(CACHE_VAR
.deferred
, psModule
=>
326 toJavaScript(psModule
)
327 .then(js
=> sourceMaps(psModule
, js
))
332 CACHE_VAR
.deferred
[0].reject(error
);
334 CACHE_VAR
.deferred
.slice(1).forEach(psModule
=> {
335 psModule
.reject(new Error('purs-loader failed'));
341 // The complation has started. Nothing to do but wait until it is
342 // done before loading all of the modules.