diff options
author | Cyril Sobierajewicz <cyril.sobierajewicz@fretlink.com> | 2019-03-22 16:25:43 +0100 |
---|---|---|
committer | Cyril Sobierajewicz <cyril.sobierajewicz@fretlink.com> | 2019-03-22 16:28:18 +0100 |
commit | ab51e7511f61e432c19f805c0f566fb674476ac2 (patch) | |
tree | b083e7a0e50470cc8df620cb10c30fe64d1718f6 | |
parent | d2dedd26a0ff0133904956198ce694c241214dfa (diff) | |
download | purs-loader-ab51e7511f61e432c19f805c0f566fb674476ac2.tar.gz purs-loader-ab51e7511f61e432c19f805c0f566fb674476ac2.tar.zst purs-loader-ab51e7511f61e432c19f805c0f566fb674476ac2.zip |
Support multiple sources directories when resolving module names
-rw-r--r-- | src/index.js | 4 | ||||
-rw-r--r-- | src/utils.js | 20 |
2 files changed, 22 insertions, 2 deletions
diff --git a/src/index.js b/src/index.js index be809c6..d713737 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -126,7 +126,8 @@ module.exports = function purescriptLoader(source, map) { | |||
126 | warnings: true, | 126 | warnings: true, |
127 | watch: false, | 127 | watch: false, |
128 | output: 'output', | 128 | output: 'output', |
129 | src: [] | 129 | src: [], |
130 | rewriteRules: {} | ||
130 | }, loaderOptions, { | 131 | }, loaderOptions, { |
131 | src: srcOption | 132 | src: srcOption |
132 | }); | 133 | }); |
@@ -227,6 +228,7 @@ module.exports = function purescriptLoader(source, map) { | |||
227 | const resolved = utils.resolvePursModule({ | 228 | const resolved = utils.resolvePursModule({ |
228 | baseModulePath, | 229 | baseModulePath, |
229 | baseModuleName, | 230 | baseModuleName, |
231 | rewriteRules: options.rewriteRules, | ||
230 | targetModuleName | 232 | targetModuleName |
231 | }); | 233 | }); |
232 | this.addDependency(resolved); | 234 | this.addDependency(resolved); |
diff --git a/src/utils.js b/src/utils.js index 829ba72..446402c 100644 --- a/src/utils.js +++ b/src/utils.js | |||
@@ -23,7 +23,25 @@ const diffPursModuleNames = (from, target, parts) => { | |||
23 | ? diffPursModuleNames(tail_from, tail_target, parts) | 23 | ? diffPursModuleNames(tail_from, tail_target, parts) |
24 | : parts.concat(repeat('..', from.length), target); | 24 | : parts.concat(repeat('..', from.length), target); |
25 | }; | 25 | }; |
26 | exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => { | 26 | |
27 | const normalizeRewriteRuleDest = ({ dest, moduleName }) => | ||
28 | typeof dest === 'function' ? dest(moduleName) : dest; | ||
29 | const rewrite = ({ rules, moduleName }) => { | ||
30 | const moduleNameParts = moduleName.split('.') | ||
31 | for (const [rule, dest] of Object.entries(rules)) { | ||
32 | const ruleParts = rule.split('.'); | ||
33 | const matched = ruleParts.every((part, i) => | ||
34 | part === '*' || part === moduleNameParts[i]); | ||
35 | if (!matched) continue; | ||
36 | const rest = moduleNameParts.slice(ruleParts.length); | ||
37 | const base = normalizeRewriteRuleDest({ dest, moduleName }); | ||
38 | return `${path.join(base, ...rest)}.purs`; | ||
39 | } | ||
40 | }; | ||
41 | |||
42 | exports.resolvePursModule = ({ baseModulePath, baseModuleName, rewriteRules, targetModuleName }) => { | ||
43 | const rewrittenModulePath = rewrite({ rules: rewriteRules, moduleName: targetModuleName }); | ||
44 | if (rewrittenModulePath) return rewrittenModulePath; | ||
27 | const parts = diffPursModuleNames( | 45 | const parts = diffPursModuleNames( |
28 | baseModuleName.split('.'), | 46 | baseModuleName.split('.'), |
29 | targetModuleName.split('.'), | 47 | targetModuleName.split('.'), |