UNPKG

@angular-builders/custom-webpack

Version:

Custom webpack builders for Angular build facade. Allow to modify Angular build configuration without ejecting it

76 lines 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAdd = ngAdd; const schematics_1 = require("@angular-devkit/schematics"); const utility_1 = require("@schematics/angular/utility"); const schematics_2 = require("@angular-builders/common/schematics"); const PACKAGE_NAME = '@angular-builders/custom-webpack'; const BROWSER_BUILDER = `${PACKAGE_NAME}:browser`; const DEV_SERVER_BUILDER = `${PACKAGE_NAME}:dev-server`; const DEFAULT_CONFIG_FILE = 'webpack.config.js'; const SELF_VERSION_RANGE = '^22.0.0'; function webpackConfigFileExists(tree) { return (tree.exists(`/${DEFAULT_CONFIG_FILE}`) || tree.exists('/webpack.config.ts') || tree.exists('/webpack.config.cjs') || tree.exists('/webpack.config.mjs')); } function rewriteTargets(projectName) { return (0, utility_1.updateWorkspace)(workspace => { const project = workspace.projects.get(projectName); if (!project) return; const build = project.targets.get('build'); if (build) build.builder = BROWSER_BUILDER; const serve = project.targets.get('serve'); if (serve) serve.builder = DEV_SERVER_BUILDER; }); } function scaffoldConfig(projectName) { return async (tree, context) => { const workspace = await (0, utility_1.readWorkspace)(tree); const project = workspace.projects.get(projectName); const buildOptions = project?.targets.get('build')?.options ?? {}; const alreadyReferenced = buildOptions['customWebpackConfig'] !== undefined && buildOptions['customWebpackConfig'] !== false; if (alreadyReferenced || webpackConfigFileExists(tree)) { context.logger.info('[custom-webpack] A webpack config is already present; leaving it untouched.'); return (0, schematics_1.noop)(); } const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [(0, schematics_1.applyTemplates)({}), (0, schematics_1.move)('/')]); return (0, schematics_1.chain)([ (0, schematics_1.mergeWith)(templateSource), (0, utility_1.updateWorkspace)(ws => { const buildTarget = ws.projects.get(projectName)?.targets.get('build'); if (buildTarget) { buildTarget.options = { ...(buildTarget.options ?? {}), customWebpackConfig: { path: DEFAULT_CONFIG_FILE }, }; } }), ]); }; } function ngAdd(options) { return async (tree, context) => { const workspace = await (0, utility_1.readWorkspace)(tree); const projects = (0, schematics_2.getProjectsToTarget)(workspace, options.project); if (projects.length === 0) { context.logger.warn('[custom-webpack] No projects found to configure.'); return (0, schematics_1.noop)(); } const perProject = []; for (const projectName of projects) { perProject.push(rewriteTargets(projectName)); perProject.push(scaffoldConfig(projectName)); } return (0, schematics_1.chain)([ (0, schematics_2.addBuilderDevDependency)(PACKAGE_NAME, SELF_VERSION_RANGE, { install: true }), ...perProject, ]); }; } //# sourceMappingURL=index.js.map