-
Notifications
You must be signed in to change notification settings - Fork 295
/
rollup.ts
84 lines (77 loc) · 2.79 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
import * as rollup from 'rollup';
import * as nodeResolve from 'rollup-plugin-node-resolve';
import * as sourcemaps from 'rollup-plugin-sourcemaps';
import * as commonJs from 'rollup-plugin-commonjs';
import * as rollupJson from 'rollup-plugin-json';
import * as log from '../util/log';
import { ExternalModuleIdStrategy, DependencyList } from './external-module-id-strategy';
import { umdModuleIdStrategy } from './umd-module-id-strategy';
import { TransformHook } from 'rollup';
import { ensureUnixPath } from '../util/path';
/**
* Options used in `ng-packagr` for writing flat bundle files.
*
* These options are passed through to rollup.
*/
export interface RollupOptions {
moduleName: string;
entry: string;
format: rollup.ModuleFormat;
dest: string;
sourceRoot: string;
umdModuleIds?: { [key: string]: string };
amd?: { id: string };
transform?: TransformHook;
dependencyList?: DependencyList;
}
/** Runs rollup over the given entry file, writes a bundle file. */
export async function rollupBundleFile(opts: RollupOptions): Promise<void> {
log.debug(`rollup (v${rollup.VERSION}) ${opts.entry} to ${opts.dest} (${opts.format})`);
const externalModuleIdStrategy = new ExternalModuleIdStrategy(opts.format, opts.dependencyList);
// Create the bundle
const bundle = await rollup.rollup({
context: 'this',
external: moduleId => externalModuleIdStrategy.isExternalDependency(moduleId),
inlineDynamicImports: false,
input: opts.entry,
plugins: [rollupJson(), nodeResolve(), commonJs(), sourcemaps(), { transform: opts.transform }],
onwarn: warning => {
if (typeof warning === 'string') {
log.warn(warning);
} else {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
log.warn(warning.message);
}
},
preserveSymlinks: true
});
// Output the bundle to disk
await bundle.write({
name: opts.moduleName,
format: opts.format,
amd: opts.amd,
file: opts.dest,
banner: '',
globals: moduleId => umdModuleIdStrategy(moduleId, opts.umdModuleIds || {}),
sourcemap: true,
sourcemapPathTransform: (sourcePath: string) => {
sourcePath = ensureUnixPath(sourcePath);
// relocate sourcemaps
if (!sourcePath) {
return sourcePath;
}
// the replace here is because during the compilation one of the `/` gets lost sometimes
const sourceRoot = ensureUnixPath(opts.sourceRoot);
const mapRootUrl = sourceRoot.replace('//', '/');
if (sourcePath.indexOf(mapRootUrl) >= 0) {
return `${sourceRoot}${sourcePath.substr(sourcePath.indexOf(mapRootUrl) + mapRootUrl.length)}`;
} else if (sourcePath.indexOf(sourceRoot) >= 0) {
return sourcePath.substr(sourcePath.indexOf(mapRootUrl));
} else {
return sourcePath;
}
}
});
}