diff options
author | eric thul <thul.eric@gmail.com> | 2017-04-23 17:29:44 -0400 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2017-04-23 18:20:22 -0400 |
commit | e17196589a380eed3ffc47705f8c6c87f99b58c2 (patch) | |
tree | 3e924b370d11a647c3e86647fd3cd5965572aaf9 /src/index.js | |
parent | f564370713291980820cdce342161fe89cd2e354 (diff) | |
download | purs-loader-e17196589a380eed3ffc47705f8c6c87f99b58c2.tar.gz purs-loader-e17196589a380eed3ffc47705f8c6c87f99b58c2.tar.zst purs-loader-e17196589a380eed3ffc47705f8c6c87f99b58c2.zip |
Caching the result of psc-package
Resolves #83
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 80 |
1 files changed, 58 insertions, 22 deletions
diff --git a/src/index.js b/src/index.js index 7cf942c..19a549e 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -31,31 +31,71 @@ const eol = require('os').EOL | |||
31 | module.exports = function purescriptLoader(source, map) { | 31 | module.exports = function purescriptLoader(source, map) { |
32 | this.cacheable && this.cacheable(); | 32 | this.cacheable && this.cacheable(); |
33 | 33 | ||
34 | const callback = this.async(); | ||
35 | |||
36 | const webpackConfig = this.options; | 34 | const webpackConfig = this.options; |
37 | 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 | |||
38 | const loaderOptions = loaderUtils.getOptions(this) || {}; | 52 | const loaderOptions = loaderUtils.getOptions(this) || {}; |
39 | 53 | ||
40 | const srcOption = (pscPackage => { | 54 | const srcOption = (pscPackage => { |
41 | if (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) { | ||
42 | const pscPackageCommand = 'psc-package'; | 63 | const pscPackageCommand = 'psc-package'; |
43 | 64 | ||
44 | const pscPackageArgs = ['sources']; | 65 | const pscPackageArgs = ['sources']; |
45 | 66 | ||
67 | const loaderSrc = loaderOptions.src || [ | ||
68 | srcPath | ||
69 | ]; | ||
70 | |||
46 | debug('psc-package %s %o', pscPackageCommand, pscPackageArgs); | 71 | debug('psc-package %s %o', pscPackageCommand, pscPackageArgs); |
47 | 72 | ||
48 | return spawn(pscPackageCommand, pscPackageArgs).stdout.toString().split(eol).filter(v => v != '').concat( | 73 | const cmd = spawn(pscPackageCommand, pscPackageArgs); |
49 | loaderOptions.src || [ | 74 | |
50 | path.join('src', '**', '*.purs'), | 75 | if (cmd.status !== 0) { |
51 | ] | 76 | const error = cmd.stdout.toString(); |
52 | ) | 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 | } | ||
53 | } | 89 | } |
54 | else { | 90 | else { |
55 | return loaderOptions.src || [ | 91 | const result = loaderOptions.src || [ |
56 | path.join('bower_components', 'purescript-*', 'src', '**', '*.purs'), | 92 | bowerPath, |
57 | path.join('src', '**', '*.purs'), | 93 | srcPath |
58 | ]; | 94 | ]; |
95 | |||
96 | cache.srcOption = result; | ||
97 | |||
98 | return result; | ||
59 | } | 99 | } |
60 | })(loaderOptions.pscPackage); | 100 | })(loaderOptions.pscPackage); |
61 | 101 | ||
@@ -80,18 +120,10 @@ module.exports = function purescriptLoader(source, map) { | |||
80 | src: srcOption | 120 | src: srcOption |
81 | }); | 121 | }); |
82 | 122 | ||
83 | var cache = webpackConfig.purescriptLoaderCache = webpackConfig.purescriptLoaderCache || { | 123 | if (!cache.installed) { |
84 | rebuild: false, | ||
85 | deferred: [], | ||
86 | bundleModules: [], | ||
87 | warnings: [], | ||
88 | errors: [] | ||
89 | }; | ||
90 | |||
91 | if (!webpackConfig.purescriptLoaderInstalled) { | ||
92 | debugVerbose('installing purs-loader with options: %O', options); | 124 | debugVerbose('installing purs-loader with options: %O', options); |
93 | 125 | ||
94 | webpackConfig.purescriptLoaderInstalled = true | 126 | cache.installed = true; |
95 | 127 | ||
96 | // invalidate loader cache when bundle is marked as invalid (in watch mode) | 128 | // invalidate loader cache when bundle is marked as invalid (in watch mode) |
97 | this._compiler.plugin('invalid', () => { | 129 | this._compiler.plugin('invalid', () => { |
@@ -104,7 +136,11 @@ module.exports = function purescriptLoader(source, map) { | |||
104 | ideServer: cache.ideServer, | 136 | ideServer: cache.ideServer, |
105 | psModuleMap: cache.psModuleMap, | 137 | psModuleMap: cache.psModuleMap, |
106 | warnings: [], | 138 | warnings: [], |
107 | errors: [] | 139 | errors: [], |
140 | compilationStarted: cache.compilationStarted, | ||
141 | compilationFinished: cache.compilationFinished, | ||
142 | installed: cache.installed, | ||
143 | srcOption: cache.srcOption | ||
108 | }; | 144 | }; |
109 | }); | 145 | }); |
110 | 146 | ||