| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Ivan Kopeykin @vankop
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const WebpackError = require("../errors/WebpackError");
|
|---|
| 9 | const { cssExportConvention } = require("../util/conventions");
|
|---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 11 | const CssImportDependency = require("./CssImportDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 15 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 16 | /** @typedef {import("../Module")} Module */
|
|---|
| 17 | /** @typedef {import("../css/CssGenerator")} CssGenerator */
|
|---|
| 18 | /** @typedef {import("../css/CssModule")} CssModule */
|
|---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 20 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 21 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 24 | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 25 | /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
|
|---|
| 26 | /** @typedef {import("./CssIcssExportDependency").ExportMode} ExportMode */
|
|---|
| 27 | /** @typedef {import("./CssIcssExportDependency").ExportType} ExportType */
|
|---|
| 28 |
|
|---|
| 29 | class CssIcssImportDependency extends CssImportDependency {
|
|---|
| 30 | /**
|
|---|
| 31 | * Example of dependency:
|
|---|
| 32 | *
|
|---|
| 33 | * :import('./style.css') { value: name }
|
|---|
| 34 | * @param {string} request request request path which needs resolving
|
|---|
| 35 | * @param {Range} range the range of dependency
|
|---|
| 36 | * @param {"local" | "global"} mode mode of the parsed CSS
|
|---|
| 37 | * @param {string} importName import name (`name` from example)
|
|---|
| 38 | * @param {string} localName local name (`value` from example)
|
|---|
| 39 | */
|
|---|
| 40 | constructor(request, range, mode, importName, localName) {
|
|---|
| 41 | super(request, range, mode);
|
|---|
| 42 | this.importName = importName;
|
|---|
| 43 | this.localName = localName;
|
|---|
| 44 | /** @type {undefined | string[]} */
|
|---|
| 45 | this._importNameConventionNames = undefined;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Memoized `cssExportConvention(this.importName, convention)`. The target
|
|---|
| 50 | * module is fixed for a given dep, so its `exportsConvention` is fixed,
|
|---|
| 51 | * so the convention-derived aliases of `importName` can be cached on the dep.
|
|---|
| 52 | * @param {CssGeneratorExportsConvention} convention convention from the target module's generator
|
|---|
| 53 | * @returns {string[]} convention results
|
|---|
| 54 | */
|
|---|
| 55 | getImportNameConventionNames(convention) {
|
|---|
| 56 | if (this._importNameConventionNames) {
|
|---|
| 57 | return this._importNameConventionNames;
|
|---|
| 58 | }
|
|---|
| 59 | this._importNameConventionNames = cssExportConvention(
|
|---|
| 60 | this.importName,
|
|---|
| 61 | convention
|
|---|
| 62 | );
|
|---|
| 63 | return this._importNameConventionNames;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | get type() {
|
|---|
| 67 | return "css :import";
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Returns list of exports referenced by this dependency
|
|---|
| 72 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 73 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 74 | * @returns {ReferencedExports} referenced exports
|
|---|
| 75 | */
|
|---|
| 76 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 77 | return [
|
|---|
| 78 | {
|
|---|
| 79 | name: [this.importName],
|
|---|
| 80 | canMangle: true
|
|---|
| 81 | }
|
|---|
| 82 | ];
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Returns warnings.
|
|---|
| 87 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 88 | * @returns {WebpackError[] | null | undefined} warnings
|
|---|
| 89 | */
|
|---|
| 90 | getWarnings(moduleGraph) {
|
|---|
| 91 | const module = /** @type {CssModule | undefined} */ (
|
|---|
| 92 | moduleGraph.getModule(this)
|
|---|
| 93 | );
|
|---|
| 94 |
|
|---|
| 95 | if (!module) return null;
|
|---|
| 96 |
|
|---|
| 97 | // The target module stores exports under the names produced by its
|
|---|
| 98 | // `exportsConvention`, so a raw `isExportProvided(this.importName)`
|
|---|
| 99 | // would false-positive against `camel-case-only` / `dashes-only` (and
|
|---|
| 100 | // any custom function that drops the original spelling). Expand
|
|---|
| 101 | // `importName` through the *target* module's convention and accept
|
|---|
| 102 | // if any alias is provided.
|
|---|
| 103 | const generator =
|
|---|
| 104 | /** @type {CssGenerator | undefined} */
|
|---|
| 105 | (module.generator);
|
|---|
| 106 | const convention =
|
|---|
| 107 | generator &&
|
|---|
| 108 | /** @type {CssGeneratorExportsConvention | undefined} */
|
|---|
| 109 | (generator.options && generator.options.exportsConvention);
|
|---|
| 110 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
|---|
| 111 | const names = convention
|
|---|
| 112 | ? this.getImportNameConventionNames(convention)
|
|---|
| 113 | : [this.importName];
|
|---|
| 114 | const isProvided = names.some((name) => exportsInfo.isExportProvided(name));
|
|---|
| 115 |
|
|---|
| 116 | if (!isProvided) {
|
|---|
| 117 | const error = new WebpackError(
|
|---|
| 118 | `Referenced name "${this.importName}" in "${this.userRequest}" not found`
|
|---|
| 119 | );
|
|---|
| 120 | error.module = module;
|
|---|
| 121 |
|
|---|
| 122 | return [error];
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return null;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Serializes this instance into the provided serializer context.
|
|---|
| 130 | * @param {ObjectSerializerContext} context context
|
|---|
| 131 | */
|
|---|
| 132 | serialize(context) {
|
|---|
| 133 | const { write } = context;
|
|---|
| 134 | write(this.importName);
|
|---|
| 135 | write(this.localName);
|
|---|
| 136 | super.serialize(context);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Restores this instance from the provided deserializer context.
|
|---|
| 141 | * @param {ObjectDeserializerContext} context context
|
|---|
| 142 | */
|
|---|
| 143 | deserialize(context) {
|
|---|
| 144 | const { read } = context;
|
|---|
| 145 | this.importName = read();
|
|---|
| 146 | this.localName = read();
|
|---|
| 147 | super.deserialize(context);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
|
|---|
| 152 | CssImportDependency.Template
|
|---|
| 153 | ) {
|
|---|
| 154 | /**
|
|---|
| 155 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 156 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 157 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 158 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 159 | * @returns {void}
|
|---|
| 160 | */
|
|---|
| 161 | apply(dependency, source, templateContext) {
|
|---|
| 162 | // Nothing
|
|---|
| 163 | }
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | makeSerializable(
|
|---|
| 167 | CssIcssImportDependency,
|
|---|
| 168 | "webpack/lib/dependencies/CssIcssImportDependency"
|
|---|
| 169 | );
|
|---|
| 170 |
|
|---|
| 171 | module.exports = CssIcssImportDependency;
|
|---|