-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
147 lines (142 loc) · 4.61 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
import {appendFile, copyFile} from 'node:fs/promises';
import {babel} from '@rollup/plugin-babel';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
// import nodeGlobals from 'rollup-plugin-node-globals';
import json from '@rollup/plugin-json';
// @ts-expect-error Missing types
import replace from 'rollup-plugin-re';
import terser from '@rollup/plugin-terser';
// @ts-expect-error Missing types
import postProcess from '@stadtlandnetz/rollup-plugin-postprocess';
// @ts-expect-error Missing types
import {importMetaAssets} from '@web/rollup-plugin-import-meta-assets';
/*
Because JsonRefs does not have an ESM version, and adding appears to be
difficult in that Rollup's ecosystem for polyfilling Node is apparently
less mature than for Webpack (though Webpack may now have ESM export),
we convert the global file to ESM which can be modularly imported by the
including files (yet safely tree-shaked, avoiding dupes).
*/
const jsonRefsTarget = new URL('resources/vendor/json-refs-min.js', import.meta.url);
await copyFile(
new URL('node_modules/json-refs/dist/json-refs-min.js', import.meta.url),
jsonRefsTarget
);
await appendFile(jsonRefsTarget, '\nexport default JsonRefs;');
const importerReplace = {
// Temporarily hide this from parser (using the Babel
// syntax plugin or experimental Rollup flag didn't
// seem to work)
include: ['resources/utils/WorkInfo.js', 'node_modules/simple-get-json/dist/index-polyglot.mjs'],
test: 'import(',
replace: 'window.xyz('
};
const importerRevert = [/window.xyz\(/, 'import('];
// Todo: Monitor https://github.com/rollup/rollup/issues/1978
// to suppress (known) circular dependency warnings
/**
* @external RollupConfig
* @type {object}
* @see {@link https://rollupjs.org/guide/en#big-list-of-options}
*/
/**
* @param {object} [config]
* @param {boolean} [config.minifying]
* @param {import('rollup').ModuleFormat} [config.format]
* @returns {import('rollup').RollupOptions}
*/
function getRollupObject ({minifying, format = 'umd'} = {}) {
const nonMinified = {
input: 'resources/index.js',
external: [
// Not used in browser
'node:path', 'node:stream', 'node:http', 'node:url', 'node:https',
'node:zlib', 'node:child_process', 'node:fs', 'node:buffer',
'node:util', 'node:net'
],
output: {
format,
file: `dist/index-${format}${minifying ? '.min' : ''}.js`,
name: 'TextBrowser' /* ,
globals: {
'json-refs': 'JsonRefs'
} */
},
plugins: [
// ... do replace before commonjs
replace({
// Switch to non-bundled ESM browser-friendly version
patterns: [{
include: ['resources/resultsDisplay.js'],
test: "import JsonRefs from 'json-refs';",
replace: "import JsonRefs from './vendor/json-refs-min.js';"
}, {
include: ['resources/utils/Metadata.js'],
test: "import JsonRefs from 'json-refs';",
replace: "import JsonRefs from '../vendor/json-refs-min.js';"
}, importerReplace]
}),
json(),
babel({
babelHelpers: 'bundled'
}),
importMetaAssets(),
// nodeGlobals(),
nodeResolve({
// exportConditions: ['module'],
mainFields: ['module']
}),
commonjs(),
postProcess([
importerRevert
])
]
};
if (minifying) {
nonMinified.plugins.push(terser());
}
return nonMinified;
}
// console.log('typeof', typeof getRollupObject); // Keep for ESLint when commenting out below
export default [
/**/
getRollupObject({minifying: false, format: 'es'}),
getRollupObject({minifying: true, format: 'es'}),
{
input: 'resources/utils/WorkInfo.js',
output: {
format: 'esm',
file: 'dist/WorkInfo-es.js',
name: 'WorkInfo'
},
plugins: [
// Switch to non-bundled ESM browser-friendly version
replace({
// ... do replace before commonjs
patterns: [importerReplace, {
include: ['resources/resultsDisplay.js'],
test: "import JsonRefs from 'json-refs';",
replace: "import JsonRefs from './vendor/json-refs-min.js';"
}, {
include: ['resources/utils/Metadata.js'],
test: "import JsonRefs from 'json-refs';",
replace: "import JsonRefs from '../vendor/json-refs-min.js';"
}]
}),
nodeResolve(),
commonjs(),
postProcess([
importerRevert
])
]
},
{
input: 'resources/activateCallback.js',
output: {
format: 'esm',
file: 'dist/activateCallback-es.js',
name: 'activateCallback'
}
}
];