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
245import path from 'node:path'
import { performance } from 'node:perf_hooks'
import os from 'node:os'
import fs from 'fs-extra'
import { execaSync } from 'execa'
import pc from 'picocolors'
import { createWorkerFork, cwd, getProjectPackageJson, meteorPackagePath } from './workers'
if (process.env.NODE_ENV !== 'production')
return
// Not in a project (publishing the package)
if (process.env.VITE_METEOR_DISABLED)
return
const pkg = getProjectPackageJson()
const meteorMainModule = pkg.meteor?.mainModule?.client
// Meteor packages to omit or replace the temporary build.
// Useful for other build-time packages that may conflict with Meteor-Vite's temporary build.
const replaceMeteorPackages = [
{ startsWith: 'standard-minifier', replaceWith: '' },
{ startsWith: 'refapp:meteor-typescript', replaceWith: 'typescript' },
...pkg?.meteorVite?.replacePackages || [],
]
if (!meteorMainModule)
throw new Error('No meteor main module found, please add meteor.mainModule.client to your package.json')
const tempDir = getTempDir()
const tempMeteorProject = path.resolve(tempDir, 'meteor')
const tempMeteorOutDir = path.join(tempDir, 'bundle', 'meteor')
const viteOutDir = path.join(tempDir, 'bundle', 'vite')
// Temporary Meteor build
const filesToCopy = [
path.join('.meteor', '.finished-upgraders'),
path.join('.meteor', '.id'),
path.join('.meteor', 'packages'),
path.join('.meteor', 'platforms'),
path.join('.meteor', 'release'),
path.join('.meteor', 'versions'),
'package.json',
meteorMainModule,
]
const optionalFiles = [
'tsconfig.json',
]
// Temporary Meteor build
console.log(pc.blue('⚡️ Building packages to make them available to export analyzer...'))
let startTime = performance.now()
// Check for project files that may be important if available
for (const file of optionalFiles) {
if (fs.existsSync(path.join(cwd, file)))
filesToCopy.push(file)
}
// Copy files from `.meteor`
for (const file of filesToCopy) {
const from = path.join(cwd, file)
const to = path.join(tempMeteorProject, file)
fs.ensureDirSync(path.dirname(to))
fs.copyFileSync(from, to)
}
// Symblink to `packages` folder
if (fs.existsSync(path.join(cwd, 'packages')) && !fs.existsSync(path.join(tempMeteorProject, 'packages')))
fs.symlinkSync(path.join(cwd, 'packages'), path.join(tempMeteorProject, 'packages'))
// Remove/replace conflicting Atmosphere packages
{
const file = path.join(tempMeteorProject, '.meteor', 'packages')
let content = fs.readFileSync(file, 'utf8')
for (const pack of replaceMeteorPackages) {
const lines = content.split('\n')
content = lines.map((line) => {
if (!line.startsWith(pack.startsWith))
return line
return pack.replaceWith || ''
}).join('\n')
}
fs.writeFileSync(file, content)
}
// Remove server entry
{
const file = path.join(tempMeteorProject, 'package.json')
const data = JSON.parse(fs.readFileSync(file, 'utf8'))
data.meteor = {
mainModule: {
client: data.meteor.mainModule.client,
},
}
fs.writeFileSync(file, JSON.stringify(data, null, 2))
}
// Only keep meteor package imports to enable lazy packages
{
const file = path.join(tempMeteorProject, meteorMainModule)
const lines = fs.readFileSync(file, 'utf8').split('\n')
const imports = lines.filter(line => line.startsWith('import') && line.includes('meteor/'))
fs.writeFileSync(file, imports.join('\n'))
}
execaSync('meteor', [
'build',
tempMeteorOutDir,
'--directory',
], {
cwd: tempMeteorProject,
// stdio: ['inherit', 'inherit', 'inherit'],
env: {
FORCE_COLOR: '3',
VITE_METEOR_DISABLED: 'true',
},
})
let endTime = performance.now()
console.log(pc.green(`⚡️ Packages built (${Math.round((endTime - startTime) * 100) / 100}ms)`))
// Vite
console.log(pc.blue('⚡️ Building with Vite...'))
startTime = performance.now()
fs.ensureDirSync(path.dirname(viteOutDir))
// Build with vite
const { payload } = Promise.await(new Promise((resolve) => {
const worker = createWorkerFork({
buildResult: result => resolve(result),
})
worker.call({
method: 'buildForProduction',
params: [{
viteOutDir,
packageJson: pkg,
meteor: {
packagePath: path.join(tempMeteorOutDir, 'bundle', 'programs', 'web.browser', 'packages'),
isopackPath: path.join(tempMeteorProject, '.meteor', 'local', 'isopacks'),
globalMeteorPackagesDir: meteorPackagePath,
},
}],
})
}))
if (payload.success) {
endTime = performance.now()
console.log(pc.green(`⚡️ Build successful (${Math.round((endTime - startTime) * 100) / 100}ms)`))
const entryAsset = payload.output.find(o => o.fileName === 'meteor-entry.js' && o.type === 'chunk')
if (!entryAsset)
throw new Error('No meteor-entry chunk found')
// Add assets to Meteor
// Copy the assets to the Meteor auto-imported sources
const viteOutSrcDir = path.join(cwd, 'client', 'vite')
fs.ensureDirSync(viteOutSrcDir)
fs.emptyDirSync(viteOutSrcDir)
const files = payload.output.map(o => o.fileName)
for (const file of files) {
const from = path.join(viteOutDir, file)
const to = path.join(viteOutSrcDir, file)
fs.ensureDirSync(path.dirname(to))
if (path.extname(from) === '.js') {
// Transpile to Meteor target (Dynamic import support)
// @TODO don't use Babel
const source = fs.readFileSync(from, 'utf8')
const babelOptions = Babel.getDefaultOptions()
babelOptions.babelrc = true
babelOptions.sourceMaps = true
babelOptions.filename = babelOptions.sourceFileName = from
const transpiled = Babel.compile(source, babelOptions, {
cacheDirectory: path.join(cwd, 'node_modules', '.babel-cache'),
})
fs.writeFileSync(to, transpiled.code, 'utf8')
}
else {
fs.copyFileSync(from, to)
}
}
// Patch meteor entry
const meteorEntry = path.join(cwd, meteorMainModule)
const originalEntryContent = fs.readFileSync(meteorEntry, 'utf8')
const patchedEntryContent = `import ${JSON.stringify(`./${path.relative(path.dirname(meteorEntry), path.join(viteOutSrcDir, entryAsset.fileName))}`)}\n${originalEntryContent}`
fs.writeFileSync(meteorEntry, patchedEntryContent, 'utf8')
class Compiler {
processFilesForTarget(files) {
files.forEach((file) => {
switch (path.extname(file.getBasename())) {
case '.js':
file.addJavaScript({
path: file.getPathInPackage(),
data: file.getContentsAsString(),
})
break
case '.css':
file.addStylesheet({
path: file.getPathInPackage(),
data: file.getContentsAsString(),
})
break
default:
file.addAsset({
path: file.getPathInPackage(),
data: file.getContentsAsBuffer(),
})
}
})
}
afterLink() {
fs.removeSync(viteOutSrcDir)
fs.writeFileSync(meteorEntry, originalEntryContent, 'utf8')
}
}
Plugin.registerCompiler({
extensions: [],
filenames: files.map(file => path.basename(file)),
}, () => new Compiler())
}
else {
throw new Error('Vite build failed')
}
function getTempDir() {
try {
const tempDir = path.resolve(pkg?.meteorVite?.tempDir || os.tmpdir(), 'meteor-vite', pkg.name)
fs.mkdirSync(tempDir, { recursive: true })
return tempDir
}
catch (error) {
console.warn(new Error(`⚡ Unable to set up temp directory for meteor-vite bundles. Will use node_modules instead`, { cause: error }))
return path.resolve(cwd, 'node_modules', '.vite-meteor-temp')
}
}