tldraw/apps/vscode/editor/scripts/dev.ts
2023-04-25 12:01:25 +01:00

71 lines
1.8 KiB
TypeScript

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 } })
esbuild.build({
entryPoints,
outfile: `${rootDir}/dist/index.js`,
minify: false,
bundle: true,
incremental: 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"',
},
watch: {
onRebuild(err) {
if (err) {
log({ cmd: 'esbuild:error', args: { err } })
} else {
copyEditor({ log })
log({ cmd: 'esbuild:success', args: {} })
}
},
},
})
} catch (error) {
log({ cmd: 'esbuild:error', args: { error } })
throw error
}
}
run()