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