UNPKG

@harves/nx-node-esm-plugin

Version:

Node executor including ESM module resolution for buildable libraries within Nx workspaces.

149 lines 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sampleAppGenerator = void 0; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const path = require("path"); const js_1 = require("@nx/js"); const path_1 = require("path"); /** * Generate sample app containing ESM app calling buildable ESM lib, * demonstrating the use of the `@harves/nx-node-esm-plugin:node` executor. * * Includes targets running the app using Node, one with the * @nx/js:node executor which does not resolve the library import (fails), * and one with the @harves/nx-node-esm-plugin:node executor which does. * * @param tree * @param options */ function sampleAppGenerator(tree, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const ws = (0, devkit_1.workspaceLayout)(); const pkg = (0, devkit_1.readJson)(tree, 'package.json'); const scope = pkg.name.split('/')[0]; const appName = `${options.name}-app`; const appDirectory = `${ws.appsDir}/${options.name}-app`; const libName = `${options.name}-lib`; const libDirectory = `${ws.libsDir}/${options.name}-lib`; /* * create app */ devkit_1.logger.info(`NX Generating sample app ${appDirectory}`); yield (0, js_1.libraryGenerator)(tree, // virtual file system tree // options for the generator { name: appName, directory: appDirectory, projectNameAndRootFormat: 'as-provided', config: 'project', importPath: `${scope}/${appName}`, buildable: true, bundler: 'tsc', strict: true, }); // app Nx project (0, devkit_1.updateJson)(tree, (0, path_1.join)(appDirectory, 'project.json'), (projectJson) => { projectJson.projectType = 'application'; projectJson.targets['run-js-node'] = { executor: '@nx/js:node', dependsOn: ['build'], options: { buildTarget: 'build', watch: false, }, }; projectJson.targets['run-node-esm-plugin'] = { executor: '@harves/nx-node-esm-plugin:node', dependsOn: ['build'], options: { buildTarget: 'build', }, }; return projectJson; }); // app package.json (0, devkit_1.updateJson)(tree, (0, path_1.join)(appDirectory, 'package.json'), (pkgJson) => { pkgJson.type = 'module'; return pkgJson; }); // app tsconfig (0, devkit_1.updateJson)(tree, (0, path_1.join)(appDirectory, 'tsconfig.json'), (tsconfig) => { tsconfig.compilerOptions.module = 'esnext'; return tsconfig; }); /* * create library */ devkit_1.logger.info(`NX Generating sample library ${libDirectory}`); yield (0, js_1.libraryGenerator)(tree, // virtual file system tree // options for the generator { name: libName, directory: libDirectory, projectNameAndRootFormat: 'as-provided', config: 'project', importPath: `${scope}/${libName}`, buildable: true, bundler: 'tsc', strict: true, }); // library package.json (0, devkit_1.updateJson)(tree, (0, path_1.join)(libDirectory, 'package.json'), (pkgJson) => { pkgJson.type = 'module'; return pkgJson; }); // library tsconfig (0, devkit_1.updateJson)(tree, (0, path_1.join)(libDirectory, 'tsconfig.json'), (tsconfig) => { tsconfig.compilerOptions.module = 'esnext'; return tsconfig; }); // console.log('TREE', tree); // const projects = getProjects(tree); // console.log('OBJECT', Object.keys(projects)); // const appConf = readProjectConfiguration(tree, appName); // appConf.projectType = 'application'; // updateProjectConfiguration(tree, appName, appConf); /* * app + library source code */ const appNames = (0, devkit_1.names)(appName); const libNames = (0, devkit_1.names)(libName); const substitutions = { appName, appNameFunc: appNames.propertyName, appNameFn: appNames.fileName, libName, libNameFunc: libNames.propertyName, libNameFn: libNames.fileName, libImport: `${scope}/${libName}`, tmpl: '', }; (0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'app'), appDirectory, substitutions); (0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files', 'lib'), libDirectory, substitutions); /* * app has dependency on library package */ const libPkg = (0, devkit_1.readJson)(tree, (0, path_1.join)(libDirectory, 'package.json')); (0, devkit_1.addDependenciesToPackageJson)(tree, { [`${scope}/${libName}`]: libPkg.version, }, {}, (0, path_1.join)(appDirectory, 'package.json')); // const srcPath = `${appDirectory}/src/index.ts`; // const srcBuf = tree.read(srcPath); // const src = srcBuf.toString('utf-8'); // console.log('SRC', src); // console.log('PKG', readJson(tree, join(appDirectory, 'package.json'))); // console.log('PROJECT', JSON.stringify(readJson(tree, join(appDirectory, 'project.json')), null, 2)); // console.log('TREE', tree); yield (0, devkit_1.formatFiles)(tree); devkit_1.logger.info(`NX Run app with @nx/js:node using \`nx run ${appName}:run-js-node\` (fails)`); devkit_1.logger.info(`NX Run app with this plugin using \`nx run ${appName}:run-node-esm-plugin\``); return () => { // this is performed after the changes to the filesystem are applied (0, devkit_1.installPackagesTask)(tree); }; }); } exports.sampleAppGenerator = sampleAppGenerator; exports.default = sampleAppGenerator; //# sourceMappingURL=generator.js.map