[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 makeSerializable = require("../util/makeSerializable");
|
---|
| 9 | const CssIcssExportDependency = require("./CssIcssExportDependency");
|
---|
| 10 | const CssLocalIdentifierDependency = require("./CssLocalIdentifierDependency");
|
---|
| 11 | const ModuleDependency = require("./ModuleDependency");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 15 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
| 16 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
---|
| 17 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 20 |
|
---|
| 21 | class CssIcssImportDependency extends ModuleDependency {
|
---|
| 22 | /**
|
---|
| 23 | * Example of dependency:
|
---|
| 24 | *
|
---|
| 25 | *:import('./style.css') { IMPORTED_NAME: v-primary }
|
---|
| 26 | * @param {string} request request request path which needs resolving
|
---|
| 27 | * @param {string} exportName export name
|
---|
| 28 | * @param {[number, number]} range the range of dependency
|
---|
| 29 | */
|
---|
| 30 | constructor(request, exportName, range) {
|
---|
| 31 | super(request);
|
---|
| 32 | this.range = range;
|
---|
| 33 | this.exportName = exportName;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | get type() {
|
---|
| 37 | return "css :import";
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | get category() {
|
---|
| 41 | return "css-import";
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param {ObjectSerializerContext} context context
|
---|
| 46 | */
|
---|
| 47 | serialize(context) {
|
---|
| 48 | const { write } = context;
|
---|
| 49 | write(this.range);
|
---|
| 50 | write(this.exportName);
|
---|
| 51 | super.serialize(context);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * @param {ObjectDeserializerContext} context context
|
---|
| 56 | */
|
---|
| 57 | deserialize(context) {
|
---|
| 58 | const { read } = context;
|
---|
| 59 | this.range = read();
|
---|
| 60 | this.exportName = read();
|
---|
| 61 | super.deserialize(context);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends (
|
---|
| 66 | ModuleDependency.Template
|
---|
| 67 | ) {
|
---|
| 68 | /**
|
---|
| 69 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 70 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 71 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 72 | * @returns {void}
|
---|
| 73 | */
|
---|
| 74 | apply(dependency, source, templateContext) {
|
---|
| 75 | const dep = /** @type {CssIcssImportDependency} */ (dependency);
|
---|
| 76 | const { range } = dep;
|
---|
| 77 | const module = templateContext.moduleGraph.getModule(dep);
|
---|
| 78 | let value;
|
---|
| 79 |
|
---|
| 80 | for (const item of module.dependencies) {
|
---|
| 81 | if (
|
---|
| 82 | item instanceof CssLocalIdentifierDependency &&
|
---|
| 83 | dep.exportName === item.name
|
---|
| 84 | ) {
|
---|
| 85 | value = CssLocalIdentifierDependency.Template.getIdentifier(
|
---|
| 86 | item,
|
---|
| 87 | dep.exportName,
|
---|
| 88 | {
|
---|
| 89 | ...templateContext,
|
---|
| 90 | module
|
---|
| 91 | }
|
---|
| 92 | );
|
---|
| 93 | break;
|
---|
| 94 | } else if (
|
---|
| 95 | item instanceof CssIcssExportDependency &&
|
---|
| 96 | dep.exportName === item.name
|
---|
| 97 | ) {
|
---|
| 98 | value = item.value;
|
---|
| 99 | break;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if (!value) {
|
---|
| 104 | throw new Error(
|
---|
| 105 | `Imported '${dep.exportName}' name from '${dep.request}' not found`
|
---|
| 106 | );
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | source.replace(range[0], range[1], value);
|
---|
| 110 | }
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | makeSerializable(
|
---|
| 114 | CssIcssImportDependency,
|
---|
| 115 | "webpack/lib/dependencies/CssIcssImportDependency"
|
---|
| 116 | );
|
---|
| 117 |
|
---|
| 118 | module.exports = CssIcssImportDependency;
|
---|