mirror of https://github.com/webpack/webpack.git
feat: ability to rename loader resource
This commit is contained in:
parent
eabf85d858
commit
1b9c19818c
|
@ -48,6 +48,7 @@ export interface NormalModuleLoaderContext<OptionsType> {
|
||||||
fs: InputFileSystem;
|
fs: InputFileSystem;
|
||||||
sourceMap?: boolean;
|
sourceMap?: boolean;
|
||||||
mode: "development" | "production" | "none";
|
mode: "development" | "production" | "none";
|
||||||
|
virtualResource: string | undefined;
|
||||||
webpack?: boolean;
|
webpack?: boolean;
|
||||||
_module?: NormalModule;
|
_module?: NormalModule;
|
||||||
_compilation?: Compilation;
|
_compilation?: Compilation;
|
||||||
|
|
|
@ -356,6 +356,8 @@ class NormalModule extends Module {
|
||||||
this.resourceResolveData = resourceResolveData;
|
this.resourceResolveData = resourceResolveData;
|
||||||
/** @type {string | undefined} */
|
/** @type {string | undefined} */
|
||||||
this.matchResource = matchResource;
|
this.matchResource = matchResource;
|
||||||
|
/** @type {string | undefined} */
|
||||||
|
this.virtualResource = undefined;
|
||||||
/** @type {LoaderItem[]} */
|
/** @type {LoaderItem[]} */
|
||||||
this.loaders = loaders;
|
this.loaders = loaders;
|
||||||
if (resolveOptions !== undefined) {
|
if (resolveOptions !== undefined) {
|
||||||
|
@ -431,7 +433,8 @@ class NormalModule extends Module {
|
||||||
* @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
|
* @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
|
||||||
*/
|
*/
|
||||||
nameForCondition() {
|
nameForCondition() {
|
||||||
const resource = this.matchResource || this.resource;
|
const resource =
|
||||||
|
this.virtualResource || this.matchResource || this.resource;
|
||||||
const idx = resource.indexOf("?");
|
const idx = resource.indexOf("?");
|
||||||
if (idx >= 0) return resource.slice(0, idx);
|
if (idx >= 0) return resource.slice(0, idx);
|
||||||
return resource;
|
return resource;
|
||||||
|
@ -645,11 +648,12 @@ class NormalModule extends Module {
|
||||||
(compilation.outputOptions.hashFunction)
|
(compilation.outputOptions.hashFunction)
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
const module = this;
|
||||||
/** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
|
/** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
|
||||||
const loaderContext = {
|
const loaderContext = {
|
||||||
version: 2,
|
version: 2,
|
||||||
getOptions: schema => {
|
getOptions: schema => {
|
||||||
const loader = this.getCurrentLoader(loaderContext);
|
const loader = module.getCurrentLoader(loaderContext);
|
||||||
|
|
||||||
let { options } = /** @type {LoaderItem} */ (loader);
|
let { options } = /** @type {LoaderItem} */ (loader);
|
||||||
|
|
||||||
|
@ -692,7 +696,7 @@ class NormalModule extends Module {
|
||||||
if (!(warning instanceof Error)) {
|
if (!(warning instanceof Error)) {
|
||||||
warning = new NonErrorEmittedError(warning);
|
warning = new NonErrorEmittedError(warning);
|
||||||
}
|
}
|
||||||
this.addWarning(
|
module.addWarning(
|
||||||
new ModuleWarning(warning, {
|
new ModuleWarning(warning, {
|
||||||
from: getCurrentLoaderName()
|
from: getCurrentLoaderName()
|
||||||
})
|
})
|
||||||
|
@ -702,16 +706,16 @@ class NormalModule extends Module {
|
||||||
if (!(error instanceof Error)) {
|
if (!(error instanceof Error)) {
|
||||||
error = new NonErrorEmittedError(error);
|
error = new NonErrorEmittedError(error);
|
||||||
}
|
}
|
||||||
this.addError(
|
module.addError(
|
||||||
new ModuleError(error, {
|
new ModuleError(error, {
|
||||||
from: getCurrentLoaderName()
|
from: getCurrentLoaderName()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
getLogger: name => {
|
getLogger: name => {
|
||||||
const currentLoader = this.getCurrentLoader(loaderContext);
|
const currentLoader = module.getCurrentLoader(loaderContext);
|
||||||
return compilation.getLogger(() =>
|
return compilation.getLogger(() =>
|
||||||
[currentLoader && currentLoader.loader, name, this.identifier()]
|
[currentLoader && currentLoader.loader, name, module.identifier()]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join("|")
|
.join("|")
|
||||||
);
|
);
|
||||||
|
@ -741,7 +745,7 @@ class NormalModule extends Module {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
emitFile: (name, content, sourceMap, assetInfo) => {
|
emitFile: (name, content, sourceMap, assetInfo) => {
|
||||||
const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
|
const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
|
||||||
|
|
||||||
if (!buildInfo.assets) {
|
if (!buildInfo.assets) {
|
||||||
buildInfo.assets = Object.create(null);
|
buildInfo.assets = Object.create(null);
|
||||||
|
@ -755,7 +759,7 @@ class NormalModule extends Module {
|
||||||
/** @type {NonNullable<KnownBuildInfo["assetsInfo"]>} */
|
/** @type {NonNullable<KnownBuildInfo["assetsInfo"]>} */
|
||||||
(buildInfo.assetsInfo);
|
(buildInfo.assetsInfo);
|
||||||
|
|
||||||
assets[name] = this.createSourceForAsset(
|
assets[name] = module.createSourceForAsset(
|
||||||
/** @type {string} */ (options.context),
|
/** @type {string} */ (options.context),
|
||||||
name,
|
name,
|
||||||
content,
|
content,
|
||||||
|
@ -765,7 +769,7 @@ class NormalModule extends Module {
|
||||||
assetsInfo.set(name, assetInfo);
|
assetsInfo.set(name, assetInfo);
|
||||||
},
|
},
|
||||||
addBuildDependency: dep => {
|
addBuildDependency: dep => {
|
||||||
const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
|
const buildInfo = /** @type {BuildInfo} */ (module.buildInfo);
|
||||||
|
|
||||||
if (buildInfo.buildDependencies === undefined) {
|
if (buildInfo.buildDependencies === undefined) {
|
||||||
buildInfo.buildDependencies = new LazySet();
|
buildInfo.buildDependencies = new LazySet();
|
||||||
|
@ -775,9 +779,15 @@ class NormalModule extends Module {
|
||||||
utils,
|
utils,
|
||||||
rootContext: /** @type {string} */ (options.context),
|
rootContext: /** @type {string} */ (options.context),
|
||||||
webpack: true,
|
webpack: true,
|
||||||
sourceMap: Boolean(this.useSourceMap),
|
sourceMap: Boolean(module.useSourceMap),
|
||||||
mode: options.mode || "production",
|
mode: options.mode || "production",
|
||||||
_module: this,
|
get virtualResource() {
|
||||||
|
return module.virtualResource;
|
||||||
|
},
|
||||||
|
set virtualResource(v) {
|
||||||
|
module.virtualResource = v;
|
||||||
|
},
|
||||||
|
_module: module,
|
||||||
_compilation: compilation,
|
_compilation: compilation,
|
||||||
_compiler: compilation.compiler,
|
_compiler: compilation.compiler,
|
||||||
fs
|
fs
|
||||||
|
@ -1590,6 +1600,7 @@ class NormalModule extends Module {
|
||||||
const { write } = context;
|
const { write } = context;
|
||||||
// deserialize
|
// deserialize
|
||||||
write(this._source);
|
write(this._source);
|
||||||
|
write(this.virtualResource);
|
||||||
write(this.error);
|
write(this.error);
|
||||||
write(this._lastSuccessfulBuildMeta);
|
write(this._lastSuccessfulBuildMeta);
|
||||||
write(this._forceBuild);
|
write(this._forceBuild);
|
||||||
|
@ -1626,6 +1637,7 @@ class NormalModule extends Module {
|
||||||
deserialize(context) {
|
deserialize(context) {
|
||||||
const { read } = context;
|
const { read } = context;
|
||||||
this._source = read();
|
this._source = read();
|
||||||
|
this.virtualResource = read();
|
||||||
this.error = read();
|
this.error = read();
|
||||||
this._lastSuccessfulBuildMeta = read();
|
this._lastSuccessfulBuildMeta = read();
|
||||||
this._forceBuild = read();
|
this._forceBuild = read();
|
||||||
|
|
|
@ -193,7 +193,7 @@ class AssetGenerator extends Generator {
|
||||||
getSourceFileName(module, runtimeTemplate) {
|
getSourceFileName(module, runtimeTemplate) {
|
||||||
return makePathsRelative(
|
return makePathsRelative(
|
||||||
runtimeTemplate.compilation.compiler.context,
|
runtimeTemplate.compilation.compiler.context,
|
||||||
module.matchResource || module.resource,
|
module.virtualResource || module.matchResource || module.resource,
|
||||||
runtimeTemplate.compilation.compiler.root
|
runtimeTemplate.compilation.compiler.root
|
||||||
).replace(/^\.\//, "");
|
).replace(/^\.\//, "");
|
||||||
}
|
}
|
||||||
|
@ -292,7 +292,10 @@ class AssetGenerator extends Generator {
|
||||||
null,
|
null,
|
||||||
originalSource.source(),
|
originalSource.source(),
|
||||||
{
|
{
|
||||||
filename: module.matchResource || module.resource,
|
filename:
|
||||||
|
module.virtualResource ||
|
||||||
|
module.matchResource ||
|
||||||
|
module.resource,
|
||||||
module
|
module
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -41,7 +41,10 @@ class AssetParser extends Parser {
|
||||||
|
|
||||||
if (typeof this.dataUrlCondition === "function") {
|
if (typeof this.dataUrlCondition === "function") {
|
||||||
buildInfo.dataUrl = this.dataUrlCondition(source, {
|
buildInfo.dataUrl = this.dataUrlCondition(source, {
|
||||||
filename: state.module.matchResource || state.module.resource,
|
filename:
|
||||||
|
state.module.virtualResource ||
|
||||||
|
state.module.matchResource ||
|
||||||
|
state.module.resource,
|
||||||
module: state.module
|
module: state.module
|
||||||
});
|
});
|
||||||
} else if (typeof this.dataUrlCondition === "boolean") {
|
} else if (typeof this.dataUrlCondition === "boolean") {
|
||||||
|
|
|
@ -183,7 +183,9 @@ class CssParser extends Parser {
|
||||||
if (
|
if (
|
||||||
module.type === CSS_MODULE_TYPE_AUTO &&
|
module.type === CSS_MODULE_TYPE_AUTO &&
|
||||||
IS_MODULES.test(
|
IS_MODULES.test(
|
||||||
parseResource(module.matchResource || module.resource).path
|
parseResource(
|
||||||
|
module.virtualResource || module.matchResource || module.resource
|
||||||
|
).path
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
oldDefaultMode = this.defaultMode;
|
oldDefaultMode = this.defaultMode;
|
||||||
|
|
|
@ -9134,6 +9134,7 @@ declare class NormalModule extends Module {
|
||||||
resource: string;
|
resource: string;
|
||||||
resourceResolveData?: Record<string, any>;
|
resourceResolveData?: Record<string, any>;
|
||||||
matchResource?: string;
|
matchResource?: string;
|
||||||
|
virtualResource?: string;
|
||||||
loaders: LoaderItem[];
|
loaders: LoaderItem[];
|
||||||
error: null | WebpackError;
|
error: null | WebpackError;
|
||||||
|
|
||||||
|
@ -9379,6 +9380,7 @@ declare interface NormalModuleLoaderContext<OptionsType> {
|
||||||
fs: InputFileSystem;
|
fs: InputFileSystem;
|
||||||
sourceMap?: boolean;
|
sourceMap?: boolean;
|
||||||
mode: "none" | "development" | "production";
|
mode: "none" | "development" | "production";
|
||||||
|
virtualResource?: string;
|
||||||
webpack?: boolean;
|
webpack?: boolean;
|
||||||
_module?: NormalModule;
|
_module?: NormalModule;
|
||||||
_compilation?: Compilation;
|
_compilation?: Compilation;
|
||||||
|
|
Loading…
Reference in New Issue