@ng-rsbuild/plugin-nx
Version:
Nx Plugin for building Angular applications with Rspack.
147 lines (142 loc) • 6.06 kB
JavaScript
;
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.applicationGenerator = applicationGenerator;
const generators_1 = require("@nx/angular/generators");
const devkit_1 = require("@nx/devkit");
const project_name_and_root_utils_1 = require("@nx/devkit/src/generators/project-name-and-root-utils");
const posix_1 = require("path/posix");
const versions_1 = require("../../utils/versions");
async function applicationGenerator(tree, options) {
if (!options.directory && !options.name) {
throw new Error('You must provide a directory where the application will be placed.');
}
if ((options.directory === '.' || options.directory === './') &&
!options.name) {
throw new Error('When placing an application in the current working directory, you must provide a name for the application with --name');
}
const tasks = [];
const { projectName, projectRoot } = await (0, project_name_and_root_utils_1.determineProjectNameAndRootOptions)(tree, {
name: options.name ?? (0, posix_1.basename)(options.directory),
directory: options.directory,
projectType: 'application',
});
const initTask = await (0, generators_1.applicationGenerator)(tree, {
...options,
name: projectName,
directory: projectRoot,
port: options.port ?? 4200,
projectNameAndRootFormat: 'as-provided',
style: options.style ?? 'css',
linter: options.linter ?? 'eslint',
unitTestRunner: options.unitTestRunner ?? 'jest',
e2eTestRunner: options.e2eTestRunner ?? 'playwright',
bundler: 'esbuild',
addTailwind: false,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
});
tasks.push(initTask);
const project = (0, devkit_1.readProjectConfiguration)(tree, projectName);
delete project.targets.build;
delete project.targets.serve;
delete project.targets['extract-i18n'];
(0, devkit_1.updateProjectConfiguration)(tree, projectName, project);
createRsbuildConfig(tree, projectRoot, options.ssr, options.style);
updateServer(tree, projectRoot);
// eslint-disable-next-line @typescript-eslint/no-empty-function
let installTask = () => { };
if (!options.skipPackageJson) {
installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, {
'@ng-rsbuild/plugin-angular': versions_1.ngRsbuildPluginAngularVersion,
'@rsbuild/core': versions_1.rsbuildVersion,
'sass-loader': versions_1.sassLoaderVersion,
'sass-embedded': versions_1.sassEmbeddedVersion,
});
}
tasks.push(installTask);
const nxRsbuildPluginTask = await initNxRsbuildPlugin(tree, options.skipPackageJson ?? false);
tasks.push(nxRsbuildPluginTask);
if (!options.skipFormat) {
await (0, devkit_1.formatFiles)(tree);
}
return (0, devkit_1.runTasksInSerial)(...tasks);
}
async function initNxRsbuildPlugin(tree, skipPackageJson) {
const packageJsonContents = (0, devkit_1.readJson)(tree, 'package.json');
const nxVersion = packageJsonContents?.['devDependencies']?.['nx'] ??
packageJsonContents?.['dependencies']?.['nx'];
if (!nxVersion) {
throw new Error('Nx is not installed. Please install Nx and try again.');
}
(0, devkit_1.ensurePackage)('@nx/rsbuild', nxVersion);
const { initGenerator } = await Promise.resolve().then(() => __importStar(require('@nx/rsbuild/generators')));
return await initGenerator(tree, {
skipPackageJson: skipPackageJson,
addPlugin: true,
skipFormat: true,
});
}
function createRsbuildConfig(tree, projectRoot, ssr = false, inlineStylesExtension = 'css') {
const rsbuildConfigContents = `import { createConfig } from '@ng-rsbuild/plugin-angular';
export default createConfig({
browser: './src/main.ts',${ssr
? `
server: './src/main.server.ts',
ssrEntry: './src/server.ts',`
: ''}${inlineStylesExtension !== 'css'
? `inlineStylesExtension: '${inlineStylesExtension}',
styles: ['./src/styles.${inlineStylesExtension}'],`
: ''}
});
`;
tree.write((0, devkit_1.joinPathFragments)(projectRoot, 'rsbuild.config.ts'), rsbuildConfigContents);
}
function updateServer(tree, projectRoot) {
const serverContents = `import { createServer } from '@ng-rsbuild/plugin-angular/ssr';
import bootstrap from './main.server';
const server = createServer(bootstrap);
/** Add your custom server logic here
*
* For example, you can add a custom static file server:
*
* server.app.use('/static', express.static(staticFolder));
*
* Or add additional api routes:
*
* server.app.get('/api/hello', (req, res) => {
* res.send('Hello World!');
* });
*
* Or add additional middleware:
*
* server.app.use((req, res, next) => {
* res.send('Hello World!');
* });
*/
server.listen();
`;
tree.write((0, devkit_1.joinPathFragments)(projectRoot, 'src/server.ts'), serverContents);
}
exports.default = applicationGenerator;