|
| 1 | +const Dependency = require('webpack/lib/Dependency') |
| 2 | +const makeSerializable = require('webpack/lib/util/makeSerializable') |
| 3 | +const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency') |
| 4 | +const { RetryRuntimeGlobal } = require('./RetryRuntimeModule') |
| 5 | + |
| 6 | +class ImportDependency extends ModuleDependency { |
| 7 | + /** |
| 8 | + * @param {string} request the request |
| 9 | + * @param {[number, number]} range expression range |
| 10 | + * @param {string[][]=} referencedExports list of referenced exports |
| 11 | + */ |
| 12 | + constructor (request, range, referencedExports, extraOptions) { |
| 13 | + super(request) |
| 14 | + this.range = range |
| 15 | + this.referencedExports = referencedExports |
| 16 | + this.extraOptions = extraOptions |
| 17 | + } |
| 18 | + |
| 19 | + get type () { |
| 20 | + return 'import()' |
| 21 | + } |
| 22 | + |
| 23 | + get category () { |
| 24 | + return 'esm' |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns list of exports referenced by this dependency |
| 29 | + * @param {ModuleGraph} moduleGraph module graph |
| 30 | + * @param {RuntimeSpec} runtime the runtime for which the module is analysed |
| 31 | + * @returns {(string[] | ReferencedExport)[]} referenced exports |
| 32 | + */ |
| 33 | + getReferencedExports (moduleGraph, runtime) { |
| 34 | + return this.referencedExports |
| 35 | + ? this.referencedExports.map((e) => ({ |
| 36 | + name: e, |
| 37 | + canMangle: false |
| 38 | + })) |
| 39 | + : Dependency.EXPORTS_OBJECT_REFERENCED |
| 40 | + } |
| 41 | + |
| 42 | + serialize (context) { |
| 43 | + context.write(this.range) |
| 44 | + context.write(this.referencedExports) |
| 45 | + context.write(this.extraOptions) |
| 46 | + super.serialize(context) |
| 47 | + } |
| 48 | + |
| 49 | + deserialize (context) { |
| 50 | + this.range = context.read() |
| 51 | + this.referencedExports = context.read() |
| 52 | + this.extraOptions = context.read() |
| 53 | + super.deserialize(context) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +makeSerializable(ImportDependency, '@mpxjs/webpack-plugin/lib/dependencies/ImportDependency') |
| 58 | + |
| 59 | +ImportDependency.Template = class ImportDependencyTemplate extends ( |
| 60 | + ModuleDependency.Template |
| 61 | +) { |
| 62 | + /** |
| 63 | + * @param {Dependency} dependency the dependency for which the template should be applied |
| 64 | + * @param {ReplaceSource} source the current replace source which can be modified |
| 65 | + * @param {DependencyTemplateContext} templateContext the context object |
| 66 | + * @returns {void} |
| 67 | + */ |
| 68 | + apply ( |
| 69 | + dependency, |
| 70 | + source, |
| 71 | + { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements } |
| 72 | + ) { |
| 73 | + const dep = /** @type {ImportDependency} */ (dependency) |
| 74 | + const block = /** @type {AsyncDependenciesBlock} */ ( |
| 75 | + moduleGraph.getParentBlock(dep) |
| 76 | + ) |
| 77 | + let content = runtimeTemplate.moduleNamespacePromise({ |
| 78 | + chunkGraph, |
| 79 | + block: block, |
| 80 | + module: /** @type {Module} */ (moduleGraph.getModule(dep)), |
| 81 | + request: dep.request, |
| 82 | + strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule, |
| 83 | + message: 'import()', |
| 84 | + runtimeRequirements |
| 85 | + }) |
| 86 | + // replace fakeType by 9 to fix require.async to commonjs2 module like 'module.exports = function(){...}' |
| 87 | + content = content.replace(/(__webpack_require__\.t\.bind\(.+,\s*)(\d+)(\s*\))/, (_, p1, p2, p3) => { |
| 88 | + return p1 + '9' + p3 |
| 89 | + }) |
| 90 | + |
| 91 | + // require.async 的场景且配置了重试次数才注入 RetryRuntimeModule |
| 92 | + const extraOptions = dep.extraOptions || {} |
| 93 | + if (extraOptions.isRequireAsync && extraOptions.retryRequireAsync && extraOptions.retryRequireAsync.times > 0) { |
| 94 | + runtimeRequirements.add(RetryRuntimeGlobal) |
| 95 | + content = `${RetryRuntimeGlobal}(function() { return ${content} }, ${extraOptions.retryRequireAsync.times}, ${extraOptions.retryRequireAsync.interval})` |
| 96 | + } |
| 97 | + |
| 98 | + source.replace(dep.range[0], dep.range[1] - 1, content) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = ImportDependency |
0 commit comments