]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const debug_ = require('debug'); | |
4 | ||
5 | const debug = debug_('purs-loader'); | |
6 | ||
7 | const debugVerbose = debug_('purs-loader:verbose'); | |
8 | ||
9 | const loaderUtils = require('loader-utils') | |
10 | ||
11 | const Promise = require('bluebird') | |
12 | ||
13 | const path = require('path') | |
14 | ||
15 | const PsModuleMap = require('./purs-module-map'); | |
16 | ||
17 | const compile = require('./compile'); | |
18 | ||
19 | const bundle = require('./bundle'); | |
20 | ||
21 | const ide = require('./ide'); | |
22 | ||
23 | const toJavaScript = require('./to-javascript'); | |
24 | ||
25 | const dargs = require('./dargs'); | |
26 | ||
27 | const spawn = require('cross-spawn').sync | |
28 | ||
29 | const eol = require('os').EOL | |
30 | ||
31 | module.exports = function purescriptLoader(source, map) { | |
32 | this.cacheable && this.cacheable(); | |
33 | ||
34 | const webpackConfig = this.options; | |
35 | ||
36 | var cache = webpackConfig.purescriptLoaderCache = webpackConfig.purescriptLoaderCache || { | |
37 | rebuild: false, | |
38 | deferred: [], | |
39 | bundleModules: [], | |
40 | ideServer: null, | |
41 | psModuleMap: null, | |
42 | warnings: [], | |
43 | errors: [], | |
44 | compilationStarted: false, | |
45 | compilationFinished: false, | |
46 | installed: false, | |
47 | srcOption: [] | |
48 | }; | |
49 | ||
50 | const callback = this.async(); | |
51 | ||
52 | const loaderOptions = loaderUtils.getOptions(this) || {}; | |
53 | ||
54 | const srcOption = (pscPackage => { | |
55 | const srcPath = path.join('src', '**', '*.purs'); | |
56 | ||
57 | const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs'); | |
58 | ||
59 | if (cache.srcOption.length > 0) { | |
60 | return cache.srcOption; | |
61 | } | |
62 | else if (pscPackage) { | |
63 | const pscPackageCommand = 'psc-package'; | |
64 | ||
65 | const pscPackageArgs = ['sources']; | |
66 | ||
67 | const loaderSrc = loaderOptions.src || [ | |
68 | srcPath | |
69 | ]; | |
70 | ||
71 | debug('psc-package %s %o', pscPackageCommand, pscPackageArgs); | |
72 | ||
73 | const cmd = spawn(pscPackageCommand, pscPackageArgs); | |
74 | ||
75 | if (cmd.status !== 0) { | |
76 | const error = cmd.stdout.toString(); | |
77 | ||
78 | throw new Error(error); | |
79 | } | |
80 | else { | |
81 | const result = cmd.stdout.toString().split(eol).filter(v => v != '').concat(loaderSrc); | |
82 | ||
83 | debug('psc-package result: %o', result); | |
84 | ||
85 | cache.srcOption = result; | |
86 | ||
87 | return result; | |
88 | } | |
89 | } | |
90 | else { | |
91 | const result = loaderOptions.src || [ | |
92 | bowerPath, | |
93 | srcPath | |
94 | ]; | |
95 | ||
96 | cache.srcOption = result; | |
97 | ||
98 | return result; | |
99 | } | |
100 | })(loaderOptions.pscPackage); | |
101 | ||
102 | const options = Object.assign({ | |
103 | context: webpackConfig.context, | |
104 | psc: null, | |
105 | pscArgs: {}, | |
106 | pscBundle: null, | |
107 | pscBundleArgs: {}, | |
108 | pscIde: false, | |
109 | pscIdeColors: loaderOptions.psc === 'psa', | |
110 | pscIdeArgs: {}, | |
111 | pscPackage: false, | |
112 | bundleOutput: 'output/bundle.js', | |
113 | bundleNamespace: 'PS', | |
114 | bundle: false, | |
115 | warnings: true, | |
116 | watch: false, | |
117 | output: 'output', | |
118 | src: [] | |
119 | }, loaderOptions, { | |
120 | src: srcOption | |
121 | }); | |
122 | ||
123 | if (!cache.installed) { | |
124 | debugVerbose('installing purs-loader with options: %O', options); | |
125 | ||
126 | cache.installed = true; | |
127 | ||
128 | // invalidate loader cache when bundle is marked as invalid (in watch mode) | |
129 | this._compiler.plugin('invalid', () => { | |
130 | debugVerbose('invalidating loader cache'); | |
131 | ||
132 | cache = webpackConfig.purescriptLoaderCache = { | |
133 | rebuild: options.pscIde, | |
134 | deferred: [], | |
135 | bundleModules: [], | |
136 | ideServer: cache.ideServer, | |
137 | psModuleMap: cache.psModuleMap, | |
138 | warnings: [], | |
139 | errors: [], | |
140 | compilationStarted: cache.compilationStarted, | |
141 | compilationFinished: cache.compilationFinished, | |
142 | installed: cache.installed, | |
143 | srcOption: cache.srcOption | |
144 | }; | |
145 | }); | |
146 | ||
147 | // add psc warnings to webpack compilation warnings | |
148 | this._compiler.plugin('after-compile', (compilation, callback) => { | |
149 | cache.warnings.forEach(warning => { | |
150 | compilation.warnings.push(warning); | |
151 | }); | |
152 | ||
153 | cache.errors.forEach(error => { | |
154 | compilation.errors.push(error); | |
155 | }); | |
156 | ||
157 | callback() | |
158 | }); | |
159 | } | |
160 | ||
161 | const psModuleName = PsModuleMap.matchModule(source); | |
162 | ||
163 | const psModule = { | |
164 | name: psModuleName, | |
165 | load: js => callback(null, js), | |
166 | reject: error => callback(error), | |
167 | srcPath: this.resourcePath, | |
168 | srcDir: path.dirname(this.resourcePath), | |
169 | jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), | |
170 | options: options, | |
171 | cache: cache, | |
172 | emitWarning: warning => { | |
173 | if (options.warnings && warning.length) { | |
174 | cache.warnings.push(warning); | |
175 | } | |
176 | }, | |
177 | emitError: error => { | |
178 | if (error.length) { | |
179 | cache.errors.push(error); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | debug('loading %s', psModule.name); | |
185 | ||
186 | if (options.bundle) { | |
187 | cache.bundleModules.push(psModule.name); | |
188 | } | |
189 | ||
190 | if (cache.rebuild) { | |
191 | const connect = () => { | |
192 | if (!cache.ideServer) { | |
193 | cache.ideServer = true; | |
194 | ||
195 | return ide.connect(psModule) | |
196 | .then(ideServer => { | |
197 | cache.ideServer = ideServer; | |
198 | return psModule; | |
199 | }) | |
200 | .then(ide.loadWithRetry) | |
201 | .catch(error => { | |
202 | if (cache.ideServer.kill) { | |
203 | debug('ide failed to initially load modules, stopping the ide server process'); | |
204 | ||
205 | cache.ideServer.kill(); | |
206 | } | |
207 | ||
208 | cache.ideServer = null; | |
209 | ||
210 | return Promise.reject(error); | |
211 | }) | |
212 | ; | |
213 | } | |
214 | else { | |
215 | return Promise.resolve(psModule); | |
216 | } | |
217 | }; | |
218 | ||
219 | const rebuild = () => | |
220 | ide.rebuild(psModule).catch(error => { | |
221 | if (error instanceof ide.UnknownModuleError) { | |
222 | if (!cache.compilationStarted) { | |
223 | cache.compilationStarted = true; | |
224 | ||
225 | return compile(psModule) | |
226 | .then(() => { | |
227 | cache.compilationFinished = true; | |
228 | }) | |
229 | .then(() => | |
230 | PsModuleMap.makeMap(options.src).then(map => { | |
231 | debug('rebuilt module map after unknown module forced a recompilation'); | |
232 | ||
233 | cache.psModuleMap = map; | |
234 | }) | |
235 | ) | |
236 | .then(() => ide.load(psModule)) | |
237 | .then(() => psModule) | |
238 | ; | |
239 | } | |
240 | else { | |
241 | return Promise.resolve(psModule); | |
242 | } | |
243 | } | |
244 | else { | |
245 | debug('ide rebuild failed due to an unhandled error: %o', error); | |
246 | ||
247 | return Promise.reject(error); | |
248 | } | |
249 | }) | |
250 | ; | |
251 | ||
252 | connect() | |
253 | .then(rebuild) | |
254 | .then(toJavaScript) | |
255 | .then(psModule.load) | |
256 | .catch(psModule.reject) | |
257 | ; | |
258 | } | |
259 | else if (cache.compilationFinished) { | |
260 | debugVerbose('compilation is already finished, loading module %s', psModule.name); | |
261 | ||
262 | toJavaScript(psModule) | |
263 | .then(psModule.load) | |
264 | .catch(psModule.reject); | |
265 | } | |
266 | else { | |
267 | // The compilation has not finished yet. We need to wait for | |
268 | // compilation to finish before the loaders run so that references | |
269 | // to compiled output are valid. Push the modules into the cache to | |
270 | // be loaded once the complation is complete. | |
271 | ||
272 | cache.deferred.push(psModule); | |
273 | ||
274 | if (!cache.compilationStarted) { | |
275 | cache.compilationStarted = true; | |
276 | ||
277 | compile(psModule) | |
278 | .then(() => { | |
279 | cache.compilationFinished = true; | |
280 | }) | |
281 | .then(() => { | |
282 | if (options.bundle) { | |
283 | return bundle(options, cache.bundleModules); | |
284 | } | |
285 | }) | |
286 | .then(() => | |
287 | PsModuleMap.makeMap(options.src).then(map => { | |
288 | debug('rebuilt module map after compilation'); | |
289 | ||
290 | cache.psModuleMap = map; | |
291 | }) | |
292 | ) | |
293 | .then(() => | |
294 | Promise.map(cache.deferred, psModule => | |
295 | toJavaScript(psModule).then(psModule.load) | |
296 | ) | |
297 | ) | |
298 | .catch(error => { | |
299 | cache.deferred[0].reject(error); | |
300 | ||
301 | cache.deferred.slice(1).forEach(psModule => { | |
302 | psModule.reject(new Error('purs-loader failed')); | |
303 | }) | |
304 | }) | |
305 | ; | |
306 | } | |
307 | else { | |
308 | // The complation has started. Nothing to do but wait until it is | |
309 | // done before loading all of the modules. | |
310 | } | |
311 | } | |
312 | } |