@angular-devkit/build-webpack
Version:
Webpack Builder for Architect
127 lines (126 loc) • 5.19 kB
JavaScript
;
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEmittedFiles = getEmittedFiles;
exports.getWebpackConfig = getWebpackConfig;
const node_fs_1 = require("node:fs");
const path = __importStar(require("node:path"));
const node_url_1 = require("node:url");
function getEmittedFiles(compilation) {
const files = [];
const chunkFileNames = new Set();
// adds all chunks to the list of emitted files such as lazy loaded modules
for (const chunk of compilation.chunks) {
for (const file of chunk.files) {
if (chunkFileNames.has(file)) {
continue;
}
chunkFileNames.add(file);
files.push({
id: chunk.id?.toString(),
name: chunk.name ?? undefined,
file,
extension: path.extname(file),
initial: chunk.isOnlyInitial(),
});
}
}
// add all other files
for (const file of Object.keys(compilation.assets)) {
// Chunk files have already been added to the files list above
if (chunkFileNames.has(file)) {
continue;
}
files.push({ file, extension: path.extname(file), initial: false, asset: true });
}
return files;
}
/**
* This uses a dynamic import to load a module which may be ESM.
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
* will currently, unconditionally downlevel dynamic import into a require call.
* require calls cannot load ESM code and will result in a runtime error. To workaround
* this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
* Once TypeScript provides support for keeping the dynamic import this workaround can
* be dropped.
*
* @param modulePath The path of the module to load.
* @returns A Promise that resolves to the dynamically imported module.
*/
function loadEsmModule(modulePath) {
return new Function('modulePath', `return import(modulePath);`)(modulePath);
}
async function getWebpackConfig(configPath) {
if (!(0, node_fs_1.existsSync)(configPath)) {
throw new Error(`Webpack configuration file ${configPath} does not exist.`);
}
let config;
switch (path.extname(configPath)) {
case '.mjs':
// Load the ESM configuration file using the TypeScript dynamic import workaround.
// Once TypeScript provides support for keeping the dynamic import this workaround can be
// changed to a direct dynamic import.
config = await loadEsmModule((0, node_url_1.pathToFileURL)(configPath));
break;
case '.cjs':
config = require(configPath);
break;
default:
// The file could be either CommonJS or ESM.
// CommonJS is tried first then ESM if loading fails.
try {
config = require(configPath);
break;
}
catch (e) {
if (e.code === 'ERR_REQUIRE_ESM' ||
e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
// Load the ESM configuration file using the TypeScript dynamic import workaround.
// Once TypeScript provides support for keeping the dynamic import this workaround can be
// changed to a direct dynamic import.
config = await loadEsmModule((0, node_url_1.pathToFileURL)(configPath));
}
throw e;
}
}
return 'default' in config ? config.default : config;
}