webpack
Version:
Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.
178 lines (158 loc) • 5.75 kB
JavaScript
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/
;
const { cssExportConvention } = require("../../css/conventions");
const WebpackError = require("../../errors/WebpackError");
const makeSerializable = require("../../util/makeSerializable");
const CssImportDependency = require("./CssImportDependency");
/** @import { ReplaceSource } from "webpack-sources" */
/** @import Dependency, { ReferencedExports, DependencyKinds } from "../../graph/Dependency" */
/**
* @import {
* CssDependencyTemplateContext as DependencyTemplateContext
* } from "../../template/DependencyTemplate"
*/
/** @import CssGenerator from "../../css/CssGenerator" */
/** @import CssModule from "../../css/CssModule" */
/** @import ModuleGraph from "../../graph/ModuleGraph" */
/** @import { RuntimeSpec } from "../../util/runtime" */
/** @import { Range } from "../../javascript/JavascriptParser" */
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, string]>} ObjectDeserializerContext */
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[string, string]>} ObjectSerializerContext */
/**
* @import {
* CssGeneratorExportsConvention
* } from "../../../declarations/WebpackOptions"
*/
class CssIcssImportDependency extends CssImportDependency {
/**
* Example of dependency:
*
* :import('./style.css') { value: name }
* @param {string} request request request path which needs resolving
* @param {Range} range the range of dependency
* @param {"local" | "global"} mode mode of the parsed CSS
* @param {string} importName import name (`name` from example)
* @param {string} localName local name (`value` from example)
*/
constructor(request, range, mode, importName, localName) {
super(request, range, mode);
/** @type {string} */
this.importName = importName;
/** @type {string} */
this.localName = localName;
/** @type {undefined | string[]} */
this._importNameConventionNames = undefined;
}
/**
* Memoized `cssExportConvention(this.importName, convention)`. The target
* module is fixed for a given dep, so its `exportsConvention` is fixed,
* so the convention-derived aliases of `importName` can be cached on the dep.
* @param {CssGeneratorExportsConvention} convention convention from the target module's generator
* @returns {string[]} convention results
*/
getImportNameConventionNames(convention) {
if (this._importNameConventionNames) {
return this._importNameConventionNames;
}
this._importNameConventionNames = cssExportConvention(
this.importName,
convention
);
return this._importNameConventionNames;
}
get type() {
return "css :import";
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {ReferencedExports} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return [
{
name: [this.importName],
canMangle: true
}
];
}
/**
* Returns warnings.
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[] | null | undefined} warnings
*/
getWarnings(moduleGraph) {
const module = /** @type {CssModule | undefined} */ (
moduleGraph.getModule(this)
);
if (!module) return null;
// The target stores exports under its own `exportsConvention`, so asking for
// the raw name misses under `camel-case-only` or any convention dropping the
// original spelling. Expand through that convention and accept any alias.
const generator =
/** @type {CssGenerator | undefined} */
(module.generator);
const convention =
generator && generator.options && generator.options.exportsConvention;
const exportsInfo = moduleGraph.getExportsInfo(module);
const names = convention
? this.getImportNameConventionNames(convention)
: [this.importName];
const isProvided = names.some((name) => exportsInfo.isExportProvided(name));
if (!isProvided) {
const error = new WebpackError(
`Referenced name "${this.importName}" in "${this.userRequest}" not found`
);
error.module = module;
return [error];
}
return null;
}
/**
* Serializes this instance into the provided serializer context.
* @param {ObjectSerializerContext} context context
*/
serialize(context) {
context.write(this.importName).write(this.localName);
super.serialize(context);
}
/**
* Restores this instance from the provided deserializer context.
* @param {ObjectDeserializerContext} context context
*/
deserialize(context) {
this.importName = context.read();
const c1 = context.rest;
this.localName = c1.read();
super.deserialize(c1.rest);
}
}
CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
CssImportDependency.Template
) {
/**
* Applies the plugin by registering its hooks on the compiler.
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(dependency, source, templateContext) {
// Nothing
}
};
makeSerializable(CssIcssImportDependency, [
"webpack/lib/dependencies/css/CssIcssImportDependency",
// TODO in the next major release: remove, they restore pre-move cache packs
"webpack/lib/dependencies/CssIcssImportDependency"
]);
/** @type {Set<keyof DependencyKinds>} */
CssIcssImportDependency.KINDS = new Set([
...CssImportDependency.KINDS,
"css-icss-import"
]);
module.exports = CssIcssImportDependency;