|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const webpack = require("../../../../"); |
| 4 | +const makeSerializable = require("../../../../lib/util/makeSerializable"); |
| 5 | + |
| 6 | +/** |
| 7 | + * @import { |
| 8 | + * ObjectDeserializerContext, |
| 9 | + * ObjectSerializerContext |
| 10 | + * } from "../../../../lib/serialization/ObjectMiddleware" |
| 11 | + */ |
| 12 | + |
| 13 | +const { NullDependency } = webpack.dependencies; |
| 14 | + |
| 15 | +// A dependency type of webpack's own spells `isLazy` as a method; a foreign one may |
| 16 | +// spell the same name as a plain flag, which the lazy-barrel walk must not call. |
| 17 | +class LazyFlagDependency extends NullDependency { |
| 18 | + constructor() { |
| 19 | + super(); |
| 20 | + /** @type {{ isLazy: boolean }} */ (/** @type {unknown} */ (this)).isLazy = |
| 21 | + true; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Serializes this instance into the provided serializer context. |
| 26 | + * @param {ObjectSerializerContext} context context |
| 27 | + */ |
| 28 | + serialize(context) { |
| 29 | + context.write(this.isLazy); |
| 30 | + super.serialize(context); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Restores this instance from the provided deserializer context. |
| 35 | + * @param {ObjectDeserializerContext} context context |
| 36 | + */ |
| 37 | + deserialize(context) { |
| 38 | + /** @type {{ isLazy: boolean }} */ (/** @type {unknown} */ (this)).isLazy = |
| 39 | + /** @type {boolean} */ (context.read()); |
| 40 | + super.deserialize(context); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +makeSerializable( |
| 45 | + LazyFlagDependency, |
| 46 | + "test/configCases/lazy-barrel/custom-dependency-lazy-flag/webpack.config.js", |
| 47 | + "LazyFlagDependency" |
| 48 | +); |
| 49 | + |
| 50 | +/** @type {import("../../../../").Configuration} */ |
| 51 | +module.exports = { |
| 52 | + mode: "production", |
| 53 | + target: "web", |
| 54 | + devtool: false, |
| 55 | + optimization: { |
| 56 | + sideEffects: true, |
| 57 | + providedExports: true, |
| 58 | + usedExports: true, |
| 59 | + moduleIds: "named", |
| 60 | + minimize: false, |
| 61 | + concatenateModules: false |
| 62 | + }, |
| 63 | + plugins: [ |
| 64 | + (compiler) => { |
| 65 | + compiler.hooks.compilation.tap("Test", (compilation) => { |
| 66 | + compilation.dependencyTemplates.set( |
| 67 | + LazyFlagDependency, |
| 68 | + new NullDependency.Template() |
| 69 | + ); |
| 70 | + compilation.hooks.succeedModule.tap("Test", (module) => { |
| 71 | + if (/lib[/\\]index\.js$/.test(module.identifier())) { |
| 72 | + module.addDependency(new LazyFlagDependency()); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + ] |
| 78 | +}; |
0 commit comments