Skip to content

Commit 3e6f222

Browse files
Phillip9587FrozenPandaz
authored andcommitted
feat(js): added a minimal option to the library generator (#13561)
(cherry picked from commit 8c1d035)
1 parent eb7c7bf commit 3e6f222

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

docs/generated/packages/js/generators/library.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@
122122
"type": "boolean",
123123
"description": "Whether to skip TypeScript type checking for SWC compiler.",
124124
"default": false
125+
},
126+
"minimal": {
127+
"type": "boolean",
128+
"description": "Generate a library with a minimal setup. No README.md generated.",
129+
"default": false
125130
}
126131
},
127132
"required": ["name"],

packages/js/src/generators/library/library.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,4 +1100,56 @@ describe('lib', () => {
11001100
}
11011101
);
11021102
});
1103+
1104+
describe('--minimal', () => {
1105+
it('should generate a README.md when minimal is set to false', async () => {
1106+
await libraryGenerator(tree, {
1107+
...defaultOptions,
1108+
name: 'myLib',
1109+
minimal: false,
1110+
});
1111+
1112+
expect(tree.exists('libs/my-lib/README.md')).toBeTruthy();
1113+
});
1114+
1115+
it('should not generate a README.md when minimal is set to true', async () => {
1116+
await libraryGenerator(tree, {
1117+
...defaultOptions,
1118+
name: 'myLib',
1119+
minimal: true,
1120+
});
1121+
1122+
expect(tree.exists('libs/my-lib/README.md')).toBeFalsy();
1123+
});
1124+
1125+
it('should generate a README.md and add it to the build assets when buildable is true and minimal is false', async () => {
1126+
await libraryGenerator(tree, {
1127+
...defaultOptions,
1128+
name: 'myLib',
1129+
bundler: 'tsc',
1130+
minimal: false,
1131+
});
1132+
1133+
expect(tree.exists('libs/my-lib/README.md')).toBeTruthy();
1134+
1135+
const project = readProjectConfiguration(tree, 'my-lib');
1136+
expect(project.targets.build.options.assets).toStrictEqual([
1137+
'libs/my-lib/*.md',
1138+
]);
1139+
});
1140+
1141+
it('should not generate a README.md when both bundler and minimal are set', async () => {
1142+
await libraryGenerator(tree, {
1143+
...defaultOptions,
1144+
name: 'myLib',
1145+
bundler: 'tsc',
1146+
minimal: true,
1147+
});
1148+
1149+
expect(tree.exists('libs/my-lib/README.md')).toBeFalsy();
1150+
1151+
const project = readProjectConfiguration(tree, 'my-lib');
1152+
expect(project.targets.build.options.assets).toEqual([]);
1153+
});
1154+
});
11031155
});

packages/js/src/generators/library/library.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ function addProject(
158158
outputPath,
159159
main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
160160
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
161-
// TODO(jack): assets for rollup have validation that we need to fix (assets must be under <project-root>/src)
162-
assets:
163-
options.bundler === 'rollup' ? [] : [`${options.projectRoot}/*.md`],
161+
assets: [],
164162
},
165163
};
166164

@@ -173,6 +171,17 @@ function addProject(
173171
projectConfiguration.targets.build.options.skipTypeCheck = true;
174172
}
175173

174+
if (
175+
!options.minimal &&
176+
// TODO(jack): assets for rollup have validation that we need to fix (assets must be under <project-root>/src)
177+
options.bundler !== 'rollup'
178+
) {
179+
projectConfiguration.targets.build.options.assets ??= [];
180+
projectConfiguration.targets.build.options.assets.push(
181+
joinPathFragments(options.projectRoot, '*.md')
182+
);
183+
}
184+
176185
if (options.publishable) {
177186
const publishScriptPath = addMinimalPublishScript(tree);
178187

@@ -305,6 +314,10 @@ function createFiles(tree: Tree, options: NormalizedSchema, filesDir: string) {
305314
tree.delete(packageJsonPath);
306315
}
307316

317+
if (options.minimal) {
318+
tree.delete(join(options.projectRoot, 'README.md'));
319+
}
320+
308321
updateTsConfig(tree, options);
309322
}
310323

@@ -441,6 +454,8 @@ function normalizeOptions(
441454
const importPath =
442455
options.importPath || getImportPath(npmScope, projectDirectory);
443456

457+
options.minimal ??= false;
458+
444459
return {
445460
...options,
446461
fileName,

packages/js/src/generators/library/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@
122122
"type": "boolean",
123123
"description": "Whether to skip TypeScript type checking for SWC compiler.",
124124
"default": false
125+
},
126+
"minimal": {
127+
"type": "boolean",
128+
"description": "Generate a library with a minimal setup. No README.md generated.",
129+
"default": false
125130
}
126131
},
127132
"required": ["name"],

packages/js/src/utils/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface LibraryGeneratorSchema {
3131
compiler?: Compiler;
3232
bundler?: Bundler;
3333
skipTypeCheck?: boolean;
34+
minimal?: boolean;
3435
}
3536

3637
export interface ExecutorOptions {

0 commit comments

Comments
 (0)