forked from Akryum/meteor-vite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtsup.config.ts
103 lines (91 loc) · 3.5 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
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
import Path from 'node:path';
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/server-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,
tsconfig: "tsconfig.build.json",
sourcemap: true,
format: 'esm',
esbuildPlugins: [
// fixBuildPluginCjsImports(),
meteorImportStubs({
'isobuild': () => `const PluginGlobal = Plugin; export { PluginGlobal as Plugin }`,
}),
],
noExternal: ['meteor/isobuild', 'meteor-vite']
}))
export const EsbuildPluginMeteorStubs = meteorImportStubs({
'meteor': (symbol) => `export const Meteor = ${symbol}?.Meteor || globalThis.Meteor`,
'mongo': (symbol) => `export const { Mongo } = ${symbol} || {}`,
'server-render': (symbol) => `export const { onPageLoad } = ${symbol} || {}`,
'webapp': (symbol) => [
`export const WebApp = ${symbol}?.WebApp || globalThis.WebApp`,
`export const WebAppInternals = ${symbol}?.WebAppInternals || globalThis?.WebAppInternals`,
].join('\n'),
});
const log = (...messages: unknown[]) => {
console.log(...messages.map((message) => {
if (typeof message === 'string') {
return pc.cyan(message);
}
return message;
}));
}
function fixBuildPluginCjsImports(): Plugin {
return {
name: 'fix-build-plugin-cjs-imports',
setup(build) {
build.onResolve({ filter: /^meteor-vite/ }, (args) => {
const parsed = Path.parse(args.path);
const packageRoot = parsed.dir;
const newPath = Path.join(packageRoot, 'dist', `${parsed.name}.js`);
log(`Rewriting external ${pc.yellow(packageRoot)} import for Meteor build plugin: ${pc.blue(args.path)} -> ${pc.green(newPath)}`);
return {
path: newPath,
external: true,
}
})
}
} satisfies Plugin;
}
function meteorImportStubs(packages: {
[key in string]: (symbol: string) => string;
}): Plugin {
const filter = /^meteor\//;
let stubId = 0;
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) => {
log(`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));
}
const stubSymbol = `PackageStub_${stubId++}`;
return {
contents: `
const ${stubSymbol} = globalThis.Package?.[${JSON.stringify(packageName)}];
${stubFunction(stubSymbol)}
`
}
})
}
} satisfies Plugin;
}