[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
| 9 | const ContainerEntryDependency = require("./ContainerEntryDependency");
|
---|
| 10 | const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory");
|
---|
| 11 | const ContainerExposedDependency = require("./ContainerExposedDependency");
|
---|
| 12 | const { parseOptions } = require("./options");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */
|
---|
| 15 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 16 |
|
---|
| 17 | const validate = createSchemaValidation(
|
---|
| 18 | require("../../schemas/plugins/container/ContainerPlugin.check.js"),
|
---|
| 19 | () => require("../../schemas/plugins/container/ContainerPlugin.json"),
|
---|
| 20 | {
|
---|
| 21 | name: "Container Plugin",
|
---|
| 22 | baseDataPath: "options"
|
---|
| 23 | }
|
---|
| 24 | );
|
---|
| 25 |
|
---|
| 26 | const PLUGIN_NAME = "ContainerPlugin";
|
---|
| 27 |
|
---|
| 28 | class ContainerPlugin {
|
---|
| 29 | /**
|
---|
| 30 | * @param {ContainerPluginOptions} options options
|
---|
| 31 | */
|
---|
| 32 | constructor(options) {
|
---|
| 33 | validate(options);
|
---|
| 34 |
|
---|
| 35 | this._options = {
|
---|
| 36 | name: options.name,
|
---|
| 37 | shareScope: options.shareScope || "default",
|
---|
| 38 | library: options.library || {
|
---|
| 39 | type: "var",
|
---|
| 40 | name: options.name
|
---|
| 41 | },
|
---|
| 42 | runtime: options.runtime,
|
---|
| 43 | filename: options.filename || undefined,
|
---|
| 44 | exposes: parseOptions(
|
---|
| 45 | options.exposes,
|
---|
| 46 | item => ({
|
---|
| 47 | import: Array.isArray(item) ? item : [item],
|
---|
| 48 | name: undefined
|
---|
| 49 | }),
|
---|
| 50 | item => ({
|
---|
| 51 | import: Array.isArray(item.import) ? item.import : [item.import],
|
---|
| 52 | name: item.name || undefined
|
---|
| 53 | })
|
---|
| 54 | )
|
---|
| 55 | };
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Apply the plugin
|
---|
| 60 | * @param {Compiler} compiler the compiler instance
|
---|
| 61 | * @returns {void}
|
---|
| 62 | */
|
---|
| 63 | apply(compiler) {
|
---|
| 64 | const { name, exposes, shareScope, filename, library, runtime } =
|
---|
| 65 | this._options;
|
---|
| 66 |
|
---|
| 67 | compiler.options.output.enabledLibraryTypes.push(library.type);
|
---|
| 68 |
|
---|
| 69 | compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
|
---|
| 70 | const dep = new ContainerEntryDependency(name, exposes, shareScope);
|
---|
| 71 | dep.loc = { name };
|
---|
| 72 | compilation.addEntry(
|
---|
| 73 | compilation.options.context,
|
---|
| 74 | dep,
|
---|
| 75 | {
|
---|
| 76 | name,
|
---|
| 77 | filename,
|
---|
| 78 | runtime,
|
---|
| 79 | library
|
---|
| 80 | },
|
---|
| 81 | error => {
|
---|
| 82 | if (error) return callback(error);
|
---|
| 83 | callback();
|
---|
| 84 | }
|
---|
| 85 | );
|
---|
| 86 | });
|
---|
| 87 |
|
---|
| 88 | compiler.hooks.thisCompilation.tap(
|
---|
| 89 | PLUGIN_NAME,
|
---|
| 90 | (compilation, { normalModuleFactory }) => {
|
---|
| 91 | compilation.dependencyFactories.set(
|
---|
| 92 | ContainerEntryDependency,
|
---|
| 93 | new ContainerEntryModuleFactory()
|
---|
| 94 | );
|
---|
| 95 |
|
---|
| 96 | compilation.dependencyFactories.set(
|
---|
| 97 | ContainerExposedDependency,
|
---|
| 98 | normalModuleFactory
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 | );
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | module.exports = ContainerPlugin;
|
---|