diff options
-rw-r--r-- | lib/index.js | 4 | ||||
-rw-r--r-- | lib/utils.js | 59 |
2 files changed, 58 insertions, 5 deletions
diff --git a/lib/index.js b/lib/index.js index c6e88f8..ad45eac 100644 --- a/lib/index.js +++ b/lib/index.js | |||
@@ -123,7 +123,8 @@ module.exports = function purescriptLoader(source, map) { | |||
123 | warnings: true, | 123 | warnings: true, |
124 | watch: false, | 124 | watch: false, |
125 | output: 'output', | 125 | output: 'output', |
126 | src: [] | 126 | src: [], |
127 | rewriteRules: {} | ||
127 | }, loaderOptions, { | 128 | }, loaderOptions, { |
128 | src: srcOption | 129 | src: srcOption |
129 | }); | 130 | }); |
@@ -266,6 +267,7 @@ module.exports = function purescriptLoader(source, map) { | |||
266 | var _resolved = utils.resolvePursModule({ | 267 | var _resolved = utils.resolvePursModule({ |
267 | baseModulePath: baseModulePath, | 268 | baseModulePath: baseModulePath, |
268 | baseModuleName: baseModuleName, | 269 | baseModuleName: baseModuleName, |
270 | rewriteRules: options.rewriteRules, | ||
269 | targetModuleName: targetModuleName | 271 | targetModuleName: targetModuleName |
270 | }); | 272 | }); |
271 | _this.addDependency(_resolved); | 273 | _this.addDependency(_resolved); |
diff --git a/lib/utils.js b/lib/utils.js index 3c73fc6..5767fc2 100644 --- a/lib/utils.js +++ b/lib/utils.js | |||
@@ -1,5 +1,7 @@ | |||
1 | 'use strict'; | 1 | 'use strict'; |
2 | 2 | ||
3 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
4 | |||
3 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | 5 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
4 | 6 | ||
5 | function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | 7 | function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } |
@@ -54,11 +56,60 @@ var diffPursModuleNames = function diffPursModuleNames(from, target, parts) { | |||
54 | 56 | ||
55 | return head_from === head_target ? diffPursModuleNames(tail_from, tail_target, parts) : parts.concat(repeat('..', from.length), target); | 57 | return head_from === head_target ? diffPursModuleNames(tail_from, tail_target, parts) : parts.concat(repeat('..', from.length), target); |
56 | }; | 58 | }; |
57 | exports.resolvePursModule = function (_ref) { | ||
58 | var baseModulePath = _ref.baseModulePath, | ||
59 | baseModuleName = _ref.baseModuleName, | ||
60 | targetModuleName = _ref.targetModuleName; | ||
61 | 59 | ||
60 | var normalizeRewriteRuleDest = function normalizeRewriteRuleDest(_ref) { | ||
61 | var dest = _ref.dest, | ||
62 | moduleName = _ref.moduleName; | ||
63 | return typeof dest === 'function' ? dest(moduleName) : dest; | ||
64 | }; | ||
65 | var rewrite = function rewrite(_ref2) { | ||
66 | var rules = _ref2.rules, | ||
67 | moduleName = _ref2.moduleName; | ||
68 | |||
69 | var moduleNameParts = moduleName.split('.'); | ||
70 | var _iteratorNormalCompletion = true; | ||
71 | var _didIteratorError = false; | ||
72 | var _iteratorError = undefined; | ||
73 | |||
74 | try { | ||
75 | for (var _iterator = Object.entries(rules)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
76 | var _step$value = _slicedToArray(_step.value, 2), | ||
77 | rule = _step$value[0], | ||
78 | dest = _step$value[1]; | ||
79 | |||
80 | var ruleParts = rule.split('.'); | ||
81 | var matched = ruleParts.every(function (part, i) { | ||
82 | return part === '*' || part === moduleNameParts[i]; | ||
83 | }); | ||
84 | if (!matched) continue; | ||
85 | var rest = moduleNameParts.slice(ruleParts.length); | ||
86 | var base = normalizeRewriteRuleDest({ dest: dest, moduleName: moduleName }); | ||
87 | return path.join.apply(path, [base].concat(_toConsumableArray(rest))) + '.purs'; | ||
88 | } | ||
89 | } catch (err) { | ||
90 | _didIteratorError = true; | ||
91 | _iteratorError = err; | ||
92 | } finally { | ||
93 | try { | ||
94 | if (!_iteratorNormalCompletion && _iterator.return) { | ||
95 | _iterator.return(); | ||
96 | } | ||
97 | } finally { | ||
98 | if (_didIteratorError) { | ||
99 | throw _iteratorError; | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | }; | ||
104 | |||
105 | exports.resolvePursModule = function (_ref3) { | ||
106 | var baseModulePath = _ref3.baseModulePath, | ||
107 | baseModuleName = _ref3.baseModuleName, | ||
108 | rewriteRules = _ref3.rewriteRules, | ||
109 | targetModuleName = _ref3.targetModuleName; | ||
110 | |||
111 | var rewrittenModulePath = rewrite({ rules: rewriteRules, moduleName: targetModuleName }); | ||
112 | if (rewrittenModulePath) return rewrittenModulePath; | ||
62 | var parts = diffPursModuleNames(baseModuleName.split('.'), targetModuleName.split('.'), []); | 113 | var parts = diffPursModuleNames(baseModuleName.split('.'), targetModuleName.split('.'), []); |
63 | return parts.length ? path.resolve(baseModulePath, path.join.apply(path, _toConsumableArray(parts)) + '.purs') : baseModulePath; | 114 | return parts.length ? path.resolve(baseModulePath, path.join.apply(path, _toConsumableArray(parts)) + '.purs') : baseModulePath; |
64 | }; | 115 | }; |