| 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 ModuleDependency = require("./ModuleDependency");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 12 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 13 | /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 14 | /** @typedef {import("../css/CssParser").Range} Range */
|
|---|
| 15 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 16 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 17 |
|
|---|
| 18 | class CssImportDependency extends ModuleDependency {
|
|---|
| 19 | /**
|
|---|
| 20 | * Example of dependency:
|
|---|
| 21 | * \@import url("landscape.css") layer(forms) screen and (orientation: landscape) screen and (orientation: landscape);
|
|---|
| 22 | * @param {string} request request
|
|---|
| 23 | * @param {Range} range range of the argument
|
|---|
| 24 | * @param {"local" | "global"=} mode mode of the parsed CSS
|
|---|
| 25 | * @param {string=} layer layer
|
|---|
| 26 | * @param {string=} supports list of supports conditions
|
|---|
| 27 | * @param {string=} media list of media conditions
|
|---|
| 28 | */
|
|---|
| 29 | constructor(request, range, mode, layer, supports, media) {
|
|---|
| 30 | super(request);
|
|---|
| 31 | this.range = range;
|
|---|
| 32 | this.mode = mode;
|
|---|
| 33 | this.layer = layer;
|
|---|
| 34 | this.supports = supports;
|
|---|
| 35 | this.media = media;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | get type() {
|
|---|
| 39 | return "css @import";
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | get category() {
|
|---|
| 43 | return `css-import${this.mode ? `-${this.mode}-module` : ""}`;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Returns true if this dependency can be concatenated
|
|---|
| 48 | * @returns {boolean} true if this dependency can be concatenated
|
|---|
| 49 | */
|
|---|
| 50 | canConcatenate() {
|
|---|
| 51 | return true;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Returns an identifier to merge equal requests.
|
|---|
| 56 | * @returns {string | null} an identifier to merge equal requests
|
|---|
| 57 | */
|
|---|
| 58 | getResourceIdentifier() {
|
|---|
| 59 | let str = `context${this._context || ""}|module${this.request}`;
|
|---|
| 60 |
|
|---|
| 61 | if (this.mode) {
|
|---|
| 62 | str += `|mode${this.mode}`;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (this.layer) {
|
|---|
| 66 | str += `|layer${this.layer}`;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | if (this.supports) {
|
|---|
| 70 | str += `|supports${this.supports}`;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (this.media) {
|
|---|
| 74 | str += `|media${this.media}`;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return str;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Serializes this instance into the provided serializer context.
|
|---|
| 82 | * @param {ObjectSerializerContext} context context
|
|---|
| 83 | */
|
|---|
| 84 | serialize(context) {
|
|---|
| 85 | const { write } = context;
|
|---|
| 86 | write(this.range);
|
|---|
| 87 | write(this.mode);
|
|---|
| 88 | write(this.layer);
|
|---|
| 89 | write(this.supports);
|
|---|
| 90 | write(this.media);
|
|---|
| 91 | super.serialize(context);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Restores this instance from the provided deserializer context.
|
|---|
| 96 | * @param {ObjectDeserializerContext} context context
|
|---|
| 97 | */
|
|---|
| 98 | deserialize(context) {
|
|---|
| 99 | const { read } = context;
|
|---|
| 100 | this.range = read();
|
|---|
| 101 | this.mode = read();
|
|---|
| 102 | this.layer = read();
|
|---|
| 103 | this.supports = read();
|
|---|
| 104 | this.media = read();
|
|---|
| 105 | super.deserialize(context);
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | CssImportDependency.Template = class CssImportDependencyTemplate extends (
|
|---|
| 110 | ModuleDependency.Template
|
|---|
| 111 | ) {
|
|---|
| 112 | /**
|
|---|
| 113 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 114 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 115 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 116 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 117 | * @returns {void}
|
|---|
| 118 | */
|
|---|
| 119 | apply(dependency, source, templateContext) {
|
|---|
| 120 | if (templateContext.type === "javascript") return;
|
|---|
| 121 | const dep = /** @type {CssImportDependency} */ (dependency);
|
|---|
| 122 |
|
|---|
| 123 | source.replace(dep.range[0], dep.range[1] - 1, "");
|
|---|
| 124 | }
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | makeSerializable(
|
|---|
| 128 | CssImportDependency,
|
|---|
| 129 | "webpack/lib/dependencies/CssImportDependency"
|
|---|
| 130 | );
|
|---|
| 131 |
|
|---|
| 132 | module.exports = CssImportDependency;
|
|---|