[79a0317] | 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 { cssExportConvention } = require("../util/conventions");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 | const NullDependency = require("./NullDependency");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 13 | /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
|
---|
| 14 | /** @typedef {import("../CssModule")} CssModule */
|
---|
| 15 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 16 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
| 17 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 18 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 20 | /** @typedef {import("../css/CssGenerator")} CssGenerator */
|
---|
| 21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 23 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 24 |
|
---|
| 25 | class CssIcssExportDependency extends NullDependency {
|
---|
| 26 | /**
|
---|
| 27 | * @param {string} name name
|
---|
| 28 | * @param {string} value value
|
---|
| 29 | */
|
---|
| 30 | constructor(name, value) {
|
---|
| 31 | super();
|
---|
| 32 | this.name = name;
|
---|
| 33 | this.value = value;
|
---|
| 34 | this._hashUpdate = undefined;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | get type() {
|
---|
| 38 | return "css :export";
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param {string} name export name
|
---|
| 43 | * @param {CssGeneratorExportsConvention} convention convention of the export name
|
---|
| 44 | * @returns {string[]} convention results
|
---|
| 45 | */
|
---|
| 46 | getExportsConventionNames(name, convention) {
|
---|
| 47 | if (this._conventionNames) {
|
---|
| 48 | return this._conventionNames;
|
---|
| 49 | }
|
---|
| 50 | this._conventionNames = cssExportConvention(name, convention);
|
---|
| 51 | return this._conventionNames;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * Returns the exported names
|
---|
| 56 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 57 | * @returns {ExportsSpec | undefined} export names
|
---|
| 58 | */
|
---|
| 59 | getExports(moduleGraph) {
|
---|
| 60 | const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
|
---|
| 61 | const convention =
|
---|
| 62 | /** @type {CssGenerator} */
|
---|
| 63 | (module.generator).convention;
|
---|
| 64 | const names = this.getExportsConventionNames(this.name, convention);
|
---|
| 65 | return {
|
---|
| 66 | exports: names.map(name => ({
|
---|
| 67 | name,
|
---|
| 68 | canMangle: true
|
---|
| 69 | })),
|
---|
| 70 | dependencies: undefined
|
---|
| 71 | };
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Update the hash
|
---|
| 76 | * @param {Hash} hash hash to be updated
|
---|
| 77 | * @param {UpdateHashContext} context context
|
---|
| 78 | * @returns {void}
|
---|
| 79 | */
|
---|
| 80 | updateHash(hash, { chunkGraph }) {
|
---|
| 81 | if (this._hashUpdate === undefined) {
|
---|
| 82 | const module =
|
---|
| 83 | /** @type {CssModule} */
|
---|
| 84 | (chunkGraph.moduleGraph.getParentModule(this));
|
---|
| 85 | const generator =
|
---|
| 86 | /** @type {CssGenerator} */
|
---|
| 87 | (module.generator);
|
---|
| 88 | const names = this.getExportsConventionNames(
|
---|
| 89 | this.name,
|
---|
| 90 | generator.convention
|
---|
| 91 | );
|
---|
| 92 | this._hashUpdate = JSON.stringify(names);
|
---|
| 93 | }
|
---|
| 94 | hash.update("exportsConvention");
|
---|
| 95 | hash.update(this._hashUpdate);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * @param {ObjectSerializerContext} context context
|
---|
| 100 | */
|
---|
| 101 | serialize(context) {
|
---|
| 102 | const { write } = context;
|
---|
| 103 | write(this.name);
|
---|
| 104 | write(this.value);
|
---|
| 105 | super.serialize(context);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * @param {ObjectDeserializerContext} context context
|
---|
| 110 | */
|
---|
| 111 | deserialize(context) {
|
---|
| 112 | const { read } = context;
|
---|
| 113 | this.name = read();
|
---|
| 114 | this.value = read();
|
---|
| 115 | super.deserialize(context);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
|
---|
| 120 | NullDependency.Template
|
---|
| 121 | ) {
|
---|
| 122 | /**
|
---|
| 123 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 124 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 125 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 126 | * @returns {void}
|
---|
| 127 | */
|
---|
| 128 | apply(dependency, source, { cssData, module: m, runtime, moduleGraph }) {
|
---|
| 129 | const dep = /** @type {CssIcssExportDependency} */ (dependency);
|
---|
| 130 | const module = /** @type {CssModule} */ (m);
|
---|
| 131 | const convention =
|
---|
| 132 | /** @type {CssGenerator} */
|
---|
| 133 | (module.generator).convention;
|
---|
| 134 | const names = dep.getExportsConventionNames(dep.name, convention);
|
---|
| 135 | const usedNames =
|
---|
| 136 | /** @type {string[]} */
|
---|
| 137 | (
|
---|
| 138 | names
|
---|
| 139 | .map(name =>
|
---|
| 140 | moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
|
---|
| 141 | )
|
---|
| 142 | .filter(Boolean)
|
---|
| 143 | );
|
---|
| 144 |
|
---|
| 145 | for (const used of usedNames.concat(names)) {
|
---|
| 146 | cssData.exports.set(used, dep.value);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | makeSerializable(
|
---|
| 152 | CssIcssExportDependency,
|
---|
| 153 | "webpack/lib/dependencies/CssIcssExportDependency"
|
---|
| 154 | );
|
---|
| 155 |
|
---|
| 156 | module.exports = CssIcssExportDependency;
|
---|