-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbundle.ts
303 lines (260 loc) · 9.84 KB
/
bundle.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// this file is tested extensively to build scenes in the `make test` command
// but since it runs outiside the testing harness, coverage is not collected
import { Scene } from '@dcl/schemas'
import child_process from 'child_process'
import esbuild from 'esbuild'
import { future } from 'fp-future'
import { globSync } from 'glob'
import path from 'path'
import { pathToFileURL } from 'url'
import { CliComponents } from '../components'
import { colors } from '../components/log'
import { printProgressInfo, printProgressStep, printWarning } from './beautiful-logs'
import { CliError } from './error'
import { getAllComposites } from './composite'
import { isEditorScene } from './project-validations'
export type BundleComponents = Pick<CliComponents, 'logger' | 'fs'>
export type SceneJson = {
main: string
runtimeVersion: string
}
export type CompileOptions = {
production: boolean
watch: boolean
// build a single .ts file
single?: string
// build a project folder
workingDirectory: string
// emit typescript declarations
emitDeclaration: boolean
ignoreComposite: boolean
customEntryPoint: boolean
}
const MAX_STEP = 2
/**
* Generate the entry-point code for a given original entry-point
* @param entrypointPath - file to be imported as original entry point
* @param forceCustomExport
* @returns the Typescript code
*/
function getEntrypointCode(entrypointPath: string, forceCustomExport: boolean, isEditorScene: boolean = false) {
const unixEntrypointPath = entrypointPath.replace(/(\\)/g, '/')
if (forceCustomExport) return `;"use strict";export * from '${unixEntrypointPath}'`
return `// BEGIN AUTO GENERATED CODE "~sdk/scene-entrypoint"
"use strict";
import * as entrypoint from '${unixEntrypointPath}'
import { engine, NetworkEntity } from '@dcl/sdk/ecs'
import * as sdk from '@dcl/sdk'
import { compositeProvider } from '@dcl/sdk/composite-provider'
import { compositeFromLoader } from '~sdk/all-composites'
${
isEditorScene &&
`
import { syncEntity } from '@dcl/sdk/network'
import players from '@dcl/sdk/players'
import { initAssetPacks, setSyncEntity } from '@dcl/asset-packs/dist/scene-entrypoint'
initAssetPacks(engine, { syncEntity }, players)
// TODO: do we need to do this on runtime ?
// I think we have that information at build-time and we avoid to do evaluate this on the worker.
// Read composite.json or main.crdt => If that file has a NetowrkEntity import '@dcl/@sdk/network'
`
}
if ((entrypoint as any).main !== undefined) {
function _INTERNAL_startup_system() {
try {
const maybePromise = (entrypoint as any).main()
if (maybePromise && typeof maybePromise === 'object' && typeof (maybePromise as unknown as Promise<unknown>).then === 'function') {
maybePromise.catch(console.error)
}
} catch (e) {
console.error(e)
} finally {
engine.removeSystem(_INTERNAL_startup_system)
}
}
engine.addSystem(_INTERNAL_startup_system, Infinity)
}
export * from '@dcl/sdk'
export * from '${unixEntrypointPath}'
`
}
export async function bundleProject(components: BundleComponents, options: CompileOptions, sceneJson: Scene) {
const tsconfig = path.join(options.workingDirectory, 'tsconfig.json')
/* istanbul ignore if */
if (!options.single && !sceneJson.main) {
throw new CliError('scene.json .main must be present')
}
/* istanbul ignore if */
if ((sceneJson as any).runtimeVersion !== '7') {
throw new CliError('scene.json `"runtimeVersion": "7"` must be present')
}
/* istanbul ignore if */
if (!(await components.fs.fileExists(tsconfig))) {
throw new CliError(`File ${tsconfig} must exist to compile the Typescript project`)
}
const entrypointSource = options.single ?? 'src/index.ts'
const entrypoints = globSync(entrypointSource, { cwd: options.workingDirectory, absolute: true })
/* istanbul ignore if */
if (!entrypoints.length) throw new CliError(`There are no input files to build: ${entrypointSource}`)
// const output = !options.single ? sceneJson.main : options.single.replace(/\.ts$/, '.js')
// const outfile = path.join(options.workingDirectory, output)
const inputs: { entrypoint: string; outputFile: string }[] = options.single
? entrypoints.map((entrypoint) => ({ entrypoint, outputFile: entrypoint.replace(/\.ts$/, '.js') }))
: [{ entrypoint: entrypoints[0], outputFile: sceneJson.main }]
for (const input of inputs) {
await bundleSingleProject(components, {
...options,
tsconfig,
...input
})
}
return { sceneJson, inputs }
}
type SingleProjectOptions = CompileOptions & {
tsconfig: string
entrypoint: string
outputFile: string
}
export async function bundleSingleProject(components: BundleComponents, options: SingleProjectOptions) {
printProgressStep(components.logger, `Bundling file ${colors.bold(options.entrypoint)}`, 1, MAX_STEP)
const editorScene = await isEditorScene(components, options.workingDirectory)
const context = await esbuild.context({
bundle: true,
platform: 'browser',
format: 'cjs',
preserveSymlinks: false,
outfile: options.outputFile,
allowOverwrite: false,
sourcemap: options.production ? 'external' : 'inline',
minify: options.production,
minifyIdentifiers: options.production,
minifySyntax: options.production,
minifyWhitespace: options.production,
treeShaking: true,
metafile: true,
absWorkingDir: options.workingDirectory,
target: 'es2020',
external: ['~system/*', '@dcl/inspector', '@dcl/inspector/*' /* ban importing the inspector from the SDK */],
// convert filesystem paths into file:// to enable VSCode debugger
sourceRoot: options.production ? 'dcl:///' : pathToFileURL(path.dirname(options.outputFile)).toString(),
define: {
document: 'undefined',
window: 'undefined',
DEBUG: options.production ? 'false' : 'true',
'globalThis.DEBUG': options.production ? 'false' : 'true',
'process.env.NODE_ENV': JSON.stringify(options.production ? 'production' : 'development')
},
tsconfig: options.tsconfig,
supported: {
'import-assertions': false,
'import-meta': false,
'dynamic-import': false,
hashbang: false
},
logOverride: {
'import-is-undefined': 'silent'
},
plugins: [compositeLoader(components, options)],
stdin: {
contents: getEntrypointCode(options.entrypoint, options.customEntryPoint, editorScene),
resolveDir: path.dirname(options.entrypoint),
sourcefile: path.basename(options.entrypoint) + '.entry-point.ts',
loader: 'ts'
}
})
/* istanbul ignore if */
if (options.watch) {
await context.watch({})
printProgressInfo(components.logger, `Bundle saved ${colors.bold(options.outputFile)}`)
} else {
try {
await context.rebuild()
printProgressInfo(components.logger, `Bundle saved ${colors.bold(options.outputFile)}`)
} catch (err: any) {
/* istanbul ignore next */
throw new CliError(err.toString())
}
await context.dispose()
}
/* istanbul ignore next */
if (options.watch) printProgressInfo(components.logger, `The compiler is watching for changes`)
await runTypeChecker(components, options)
}
function runTypeChecker(components: BundleComponents, options: CompileOptions) {
const args = [
require.resolve('typescript/lib/tsc'),
'-p',
'tsconfig.json',
'--preserveWatchOutput',
options.emitDeclaration ? '--emitDeclarationOnly' : '--noEmit'
]
/* istanbul ignore if */
if (options.watch) args.push('--watch')
printProgressStep(components.logger, `Running type checker`, 2, MAX_STEP)
const ts = child_process.spawn(process.execPath, args, { env: process.env, cwd: options.workingDirectory })
const typeCheckerFuture = future<number>()
ts.on('close', (code) => {
/* istanbul ignore else */
if (code === 0) {
printProgressInfo(components.logger, `Type checking completed without errors`)
} else {
typeCheckerFuture.reject(new CliError(`Typechecker exited with code ${code}.`))
return
}
typeCheckerFuture.resolve(code)
})
ts.stdout.pipe(process.stdout)
ts.stderr.pipe(process.stderr)
/* istanbul ignore if */
if (options.watch) {
typeCheckerFuture.resolve(-1)
}
return typeCheckerFuture
}
function compositeLoader(components: BundleComponents, options: SingleProjectOptions): esbuild.Plugin {
let shouldReload = true
let contents = `export const compositeFromLoader = {}` // default exports nothing
let watchFiles: string[] = [] // no files to watch
let lastBuiltSuccessful = false
return {
name: 'composite-loader',
setup(build) {
build.onStart(() => {
shouldReload = true
})
build.onResolve({ filter: /~sdk\/all-composites/ }, (_args) => {
return {
namespace: 'sdk-composite',
path: 'all-composites'
}
})
build.onLoad({ filter: /.*/, namespace: 'sdk-composite' }, async (_) => {
if (shouldReload) {
if (!options.ignoreComposite) {
const data = await getAllComposites(
components,
// we pass the build.initialOptions.absWorkingDir to build projects with multiple roots at once
build.initialOptions.absWorkingDir ?? options.workingDirectory
)
contents = `export const compositeFromLoader = {${data.compositeLines.join(',')}}`
watchFiles = data.watchFiles
if (data.withErrors) {
printWarning(
components.logger,
'Some composites are not included because of errors while compiling them. There can be unexpected behavior in the scene, check the errors and try to fix them.'
)
} else if (!lastBuiltSuccessful) {
}
lastBuiltSuccessful = !data.withErrors
}
shouldReload = false
}
return {
loader: 'js',
contents,
watchFiles
}
})
}
}
}