-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathrollup.ts
131 lines (115 loc) · 3.81 KB
/
rollup.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
import rollupJson from '@rollup/plugin-json';
import * as path from 'path';
import type { InputPluginOption, OutputAsset, OutputChunk, RollupCache } from 'rollup';
import { dts } from 'rollup-plugin-dts';
import { OutputFileCache } from '../ng-package/nodes';
import { readCacheEntry, saveCacheEntry } from '../utils/cache';
import * as log from '../utils/log';
import { fileLoaderPlugin } from './file-loader-plugin';
/**
* Options used in `ng-packagr` for writing flat bundle files.
*
* These options are passed through to rollup.
*/
export interface RollupOptions {
moduleName: string;
entry: string;
entryName: string;
dir: string;
cache?: RollupCache;
cacheDirectory?: string | false;
fileCache: OutputFileCache;
cacheKey: string;
sourcemap: boolean;
}
let rollup: typeof import('rollup') | undefined;
/** Runs rollup over the given entry file, writes a bundle file. */
export async function rollupBundleFile(
opts: RollupOptions,
): Promise<{ cache: RollupCache; files: (OutputChunk | OutputAsset)[] }> {
await ensureRollup();
log.debug(`rollup (v${rollup.VERSION}) ${opts.entry} to ${opts.dir}`);
const cacheDirectory = opts.cacheDirectory;
const dtsMode = opts.entry.endsWith('.d.ts');
let outExtension: string;
let plugins: InputPluginOption[];
if (dtsMode) {
outExtension = '.d.ts';
plugins = [fileLoaderPlugin(opts.fileCache, ['.d.ts', '/index.d.ts']), dts()];
} else {
outExtension = '.mjs';
plugins = [fileLoaderPlugin(opts.fileCache, ['.js', '/index.js']), rollupJson()];
}
// Create the bundle
const bundle = await rollup.rollup({
context: 'this',
external: moduleId => isExternalDependency(moduleId),
cache: opts.cache ?? (cacheDirectory ? await readCacheEntry(cacheDirectory, opts.cacheKey) : undefined),
input: opts.entry,
plugins,
onwarn: warning => {
switch (warning.code) {
case 'CIRCULAR_DEPENDENCY':
case 'UNUSED_EXTERNAL_IMPORT':
case 'THIS_IS_UNDEFINED':
case 'EMPTY_BUNDLE':
break;
default:
log.warn(warning.message);
break;
}
},
preserveSymlinks: true,
// Disable treeshaking when generating bundles
// see: https://github.com./angular/angular/pull/32069
treeshake: false,
});
// Output the bundle to disk
const output = await bundle.write({
name: opts.moduleName,
format: 'es',
dir: opts.dir,
inlineDynamicImports: false,
hoistTransitiveImports: false,
chunkFileNames: `${opts.entryName}-[name]-[hash]${outExtension}`,
entryFileNames: opts.entryName + outExtension,
banner: '',
sourcemap: opts.sourcemap,
});
if (cacheDirectory) {
await saveCacheEntry(cacheDirectory, opts.cacheKey, bundle.cache);
}
// Close the bundle to let plugins clean up their external processes or services
await bundle.close();
return {
files: output.output.map(f => {
/** The map contents are in an asset file type, which makes storing the map in the cache as redudant. */
if (f.type === 'chunk') {
f.map = null;
}
return f;
}),
cache: bundle.cache,
};
}
async function ensureRollup(): Promise<void> {
if (rollup) {
return;
}
try {
rollup = await import('rollup');
log.debug(`rollup using native version.`);
} catch {
rollup = await import('@rollup/wasm-node');
log.debug(`rollup using wasm version.`);
}
}
function isExternalDependency(moduleId: string): boolean {
// more information about why we don't check for 'node_modules' path
// https://github.com./rollup/rollup-plugin-node-resolve/issues/110#issuecomment-350353632
if (moduleId[0] === '.' || moduleId[0] === '/' || path.isAbsolute(moduleId)) {
// if it's either 'absolute', marked to embed, starts with a '.' or '/' or is the umd bundle and is tslib
return false;
}
return true;
}