/
rollup.impl.ts
349 lines (323 loc) · 10.1 KB
/
rollup.impl.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
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
340
341
342
343
344
345
346
347
348
349
import * as rollup from 'rollup';
import * as peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { getBabelInputPlugin } from '@rollup/plugin-babel';
import { join, relative } from 'path';
import { from, Observable, of } from 'rxjs';
import { catchError, concatMap, last, scan, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import * as autoprefixer from 'autoprefixer';
import type {
ExecutorContext,
ProjectGraphExternalNode,
ProjectGraphProjectNode,
} from '@nrwl/devkit';
import { logger, names, readJsonFile, writeJsonFile } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
computeCompilerOptionsPaths,
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import resolve from '@rollup/plugin-node-resolve';
import { AssetGlobPattern } from '../../utils/shared-models';
import { WebRollupOptions } from './schema';
import { runRollup } from './lib/run-rollup';
import {
NormalizedWebRollupOptions,
normalizeWebRollupOptions,
} from './lib/normalize';
import { analyze } from './lib/analyze-plugin';
import { deleteOutputDir } from '../../utils/fs';
import { swc } from './lib/swc-plugin';
import { validateTypes } from './lib/validate-types';
// These use require because the ES import isn't correct.
const commonjs = require('@rollup/plugin-commonjs');
const image = require('@rollup/plugin-image');
const json = require('@rollup/plugin-json');
const copy = require('rollup-plugin-copy');
const postcss = require('rollup-plugin-postcss');
const fileExtensions = ['.js', '.jsx', '.ts', '.tsx'];
export default async function* rollupExecutor(
rawOptions: WebRollupOptions,
context: ExecutorContext
) {
const project = context.workspace.projects[context.projectName];
const projectGraph = readCachedProjectGraph();
const sourceRoot = project.sourceRoot;
const { target, dependencies } = calculateProjectDependencies(
projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName,
true
);
const options = normalizeWebRollupOptions(
rawOptions,
context.root,
sourceRoot
);
const packageJson = readJsonFile(options.project);
const npmDeps = (projectGraph.dependencies[context.projectName] ?? [])
.filter((d) => d.target.startsWith('npm:'))
.map((d) => d.target.substr(4));
const rollupOptions = createRollupOptions(
options,
dependencies,
context,
packageJson,
sourceRoot,
npmDeps
);
if (options.compiler === 'swc') {
try {
await validateTypes({
workspaceRoot: context.root,
projectRoot: options.projectRoot,
tsconfig: options.tsConfig,
});
} catch {
return { success: false };
}
}
if (options.watch) {
const watcher = rollup.watch(rollupOptions);
return yield* eachValueFrom(
new Observable<{ success: boolean }>((obs) => {
watcher.on('event', (data) => {
if (data.code === 'START') {
logger.info(`Bundling ${context.projectName}...`);
} else if (data.code === 'END') {
updatePackageJson(
options,
context,
target,
dependencies,
packageJson
);
logger.info('Bundle complete. Watching for file changes...');
obs.next({ success: true });
} else if (data.code === 'ERROR') {
logger.error(`Error during bundle: ${data.error.message}`);
obs.next({ success: false });
}
});
// Teardown logic. Close watcher when unsubscribed.
return () => watcher.close();
})
);
} else {
logger.info(`Bundling ${context.projectName}...`);
// Delete output path before bundling
if (options.deleteOutputPath) {
deleteOutputDir(context.root, options.outputPath);
}
const start = process.hrtime.bigint();
return from(rollupOptions)
.pipe(
concatMap((opts) =>
runRollup(opts).pipe(
catchError((e) => {
logger.error(`Error during bundle: ${e}`);
return of({ success: false });
})
)
),
scan(
(acc, result) => {
if (!acc.success) return acc;
return result;
},
{ success: true }
),
last(),
tap({
next: (result) => {
if (result.success) {
const end = process.hrtime.bigint();
const duration = `${(Number(end - start) / 1_000_000_000).toFixed(
2
)}s`;
updatePackageJson(
options,
context,
target,
dependencies,
packageJson
);
logger.info(`⚡ Done in ${duration}`);
} else {
logger.error(`Bundle failed: ${context.projectName}`);
}
},
})
)
.toPromise();
}
}
// -----------------------------------------------------------------------------
export function createRollupOptions(
options: NormalizedWebRollupOptions,
dependencies: DependentBuildableProjectNode[],
context: ExecutorContext,
packageJson: any,
sourceRoot: string,
npmDeps: string[]
): rollup.InputOptions[] {
const useBabel = options.compiler === 'babel';
const useSwc = options.compiler === 'swc';
return options.format.map((format, idx) => {
const plugins = [
copy({
targets: convertCopyAssetsToRollupOptions(
options.outputPath,
options.assets
),
}),
image(),
useBabel &&
require('rollup-plugin-typescript2')({
check: true,
tsconfig: options.tsConfig,
tsconfigOverride: {
compilerOptions: createCompilerOptions(options, dependencies),
},
}),
useSwc && swc(),
peerDepsExternal({
packageJsonPath: options.project,
}),
postcss({
inject: true,
extract: options.extractCss,
autoModules: true,
plugins: [autoprefixer],
}),
resolve({
preferBuiltins: true,
extensions: fileExtensions,
}),
useBabel &&
getBabelInputPlugin({
// Let's `@nrwl/web/babel` preset know that we are packaging.
caller: {
// @ts-ignore
// Ignoring type checks for caller since we have custom attributes
isNxPackage: true,
// Always target esnext and let rollup handle cjs/umd
supportsStaticESM: true,
isModern: true,
},
cwd: join(context.root, sourceRoot),
rootMode: 'upward',
babelrc: true,
extensions: fileExtensions,
babelHelpers: 'bundled',
skipPreflightCheck: true, // pre-flight check may yield false positives and also slows down the build
exclude: /node_modules/,
plugins: [
format === 'esm'
? undefined
: require.resolve('babel-plugin-transform-async-to-promises'),
].filter(Boolean),
}),
commonjs(),
analyze(),
json(),
];
const globals = options.globals
? options.globals.reduce(
(acc, item) => {
acc[item.moduleId] = item.global;
return acc;
},
{ 'react/jsx-runtime': 'jsxRuntime' }
)
: { 'react/jsx-runtime': 'jsxRuntime' };
const externalPackages = dependencies
.map((d) => d.name)
.concat(options.external || [])
.concat(Object.keys(packageJson.dependencies || {}));
const rollupConfig = {
input: options.entryFile,
output: {
globals,
format,
dir: `${options.outputPath}`,
name: options.umdName || names(context.projectName).className,
entryFileNames: `[name].${format}.js`,
chunkFileNames: `[name].${format}.js`,
// umd doesn't support code-split bundles
inlineDynamicImports: format === 'umd',
},
external: (id) =>
externalPackages.some(
(name) => id === name || id.startsWith(`${name}/`)
) || npmDeps.some((name) => id === name || id.startsWith(`${name}/`)), // Could be a deep import
plugins,
};
return options.rollupConfig.reduce((currentConfig, plugin) => {
return require(plugin)(currentConfig, options);
}, rollupConfig);
});
}
function createCompilerOptions(options, dependencies) {
const compilerOptionPaths = computeCompilerOptionsPaths(
options.tsConfig,
dependencies
);
return {
rootDir: options.entryRoot,
allowJs: false,
declaration: true,
paths: compilerOptionPaths,
};
}
function updatePackageJson(
options: NormalizedWebRollupOptions,
context: ExecutorContext,
target: ProjectGraphProjectNode,
dependencies: DependentBuildableProjectNode[],
packageJson: any
) {
const entryFileTmpl = `./index.<%= extension %>.js`;
const typingsFile = relative(options.entryRoot, options.entryFile).replace(
/\.[jt]sx?$/,
'.d.ts'
);
packageJson.main = entryFileTmpl.replace('<%= extension %>', 'umd');
packageJson.module = entryFileTmpl.replace('<%= extension %>', 'esm');
packageJson.typings = `./${typingsFile}`;
writeJsonFile(`${options.outputPath}/package.json`, packageJson);
if (
dependencies.length > 0 &&
options.updateBuildableProjectDepsInPackageJson
) {
updateBuildableProjectPackageJsonDependencies(
context.root,
context.projectName,
context.targetName,
context.configurationName,
target,
dependencies,
options.buildableProjectDepsInPackageJsonType
);
}
}
interface RollupCopyAssetOption {
src: string;
dest: string;
}
function convertCopyAssetsToRollupOptions(
outputPath: string,
assets: AssetGlobPattern[]
): RollupCopyAssetOption[] {
return assets
? assets.map((a) => ({
src: join(a.input, a.glob).replace(/\\/g, '/'),
dest: join(outputPath, a.output).replace(/\\/g, '/'),
}))
: undefined;
}