diff options
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/src/index.js b/src/index.js index eeabc4b..cc779f7 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -30,24 +30,24 @@ const spawn = require('cross-spawn').sync | |||
30 | 30 | ||
31 | const eol = require('os').EOL | 31 | const eol = require('os').EOL |
32 | 32 | ||
33 | var CACHE_VAR = { | ||
34 | rebuild: false, | ||
35 | deferred: [], | ||
36 | bundleModules: [], | ||
37 | ideServer: null, | ||
38 | psModuleMap: null, | ||
39 | warnings: [], | ||
40 | errors: [], | ||
41 | compilationStarted: false, | ||
42 | compilationFinished: false, | ||
43 | installed: false, | ||
44 | srcOption: [] | ||
45 | }; | ||
46 | |||
33 | module.exports = function purescriptLoader(source, map) { | 47 | module.exports = function purescriptLoader(source, map) { |
34 | this.cacheable && this.cacheable(); | 48 | this.cacheable && this.cacheable(); |
35 | 49 | ||
36 | const webpackConfig = this.options; | 50 | const webpackContext = (this.options && this.options.context) || this.rootContext; |
37 | |||
38 | var cache = webpackConfig.purescriptLoaderCache = webpackConfig.purescriptLoaderCache || { | ||
39 | rebuild: false, | ||
40 | deferred: [], | ||
41 | bundleModules: [], | ||
42 | ideServer: null, | ||
43 | psModuleMap: null, | ||
44 | warnings: [], | ||
45 | errors: [], | ||
46 | compilationStarted: false, | ||
47 | compilationFinished: false, | ||
48 | installed: false, | ||
49 | srcOption: [] | ||
50 | }; | ||
51 | 51 | ||
52 | const callback = this.async(); | 52 | const callback = this.async(); |
53 | 53 | ||
@@ -58,8 +58,8 @@ module.exports = function purescriptLoader(source, map) { | |||
58 | 58 | ||
59 | const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs'); | 59 | const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs'); |
60 | 60 | ||
61 | if (cache.srcOption.length > 0) { | 61 | if (CACHE_VAR.srcOption.length > 0) { |
62 | return cache.srcOption; | 62 | return CACHE_VAR.srcOption; |
63 | } | 63 | } |
64 | else if (pscPackage) { | 64 | else if (pscPackage) { |
65 | const pscPackageCommand = 'psc-package'; | 65 | const pscPackageCommand = 'psc-package'; |
@@ -87,7 +87,7 @@ module.exports = function purescriptLoader(source, map) { | |||
87 | 87 | ||
88 | debug('psc-package result: %o', result); | 88 | debug('psc-package result: %o', result); |
89 | 89 | ||
90 | cache.srcOption = result; | 90 | CACHE_VAR.srcOption = result; |
91 | 91 | ||
92 | return result; | 92 | return result; |
93 | } | 93 | } |
@@ -98,14 +98,14 @@ module.exports = function purescriptLoader(source, map) { | |||
98 | srcPath | 98 | srcPath |
99 | ]; | 99 | ]; |
100 | 100 | ||
101 | cache.srcOption = result; | 101 | CACHE_VAR.srcOption = result; |
102 | 102 | ||
103 | return result; | 103 | return result; |
104 | } | 104 | } |
105 | })(loaderOptions.pscPackage); | 105 | })(loaderOptions.pscPackage); |
106 | 106 | ||
107 | const options = Object.assign({ | 107 | const options = Object.assign({ |
108 | context: webpackConfig.context, | 108 | context: webpackContext, |
109 | psc: null, | 109 | psc: null, |
110 | pscArgs: {}, | 110 | pscArgs: {}, |
111 | pscBundle: null, | 111 | pscBundle: null, |
@@ -128,37 +128,37 @@ module.exports = function purescriptLoader(source, map) { | |||
128 | src: srcOption | 128 | src: srcOption |
129 | }); | 129 | }); |
130 | 130 | ||
131 | if (!cache.installed) { | 131 | if (!CACHE_VAR.installed) { |
132 | debugVerbose('installing purs-loader with options: %O', options); | 132 | debugVerbose('installing purs-loader with options: %O', options); |
133 | 133 | ||
134 | cache.installed = true; | 134 | CACHE_VAR.installed = true; |
135 | 135 | ||
136 | // invalidate loader cache when bundle is marked as invalid (in watch mode) | 136 | // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode) |
137 | this._compiler.plugin('invalid', () => { | 137 | this._compiler.plugin('invalid', () => { |
138 | debugVerbose('invalidating loader cache'); | 138 | debugVerbose('invalidating loader CACHE_VAR'); |
139 | 139 | ||
140 | cache = webpackConfig.purescriptLoaderCache = { | 140 | CACHE_VAR = { |
141 | rebuild: options.pscIde, | 141 | rebuild: options.pscIde, |
142 | deferred: [], | 142 | deferred: [], |
143 | bundleModules: [], | 143 | bundleModules: [], |
144 | ideServer: cache.ideServer, | 144 | ideServer: CACHE_VAR.ideServer, |
145 | psModuleMap: cache.psModuleMap, | 145 | psModuleMap: CACHE_VAR.psModuleMap, |
146 | warnings: [], | 146 | warnings: [], |
147 | errors: [], | 147 | errors: [], |
148 | compilationStarted: false, | 148 | compilationStarted: false, |
149 | compilationFinished: false, | 149 | compilationFinished: false, |
150 | installed: cache.installed, | 150 | installed: CACHE_VAR.installed, |
151 | srcOption: [] | 151 | srcOption: [] |
152 | }; | 152 | }; |
153 | }); | 153 | }); |
154 | 154 | ||
155 | // add psc warnings to webpack compilation warnings | 155 | // add psc warnings to webpack compilation warnings |
156 | this._compiler.plugin('after-compile', (compilation, callback) => { | 156 | this._compiler.plugin('after-compile', (compilation, callback) => { |
157 | cache.warnings.forEach(warning => { | 157 | CACHE_VAR.warnings.forEach(warning => { |
158 | compilation.warnings.push(warning); | 158 | compilation.warnings.push(warning); |
159 | }); | 159 | }); |
160 | 160 | ||
161 | cache.errors.forEach(error => { | 161 | CACHE_VAR.errors.forEach(error => { |
162 | compilation.errors.push(error); | 162 | compilation.errors.push(error); |
163 | }); | 163 | }); |
164 | 164 | ||
@@ -178,15 +178,15 @@ module.exports = function purescriptLoader(source, map) { | |||
178 | srcDir: path.dirname(this.resourcePath), | 178 | srcDir: path.dirname(this.resourcePath), |
179 | jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), | 179 | jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), |
180 | options: options, | 180 | options: options, |
181 | cache: cache, | 181 | cache: CACHE_VAR, |
182 | emitWarning: warning => { | 182 | emitWarning: warning => { |
183 | if (options.warnings && warning.length) { | 183 | if (options.warnings && warning.length) { |
184 | cache.warnings.push(warning); | 184 | CACHE_VAR.warnings.push(warning); |
185 | } | 185 | } |
186 | }, | 186 | }, |
187 | emitError: error => { | 187 | emitError: error => { |
188 | if (error.length) { | 188 | if (error.length) { |
189 | cache.errors.push(error); | 189 | CACHE_VAR.errors.push(error); |
190 | } | 190 | } |
191 | } | 191 | } |
192 | } | 192 | } |
@@ -194,28 +194,28 @@ module.exports = function purescriptLoader(source, map) { | |||
194 | debug('loading %s', psModule.name); | 194 | debug('loading %s', psModule.name); |
195 | 195 | ||
196 | if (options.bundle) { | 196 | if (options.bundle) { |
197 | cache.bundleModules.push(psModule.name); | 197 | CACHE_VAR.bundleModules.push(psModule.name); |
198 | } | 198 | } |
199 | 199 | ||
200 | if (cache.rebuild) { | 200 | if (CACHE_VAR.rebuild) { |
201 | const connect = () => { | 201 | const connect = () => { |
202 | if (!cache.ideServer) { | 202 | if (!CACHE_VAR.ideServer) { |
203 | cache.ideServer = true; | 203 | CACHE_VAR.ideServer = true; |
204 | 204 | ||
205 | return ide.connect(psModule) | 205 | return ide.connect(psModule) |
206 | .then(ideServer => { | 206 | .then(ideServer => { |
207 | cache.ideServer = ideServer; | 207 | CACHE_VAR.ideServer = ideServer; |
208 | return psModule; | 208 | return psModule; |
209 | }) | 209 | }) |
210 | .then(ide.loadWithRetry) | 210 | .then(ide.loadWithRetry) |
211 | .catch(error => { | 211 | .catch(error => { |
212 | if (cache.ideServer.kill) { | 212 | if (CACHE_VAR.ideServer.kill) { |
213 | debug('ide failed to initially load modules, stopping the ide server process'); | 213 | debug('ide failed to initially load modules, stopping the ide server process'); |
214 | 214 | ||
215 | cache.ideServer.kill(); | 215 | CACHE_VAR.ideServer.kill(); |
216 | } | 216 | } |
217 | 217 | ||
218 | cache.ideServer = null; | 218 | CACHE_VAR.ideServer = null; |
219 | 219 | ||
220 | return Promise.reject(error); | 220 | return Promise.reject(error); |
221 | }) | 221 | }) |
@@ -240,17 +240,17 @@ module.exports = function purescriptLoader(source, map) { | |||
240 | // unknown module error. We need to wait until compilation is | 240 | // unknown module error. We need to wait until compilation is |
241 | // done before loading these files. | 241 | // done before loading these files. |
242 | 242 | ||
243 | cache.deferred.push(psModule); | 243 | CACHE_VAR.deferred.push(psModule); |
244 | 244 | ||
245 | if (!cache.compilationStarted) { | 245 | if (!CACHE_VAR.compilationStarted) { |
246 | cache.compilationStarted = true; | 246 | CACHE_VAR.compilationStarted = true; |
247 | 247 | ||
248 | return compile(psModule) | 248 | return compile(psModule) |
249 | .then(() => { | 249 | .then(() => { |
250 | cache.compilationFinished = true; | 250 | CACHE_VAR.compilationFinished = true; |
251 | }) | 251 | }) |
252 | .then(() => | 252 | .then(() => |
253 | Promise.map(cache.deferred, psModule => | 253 | Promise.map(CACHE_VAR.deferred, psModule => |
254 | ide.load(psModule) | 254 | ide.load(psModule) |
255 | .then(() => toJavaScript(psModule)) | 255 | .then(() => toJavaScript(psModule)) |
256 | .then(js => sourceMaps(psModule, js)) | 256 | .then(js => sourceMaps(psModule, js)) |
@@ -258,9 +258,9 @@ module.exports = function purescriptLoader(source, map) { | |||
258 | ) | 258 | ) |
259 | ) | 259 | ) |
260 | .catch(error => { | 260 | .catch(error => { |
261 | cache.deferred[0].reject(error); | 261 | CACHE_VAR.deferred[0].reject(error); |
262 | 262 | ||
263 | cache.deferred.slice(1).forEach(psModule => { | 263 | CACHE_VAR.deferred.slice(1).forEach(psModule => { |
264 | psModule.reject(new Error('purs-loader failed')); | 264 | psModule.reject(new Error('purs-loader failed')); |
265 | }) | 265 | }) |
266 | }) | 266 | }) |
@@ -282,7 +282,7 @@ module.exports = function purescriptLoader(source, map) { | |||
282 | 282 | ||
283 | connect().then(rebuild); | 283 | connect().then(rebuild); |
284 | } | 284 | } |
285 | else if (cache.compilationFinished) { | 285 | else if (CACHE_VAR.compilationFinished) { |
286 | debugVerbose('compilation is already finished, loading module %s', psModule.name); | 286 | debugVerbose('compilation is already finished, loading module %s', psModule.name); |
287 | 287 | ||
288 | toJavaScript(psModule) | 288 | toJavaScript(psModule) |
@@ -293,34 +293,34 @@ module.exports = function purescriptLoader(source, map) { | |||
293 | else { | 293 | else { |
294 | // The compilation has not finished yet. We need to wait for | 294 | // The compilation has not finished yet. We need to wait for |
295 | // compilation to finish before the loaders run so that references | 295 | // compilation to finish before the loaders run so that references |
296 | // to compiled output are valid. Push the modules into the cache to | 296 | // to compiled output are valid. Push the modules into the CACHE_VAR to |
297 | // be loaded once the complation is complete. | 297 | // be loaded once the complation is complete. |
298 | 298 | ||
299 | cache.deferred.push(psModule); | 299 | CACHE_VAR.deferred.push(psModule); |
300 | 300 | ||
301 | if (!cache.compilationStarted) { | 301 | if (!CACHE_VAR.compilationStarted) { |
302 | cache.compilationStarted = true; | 302 | CACHE_VAR.compilationStarted = true; |
303 | 303 | ||
304 | compile(psModule) | 304 | compile(psModule) |
305 | .then(() => { | 305 | .then(() => { |
306 | cache.compilationFinished = true; | 306 | CACHE_VAR.compilationFinished = true; |
307 | }) | 307 | }) |
308 | .then(() => { | 308 | .then(() => { |
309 | if (options.bundle) { | 309 | if (options.bundle) { |
310 | return bundle(options, cache.bundleModules); | 310 | return bundle(options, CACHE_VAR.bundleModules); |
311 | } | 311 | } |
312 | }) | 312 | }) |
313 | .then(() => | 313 | .then(() => |
314 | Promise.map(cache.deferred, psModule => | 314 | Promise.map(CACHE_VAR.deferred, psModule => |
315 | toJavaScript(psModule) | 315 | toJavaScript(psModule) |
316 | .then(js => sourceMaps(psModule, js)) | 316 | .then(js => sourceMaps(psModule, js)) |
317 | .then(psModule.load) | 317 | .then(psModule.load) |
318 | ) | 318 | ) |
319 | ) | 319 | ) |
320 | .catch(error => { | 320 | .catch(error => { |
321 | cache.deferred[0].reject(error); | 321 | CACHE_VAR.deferred[0].reject(error); |
322 | 322 | ||
323 | cache.deferred.slice(1).forEach(psModule => { | 323 | CACHE_VAR.deferred.slice(1).forEach(psModule => { |
324 | psModule.reject(new Error('purs-loader failed')); | 324 | psModule.reject(new Error('purs-loader failed')); |
325 | }) | 325 | }) |
326 | }) | 326 | }) |