From ab51e7511f61e432c19f805c0f566fb674476ac2 Mon Sep 17 00:00:00 2001 From: Cyril Sobierajewicz Date: Fri, 22 Mar 2019 16:25:43 +0100 Subject: Support multiple sources directories when resolving module names --- src/index.js | 4 +++- 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) { warnings: true, watch: false, output: 'output', - src: [] + src: [], + rewriteRules: {} }, loaderOptions, { src: srcOption }); @@ -227,6 +228,7 @@ module.exports = function purescriptLoader(source, map) { const resolved = utils.resolvePursModule({ baseModulePath, baseModuleName, + rewriteRules: options.rewriteRules, targetModuleName }); 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) => { ? diffPursModuleNames(tail_from, tail_target, parts) : parts.concat(repeat('..', from.length), target); }; -exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => { + +const normalizeRewriteRuleDest = ({ dest, moduleName }) => + typeof dest === 'function' ? dest(moduleName) : dest; +const rewrite = ({ rules, moduleName }) => { + const moduleNameParts = moduleName.split('.') + for (const [rule, dest] of Object.entries(rules)) { + const ruleParts = rule.split('.'); + const matched = ruleParts.every((part, i) => + part === '*' || part === moduleNameParts[i]); + if (!matched) continue; + const rest = moduleNameParts.slice(ruleParts.length); + const base = normalizeRewriteRuleDest({ dest, moduleName }); + return `${path.join(base, ...rest)}.purs`; + } +}; + +exports.resolvePursModule = ({ baseModulePath, baseModuleName, rewriteRules, targetModuleName }) => { + const rewrittenModulePath = rewrite({ rules: rewriteRules, moduleName: targetModuleName }); + if (rewrittenModulePath) return rewrittenModulePath; const parts = diffPursModuleNames( baseModuleName.split('.'), targetModuleName.split('.'), -- cgit v1.2.3 From 9767783f57f4d10c7633e5a652f81cf13cd5c000 Mon Sep 17 00:00:00 2001 From: Cyril Sobierajewicz Date: Fri, 22 Mar 2019 16:28:46 +0100 Subject: Version 4.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1faef7b..d9235c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "purs-loader", - "version": "4.1.0", + "version": "4.2.0", "description": "A webpack loader for PureScript.", "main": "lib/index.js", "files": [ -- cgit v1.2.3