diff options
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/index.js | 4 | ||||
-rw-r--r-- | src/utils.js | 20 |
3 files changed, 23 insertions, 3 deletions
diff --git a/package.json b/package.json index 1faef7b..d9235c8 100644 --- a/package.json +++ b/package.json | |||
@@ -1,6 +1,6 @@ | |||
1 | { | 1 | { |
2 | "name": "purs-loader", | 2 | "name": "purs-loader", |
3 | "version": "4.1.0", | 3 | "version": "4.2.0", |
4 | "description": "A webpack loader for PureScript.", | 4 | "description": "A webpack loader for PureScript.", |
5 | "main": "lib/index.js", | 5 | "main": "lib/index.js", |
6 | "files": [ | 6 | "files": [ |
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('.'), |