From 6a4636a0957a4ee2d239842e43fdc1db3ba1e122 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 26 May 2023 10:43:03 -0400 Subject: [PATCH] feat(js): allow root js lib project to be generated --- .../__snapshots__/library.spec.ts.snap | 1 + .../files/jest-config/jest.config.__ext__ | 1 + .../library/files/lib/tsconfig.json__tmpl__ | 14 -------- packages/js/src/generators/library/library.ts | 32 ++++++++++++++++++- packages/js/src/utils/swc/compile-swc.ts | 8 ++++- 5 files changed, 40 insertions(+), 16 deletions(-) delete mode 100644 packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__ diff --git a/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap index c83f6670d4f248..4dc931094e8487 100644 --- a/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap @@ -28,6 +28,7 @@ module.exports = { '^.+\\\\.[tj]s$': ['@swc/jest', swcJestConfig], }, moduleFileExtensions: ['ts', 'js', 'html'], + testEnvironment: 'jsdom', coverageDirectory: '../../coverage/libs/my-lib', }; " diff --git a/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ b/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ index 25c74092cfd2d8..aa6ab15eca99cf 100644 --- a/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ +++ b/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ @@ -25,5 +25,6 @@ if (swcJestConfig.swcrc === undefined) { '^.+\\.[tj]s$': ['@swc/jest', swcJestConfig], }, moduleFileExtensions: ['ts', 'js', 'html'], + testEnvironment: '<%= testEnvironment %>', coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>' }; diff --git a/packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__ b/packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__ deleted file mode 100644 index 8d27bd10719eba..00000000000000 --- a/packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__ +++ /dev/null @@ -1,14 +0,0 @@ -{ - "extends": "<%= rootTsConfigPath %>", - "compilerOptions": { - "module": "commonjs"<% if (js) { %>, - "allowJs": true<% } %> - }, - "files": [], - "include": [], - "references": [ - { - "path": "./tsconfig.lib.json" - } - ] -} diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index a3b386098c64a5..1c815cd2fd4261 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -37,6 +37,7 @@ import { import jsInitGenerator from '../init/init'; import { PackageJson } from 'nx/src/utils/package-json'; import setupVerdaccio from '../setup-verdaccio/generator'; +import { tsConfigBaseOptions } from '../../utils/typescript/create-ts-config'; export async function libraryGenerator( tree: Tree, @@ -63,6 +64,7 @@ export async function projectGenerator( await jsInitGenerator(tree, { ...schema, skipFormat: true, + tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json', }) ); const options = normalizeOptions(tree, schema, destinationDir); @@ -275,6 +277,9 @@ function addBabelRc(tree: Tree, options: NormalizedSchema) { function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) { const { className, name, propertyName } = names(options.name); + + createProjectTsConfigJson(tree, options); + generateFiles(tree, filesDir, options.projectRoot, { ...options, dot: '.', @@ -286,7 +291,6 @@ function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) { strict: undefined, tmpl: '', offsetFromRoot: offsetFromRoot(options.projectRoot), - rootTsConfigPath: getRelativePathToRootTsConfig(tree, options.projectRoot), buildable: options.bundler && options.bundler !== 'none', hasUnitTestRunner: options.unitTestRunner !== 'none', }); @@ -403,6 +407,7 @@ function replaceJestConfig( project: options.name, offsetFromRoot: offsetFromRoot(options.projectRoot), projectRoot: options.projectRoot, + testEnvironment: options.testEnvironment, }); } @@ -595,5 +600,30 @@ function getOutputPath(options: NormalizedSchema, destinationDir?: string) { return joinPathFragments(...parts); } +function createProjectTsConfigJson(tree: Tree, options: NormalizedSchema) { + const tsconfig = { + extends: options.rootProject + ? undefined + : getRelativePathToRootTsConfig(tree, options.projectRoot), + compilerOptions: { + ...(options.rootProject ? tsConfigBaseOptions : {}), + module: 'commonjs', + allowJs: options.js ? true : undefined, + }, + files: [], + include: [], + references: [ + { + path: './tsconfig.lib.json', + }, + ], + }; + writeJson( + tree, + joinPathFragments(options.projectRoot, 'tsconfig.json'), + tsconfig + ); +} + export default libraryGenerator; export const librarySchematic = convertNxGenerator(libraryGenerator); diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 61edec414dc431..30dbab9edb689d 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -10,7 +10,11 @@ function getSwcCmd( { swcrcPath, srcPath, destPath }: SwcCliOptions, watch = false ) { - let swcCmd = `npx swc ${srcPath} -d ${destPath} --config-file=${swcrcPath}`; + let swcCmd = `npx swc ${ + // TODO(jack): clean this up when we remove inline module support + // Handle root project + srcPath === '.' ? 'src' : srcPath + } -d ${destPath} --config-file=${swcrcPath}`; return watch ? swcCmd.concat(' --watch') : swcCmd; } @@ -38,6 +42,8 @@ export async function compileSwc( normalizedOptions: NormalizedSwcExecutorOptions, postCompilationCallback: () => Promise ) { + const isRootProject = + context.projectGraph.nodes[context.projectName].data.root === '.'; logger.log(`Compiling with SWC for ${context.projectName}...`); if (normalizedOptions.clean) {