forked from ionic-team/ionic-app-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.ts
More file actions
200 lines (154 loc) · 5.95 KB
/
Copy pathrollup.ts
File metadata and controls
200 lines (154 loc) · 5.95 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
import { BuildContext, BuildState, TaskInfo } from './util/interfaces';
import { BuildError } from './util/errors';
import { fillConfigDefaults, generateContext, getUserConfigFile, replacePathVars } from './util/config';
import { ionCompiler } from './plugins/ion-compiler';
import { join, isAbsolute, normalize, sep } from 'path';
import { Logger } from './logger/logger';
import * as rollupBundler from 'rollup';
export function rollup(context: BuildContext, configFile: string) {
context = generateContext(context);
configFile = getUserConfigFile(context, taskInfo, configFile);
const logger = new Logger('rollup');
return rollupWorker(context, configFile)
.then(() => {
context.bundleState = BuildState.SuccessfulBuild;
logger.finish();
})
.catch(err => {
context.bundleState = BuildState.RequiresBuild;
throw logger.fail(err);
});
}
export function rollupUpdate(event: string, filePath: string, context: BuildContext) {
const logger = new Logger('rollup update');
const configFile = getUserConfigFile(context, taskInfo, null);
return rollupWorker(context, configFile)
.then(() => {
context.bundleState = BuildState.SuccessfulBuild;
logger.finish();
})
.catch(err => {
context.bundleState = BuildState.RequiresBuild;
throw logger.fail(err);
});
}
export function rollupWorker(context: BuildContext, configFile: string): Promise<any> {
return new Promise((resolve, reject) => {
let rollupConfig = getRollupConfig(context, configFile);
rollupConfig.dest = getOutputDest(context, rollupConfig);
// replace any path vars like {{TMP}} with the real path
rollupConfig.entry = replacePathVars(context, normalize(rollupConfig.entry));
rollupConfig.dest = replacePathVars(context, normalize(rollupConfig.dest));
if (!context.isProd) {
// ngc does full production builds itself and the bundler
// will already have receive transpiled and AoT templates
// dev mode auto-adds the ion-compiler plugin, which will inline
// templates and transpile source typescript code to JS before bundling
rollupConfig.plugins.unshift(
ionCompiler(context)
);
}
// tell rollup to use a previous bundle as its starting point
rollupConfig.cache = cachedBundle;
if (!rollupConfig.onwarn) {
// use our own logger if one wasn't already provided
rollupConfig.onwarn = createOnWarnFn();
}
Logger.debug(`entry: ${rollupConfig.entry}, dest: ${rollupConfig.dest}, cache: ${rollupConfig.cache}, format: ${rollupConfig.format}`);
checkDeprecations(context, rollupConfig);
// bundle the app then create create css
rollupBundler.rollup(rollupConfig)
.then((bundle: RollupBundle) => {
Logger.debug(`bundle.modules: ${bundle.modules.length}`);
// set the module files used in this bundle
// this reference can be used elsewhere in the build (sass)
context.moduleFiles = bundle.modules.map((m) => {
// sometimes, Rollup appends weird prefixes to the path like commonjs:proxy
const index = m.id.indexOf(sep);
if (index >= 0) {
return m.id.substring(index);
}
return m.id;
});
// cache our bundle for later use
if (context.isWatch) {
cachedBundle = bundle;
}
// write the bundle
return bundle.write(rollupConfig);
})
.then(() => {
// clean up any references (overkill yes, but let's play it safe)
rollupConfig = rollupConfig.cache = rollupConfig.onwarn = rollupConfig.plugins = null;
resolve();
})
.catch((err: any) => {
// ensure references are cleared up when there's an error
cachedBundle = rollupConfig = rollupConfig.cache = rollupConfig.onwarn = rollupConfig.plugins = null;
reject(new BuildError(err));
});
});
}
export function getRollupConfig(context: BuildContext, configFile: string): RollupConfig {
configFile = getUserConfigFile(context, taskInfo, configFile);
return fillConfigDefaults(configFile, taskInfo.defaultConfigFile);
}
export function getOutputDest(context: BuildContext, rollupConfig: RollupConfig) {
if (!isAbsolute(rollupConfig.dest)) {
// user can pass in absolute paths
// otherwise save it in the build directory
return join(context.buildDir, rollupConfig.dest);
}
return rollupConfig.dest;
}
function checkDeprecations(context: BuildContext, rollupConfig: RollupConfig) {
if (!context.isProd) {
if (rollupConfig.entry.indexOf('.tmp') > -1 || rollupConfig.entry.endsWith('.js')) {
// warning added 2016-10-05, v0.0.29
throw new BuildError('\nDev builds no longer use the ".tmp" directory. Please update your rollup config\'s\n' +
'entry to use your "src" directory\'s "main.dev.ts" TypeScript file.\n' +
'For example, the entry for dev builds should be: "src/app/main.dev.ts"');
}
}
}
let cachedBundle: RollupBundle = null;
function createOnWarnFn() {
const previousWarns: {[key: string]: boolean} = {};
return function onWarningMessage(msg: string) {
if (msg in previousWarns) {
return;
}
previousWarns[msg] = true;
if (!(IGNORE_WARNS.some(warnIgnore => msg.indexOf(warnIgnore) > -1))) {
Logger.warn(`rollup: ${msg}`);
}
};
}
const IGNORE_WARNS = [
'keyword is equivalent to'
];
const taskInfo: TaskInfo = {
fullArg: '--rollup',
shortArg: '-r',
envVar: 'IONIC_ROLLUP',
packageConfig: 'ionic_rollup',
defaultConfigFile: 'rollup.config'
};
export interface RollupConfig {
// https://github.com/rollup/rollup/wiki/JavaScript-API
entry?: string;
sourceMap?: boolean;
plugins?: any[];
format?: string;
dest?: string;
cache?: RollupBundle;
onwarn?: Function;
}
export interface RollupBundle {
// https://github.com/rollup/rollup/wiki/JavaScript-API
write?: Function;
modules: RollupModule[];
}
export interface RollupModule {
id: string;
}