[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const Template = require("../Template");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 | const HarmonyImportDependency = require("./HarmonyImportDependency");
|
---|
| 11 | const NullDependency = require("./NullDependency");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 16 | /** @typedef {import("./HarmonyAcceptImportDependency")} HarmonyAcceptImportDependency */
|
---|
| 17 |
|
---|
| 18 | class HarmonyAcceptDependency extends NullDependency {
|
---|
| 19 | /**
|
---|
| 20 | * @param {[number, number]} range expression range
|
---|
| 21 | * @param {HarmonyAcceptImportDependency[]} dependencies import dependencies
|
---|
| 22 | * @param {boolean} hasCallback true, if the range wraps an existing callback
|
---|
| 23 | */
|
---|
| 24 | constructor(range, dependencies, hasCallback) {
|
---|
| 25 | super();
|
---|
| 26 | this.range = range;
|
---|
| 27 | this.dependencies = dependencies;
|
---|
| 28 | this.hasCallback = hasCallback;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | get type() {
|
---|
| 32 | return "accepted harmony modules";
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | serialize(context) {
|
---|
| 36 | const { write } = context;
|
---|
| 37 | write(this.range);
|
---|
| 38 | write(this.dependencies);
|
---|
| 39 | write(this.hasCallback);
|
---|
| 40 | super.serialize(context);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | deserialize(context) {
|
---|
| 44 | const { read } = context;
|
---|
| 45 | this.range = read();
|
---|
| 46 | this.dependencies = read();
|
---|
| 47 | this.hasCallback = read();
|
---|
| 48 | super.deserialize(context);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | makeSerializable(
|
---|
| 53 | HarmonyAcceptDependency,
|
---|
| 54 | "webpack/lib/dependencies/HarmonyAcceptDependency"
|
---|
| 55 | );
|
---|
| 56 |
|
---|
| 57 | HarmonyAcceptDependency.Template = class HarmonyAcceptDependencyTemplate extends (
|
---|
| 58 | NullDependency.Template
|
---|
| 59 | ) {
|
---|
| 60 | /**
|
---|
| 61 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 62 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 63 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 64 | * @returns {void}
|
---|
| 65 | */
|
---|
| 66 | apply(dependency, source, templateContext) {
|
---|
| 67 | const dep = /** @type {HarmonyAcceptDependency} */ (dependency);
|
---|
| 68 | const {
|
---|
| 69 | module,
|
---|
| 70 | runtime,
|
---|
| 71 | runtimeRequirements,
|
---|
| 72 | runtimeTemplate,
|
---|
| 73 | moduleGraph,
|
---|
| 74 | chunkGraph
|
---|
| 75 | } = templateContext;
|
---|
| 76 | const content = dep.dependencies
|
---|
| 77 | .map(dependency => {
|
---|
| 78 | const referencedModule = moduleGraph.getModule(dependency);
|
---|
| 79 | return {
|
---|
| 80 | dependency,
|
---|
| 81 | runtimeCondition: referencedModule
|
---|
| 82 | ? HarmonyImportDependency.Template.getImportEmittedRuntime(
|
---|
| 83 | module,
|
---|
| 84 | referencedModule
|
---|
| 85 | )
|
---|
| 86 | : false
|
---|
| 87 | };
|
---|
| 88 | })
|
---|
| 89 | .filter(({ runtimeCondition }) => runtimeCondition !== false)
|
---|
| 90 | .map(({ dependency, runtimeCondition }) => {
|
---|
| 91 | const condition = runtimeTemplate.runtimeConditionExpression({
|
---|
| 92 | chunkGraph,
|
---|
| 93 | runtime,
|
---|
| 94 | runtimeCondition,
|
---|
| 95 | runtimeRequirements
|
---|
| 96 | });
|
---|
| 97 | const s = dependency.getImportStatement(true, templateContext);
|
---|
| 98 | const code = s[0] + s[1];
|
---|
| 99 | if (condition !== "true") {
|
---|
| 100 | return `if (${condition}) {\n${Template.indent(code)}\n}\n`;
|
---|
| 101 | }
|
---|
| 102 | return code;
|
---|
| 103 | })
|
---|
| 104 | .join("");
|
---|
| 105 |
|
---|
| 106 | if (dep.hasCallback) {
|
---|
| 107 | if (runtimeTemplate.supportsArrowFunction()) {
|
---|
| 108 | source.insert(
|
---|
| 109 | dep.range[0],
|
---|
| 110 | `__WEBPACK_OUTDATED_DEPENDENCIES__ => { ${content}(`
|
---|
| 111 | );
|
---|
| 112 | source.insert(dep.range[1], ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }");
|
---|
| 113 | } else {
|
---|
| 114 | source.insert(
|
---|
| 115 | dep.range[0],
|
---|
| 116 | `function(__WEBPACK_OUTDATED_DEPENDENCIES__) { ${content}(`
|
---|
| 117 | );
|
---|
| 118 | source.insert(
|
---|
| 119 | dep.range[1],
|
---|
| 120 | ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)"
|
---|
| 121 | );
|
---|
| 122 | }
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | const arrow = runtimeTemplate.supportsArrowFunction();
|
---|
| 127 | source.insert(
|
---|
| 128 | dep.range[1] - 0.5,
|
---|
| 129 | `, ${arrow ? "() =>" : "function()"} { ${content} }`
|
---|
| 130 | );
|
---|
| 131 | }
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | module.exports = HarmonyAcceptDependency;
|
---|