forked from Akryum/meteor-vite
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtsup.config.ts
66 lines (58 loc) · 2.18 KB
/
tsup.config.ts
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
import pc from 'picocolors';
import { defineConfig, type Options } from 'tsup';
type Plugin = Required<Options>['esbuildPlugins'][number];
export default defineConfig(() => ({
name: 'jorgenvatle:vite',
entry: [
'./packages/vite/src/entry/package-runtime.ts',
'./packages/vite/src/entry/build-plugin.ts'
],
outDir: './packages/vite/dist',
skipNodeModulesBundle: true,
splitting: false,
target: 'es2022',
platform: 'node',
keepNames: false,
minify: false,
sourcemap: true,
format: 'esm',
noExternal: ['meteor'],
esbuildPlugins: [
EsbuildPluginMeteorStubs
]
}))
export const EsbuildPluginMeteorStubs = meteorImportStubs({
'meteor': () => 'export const Meteor = PackageStub.Meteor || globalThis.Meteor',
'isobuild': () => `const PluginGlobal = Plugin; export { PluginGlobal as Plugin }`,
'webapp': () => [
'export const WebApp = PackageStub.WebApp || globalThis.WebApp',
'export const WebAppInternals = PackageStub.WebAppInternals || globalThis.WebAppInternals',
].join('\n'),
})
function meteorImportStubs(packages: {
[key in string]: () => string;
}): Plugin {
const filter = /^meteor\//;
return {
name: 'meteor-import-stubs',
setup(build) {
build.onResolve({ filter }, (args) => {
return { path: args.path.replace(filter, '') , namespace: 'meteor' }
})
build.onLoad({ filter: /.*/, namespace: 'meteor' }, (args) => {
console.log(pc.cyan(`Stubbing Meteor package import: '${pc.green(args.path)}'`));
const [packageName] = args.path.split('/');
const stubFunction = packages[packageName];
if (!stubFunction) {
throw new Error('Meteor package is missing stubs: ' + pc.yellow(args.path));
}
return {
contents: `
const PackageStub = Package?.[${JSON.stringify(packageName)}] || {};
${stubFunction()}
`
}
})
}
} satisfies Plugin;
}