tldraw/apps/vscode/editor/scripts/dev.ts

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-04-25 11:01:25 +00:00
import dotenv from 'dotenv'
import esbuild from 'esbuild'
import fs from 'fs'
import fse from 'fs-extra'
import path from 'path'
import { logEnv } from '../../vscode-script-utils/cli'
import { copyEditor } from '../../vscode-script-utils/helpers'
import { exists, getDirname } from '../../vscode-script-utils/path'
dotenv.config()
const rootDir = getDirname(import.meta.url, '../')
const log = logEnv('editor')
export async function run() {
try {
const targetFolder = `${rootDir}dist/`
if (await exists(targetFolder)) {
log({ cmd: 'remove', args: { target: targetFolder } })
await fs.promises.rm(targetFolder, { recursive: true })
}
await fs.promises.mkdir(targetFolder)
const topSource = `${rootDir}public`
const files = await fs.promises.readdir(topSource)
for (const file of files) {
const dest = targetFolder + path.basename(file)
const source = path.join(topSource, file)
log({ cmd: 'copy', args: { source, dest } })
await fse.copy(source, dest)
}
const entryPoints = [`${rootDir}src/index.tsx`]
log({ cmd: 'esbuild', args: { entryPoints } })
const builder = await esbuild.context({
2023-04-25 11:01:25 +00:00
entryPoints,
outfile: `${rootDir}/dist/index.js`,
minify: false,
bundle: true,
target: 'es6',
jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
loader: {
'.woff2': 'dataurl',
'.woff': 'dataurl',
'.svg': 'file',
'.png': 'file',
'.json': 'file',
},
define: {
'process.env.NODE_ENV': '"development"',
},
plugins: [
{
name: 'log-builds',
setup(build) {
build.onEnd((result) => {
if (result.errors.length) {
log({ cmd: 'esbuild:error', args: { err: result.errors } })
} else {
copyEditor({ log })
log({ cmd: 'esbuild:success', args: {} })
}
})
},
2023-04-25 11:01:25 +00:00
},
],
2023-04-25 11:01:25 +00:00
})
await builder.watch()
2023-04-25 11:01:25 +00:00
} catch (error) {
log({ cmd: 'esbuild:error', args: { error } })
throw error
}
}
run()