theatre/packages/playground/devEnv/servePlayground.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

import {existsSync, writeFileSync} from 'fs'
2021-06-18 13:05:06 +02:00
import path from 'path'
import {definedGlobals} from '../../../theatre/devEnv/buildUtils'
2021-06-18 13:05:06 +02:00
const playgroundDir = path.join(__dirname, '..')
const port = 8080
const playgroundIndexContent = `
/**
* This file is created automatically and won't be comitted to the repo.
* You can change the import statement and import your own playground code.
*
* Your own playground code should reside in './personal', which is a folder
* that won't be committed to the repo.
*
* The shared playgrounds which other contributors can use are in the './shared' folder,
* which are comitted to the repo.
*
* Happy playing!
* */
import './shared/r3f-rocket'
`
const playgroundEntry = path.join(playgroundDir, 'src/index.ts')
if (!existsSync(playgroundEntry)) {
writeFileSync(playgroundEntry, playgroundIndexContent, {encoding: 'utf-8'})
}
2021-06-18 13:05:06 +02:00
require('esbuild')
.serve(
{
port,
servedir: path.join(playgroundDir, 'src'),
},
{
entryPoints: [playgroundEntry],
2021-06-18 13:05:06 +02:00
target: ['firefox88'],
2021-09-18 21:43:29 +02:00
loader: {
'.png': 'file',
'.glb': 'file',
'.gltf': 'file',
'.svg': 'dataurl',
},
2021-06-18 13:05:06 +02:00
bundle: true,
sourcemap: true,
define: definedGlobals,
2021-06-18 13:05:06 +02:00
},
)
.then((server: unknown) => {
console.log('Playground running at', 'http://localhost:' + port)
2021-06-18 13:05:06 +02:00
})