-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrollup.config.js
435 lines (384 loc) · 15.8 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import fs from 'fs';
import path from 'path';
import { checkResourceExists, normalizePathnameForWindows } from '../lib/resource-utils.js';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import * as walk from 'acorn-walk';
// https://github.com/rollup/rollup/issues/2121
function cleanRollupId(id) {
return id.replace('\x00', '');
}
// specifically to handle escodegen and other node modules
// using require for package.json or other json files
// https://github.com/estools/escodegen/issues/455
function greenwoodJsonLoader() {
return {
name: 'greenwood-json-loader',
async load(id) {
const idUrl = new URL(`file://${cleanRollupId(id)}`);
const extension = idUrl.pathname.split('.').pop();
if (extension === 'json') {
const json = JSON.parse(await fs.promises.readFile(idUrl, 'utf-8'));
const contents = `export default ${JSON.stringify(json)}`;
return contents;
}
}
};
}
function greenwoodResourceLoader (compilation) {
const resourcePlugins = compilation.config.plugins.filter((plugin) => {
return plugin.type === 'resource';
}).map((plugin) => {
return plugin.provider(compilation);
});
return {
name: 'greenwood-resource-loader',
async resolveId(id) {
const normalizedId = cleanRollupId(id); // idUrl.pathname;
const { projectDirectory, userWorkspace } = compilation.context;
if (normalizedId.startsWith('.') && !normalizedId.startsWith(projectDirectory.pathname)) {
const prefix = normalizedId.startsWith('..') ? './' : '';
const userWorkspaceUrl = new URL(`${prefix}${normalizedId.replace(/\.\.\//g, '')}`, userWorkspace);
if (await checkResourceExists(userWorkspaceUrl)) {
return normalizePathnameForWindows(userWorkspaceUrl);
}
}
},
async load(id) {
const idUrl = new URL(`file://${cleanRollupId(id)}`);
const { pathname } = idUrl;
const extension = pathname.split('.').pop();
// filter first for any bare specifiers
if (await checkResourceExists(idUrl) && extension !== '' && extension !== 'js') {
const url = new URL(`${idUrl.href}?type=${extension}`);
const request = new Request(url.href);
let response = new Response('');
for (const plugin of resourcePlugins) {
if (plugin.shouldServe && await plugin.shouldServe(url, request)) {
response = await plugin.serve(url, request);
}
}
for (const plugin of resourcePlugins) {
if (plugin.shouldIntercept && await plugin.shouldIntercept(url, request, response.clone())) {
response = await plugin.intercept(url, request, response.clone());
}
}
return await response.text();
}
}
};
}
function greenwoodSyncPageResourceBundlesPlugin(compilation) {
return {
name: 'greenwood-sync-page-resource-bundles-plugin',
async writeBundle(outputOptions, bundles) {
const { outputDir } = compilation.context;
for (const resource of compilation.resources.values()) {
const resourceKey = normalizePathnameForWindows(resource.sourcePathURL);
for (const bundle in bundles) {
let facadeModuleId = (bundles[bundle].facadeModuleId || '').replace(/\\/g, '/');
/*
* this is an odd issue related to symlinking in our Greenwood monorepo when building the website
* and managing packages that we create as "virtual" modules, like for the mpa router
*
* ex. import @greenwood/router/router.js -> /node_modules/@greenwood/cli/src/lib/router.js
*
* when running our tests, which better emulates a real user
* facadeModuleId will be in node_modules, which is like how it would be for a user:
* /node_modules/@greenwood/cli/src/lib/router.js
*
* however, when building our website, where symlinking points back to our packages/ directory
* facadeModuleId will look like this:
* /<workspace>/greenwood/packages/cli/src/lib/router.js
*
* so we need to massage facadeModuleId a bit for Rollup for our internal development
* pathToMatch (before): /node_modules/@greenwood/cli/src/lib/router.js
* pathToMatch (after): /cli/src/lib/router.js
*/
if (resourceKey?.indexOf('/node_modules/@greenwood/cli') > 0 && facadeModuleId?.indexOf('/packages/cli') > 0) {
if (await checkResourceExists(new URL(`file://${facadeModuleId}`))) {
facadeModuleId = facadeModuleId.replace('/packages/cli', '/node_modules/@greenwood/cli');
}
}
if (resourceKey === facadeModuleId) {
const { fileName } = bundles[bundle];
const { rawAttributes, contents } = resource;
const noop = rawAttributes && rawAttributes.indexOf('data-gwd-opt="none"') >= 0 || compilation.config.optimization === 'none';
const outputPath = new URL(`./${fileName}`, outputDir);
compilation.resources.set(resource.sourcePathURL.pathname, {
...compilation.resources.get(resource.sourcePathURL.pathname),
optimizedFileName: fileName,
optimizedFileContents: await fs.promises.readFile(outputPath, 'utf-8'),
contents
});
if (noop) {
await fs.promises.writeFile(outputPath, contents);
}
}
}
}
}
};
}
function getMetaImportPath(node) {
return node.arguments[0].value.split('/').join(path.sep);
}
function isNewUrlImportMetaUrl(node) {
return (
node.type === 'NewExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'URL' &&
node.arguments.length === 2 &&
node.arguments[0].type === 'Literal' &&
typeof getMetaImportPath(node) === 'string' &&
node.arguments[1].type === 'MemberExpression' &&
node.arguments[1].object.type === 'MetaProperty' &&
node.arguments[1].property.type === 'Identifier' &&
node.arguments[1].property.name === 'url'
);
}
// adapted from, and with credit to @web/rollup-plugin-import-meta-assets
// https://modern-web.dev/docs/building/rollup-plugin-import-meta-assets/
function greenwoodImportMetaUrl(compilation) {
return {
name: 'greenwood-import-meta-url',
async transform(code, id) {
const resourcePlugins = compilation.config.plugins.filter((plugin) => {
return plugin.type === 'resource';
}).map((plugin) => {
return plugin.provider(compilation);
});
const customResourcePlugins = compilation.config.plugins.filter((plugin) => {
return plugin.type === 'resource' && !plugin.isGreenwoodDefaultPlugin;
}).map((plugin) => {
return plugin.provider(compilation);
});
const idUrl = new URL(`file://${cleanRollupId(id)}`);
const { pathname } = idUrl;
const extension = pathname.split('.').pop();
const urlWithType = new URL(`${idUrl.href}?type=${extension}`);
const request = new Request(urlWithType.href);
let canTransform = false;
let response = new Response(code);
// handle any custom imports or pre-processing needed before passing to Rollup this.parse
if (await checkResourceExists(idUrl) && extension !== '' && extension !== 'json') {
for (const plugin of resourcePlugins) {
if (plugin.shouldServe && await plugin.shouldServe(urlWithType, request)) {
response = await plugin.serve(urlWithType, request);
canTransform = true;
}
}
for (const plugin of resourcePlugins) {
if (plugin.shouldIntercept && await plugin.shouldIntercept(urlWithType, request, response.clone())) {
response = await plugin.intercept(urlWithType, request, response.clone());
canTransform = true;
}
}
}
if (!canTransform) {
return null;
}
const ast = this.parse(await response.text());
const assetUrls = [];
let modifiedCode = false;
// aggregate all references of new URL + import.meta.url
walk.simple(ast, {
NewExpression(node) {
if (isNewUrlImportMetaUrl(node)) {
const absoluteScriptDir = path.dirname(id);
const relativeAssetPath = getMetaImportPath(node);
const absoluteAssetPath = path.resolve(absoluteScriptDir, relativeAssetPath);
const assetName = path.basename(absoluteAssetPath);
const assetExtension = assetName.split('.').pop();
assetUrls.push({
url: new URL(`file://${absoluteAssetPath}?type=${assetExtension}`),
relativeAssetPath
});
}
}
});
for (const assetUrl of assetUrls) {
const { url } = assetUrl;
const { pathname } = url;
const { relativeAssetPath } = assetUrl;
const assetName = path.basename(pathname);
const assetExtension = assetName.split('.').pop();
const assetContents = await fs.promises.readFile(url, 'utf-8');
const name = assetName.replace(`.${assetExtension}`, '');
let bundleExtensions = ['js'];
for (const plugin of customResourcePlugins) {
if (plugin.shouldServe && await plugin.shouldServe(url)) {
const response = await plugin.serve(url);
if (response?.headers?.get('content-type') || ''.indexOf('text/javascript') >= 0) {
bundleExtensions = [...bundleExtensions, ...plugin.extensions];
}
}
}
const type = bundleExtensions.indexOf(assetExtension) >= 0
? 'chunk'
: 'asset';
const emitConfig = type === 'chunk'
? { type, id: normalizePathnameForWindows(url), name }
: { type, name: assetName, source: assetContents };
const ref = this.emitFile(emitConfig);
// handle Windows style paths
const normalizedRelativeAssetPath = relativeAssetPath.replace(/\\/g, '/');
const importRef = `import.meta.ROLLUP_FILE_URL_${ref}`;
modifiedCode = code
.replace(`'${normalizedRelativeAssetPath}'`, importRef)
.replace(`"${normalizedRelativeAssetPath}"`, importRef);
}
return {
code: modifiedCode ? modifiedCode : code,
map: null
};
}
};
}
// TODO could we use this instead?
// https://github.com/rollup/rollup/blob/v2.79.1/docs/05-plugin-development.md#resolveimportmeta
// https://github.com/ProjectEvergreen/greenwood/issues/1087
function greenwoodPatchSsrPagesEntryPointRuntimeImport() {
return {
name: 'greenwood-patch-ssr-pages-entry-point-runtime-import',
generateBundle(options, bundle) {
Object.keys(bundle).forEach((key) => {
if (key.startsWith('__')) {
// ___GWD_ENTRY_FILE_URL=${filename}___
const needle = bundle[key].code.match(/___GWD_ENTRY_FILE_URL=(.*.)___/);
if (needle) {
const entryPathMatch = needle[1];
bundle[key].code = bundle[key].code.replace(/'___GWD_ENTRY_FILE_URL=(.*.)___'/, `new URL('./_${entryPathMatch}', import.meta.url)`);
} else {
console.warn(`Could not find entry path match for bundle => ${key}`);
}
}
});
}
};
}
const getRollupConfigForScriptResources = async (compilation) => {
const { outputDir } = compilation.context;
const input = [...compilation.resources.values()]
.filter(resource => resource.type === 'script')
.map(resource => normalizePathnameForWindows(resource.sourcePathURL));
const customRollupPlugins = compilation.config.plugins.filter(plugin => {
return plugin.type === 'rollup';
}).map(plugin => {
return plugin.provider(compilation);
}).flat();
return [{
preserveEntrySignatures: 'strict', // https://github.com/ProjectEvergreen/greenwood/pull/990
input,
output: {
dir: normalizePathnameForWindows(outputDir),
entryFileNames: '[name].[hash].js',
chunkFileNames: '[name].[hash].js',
sourcemap: true
},
plugins: [
greenwoodResourceLoader(compilation),
greenwoodSyncPageResourceBundlesPlugin(compilation),
greenwoodImportMetaUrl(compilation),
...customRollupPlugins
],
context: 'window',
onwarn: (errorObj) => {
const { code, message } = errorObj;
switch (code) {
case 'EMPTY_BUNDLE':
// since we use .html files as entry points
// we "ignore" them as bundles (see greenwoodHtmlPlugin#load hook)
// but don't want the logs to be noisy, so this suppresses those warnings
break;
case 'UNRESOLVED_IMPORT':
// this could be a legit warning for users, but...
if (process.env.__GWD_ROLLUP_MODE__ === 'strict') { // eslint-disable-line no-underscore-dangle
// if we see it happening in our tests / website build
// treat it as an error for us since it usually is...
// https://github.com/ProjectEvergreen/greenwood/issues/620
throw new Error(message);
} else {
// we should still log it so the user knows at least
console.debug(message);
}
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);
}
}
}];
};
const getRollupConfigForApis = async (compilation) => {
const { outputDir, userWorkspace } = compilation.context;
const input = [...compilation.manifest.apis.values()]
.map(api => normalizePathnameForWindows(new URL(`.${api.path}`, userWorkspace)));
// why is this needed?
await fs.promises.mkdir(new URL('./api/assets/', outputDir), {
recursive: true
});
// TODO should routes and APIs have chunks?
// https://github.com/ProjectEvergreen/greenwood/issues/1118
return [{
input,
output: {
dir: `${normalizePathnameForWindows(outputDir)}/api`,
entryFileNames: '[name].js',
chunkFileNames: '[name].[hash].js'
},
plugins: [
greenwoodJsonLoader(),
greenwoodResourceLoader(compilation),
nodeResolve(),
commonjs(),
greenwoodImportMetaUrl(compilation)
]
}];
};
const getRollupConfigForSsr = async (compilation, input) => {
const { outputDir } = compilation.context;
// TODO should routes and APIs have chunks?
// https://github.com/ProjectEvergreen/greenwood/issues/1118
return [{
input,
output: {
dir: normalizePathnameForWindows(outputDir),
entryFileNames: '_[name].js',
chunkFileNames: '[name].[hash].js'
},
plugins: [
greenwoodJsonLoader(),
greenwoodResourceLoader(compilation),
// TODO let this through for lit to enable nodeResolve({ preferBuiltins: true })
// https://github.com/lit/lit/issues/449
// https://github.com/ProjectEvergreen/greenwood/issues/1118
nodeResolve({
preferBuiltins: true
}),
commonjs(),
greenwoodImportMetaUrl(compilation),
greenwoodPatchSsrPagesEntryPointRuntimeImport() // TODO a little hacky but works for now
],
onwarn: (errorObj) => {
const { code, message } = errorObj;
switch (code) {
case 'CIRCULAR_DEPENDENCY':
// TODO let this through for lit by suppressing it
// Error: the string "Circular dependency: ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js ->
// ../../../../../node_modules/@lit-labs/ssr/lib/lit-element-renderer.js -> ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js\n" was thrown, throw an Error :)
// https://github.com/lit/lit/issues/449
// https://github.com/ProjectEvergreen/greenwood/issues/1118
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);
}
}
}];
};
export {
getRollupConfigForApis,
getRollupConfigForScriptResources,
getRollupConfigForSsr
};