absolute-module-mapper-plugin
Advanced tools
Comparing version 1.0.0 to 1.2.0
@@ -47,2 +47,43 @@ /** | ||
}); | ||
it('should requestMapper', function () { | ||
const resolver = ResolverFactory.createResolver({ | ||
...resolverOptions, | ||
plugins: [new AbsoluteModuleMapperPlugin({ | ||
root: fixture(''), | ||
silent: false, | ||
requestMapper: { | ||
'^./a.js': '<root>/to/b.js' | ||
} | ||
})] | ||
}) | ||
const path = resolver.resolveSync({ | ||
issuer: fixture('module/index.js') | ||
}, fixture('module'), './a.js') | ||
expect(path).toBe(fixture('to/b.js')) | ||
}); | ||
it('should mapper & requestMapper', function () { | ||
const resolver = ResolverFactory.createResolver({ | ||
...resolverOptions, | ||
plugins: [new AbsoluteModuleMapperPlugin({ | ||
root: fixture(''), | ||
silent: false, | ||
requestMapper: { | ||
'^./a.js': '<root>/module/b.js' | ||
}, | ||
mapper: { | ||
'^<root>/module/(\\w+)': '<root>/to/$1' | ||
} | ||
})] | ||
}) | ||
const path = resolver.resolveSync({ | ||
issuer: fixture('module/index.js') | ||
}, fixture('module'), './a.js') | ||
expect(path).toBe(fixture('to/b.js')) | ||
}); | ||
}) |
102
index.js
@@ -36,2 +36,22 @@ /** | ||
function normalizeMapper(mapper, replace) { | ||
if (mapper && typeof mapper !== 'function') { | ||
const map = {} | ||
for (let [key, value] of Object.entries(mapper)) { | ||
map[replace(key)] = typeof value === 'string' ? replace(value) : value | ||
} | ||
return filename => { | ||
for (let [regStr, replacer] of Object.entries(map)) { | ||
if (new RegExp(regStr).test(filename)) { | ||
return filename.replace(new RegExp(regStr), replacer) | ||
} | ||
} | ||
return filename | ||
} | ||
} | ||
return mapper | ||
} | ||
function normalizeOptions(opts) { | ||
@@ -43,10 +63,10 @@ opts = Object.assign( | ||
silent: true, | ||
mapper: null | ||
mapper: null, | ||
requestMapper: null | ||
}, | ||
opts | ||
) | ||
const replace = value => (opts.root ? replaceRoot(value, opts.root) : value) | ||
if (opts.root) { | ||
const replace = value => replaceRoot(value, opts.root) | ||
if (!opts.include.length) { | ||
@@ -56,23 +76,7 @@ opts.include = opts.include.concat(opts.root) | ||
opts.include = opts.include.map(replace) | ||
} | ||
if (opts.mapper && typeof opts.mapper !== 'function') { | ||
const map = {} | ||
for (let [key, value] of Object.entries(opts.mapper)) { | ||
map[replace(key)] = typeof value === 'string' ? replace(value) : value | ||
} | ||
opts.mapper = map; | ||
opts.mapper = normalizeMapper(opts.mapper, replace) | ||
opts.requestMapper = normalizeMapper(opts.requestMapper, replace) | ||
const mapObj = opts.mapper | ||
opts.mapper = filename => { | ||
for (let [regStr, replacer] of Object.entries(mapObj)) { | ||
if (new RegExp(regStr).test(filename)) { | ||
return filename.replace(new RegExp(regStr), replacer) | ||
} | ||
} | ||
return filename | ||
} | ||
} | ||
} | ||
return opts | ||
@@ -87,21 +91,45 @@ } | ||
apply(resolver) { | ||
const target = resolver.ensureHook('resolved') | ||
const { mapper, include, root, silent } = this.options | ||
resolver.getHook('existingFile').tapAsync('AbsoluteModuleMapperPlugin', (request, resolveContext, callback) => { | ||
const from = request.context.issuer | ||
const { mapper, requestMapper, include, root, silent } = this.options | ||
if (from && mapper && isMatch(include, from)) { | ||
const old = request.path | ||
request.path = replaceRoot(mapper(request.path, request), root) | ||
if (requestMapper) { | ||
const requestTarget = resolver.ensureHook('parsedResolve') | ||
resolver.getHook('resolve').tapAsync('AbsoluteModuleMapperPlugin', (request, resolveContext, callback) => { | ||
const from = request.context.issuer | ||
if (from && isMatch(include, from)) { | ||
const old = request.request | ||
request.request = requestMapper(requestMapper(old, request), root) | ||
!silent && old !== request.path && console.log('AbsoluteModuleMapperPlugin: in %s\n %s => %s', from, old, request.path) | ||
} | ||
resolver.doResolve(target, request, null, resolveContext, callback) | ||
}) | ||
!silent && | ||
old !== request.request && | ||
console.log('AbsoluteModuleMapperPlugin resolveRequest: in %s\n %s => %s', from, old, request.request) | ||
} | ||
callback() | ||
// resolver.doResolve(requestTarget, request, null, resolveContext, callback) | ||
}) | ||
} | ||
if (mapper) { | ||
const target = resolver.ensureHook('resolved') | ||
resolver.getHook('existingFile').tapAsync('AbsoluteModuleMapperPlugin', (request, resolveContext, callback) => { | ||
const from = request.context.issuer | ||
if (from && isMatch(include, from)) { | ||
const old = request.path | ||
request.path = replaceRoot(mapper(old, request), root) | ||
!silent && | ||
old !== request.path && | ||
console.log('AbsoluteModuleMapperPlugin path: in %s\n %s => %s', from, old, request.path) | ||
} | ||
callback() | ||
// resolver.doResolve(target, request, null, resolveContext, callback) | ||
}) | ||
} | ||
} | ||
} | ||
AbsoluteModuleMapperPlugin.isMatch = isMatch; | ||
AbsoluteModuleMapperPlugin.replaceRoot = replaceRoot; | ||
AbsoluteModuleMapperPlugin.isMatch = normalizeOptions; | ||
AbsoluteModuleMapperPlugin.isMatch = isMatch | ||
AbsoluteModuleMapperPlugin.replaceRoot = replaceRoot | ||
AbsoluteModuleMapperPlugin.isMatch = normalizeOptions | ||
module.exports = AbsoluteModuleMapperPlugin | ||
module.exports = AbsoluteModuleMapperPlugin |
{ | ||
"name": "absolute-module-mapper-plugin", | ||
"version": "1.0.0", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -49,3 +49,3 @@ "description": "The plugin on enhanced-resolver to map module path", | ||
"commitlint": "^8.2.0", | ||
"conventional-changelog": "^3.1.12", | ||
"conventional-changelog-cli": "^2.0.27", | ||
"enhanced-resolve": "^4.1.1", | ||
@@ -52,0 +52,0 @@ "jest": "^24.9.0", |
@@ -86,2 +86,4 @@ # absolute-module-mapper-plugin | ||
absolute filename mapper. | ||
- Type: `(filename, ctx) => string | {}` | ||
@@ -95,2 +97,14 @@ - Example | ||
### `requestMapper` | ||
request mapper. | ||
- Type: `(request, ctx) => string | {}` | ||
- Example | ||
```javascript | ||
{ | ||
'^./a.js$': './b.js' | ||
} | ||
``` | ||
## Contributing | ||
@@ -97,0 +111,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13783
16
214
127