]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
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'); | |
13 | ||
14 | const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g | |
15 | ||
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 || {} | |
21 | ||
22 | const options = Object.assign({ | |
23 | context: config.context, | |
24 | psc: 'psc', | |
25 | pscArgs: {}, | |
26 | pscBundle: 'psc-bundle', | |
27 | pscBundleArgs: {}, | |
28 | pscIde: false, | |
29 | pscIdeColors: webpackOptions.psc === 'psa' || query.psc === 'psa', | |
30 | pscIdeArgs: {}, | |
31 | bundleOutput: 'output/bundle.js', | |
32 | bundleNamespace: 'PS', | |
33 | bundle: false, | |
34 | warnings: true, | |
35 | output: 'output', | |
36 | src: [ | |
37 | path.join('src', '**', '*.purs'), | |
38 | path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') | |
39 | ] | |
40 | }, webpackOptions, query) | |
41 | ||
42 | this.cacheable && this.cacheable() | |
43 | ||
44 | let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || { | |
45 | rebuild: false, | |
46 | deferred: [], | |
47 | bundleModules: [] | |
48 | } | |
49 | ||
50 | if (!config.purescriptLoaderInstalled) { | |
51 | config.purescriptLoaderInstalled = true | |
52 | ||
53 | // invalidate loader cache when bundle is marked as invalid (in watch mode) | |
54 | this._compiler.plugin('invalid', () => { | |
55 | debug('invalidating loader cache'); | |
56 | ||
57 | cache = config.purescriptLoaderCache = { | |
58 | rebuild: options.pscIde, | |
59 | deferred: [], | |
60 | bundleModules: [], | |
61 | ideServer: cache.ideServer, | |
62 | psModuleMap: cache.psModuleMap | |
63 | } | |
64 | }) | |
65 | ||
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 | } | |
71 | ||
72 | if (cache.errors) { | |
73 | compilation.errors.unshift(`PureScript compilation:\n${cache.errors}`) | |
74 | } | |
75 | ||
76 | callback() | |
77 | }) | |
78 | } | |
79 | ||
80 | const psModuleName = PsModuleMap.match(source) | |
81 | const psModule = { | |
82 | name: psModuleName, | |
83 | load: js => callback(null, js), | |
84 | reject: error => callback(error), | |
85 | srcPath: this.resourcePath, | |
86 | srcDir: path.dirname(this.resourcePath), | |
87 | jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), | |
88 | options: options, | |
89 | cache: cache, | |
90 | } | |
91 | ||
92 | debug('loader called', psModule.name) | |
93 | ||
94 | if (options.bundle) { | |
95 | cache.bundleModules.push(psModule.name) | |
96 | } | |
97 | ||
98 | if (cache.rebuild) { | |
99 | return PscIde.connect(psModule) | |
100 | .then(PscIde.rebuild) | |
101 | .then(toJavaScript) | |
102 | .then(psModule.load) | |
103 | .catch(psModule.reject) | |
104 | } | |
105 | ||
106 | if (cache.compilationFinished) { | |
107 | return toJavaScript(psModule).then(psModule.load).catch(psModule.reject) | |
108 | } | |
109 | ||
110 | // We need to wait for compilation to finish before the loaders run so that | |
111 | // references to compiled output are valid. | |
112 | cache.deferred.push(psModule) | |
113 | ||
114 | if (!cache.compilationStarted) { | |
115 | return Psc.compile(psModule) | |
116 | .then(() => PsModuleMap.makeMap(options.src).then(map => { | |
117 | debug('rebuilt module map'); | |
118 | cache.psModuleMap = map; | |
119 | })) | |
120 | .then(() => Promise.map(cache.deferred, psModule => { | |
121 | if (typeof cache.ideServer === 'object') cache.ideServer.kill() | |
122 | return toJavaScript(psModule).then(psModule.load) | |
123 | })) | |
124 | .catch(error => { | |
125 | cache.deferred[0].reject(error) | |
126 | cache.deferred.slice(1).forEach(psModule => psModule.reject(true)) | |
127 | }) | |
128 | } | |
129 | } | |
130 | ||
131 | function updatePsModuleMap(psModule) { | |
132 | const options = psModule.options | |
133 | const cache = psModule.cache | |
134 | const filePurs = psModule.srcPath | |
135 | if (!cache.psModuleMap) { | |
136 | debug('module mapping does not exist'); | |
137 | return PsModuleMap.makeMap(options.src).then(map => { | |
138 | cache.psModuleMap = map; | |
139 | return cache.psModuleMap; | |
140 | }); | |
141 | } | |
142 | else { | |
143 | return PsModuleMap.makeMapEntry(filePurs).then(result => { | |
144 | const map = Object.assign(cache.psModuleMap, result) | |
145 | cache.psModuleMap = map; | |
146 | return cache.psModuleMap; | |
147 | }); | |
148 | } | |
149 | } | |
150 | ||
151 | // The actual loader is executed *after* purescript compilation. | |
152 | function toJavaScript(psModule) { | |
153 | const options = psModule.options | |
154 | const cache = psModule.cache | |
155 | const bundlePath = path.resolve(options.bundleOutput) | |
156 | const jsPath = cache.bundle ? bundlePath : psModule.jsPath | |
157 | ||
158 | debug('loading JavaScript for', psModule.name) | |
159 | ||
160 | return Promise.props({ | |
161 | js: fs.readFileAsync(jsPath, 'utf8'), | |
162 | psModuleMap: updatePsModuleMap(psModule) | |
163 | }).then(result => { | |
164 | let js = '' | |
165 | ||
166 | if (options.bundle) { | |
167 | // if bundling, return a reference to the bundle | |
168 | js = 'module.exports = require("' | |
169 | + jsStringEscape(path.relative(psModule.srcDir, options.bundleOutput)) | |
170 | + '")["' + psModule.name + '"]' | |
171 | } else { | |
172 | // replace require paths to output files generated by psc with paths | |
173 | // to purescript sources, which are then also run through this loader. | |
174 | js = result.js | |
175 | .replace(requireRegex, (m, p1) => { | |
176 | return 'require("' + jsStringEscape(result.psModuleMap[p1].src) + '")' | |
177 | }) | |
178 | .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => { | |
179 | return 'require("' + jsStringEscape(result.psModuleMap[psModule.name].ffi) + '")' | |
180 | }) | |
181 | } | |
182 | ||
183 | return js | |
184 | }) | |
185 | } |