-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollupAll.js
267 lines (224 loc) · 9.63 KB
/
rollupAll.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
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { generateDTS } from '@typhonjs-build-test/esm-d-ts';
import { importsExternal } from '@typhonjs-build-test/rollup-plugin-pkg-imports';
import { getFileList } from '@typhonjs-utils/file-util';
import fs from 'fs-extra';
import { rollup } from 'rollup';
import { externalPathsNPM } from './.rollup/local/externalPathsNPM.js';
import { typhonjsRuntime } from './.rollup/local/index.js';
// Defines the node-resolve config.
const s_RESOLVE_CONFIG = {
browser: true,
dedupe: ['svelte', '@typhonjs-svelte/runtime-base']
};
// Defines potential output plugins to use conditionally if the .env file indicates the bundles should be
// minified / mangled.
const outputPlugins = [];
const external = [/^svelte/, /@typhonjs-svelte\/runtime-base\/*/, /@typhonjs-fvtt\/svelte\/*/];
// Defines whether source maps are generated / loaded from the .env file.
const sourcemap = true;
// GenerateDTS options -----------------------------------------------------------------------------------------------
// Provides naive search / replace of bundled declaration file rewriting the re-bundled definitions. This will alter
// the JSDoc comments and import symbols.
const dtsReplace = {
_svelte_fvtt_: '_typhonjs_fvtt_svelte_',
'#svelte-fvtt/': '@typhonjs-fvtt/svelte/',
'/\\/\\/ <reference.*\\/>': '' // Svelte v4 types currently add triple slash references.
};
// Rollup plugin options for generateDTS.
const dtsPluginOptions = { bundlePackageExports: true, dtsReplace };
// -------------------------------------------------------------------------------------------------------------------
/**
* Adds a getter for position after `get elementTarget()`. This is necessary to perform as a DTS replacement as
* Foundry defines a `position` property on Application.
*
* @type {string}
*/
const dtsReplacePositionGetter = ` get elementTarget(): HTMLElement;
/**
* Returns the TJSPosition instance.
*
* @returns {import('@typhonjs-svelte/runtime-base/svelte/store/position').TJSPosition} The TJSPosition instance.
*/
get position(): TJSPosition;
`;
// Application generateDTS options.
const applicationDTSOptions = {
dtsReplace: {
...dtsReplace,
'get elementTarget\\(\\): HTMLElement;': dtsReplacePositionGetter,
// Handle the case of removing all generics from the extension of SvelteApp by TJSDialog as it internally defines the loaded component.
'TJSDialog extends SvelteApplication<SvelteApp.Options<svelte.SvelteComponent<any, any, any>, SvelteApp.Context.AbstractExternal>>': 'TJSDialog extends SvelteApplication',
// The following replacements handle cases where JSDoc can't properly define generic extends clauses.
'declare class SvelteApp': 'declare class SvelteApp<Options extends SvelteApp.Options = SvelteApp.Options> extends Application<Options>',
// Make the base SvelteApp.OptionsCore extend the global Foundry ApplicationOptions.
'interface OptionsCore': 'interface OptionsCore extends Partial<ApplicationOptions>',
// Remove unused barrel import of `svelte`.
[`import \\* as svelte from 'svelte';`]: ''
},
rollupExternal: external
};
// -------------------------------------------------------------------------------------------------------------------
const rollupConfigs = [
{
input: {
input: 'src/animate/gsap/index.js',
external,
plugins: [
importsExternal(),
typhonjsRuntime({ exclude: [`@typhonjs-fvtt/svelte/animate/gsap`] }),
generateDTS.plugin(dtsPluginOptions)
]
},
output: {
file: '_dist/animate/gsap/index.js',
format: 'es',
generatedCode: { constBindings: true },
paths: externalPathsNPM,
plugins: outputPlugins,
sourcemap
}
},
{
input: {
input: 'src/application/index.js',
external,
plugins: [
importsExternal(),
typhonjsRuntime({ exclude: [`@typhonjs-fvtt/svelte/application`] }),
generateDTS.plugin(applicationDTSOptions)
]
},
output: {
file: '_dist/application/index.js',
format: 'es',
generatedCode: { constBindings: true },
paths: externalPathsNPM,
plugins: outputPlugins,
sourcemap
}
},
{
input: {
input: 'src/store/fvtt/document/index.ts',
external,
plugins: [
importsExternal(),
resolve(s_RESOLVE_CONFIG),
typescript({ tsconfig: './src/store/fvtt/document/tsconfig.json' }),
generateDTS.plugin({
...dtsPluginOptions
// TODO: Uncomment when `esm-d-ts` is updated to allow default filter + user filter to be combined.
// tsDiagnosticFilter: ({ message }) => !!message.includes(`Cannot find namespace 'fvtt'`)
})
]
},
output: {
file: '_dist/store/fvtt/document/index.js',
format: 'es',
generatedCode: { constBindings: true },
paths: externalPathsNPM,
plugins: outputPlugins,
sourcemap
}
},
{
input: {
input: 'src/store/fvtt/settings/index.js',
external,
plugins: [
importsExternal(),
resolve(s_RESOLVE_CONFIG),
generateDTS.plugin(dtsPluginOptions)
]
},
output: {
file: '_dist/store/fvtt/settings/index.js',
format: 'es',
generatedCode: { constBindings: true },
paths: externalPathsNPM,
plugins: outputPlugins,
sourcemap
}
},
{
input: {
input: 'src/store/fvtt/settings/world/index.js',
external,
plugins: [
importsExternal(),
resolve(s_RESOLVE_CONFIG),
generateDTS.plugin(dtsPluginOptions)
]
},
output: {
file: '_dist/store/fvtt/settings/world/index.js',
format: 'es',
generatedCode: { constBindings: true },
paths: externalPathsNPM,
plugins: outputPlugins,
sourcemap
}
}
];
for (const config of rollupConfigs)
{
console.log(`Generating bundle: ${config.input.input}`);
const bundle = await rollup(config.input);
await bundle.write(config.output);
await bundle.close();
}
// Application rewriting types / exports -----------------------------------------------------------------------------
// The manipulation below is to rewrite `SvelteApplication` as `SvelteApp` to be able to merge with the namespace
// `SvelteApp`. `SvelteApp` is then reexported as `SvelteApplication` in both the sub-path JS bundle and declarations.
{
// JS Bundle mods --------------
let applicationIndexJS = fs.readFileSync('./_dist/application/index.js', 'utf-8');
applicationIndexJS = applicationIndexJS.replaceAll('TJSDialogNS', 'TJSDialog');
applicationIndexJS = applicationIndexJS.replaceAll('SvelteAppNS', 'SvelteApp');
fs.writeFileSync('./_dist/application/index.js', applicationIndexJS);
// DTS Bundle mods -------------
let applicationIndexDTS = fs.readFileSync('./_dist/application/index.d.ts', 'utf-8');
// Remove merged class / namespace exports.
applicationIndexDTS = applicationIndexDTS.replace(
'export { SvelteApp, SvelteAppNS, SvelteApp as SvelteApplication, TJSDialog, TJSDialogNS };',
'export { SvelteApp, SvelteApp as SvelteApplication, TJSDialog };'
);
applicationIndexDTS = applicationIndexDTS.replaceAll('TJSDialogNS', 'TJSDialog');
applicationIndexDTS = applicationIndexDTS.replaceAll('SvelteAppNS', 'SvelteApp');
fs.writeFileSync('./_dist/application/index.d.ts', applicationIndexDTS);
}
// Copy components to dist -------------------------------------------------------------------------------------------
fs.emptyDirSync('./_dist/component');
fs.copySync('./src/component', './_dist/component');
const compFiles = await getFileList({ dir: './_dist/component', resolve: true, walk: true });
for (const compFile of compFiles)
{
let fileData = fs.readFileSync(compFile, 'utf-8').toString();
// Ignore any `{@link #runtime...}` enclosed references.
fileData = fileData.replaceAll(/(?<!\{@link\s*)#runtime\//g, '@typhonjs-svelte/runtime-base/');
fileData = fileData.replaceAll('#svelte-fvtt/', '@typhonjs-fvtt/svelte/');
fileData = fileData.replaceAll('\'#svelte', '\'svelte');
// For types
// fileData = fileData.replaceAll('_typhonjs_fvtt_svelte_', '_typhonjs_fvtt_runtime_svelte_');
fs.writeFileSync(compFile, fileData);
}
// GSAP plugin loading code is also bespoke and must be copied over.
fs.emptyDirSync('./_dist/animate/gsap/plugin');
fs.copySync('./src/animate/gsap/plugin', './_dist/animate/gsap/plugin');
const gsapFiles = await getFileList({ dir: './_dist/animate/gsap/plugin', resolve: true, walk: true });
for (const gsapFile of gsapFiles)
{
let fileData = fs.readFileSync(gsapFile, 'utf-8').toString();
// Ignore any `{@link #runtime...}` enclosed references.
fileData = fileData.replaceAll(/(?<!\{@link\s*)#runtime\//g, '@typhonjs-svelte/runtime-base/');
fileData = fileData.replaceAll('#svelte-fvtt/', '@typhonjs-fvtt/svelte/');
fileData = fileData.replaceAll('\'#svelte', '\'svelte');
// For types
// fileData = fileData.replaceAll('_typhonjs_fvtt_svelte_', '_typhonjs_fvtt_runtime_svelte_');
fs.writeFileSync(gsapFile, fileData);
}
// Svelte components
await generateDTS({ input: './_dist/component/application/index.js' });
await generateDTS({ input: './_dist/component/internal/index.js' });