[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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 9 | const RawDataUrlModule = require("../asset/RawDataUrlModule");
|
---|
| 10 | const {
|
---|
| 11 | getDependencyUsedByExportsCondition
|
---|
| 12 | } = require("../optimize/InnerGraph");
|
---|
| 13 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 14 | const memoize = require("../util/memoize");
|
---|
| 15 | const ModuleDependency = require("./ModuleDependency");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 18 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 19 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 20 | /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
|
---|
| 21 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 22 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 23 | /** @typedef {import("../Module")} Module */
|
---|
| 24 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 25 | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
---|
| 26 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
---|
| 27 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 28 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 29 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 30 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 31 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 32 |
|
---|
| 33 | const getIgnoredRawDataUrlModule = memoize(
|
---|
| 34 | () => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)")
|
---|
| 35 | );
|
---|
| 36 |
|
---|
| 37 | class URLDependency extends ModuleDependency {
|
---|
| 38 | /**
|
---|
| 39 | * @param {string} request request
|
---|
| 40 | * @param {Range} range range of the arguments of new URL( |> ... <| )
|
---|
| 41 | * @param {Range} outerRange range of the full |> new URL(...) <|
|
---|
| 42 | * @param {boolean=} relative use relative urls instead of absolute with base uri
|
---|
| 43 | */
|
---|
| 44 | constructor(request, range, outerRange, relative) {
|
---|
| 45 | super(request);
|
---|
| 46 | this.range = range;
|
---|
| 47 | this.outerRange = outerRange;
|
---|
| 48 | this.relative = relative || false;
|
---|
| 49 | /** @type {Set<string> | boolean | undefined} */
|
---|
| 50 | this.usedByExports = undefined;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | get type() {
|
---|
| 54 | return "new URL()";
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | get category() {
|
---|
| 58 | return "url";
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 63 | * @returns {null | false | GetConditionFn} function to determine if the connection is active
|
---|
| 64 | */
|
---|
| 65 | getCondition(moduleGraph) {
|
---|
| 66 | return getDependencyUsedByExportsCondition(
|
---|
| 67 | this,
|
---|
| 68 | this.usedByExports,
|
---|
| 69 | moduleGraph
|
---|
| 70 | );
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * @param {string} context context directory
|
---|
| 75 | * @returns {Module | null} a module
|
---|
| 76 | */
|
---|
| 77 | createIgnoredModule(context) {
|
---|
| 78 | return getIgnoredRawDataUrlModule();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * @param {ObjectSerializerContext} context context
|
---|
| 83 | */
|
---|
| 84 | serialize(context) {
|
---|
| 85 | const { write } = context;
|
---|
| 86 | write(this.outerRange);
|
---|
| 87 | write(this.relative);
|
---|
| 88 | write(this.usedByExports);
|
---|
| 89 | super.serialize(context);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * @param {ObjectDeserializerContext} context context
|
---|
| 94 | */
|
---|
| 95 | deserialize(context) {
|
---|
| 96 | const { read } = context;
|
---|
| 97 | this.outerRange = read();
|
---|
| 98 | this.relative = read();
|
---|
| 99 | this.usedByExports = read();
|
---|
| 100 | super.deserialize(context);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | URLDependency.Template = class URLDependencyTemplate extends (
|
---|
| 105 | ModuleDependency.Template
|
---|
| 106 | ) {
|
---|
| 107 | /**
|
---|
| 108 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 109 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 110 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 111 | * @returns {void}
|
---|
| 112 | */
|
---|
| 113 | apply(dependency, source, templateContext) {
|
---|
| 114 | const {
|
---|
| 115 | chunkGraph,
|
---|
| 116 | moduleGraph,
|
---|
| 117 | runtimeRequirements,
|
---|
| 118 | runtimeTemplate,
|
---|
| 119 | runtime
|
---|
| 120 | } = templateContext;
|
---|
| 121 | const dep = /** @type {URLDependency} */ (dependency);
|
---|
| 122 | const connection = moduleGraph.getConnection(dep);
|
---|
| 123 | // Skip rendering depending when dependency is conditional
|
---|
| 124 | if (connection && !connection.isTargetActive(runtime)) {
|
---|
| 125 | source.replace(
|
---|
| 126 | dep.outerRange[0],
|
---|
| 127 | dep.outerRange[1] - 1,
|
---|
| 128 | "/* unused asset import */ undefined"
|
---|
| 129 | );
|
---|
| 130 | return;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | runtimeRequirements.add(RuntimeGlobals.require);
|
---|
| 134 |
|
---|
| 135 | if (dep.relative) {
|
---|
| 136 | runtimeRequirements.add(RuntimeGlobals.relativeUrl);
|
---|
| 137 | source.replace(
|
---|
| 138 | dep.outerRange[0],
|
---|
| 139 | dep.outerRange[1] - 1,
|
---|
| 140 | `/* asset import */ new ${
|
---|
| 141 | RuntimeGlobals.relativeUrl
|
---|
| 142 | }(${runtimeTemplate.moduleRaw({
|
---|
| 143 | chunkGraph,
|
---|
| 144 | module: moduleGraph.getModule(dep),
|
---|
| 145 | request: dep.request,
|
---|
| 146 | runtimeRequirements,
|
---|
| 147 | weak: false
|
---|
| 148 | })})`
|
---|
| 149 | );
|
---|
| 150 | } else {
|
---|
| 151 | runtimeRequirements.add(RuntimeGlobals.baseURI);
|
---|
| 152 |
|
---|
| 153 | source.replace(
|
---|
| 154 | dep.range[0],
|
---|
| 155 | dep.range[1] - 1,
|
---|
| 156 | `/* asset import */ ${runtimeTemplate.moduleRaw({
|
---|
| 157 | chunkGraph,
|
---|
| 158 | module: moduleGraph.getModule(dep),
|
---|
| 159 | request: dep.request,
|
---|
| 160 | runtimeRequirements,
|
---|
| 161 | weak: false
|
---|
| 162 | })}, ${RuntimeGlobals.baseURI}`
|
---|
| 163 | );
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | };
|
---|
| 167 |
|
---|
| 168 | makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency");
|
---|
| 169 |
|
---|
| 170 | module.exports = URLDependency;
|
---|