3 const debug
= require('debug')('purs-loader')
4 const loaderUtils
= require('loader-utils')
5 const Promise
= require('bluebird')
6 const fs
= Promise
.promisifyAll(require('fs'))
7 const path
= require('path')
8 const jsStringEscape
= require('js-string-escape')
9 const PsModuleMap
= require('./PsModuleMap');
10 const Psc
= require('./Psc');
11 const PscIde
= require('./PscIde');
12 const dargs
= require('./dargs');
14 const requireRegex
= /require\(['"]\.\.\/([\w\.]+)['"]\)/g
16 module
.exports
= function purescriptLoader(source
, map
) {
17 const callback
= this.async()
18 const config
= this.options
19 const query
= loaderUtils
.parseQuery(this.query
)
20 const webpackOptions
= this.options
.purescriptLoader
|| {}
22 const options
= Object
.assign({
23 context: config
.context
,
26 pscBundle: 'psc-bundle',
29 pscIdeColors: webpackOptions
.psc
=== 'psa' || query
.psc
=== 'psa',
31 bundleOutput: 'output/bundle.js',
32 bundleNamespace: 'PS',
37 path
.join('src', '**', '*.purs'),
38 path
.join('bower_components', 'purescript-*', 'src', '**', '*.purs')
40 }, webpackOptions
, query
)
42 this.cacheable
&& this.cacheable()
44 let cache
= config
.purescriptLoaderCache
= config
.purescriptLoaderCache
|| {
50 if (!config
.purescriptLoaderInstalled
) {
51 config
.purescriptLoaderInstalled
= true
53 // invalidate loader cache when bundle is marked as invalid (in watch mode)
54 this._compiler
.plugin('invalid', () => {
55 debug('invalidating loader cache');
57 cache
= config
.purescriptLoaderCache
= {
58 rebuild: options
.pscIde
,
61 ideServer: cache
.ideServer
,
62 psModuleMap: cache
.psModuleMap
66 // add psc warnings to webpack compilation warnings
67 this._compiler
.plugin('after-compile', (compilation
, callback
) => {
68 if (options
.warnings
&& cache
.warnings
) {
69 compilation
.warnings
.unshift(`PureScript compilation:\n${cache.warnings}`)
70 cache
.warnings
= null;
74 compilation
.errors
.unshift(`PureScript compilation:\n${cache.errors}`)
82 const psModuleName
= PsModuleMap
.match(source
)
85 load: js
=> callback(null, js
),
86 reject: error
=> callback(error
),
87 srcPath: this.resourcePath
,
88 srcDir: path
.dirname(this.resourcePath
),
89 jsPath: path
.resolve(path
.join(options
.output
, psModuleName
, 'index.js')),
94 debug('loader called', psModule
.name
)
97 cache
.bundleModules
.push(psModule
.name
)
101 return PscIde
.connect(psModule
)
102 .then(PscIde
.rebuild
)
105 .catch(psModule
.reject
)
108 if (cache
.compilationFinished
) {
109 return toJavaScript(psModule
).then(psModule
.load
).catch(psModule
.reject
)
112 // We need to wait for compilation to finish before the loaders run so that
113 // references to compiled output are valid.
114 cache
.deferred
.push(psModule
)
116 if (!cache
.compilationStarted
) {
117 return Psc
.compile(psModule
)
118 .then(() => PsModuleMap
.makeMap(options
.src
).then(map
=> {
119 debug('rebuilt module map');
120 cache
.psModuleMap
= map
;
122 .then(() => Promise
.map(cache
.deferred
, psModule
=> {
123 if (typeof cache
.ideServer
=== 'object') cache
.ideServer
.kill()
124 return toJavaScript(psModule
).then(psModule
.load
)
127 cache
.deferred
[0].reject(error
)
128 cache
.deferred
.slice(1).forEach(psModule
=> psModule
.reject(true))
133 function updatePsModuleMap(psModule
) {
134 const options
= psModule
.options
135 const cache
= psModule
.cache
136 const filePurs
= psModule
.srcPath
137 if (!cache
.psModuleMap
) {
138 debug('module mapping does not exist');
139 return PsModuleMap
.makeMap(options
.src
).then(map
=> {
140 cache
.psModuleMap
= map
;
141 return cache
.psModuleMap
;
145 return PsModuleMap
.makeMapEntry(filePurs
).then(result
=> {
146 const map
= Object
.assign(cache
.psModuleMap
, result
)
147 cache
.psModuleMap
= map
;
148 return cache
.psModuleMap
;
153 // The actual loader is executed *after* purescript compilation.
154 function toJavaScript(psModule
) {
155 const options
= psModule
.options
156 const cache
= psModule
.cache
157 const bundlePath
= path
.resolve(options
.bundleOutput
)
158 const jsPath
= cache
.bundle
? bundlePath : psModule
.jsPath
160 debug('loading JavaScript for', psModule
.name
)
162 return Promise
.props({
163 js: fs
.readFileAsync(jsPath
, 'utf8'),
164 psModuleMap: updatePsModuleMap(psModule
)
168 if (options
.bundle
) {
169 // if bundling, return a reference to the bundle
170 js
= 'module.exports = require("'
171 + jsStringEscape(path
.relative(psModule
.srcDir
, options
.bundleOutput
))
172 + '")["' + psModule
.name
+ '"]'
174 // replace require paths to output files generated by psc with paths
175 // to purescript sources, which are then also run through this loader.
177 .replace(requireRegex
, (m
, p1
) => {
178 return 'require("' + jsStringEscape(result
.psModuleMap
[p1
].src
) + '")'
180 .replace(/require\(['"]\.\/foreign['"]\)/g, (m
, p1
) => {
181 return 'require("' + jsStringEscape(result
.psModuleMap
[psModule
.name
].ffi
) + '")'