-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathlibrary.ts
More file actions
339 lines (309 loc) · 9.83 KB
/
library.ts
File metadata and controls
339 lines (309 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { getRelativeCwd, logShowProjectCommand } from '@nx/devkit/internal';
import {
addProjectConfiguration,
ensurePackage,
formatFiles,
GeneratorCallback,
installPackagesTask,
joinPathFragments,
logger,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Tree,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { addTsConfigPath, initGenerator as jsInitGenerator } from '@nx/js';
import { relative } from 'path';
import {
addReleaseConfigForNonTsSolution,
addReleaseConfigForTsSolution,
releaseTasks,
} from '@nx/js/src/generators/library/utils/add-release-config';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
import {
addProjectToTsSolutionWorkspace,
shouldConfigureTsSolutionSetup,
updateTsconfigFiles,
} from '@nx/js/src/utils/typescript/ts-solution-setup';
import type { PackageJson } from 'nx/src/utils/package-json';
import { extractTsConfigBase } from '../../utils/create-ts-config';
import { updateJestConfigContent } from '../../utils/jest-utils';
import { maybeJs } from '../../utils/maybe-js';
import { nxVersion } from '../../utils/versions';
import componentGenerator from '../component/component';
import initGenerator from '../init/init';
import { addLinting } from './lib/add-linting';
import { addRollupBuildTarget } from './lib/add-rollup-build-target';
import { createFiles } from './lib/create-files';
import { determineEntryFields } from './lib/determine-entry-fields';
import { installCommonDependencies } from './lib/install-common-dependencies';
import { normalizeOptions } from './lib/normalize-options';
import { setDefaults } from './lib/set-defaults';
import { updateAppRoutes } from './lib/update-app-routes';
import { Schema } from './schema';
export async function libraryGenerator(host: Tree, schema: Schema) {
return await libraryGeneratorInternal(host, {
addPlugin: false,
useProjectJson: true,
...schema,
});
}
export async function libraryGeneratorInternal(host: Tree, schema: Schema) {
const tasks: GeneratorCallback[] = [];
const addTsPlugin = shouldConfigureTsSolutionSetup(host, schema.addPlugin);
const jsInitTask = await jsInitGenerator(host, {
...schema,
addTsPlugin,
skipFormat: true,
});
tasks.push(jsInitTask);
const options = await normalizeOptions(host, schema);
if (
options.publishable === true &&
!options.isUsingTsSolutionConfig &&
!schema.importPath
) {
throw new Error(
`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`
);
}
if (!options.component) {
options.style = 'none';
}
const initTask = await initGenerator(host, {
...options,
skipFormat: true,
useReactRouterPlugin: false,
});
tasks.push(initTask);
const packageJson: PackageJson = {
name: options.importPath,
version: '0.0.1',
...determineEntryFields(options),
files: options.publishable ? ['dist', '!**/*.tsbuildinfo'] : undefined,
};
if (!options.useProjectJson) {
if (options.name !== options.importPath) {
packageJson.nx = { name: options.name };
}
if (options.parsedTags?.length) {
packageJson.nx ??= {};
packageJson.nx.tags = options.parsedTags;
}
} else {
addProjectConfiguration(host, options.name, {
root: options.projectRoot,
sourceRoot: joinPathFragments(options.projectRoot, 'src'),
projectType: 'library',
tags: options.parsedTags,
targets: {},
});
}
if (!options.useProjectJson || options.isUsingTsSolutionConfig) {
writeJson(host, `${options.projectRoot}/package.json`, packageJson);
}
createFiles(host, options);
if (options.isUsingTsSolutionConfig) {
await addProjectToTsSolutionWorkspace(host, options.projectRoot);
}
const lintTask = await addLinting(host, options);
tasks.push(lintTask);
// Set up build target
if (options.buildable && options.bundler === 'vite') {
const { viteConfigurationGenerator, createOrEditViteConfig } =
ensurePackage<typeof import('@nx/vite')>('@nx/vite', nxVersion);
const viteTask = await viteConfigurationGenerator(host, {
uiFramework: 'react',
project: options.name,
newProject: true,
includeLib: true,
inSourceTests: options.inSourceTests,
includeVitest: options.unitTestRunner === 'vitest',
compiler: options.compiler,
skipFormat: true,
testEnvironment: 'jsdom',
addPlugin: options.addPlugin,
});
tasks.push(viteTask);
createOrEditViteConfig(
host,
{
project: options.name,
includeLib: true,
includeVitest: options.unitTestRunner === 'vitest',
inSourceTests: options.inSourceTests,
rollupOptionsExternal: [
"'react'",
"'react-dom'",
"'react/jsx-runtime'",
],
imports: [
options.compiler === 'swc'
? `import react from '@vitejs/plugin-react-swc'`
: `import react from '@vitejs/plugin-react'`,
],
plugins: ['react()'],
useEsmExtension: true,
},
false
);
} else if (options.buildable && options.bundler === 'rollup') {
const rollupTask = await addRollupBuildTarget(host, options);
tasks.push(rollupTask);
}
// Set up test target
if (options.unitTestRunner === 'jest') {
const { configurationGenerator } = ensurePackage<typeof import('@nx/jest')>(
'@nx/jest',
nxVersion
);
const jestTask = await configurationGenerator(host, {
...options,
project: options.name,
setupFile: 'none',
supportTsx: true,
skipSerializers: true,
compiler: options.compiler,
skipFormat: true,
});
tasks.push(jestTask);
const jestConfigPath = joinPathFragments(
options.projectRoot,
options.js ? 'jest.config.js' : 'jest.config.cts'
);
if (options.compiler === 'babel' && host.exists(jestConfigPath)) {
const updatedContent = updateJestConfigContent(
host.read(jestConfigPath, 'utf-8')
);
host.write(jestConfigPath, updatedContent);
}
} else if (
options.unitTestRunner === 'vitest' &&
options.bundler !== 'vite' // tests are already configured if bundler is vite
) {
const { createOrEditViteConfig } = ensurePackage<typeof import('@nx/vite')>(
'@nx/vite',
nxVersion
);
ensurePackage('@nx/vitest', nxVersion);
const { configurationGenerator } = await import('@nx/vitest/generators');
const vitestTask = await configurationGenerator(host, {
uiFramework: 'react',
project: options.name,
coverageProvider: 'v8',
inSourceTests: options.inSourceTests,
skipFormat: true,
testEnvironment: 'jsdom',
addPlugin: options.addPlugin,
compiler: options.compiler,
});
tasks.push(vitestTask);
createOrEditViteConfig(
host,
{
project: options.name,
includeLib: true,
includeVitest: true,
inSourceTests: options.inSourceTests,
rollupOptionsExternal: [
"'react'",
"'react-dom'",
"'react/jsx-runtime'",
],
imports: [
options.compiler === 'swc'
? `import react from '@vitejs/plugin-react-swc'`
: `import react from '@vitejs/plugin-react'`,
],
plugins: ['react()'],
useEsmExtension: true,
},
true
);
}
if (options.component) {
const relativeCwd = getRelativeCwd();
const path = joinPathFragments(
options.projectRoot,
'src/lib',
options.js ? `${options.fileName}.js` : options.fileName
);
const componentTask = await componentGenerator(host, {
path: relativeCwd ? relative(relativeCwd, path) : path,
style: options.style,
skipTests:
options.unitTestRunner === 'none' ||
(options.unitTestRunner === 'vitest' && options.inSourceTests == true),
export: true,
routing: options.routing,
name: options.name,
inSourceTests: options.inSourceTests,
skipFormat: true,
globalCss: options.globalCss,
});
tasks.push(componentTask);
}
if (options.publishable) {
const projectConfiguration = readProjectConfiguration(host, options.name);
if (options.isUsingTsSolutionConfig) {
await addReleaseConfigForTsSolution(
host,
options.name,
projectConfiguration
);
} else {
const nxJson = readNxJson(host);
await addReleaseConfigForNonTsSolution(
host,
options.name,
projectConfiguration
);
}
updateProjectConfiguration(host, options.name, projectConfiguration);
tasks.push(await releaseTasks(host));
}
if (!options.skipPackageJson) {
const installReactTask = await installCommonDependencies(host, options);
tasks.push(installReactTask);
}
const routeTask = updateAppRoutes(host, options);
tasks.push(routeTask);
setDefaults(host, options);
extractTsConfigBase(host);
if (!options.skipTsConfig && !options.isUsingTsSolutionConfig) {
addTsConfigPath(host, options.importPath, [
maybeJs(
options,
joinPathFragments(options.projectRoot, './src/index.ts')
),
]);
}
updateTsconfigFiles(
host,
options.projectRoot,
'tsconfig.lib.json',
{
jsx: 'react-jsx',
module: 'esnext',
moduleResolution: 'bundler',
},
options.linter === 'eslint'
? ['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs']
: undefined
);
sortPackageJsonFields(host, options.projectRoot);
if (!options.skipFormat) {
await formatFiles(host);
}
// Always run install to link packages.
if (options.isUsingTsSolutionConfig) {
tasks.push(() => installPackagesTask(host, true));
}
tasks.push(() => {
logShowProjectCommand(options.name);
});
return runTasksInSerial(...tasks);
}
export default libraryGenerator;