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