-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new option extensionAlias which maps extension to extension alias
- Loading branch information
Showing
10 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Ivan Kopeykin @vankop | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const forEachBail = require("./forEachBail"); | ||
|
||
/** @typedef {import("./Resolver")} Resolver */ | ||
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */ | ||
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */ | ||
/** @typedef {{ alias: string|string[], extension: string }} ExtensionAliasOption */ | ||
|
||
module.exports = class ExtensionAliasPlugin { | ||
/** | ||
* @param {string | ResolveStepHook} source source | ||
* @param {ExtensionAliasOption} options options | ||
* @param {string | ResolveStepHook} target target | ||
*/ | ||
constructor(source, options, target) { | ||
this.source = source; | ||
this.options = options; | ||
this.target = target; | ||
} | ||
|
||
/** | ||
* @param {Resolver} resolver the resolver | ||
* @returns {void} | ||
*/ | ||
apply(resolver) { | ||
const target = resolver.ensureHook(this.target); | ||
const { extension, alias: aliasArray } = this.options; | ||
resolver | ||
.getHook(this.source) | ||
.tapAsync("ExtensionAliasPlugin", (request, resolveContext, callback) => { | ||
const requestPath = request.path; | ||
if (!requestPath || !requestPath.endsWith(extension)) return callback(); | ||
const resolve = (alias, callback) => { | ||
resolver.doResolve( | ||
target, | ||
{ | ||
...request, | ||
path: `${requestPath.slice(0, -extension.length)}${alias}`, | ||
relativePath: request.relativePath | ||
? `${request.relativePath.slice(0, -extension.length)}${alias}` | ||
: request.relativePath | ||
}, | ||
`aliased from extension alias with mapping '${extension}' to '${alias}'`, | ||
resolveContext, | ||
callback | ||
); | ||
}; | ||
|
||
if (aliasArray.length > 1) { | ||
forEachBail(aliasArray, resolve, callback); | ||
} else { | ||
resolve(aliasArray[0], callback); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const should = require("should"); | ||
|
||
const CachedInputFileSystem = require("../lib/CachedInputFileSystem"); | ||
const ResolverFactory = require("../lib/ResolverFactory"); | ||
|
||
/** @typedef {import("../lib/util/entrypoints").ImportsField} ImportsField */ | ||
|
||
describe("extension-alias", () => { | ||
const fixture = path.resolve(__dirname, "fixtures", "extension-alias"); | ||
const nodeFileSystem = new CachedInputFileSystem(fs, 4000); | ||
|
||
const resolver = ResolverFactory.createResolver({ | ||
extensions: [".js"], | ||
fileSystem: nodeFileSystem, | ||
mainFiles: ["index.js"], | ||
extensionAlias: { | ||
".js": [".ts", ".js"] | ||
} | ||
}); | ||
|
||
it("should alias fully specified file", done => { | ||
resolver.resolve({}, fixture, "./index.js", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "index.ts")); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should alias specified extension", done => { | ||
resolver.resolve({}, fixture, "./dir", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "dir", "index.ts")); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should result successfully without aliasing #1", done => { | ||
resolver.resolve({}, fixture, "./dir2", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "dir2", "index.js")); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should result successfully without aliasing #2", done => { | ||
resolver.resolve({}, fixture, "./dir2/index.js", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "dir2", "index.js")); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe("should result successfully without alias array", () => { | ||
const resolver = ResolverFactory.createResolver({ | ||
extensions: [".js"], | ||
fileSystem: nodeFileSystem, | ||
mainFiles: ["index.js"], | ||
extensionAlias: { | ||
".js": [] | ||
} | ||
}); | ||
|
||
it("directory", done => { | ||
resolver.resolve({}, fixture, "./dir2", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "dir2", "index.js")); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("file", done => { | ||
resolver.resolve({}, fixture, "./dir2/index.js", {}, (err, result) => { | ||
if (err) return done(err); | ||
should(result).be.eql(path.resolve(fixture, "dir2", "index.js")); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters