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