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',
52 path
.join('src', '**', '*.purs'),
57 this.cacheable
&& this.cacheable()
59 let cache
= config
.purescriptLoaderCache
= config
.purescriptLoaderCache
|| {
67 if (options
.pscPackage
&& options
.src
) {
68 options
.src
= options
.src
.concat(defaultDeps
) // append psc-package-provided source paths with users'
71 options
= Object
.assign(defaultOptions
, options
)
73 if (!config
.purescriptLoaderInstalled
) {
74 config
.purescriptLoaderInstalled
= true
76 // invalidate loader cache when bundle is marked as invalid (in watch mode)
77 this._compiler
.plugin('invalid', () => {
78 debug('invalidating loader cache');
80 cache
= config
.purescriptLoaderCache
= {
81 rebuild: options
.pscIde
,
84 ideServer: cache
.ideServer
,
85 psModuleMap: cache
.psModuleMap
,
91 // add psc warnings to webpack compilation warnings
92 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
93 cache
.warnings
.forEach(warning
=> {
94 compilation
.warnings
.push(warning
);
97 cache
.errors
.forEach(error
=> {
98 compilation
.errors
.push(error
);
105 const psModuleName
= PsModuleMap
.match(source
)
108 load: js
=> callback(null, js
),
109 reject: error
=> callback(error
),
110 srcPath: this.resourcePath
,
111 srcDir: path
.dirname(this.resourcePath
),
112 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
115 emitWarning: warning
=> {
116 if (options
.warnings
&& warning
.length
) {
117 cache
.warnings
.push(warning
);
120 emitError: error
=> {
122 cache
.errors
.push(error
);
127 debug('loader called', psModule
.name
)
129 if (options
.bundle
) {
130 cache
.bundleModules
.push(psModule
.name
)
134 return PscIde
.connect(psModule
)
135 .then(PscIde
.rebuild
)
138 .catch(psModule
.reject
)
141 if (cache
.compilationFinished
) {
142 return toJavaScript(psModule
).then(psModule
.load
).catch(psModule
.reject
)
145 // We need to wait for compilation to finish before the loaders run so that
146 // references to compiled output are valid.
147 cache
.deferred
.push(psModule
)
149 if (!cache
.compilationStarted
) {
150 return Psc
.compile(psModule
)
151 .then(() => PsModuleMap
.makeMap(options
.src
).then(map
=> {
152 debug('rebuilt module map after compile');
153 cache
.psModuleMap
= map
;
155 .then(() => Promise
.map(cache
.deferred
, psModule
=> {
156 if (typeof cache
.ideServer
=== 'object') cache
.ideServer
.kill()
157 return toJavaScript(psModule
).then(psModule
.load
)
160 cache
.deferred
[0].reject(error
)
161 cache
.deferred
.slice(1).forEach(psModule
=> psModule
.reject(new Error('purs-loader failed')))