Skip to content

Commit

Permalink
feat(js): allow root js lib project to be generated
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed May 26, 2023
1 parent 39f7459 commit 6a4636a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'^.+\\\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
moduleFileExtensions: ['ts', 'js', 'html'],
testEnvironment: 'jsdom',
coverageDirectory: '../../coverage/libs/my-lib',
};
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ if (swcJestConfig.swcrc === undefined) {
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
moduleFileExtensions: ['ts', 'js', 'html'],
testEnvironment: '<%= testEnvironment %>',
coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>'
};
14 changes: 0 additions & 14 deletions packages/js/src/generators/library/files/lib/tsconfig.json__tmpl__

This file was deleted.

32 changes: 31 additions & 1 deletion packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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: '.',
Expand All @@ -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',
});
Expand Down Expand Up @@ -403,6 +407,7 @@ function replaceJestConfig(
project: options.name,
offsetFromRoot: offsetFromRoot(options.projectRoot),
projectRoot: options.projectRoot,
testEnvironment: options.testEnvironment,
});
}

Expand Down Expand Up @@ -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);
8 changes: 7 additions & 1 deletion packages/js/src/utils/swc/compile-swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -38,6 +42,8 @@ export async function compileSwc(
normalizedOptions: NormalizedSwcExecutorOptions,
postCompilationCallback: () => Promise<void>
) {
const isRootProject =
context.projectGraph.nodes[context.projectName].data.root === '.';
logger.log(`Compiling with SWC for ${context.projectName}...`);

if (normalizedOptions.clean) {
Expand Down

0 comments on commit 6a4636a

Please sign in to comment.