123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- "use strict";
- const util = require("util");
- const { RawSource, ReplaceSource } = require("webpack-sources");
- const Generator = require("../Generator");
- const InitFragment = require("../InitFragment");
- const HarmonyCompatibilityDependency = require("../dependencies/HarmonyCompatibilityDependency");
- const deprecatedGetInitFragments = util.deprecate(
- (template, dependency, templateContext) =>
- template.getInitFragments(dependency, templateContext),
- "DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
- "DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
- );
- const TYPES = new Set(["javascript"]);
- class JavascriptGenerator extends Generator {
-
- getTypes(module) {
- return TYPES;
- }
-
- getSize(module, type) {
- const originalSource = module.originalSource();
- if (!originalSource) {
- return 39;
- }
- return originalSource.size();
- }
-
- getConcatenationBailoutReason(module, context) {
-
- if (
- !module.buildMeta ||
- module.buildMeta.exportsType !== "namespace" ||
- module.presentationalDependencies === undefined ||
- !module.presentationalDependencies.some(
- d => d instanceof HarmonyCompatibilityDependency
- )
- ) {
- return "Module is not an ECMAScript module";
- }
-
-
-
- if (module.buildInfo && module.buildInfo.moduleConcatenationBailout) {
- return `Module uses ${module.buildInfo.moduleConcatenationBailout}`;
- }
- }
-
- generate(module, generateContext) {
- const originalSource = module.originalSource();
- if (!originalSource) {
- return new RawSource("throw new Error('No source available');");
- }
- const source = new ReplaceSource(originalSource);
- const initFragments = [];
- this.sourceModule(module, initFragments, source, generateContext);
- return InitFragment.addToSource(source, initFragments, generateContext);
- }
-
- sourceModule(module, initFragments, source, generateContext) {
- for (const dependency of module.dependencies) {
- this.sourceDependency(
- module,
- dependency,
- initFragments,
- source,
- generateContext
- );
- }
- if (module.presentationalDependencies !== undefined) {
- for (const dependency of module.presentationalDependencies) {
- this.sourceDependency(
- module,
- dependency,
- initFragments,
- source,
- generateContext
- );
- }
- }
- for (const childBlock of module.blocks) {
- this.sourceBlock(
- module,
- childBlock,
- initFragments,
- source,
- generateContext
- );
- }
- }
-
- sourceBlock(module, block, initFragments, source, generateContext) {
- for (const dependency of block.dependencies) {
- this.sourceDependency(
- module,
- dependency,
- initFragments,
- source,
- generateContext
- );
- }
- for (const childBlock of block.blocks) {
- this.sourceBlock(
- module,
- childBlock,
- initFragments,
- source,
- generateContext
- );
- }
- }
-
- sourceDependency(module, dependency, initFragments, source, generateContext) {
- const constructor = /** @type {new (...args: any[]) => Dependency} */ (
- dependency.constructor
- );
- const template = generateContext.dependencyTemplates.get(constructor);
- if (!template) {
- throw new Error(
- "No template for dependency: " + dependency.constructor.name
- );
- }
- const templateContext = {
- runtimeTemplate: generateContext.runtimeTemplate,
- dependencyTemplates: generateContext.dependencyTemplates,
- moduleGraph: generateContext.moduleGraph,
- chunkGraph: generateContext.chunkGraph,
- module,
- runtime: generateContext.runtime,
- runtimeRequirements: generateContext.runtimeRequirements,
- concatenationScope: generateContext.concatenationScope,
- codeGenerationResults: generateContext.codeGenerationResults,
- initFragments
- };
- template.apply(dependency, source, templateContext);
- // TODO remove in webpack 6
- if ("getInitFragments" in template) {
- const fragments = deprecatedGetInitFragments(
- template,
- dependency,
- templateContext
- );
- if (fragments) {
- for (const fragment of fragments) {
- initFragments.push(fragment);
- }
- }
- }
- }
- }
- module.exports = JavascriptGenerator;
|