-
Notifications
You must be signed in to change notification settings - Fork 178
/
rollup.config.js
277 lines (255 loc) · 7.29 KB
/
rollup.config.js
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
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import { resolve as resolvePath, dirname } from 'path';
import { fileURLToPath } from 'url';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { babel } from '@rollup/plugin-babel';
import url from '@rollup/plugin-url';
import json from '@rollup/plugin-json';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import svgr from '@svgr/rollup';
import terser from '@rollup/plugin-terser';
import del from 'rollup-plugin-delete';
import copy from 'rollup-plugin-copy';
import webWorkerLoader from 'rollup-plugin-web-worker-loader';
import workspacesRun from 'workspaces-run';
const __dirname = fileURLToPath(dirname(import.meta.url));
const plugins = [
resolve({
browser: true, // To correctly import browser version of @ffmpeg/ffmpeg for example.
preferBuiltins: true,
extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.json'],
}),
babel({
babelrc: false,
extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs'],
babelHelpers: 'inline',
exclude: 'node_modules/**',
presets: ['@babel/env', '@babel/preset-react', '@babel/preset-typescript'],
plugins: [
'babel-plugin-styled-components',
[
'babel-plugin-transform-react-remove-prop-types',
{
removeImport: true,
},
],
],
}),
url({
include: '**/inline-icons/*.svg',
}),
svgr({
include: '**/icons/*.svg',
titleProp: true,
svgo: true,
memo: true,
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
convertColors: {
currentColor: /^(?!url|none)/i,
},
},
},
},
'removeDimensions',
],
},
}),
svgr({
include: '**/images/*.svg',
titleProp: true,
svgo: true,
memo: true,
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
convertColors: {
// See https://github.com/googleforcreators/web-stories-wp/pull/6361
currentColor: false,
},
},
},
},
'removeDimensions',
],
},
}),
commonjs(),
json({
compact: true,
}),
dynamicImportVars(),
webWorkerLoader({
inline: true,
preserveSource: true,
'web-worker': /\.worker\.js$/,
}),
terser({
// We preserve function names that start with capital letters as
// they're _likely_ component names, and these are useful to have
// in tracebacks and error messages.
keep_fnames: /__|_x|_n|_nx|sprintf|^[A-Z].+$/,
output: {
comments: /translators:/i,
},
mangle: {
reserved: ['__', '_x', '_n', '_nx', 'sprintf'],
},
}),
];
/**
* @typedef {Object} CustomInputOptions
* @property {string} configPackages Comma-separated list of package names to include.
* @property {string} configEntries Comma-separated list of entries to build. Supports "es" and "cjs".
* @typedef {import('rollup').RollupOptions & CustomInputOptions} CustomInputOptions
*/
/**
* Main rollup configuration for building npm packages.
*
* To build only a subset of all public packages, run:
*
* `npx rollup -c --configPackages=i18n,fonts`
*
* @param {CustomInputOptions} cliArgs CLI arguments.
* @return {import('rollup').RollupOptions} Rollup configuration.
*/
async function config(cliArgs) {
const packages = [];
const entries = [];
// Collect the list of packages
await workspacesRun({ cwd: __dirname, orderByDeps: true }, (pkg) => {
if (!pkg.config.private) {
packages.push(pkg);
}
});
const allPackageNames = packages.map(({ name }) => name.split('/')[1]);
const packagesToBuild = cliArgs?.configPackages
? cliArgs.configPackages.split(',')
: allPackageNames;
const entriesToBuild = cliArgs?.configEntries
? cliArgs.configEntries.split(',')
: ['es', 'cjs'];
for (const pkg of packages) {
if (!packagesToBuild.includes(pkg.name.split('/')[1])) {
continue;
}
const input = resolvePath(pkg.dir, pkg.config.source);
const external = [
...new Set([
...Object.keys(pkg.config.dependencies || {}),
...Object.keys(pkg.config.peerDependencies || {}),
'react',
'react-dom',
'react-dom/server',
/@babel\/runtime/,
/node_modules/,
]),
];
const output = {
sourcemap: true,
exports: 'auto',
preserveModules: true,
banner: `/*
* Copyright ${new Date().getFullYear()} Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/`,
};
const sourceDir = dirname(resolvePath(pkg.dir, pkg.config.source));
if (entriesToBuild.includes('es')) {
const _plugins = [];
const moduleDir = dirname(resolvePath(pkg.dir, pkg.config.module));
_plugins.push(
del({
targets: [moduleDir],
runOnce: false !== cliArgs.watch,
})
);
if ('@googleforcreators/fonts' === pkg.name) {
_plugins.push(
copy({
targets: [{ src: `${sourceDir}/fonts.json`, dest: moduleDir }],
})
);
}
entries.push({
input,
output: {
...output,
dir: moduleDir,
format: 'es',
},
plugins: [...plugins, ..._plugins],
external,
context: 'window',
});
}
if (entriesToBuild.includes('cjs')) {
const _plugins = [];
const mainDir = dirname(resolvePath(pkg.dir, pkg.config.main));
_plugins.push(
del({
targets: [mainDir],
runOnce: false !== cliArgs.watch,
})
);
if ('@googleforcreators/fonts' === pkg.name) {
_plugins.push(
copy({
targets: [{ src: `${sourceDir}/fonts.json`, dest: mainDir }],
})
);
}
entries.push({
input,
output: {
...output,
dir: mainDir,
format: 'cjs',
},
plugins: [...plugins, ..._plugins],
external,
context: 'window',
});
}
}
return entries;
}
export default config;