3 const debug
= require('debug')('purs-loader')
4 const loaderUtils
= require('loader-utils')
5 const Promise
= require('bluebird')
6 const path
= require('path')
7 const PsModuleMap
= require('./PsModuleMap');
8 const Psc
= require('./Psc');
9 const PscIde
= require('./PscIde');
10 const toJavaScript
= require('./to-javascript');
11 const dargs
= require('./dargs');
12 const spawn
= require('cross-spawn').sync
13 const eol
= require('os').EOL
15 module
.exports
= function purescriptLoader(source
, map
) {
16 const callback
= this.async()
17 const config
= this.options
18 const query
= loaderUtils
.parseQuery(this.query
)
19 const webpackOptions
= this.options
.purescriptLoader
|| {}
21 const depsPaths
= (pscPackage
=> {
23 debug('calling psc-package...')
25 return spawn('psc-package', ['sources']).stdout
.toString().split(eol
).filter(v
=> v
!= '')
28 return [ path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ]
32 let options
= Object
.assign(webpackOptions
, query
)
34 const defaultDeps
= depsPaths(options
.pscPackage
)
35 const defaultOptions
= {
36 context: config
.context
,
39 pscBundle: 'psc-bundle',
42 pscIdeColors: options
.psc
=== 'psa',
45 bundleOutput: 'output/bundle.js',
46 bundleNamespace: 'PS',
51 path
.join('src', '**', '*.purs'),
56 this.cacheable
&& this.cacheable()
58 let cache
= config
.purescriptLoaderCache
= config
.purescriptLoaderCache
|| {
66 if (options
.pscPackage
&& options
.src
) {
67 options
.src
= options
.src
.concat(defaultDeps
) // append psc-package-provided source paths with users'
70 options
= Object
.assign(defaultOptions
, options
)
72 if (!config
.purescriptLoaderInstalled
) {
73 config
.purescriptLoaderInstalled
= true
75 // invalidate loader cache when bundle is marked as invalid (in watch mode)
76 this._compiler
.plugin('invalid', () => {
77 debug('invalidating loader cache');
79 cache
= config
.purescriptLoaderCache
= {
80 rebuild: options
.pscIde
,
83 ideServer: cache
.ideServer
,
84 psModuleMap: cache
.psModuleMap
,
90 // add psc warnings to webpack compilation warnings
91 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
92 cache
.warnings
.forEach(warning
=> {
93 compilation
.warnings
.push(warning
);
96 cache
.errors
.forEach(error
=> {
97 compilation
.errors
.push(error
);
104 const psModuleName
= PsModuleMap
.match(source
)
107 load: js
=> callback(null, js
),
108 reject: error
=> callback(error
),
109 srcPath: this.resourcePath
,
110 srcDir: path
.dirname(this.resourcePath
),
111 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
114 emitWarning: warning
=> {
115 if (options
.warnings
&& warning
.length
) {
116 cache
.warnings
.push(warning
);
119 emitError: error
=> {
121 cache
.errors
.push(error
);
126 debug('loader called', psModule
.name
)
128 if (options
.bundle
) {
129 cache
.bundleModules
.push(psModule
.name
)
133 return PscIde
.connect(psModule
)
134 .then(PscIde
.rebuild
)
137 .catch(psModule
.reject
)
140 if (cache
.compilationFinished
) {
141 return toJavaScript(psModule
).then(psModule
.load
).catch(psModule
.reject
)
144 // We need to wait for compilation to finish before the loaders run so that
145 // references to compiled output are valid.
146 cache
.deferred
.push(psModule
)
148 if (!cache
.compilationStarted
) {
149 return Psc
.compile(psModule
)
150 .then(() => PsModuleMap
.makeMap(options
.src
).then(map
=> {
151 debug('rebuilt module map');
152 cache
.psModuleMap
= map
;
154 .then(() => Promise
.map(cache
.deferred
, psModule
=> {
155 if (typeof cache
.ideServer
=== 'object') cache
.ideServer
.kill()
156 return toJavaScript(psModule
).then(psModule
.load
)
159 cache
.deferred
[0].reject(error
)
160 cache
.deferred
.slice(1).forEach(psModule
=> psModule
.reject(new Error('purs-loader failed')))