aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/index.js4
-rw-r--r--src/utils.js20
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};
26exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => { 26
27const normalizeRewriteRuleDest = ({ dest, moduleName }) =>
28 typeof dest === 'function' ? dest(moduleName) : dest;
29const 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
42exports.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('.'),