[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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("../Module")} Module */
|
---|
| 11 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
---|
| 12 | /** @typedef {"exports" | "module.exports" | "this" | "Object.defineProperty(exports)" | "Object.defineProperty(module.exports)" | "Object.defineProperty(this)"} CommonJSDependencyBaseKeywords */
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * @param {CommonJSDependencyBaseKeywords} depBase commonjs dependency base
|
---|
| 16 | * @param {Module} module module
|
---|
| 17 | * @param {RuntimeRequirements} runtimeRequirements runtime requirements
|
---|
| 18 | * @returns {[string, string]} type and base
|
---|
| 19 | */
|
---|
| 20 | module.exports.handleDependencyBase = (
|
---|
| 21 | depBase,
|
---|
| 22 | module,
|
---|
| 23 | runtimeRequirements
|
---|
| 24 | ) => {
|
---|
| 25 | let base;
|
---|
| 26 | let type;
|
---|
| 27 | switch (depBase) {
|
---|
| 28 | case "exports":
|
---|
| 29 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
| 30 | base = module.exportsArgument;
|
---|
| 31 | type = "expression";
|
---|
| 32 | break;
|
---|
| 33 | case "module.exports":
|
---|
| 34 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
| 35 | base = `${module.moduleArgument}.exports`;
|
---|
| 36 | type = "expression";
|
---|
| 37 | break;
|
---|
| 38 | case "this":
|
---|
| 39 | runtimeRequirements.add(RuntimeGlobals.thisAsExports);
|
---|
| 40 | base = "this";
|
---|
| 41 | type = "expression";
|
---|
| 42 | break;
|
---|
| 43 | case "Object.defineProperty(exports)":
|
---|
| 44 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
| 45 | base = module.exportsArgument;
|
---|
| 46 | type = "Object.defineProperty";
|
---|
| 47 | break;
|
---|
| 48 | case "Object.defineProperty(module.exports)":
|
---|
| 49 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
| 50 | base = `${module.moduleArgument}.exports`;
|
---|
| 51 | type = "Object.defineProperty";
|
---|
| 52 | break;
|
---|
| 53 | case "Object.defineProperty(this)":
|
---|
| 54 | runtimeRequirements.add(RuntimeGlobals.thisAsExports);
|
---|
| 55 | base = "this";
|
---|
| 56 | type = "Object.defineProperty";
|
---|
| 57 | break;
|
---|
| 58 | default:
|
---|
| 59 | throw new Error(`Unsupported base ${depBase}`);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | return [type, base];
|
---|
| 63 | };
|
---|