forked from nrwl/nx
/
add-rollup-build-target.ts
80 lines (71 loc) · 2.09 KB
/
add-rollup-build-target.ts
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
import { Tree } from 'nx/src/generators/tree';
import {
addDependenciesToPackageJson,
ensurePackage,
getWorkspaceLayout,
joinPathFragments,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { maybeJs } from './maybe-js';
import { NormalizedSchema } from '../schema';
import {
nxVersion,
rollupPluginUrlVersion,
svgrRollupVersion,
} from '../../../utils/versions';
export async function addRollupBuildTarget(
host: Tree,
options: NormalizedSchema
) {
await ensurePackage(host, '@nrwl/rollup', nxVersion);
const { rollupInitGenerator } = await import('@nrwl/rollup');
// These are used in `@nrwl/react/plugins/bundle-rollup`
addDependenciesToPackageJson(
host,
{},
{
'@rollup/plugin-url': rollupPluginUrlVersion,
'@svgr/rollup': svgrRollupVersion,
}
);
const { targets } = readProjectConfiguration(host, options.name);
const { libsDir } = getWorkspaceLayout(host);
const external: string[] = [];
if (options.style === '@emotion/styled') {
external.push('@emotion/react/jsx-runtime');
} else {
external.push('react/jsx-runtime');
}
targets.build = {
executor: '@nrwl/rollup:rollup',
outputs: ['{options.outputPath}'],
options: {
outputPath:
libsDir !== '.'
? `dist/${libsDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`,
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
project: `${options.projectRoot}/package.json`,
entryFile: maybeJs(options, `${options.projectRoot}/src/index.ts`),
external,
rollupConfig: `@nrwl/react/plugins/bundle-rollup`,
compiler: options.compiler ?? 'babel',
assets: [
{
glob: `${options.projectRoot}/README.md`,
input: '.',
output: '.',
},
],
},
};
updateProjectConfiguration(host, options.name, {
root: options.projectRoot,
sourceRoot: joinPathFragments(options.projectRoot, 'src'),
projectType: 'library',
tags: options.parsedTags,
targets,
});
return rollupInitGenerator(host, options);
}