2022-06-13 14:47:07 +02:00
|
|
|
import type {BuildOptions} from 'esbuild'
|
|
|
|
import esbuild from 'esbuild'
|
2022-07-25 10:37:34 +02:00
|
|
|
import {readdir, readFile, stat, writeFile} from 'fs/promises'
|
2022-06-13 14:47:07 +02:00
|
|
|
import {mapValues} from 'lodash-es'
|
2022-07-25 10:37:34 +02:00
|
|
|
import path from 'path'
|
2022-06-13 14:47:07 +02:00
|
|
|
import React from 'react'
|
|
|
|
import {renderToStaticMarkup} from 'react-dom/server'
|
2022-06-17 21:23:35 +02:00
|
|
|
import {ServerStyleSheet} from 'styled-components'
|
2022-07-25 10:37:34 +02:00
|
|
|
import {definedGlobals} from '../../../theatre/devEnv/definedGlobals'
|
|
|
|
import {createEsbuildLiveReloadTools} from './createEsbuildLiveReloadTools'
|
|
|
|
import {createProxyServer} from './createProxyServer'
|
2022-06-17 21:23:35 +02:00
|
|
|
import {PlaygroundPage} from './home/PlaygroundPage'
|
2022-06-15 20:23:26 +02:00
|
|
|
import {openForOS} from './openForOS'
|
|
|
|
import {tryMultiplePorts} from './tryMultiplePorts'
|
2022-06-13 14:47:07 +02:00
|
|
|
|
2022-06-13 15:09:32 +02:00
|
|
|
const playgroundDir = (folder: string) => path.join(__dirname, '..', folder)
|
|
|
|
const buildDir = playgroundDir('build')
|
2022-06-24 14:13:58 +02:00
|
|
|
const srcDir = playgroundDir('src')
|
2022-06-13 15:09:32 +02:00
|
|
|
const sharedDir = playgroundDir('src/shared')
|
|
|
|
const personalDir = playgroundDir('src/personal')
|
|
|
|
const testDir = playgroundDir('src/tests')
|
2022-06-13 14:47:07 +02:00
|
|
|
|
2023-02-07 18:50:57 +01:00
|
|
|
async function start(options: {
|
2022-07-25 10:37:34 +02:00
|
|
|
/** enable live reload and watching stuff */
|
2022-06-15 20:23:26 +02:00
|
|
|
dev: boolean
|
2022-08-02 15:11:19 +02:00
|
|
|
/** make some UI elements predictable by setting the __IS_VISUAL_REGRESSION_TESTING value on window */
|
|
|
|
isVisualRegressionTesting: boolean
|
2022-07-25 10:37:34 +02:00
|
|
|
serve?: {
|
|
|
|
findAvailablePort: boolean
|
|
|
|
openBrowser: boolean
|
|
|
|
/** defaults to 8080 */
|
|
|
|
defaultPort?: number
|
|
|
|
}
|
2023-02-07 18:50:57 +01:00
|
|
|
}): Promise<void> {
|
2022-07-25 10:37:34 +02:00
|
|
|
const defaultPort = options.serve?.defaultPort ?? 8080
|
2022-06-15 20:23:26 +02:00
|
|
|
|
2022-07-25 10:37:34 +02:00
|
|
|
const liveReload =
|
|
|
|
options.serve && options.dev ? createEsbuildLiveReloadTools() : undefined
|
2022-06-15 20:23:26 +02:00
|
|
|
|
2022-06-24 14:13:58 +02:00
|
|
|
type PlaygroundExample = {
|
|
|
|
useHtml?: string
|
|
|
|
entryFilePath: string
|
|
|
|
outDir: string
|
|
|
|
}
|
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
type Groups = {
|
|
|
|
[group: string]: {
|
2022-06-24 14:13:58 +02:00
|
|
|
[module: string]: PlaygroundExample
|
2022-06-13 14:47:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
// Collect all entry directories per module per group
|
2022-07-25 10:37:34 +02:00
|
|
|
const groups: Groups = await Promise.all(
|
2023-02-07 18:50:57 +01:00
|
|
|
[sharedDir, personalDir, testDir].map(async (groupDir) => {
|
|
|
|
let groupDirItems: string[]
|
|
|
|
|
|
|
|
try {
|
|
|
|
groupDirItems = await readdir(groupDir)
|
|
|
|
} catch (error) {
|
|
|
|
// If the group dir doesn't exist, we just set its entry to undefined
|
|
|
|
return [path.basename(groupDir), undefined]
|
|
|
|
}
|
|
|
|
|
|
|
|
const allEntries = await Promise.all(
|
|
|
|
groupDirItems.map(
|
|
|
|
async (
|
|
|
|
moduleDirName,
|
|
|
|
): Promise<[string, PlaygroundExample | undefined]> => {
|
|
|
|
const playgroundKey = path.basename(moduleDirName)
|
|
|
|
const entryFilePath = path.join(
|
|
|
|
groupDir,
|
|
|
|
moduleDirName,
|
|
|
|
'index.tsx',
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
!(await stat(entryFilePath)
|
|
|
|
.then((s) => s.isFile())
|
|
|
|
.catch(() => false))
|
|
|
|
)
|
|
|
|
return [playgroundKey, undefined]
|
|
|
|
|
|
|
|
const playgroundExample = {
|
|
|
|
useHtml: await readFile(
|
|
|
|
path.join(groupDir, moduleDirName, 'index.html'),
|
|
|
|
'utf-8',
|
|
|
|
).catch(() => undefined),
|
|
|
|
entryFilePath,
|
|
|
|
outDir: path.join(
|
|
|
|
buildDir,
|
|
|
|
path.basename(groupDir),
|
2022-07-25 10:37:34 +02:00
|
|
|
moduleDirName,
|
2023-02-07 18:50:57 +01:00
|
|
|
),
|
|
|
|
}
|
2022-06-24 14:13:58 +02:00
|
|
|
|
2023-02-07 18:50:57 +01:00
|
|
|
return [playgroundKey, playgroundExample]
|
|
|
|
},
|
2022-07-25 10:37:34 +02:00
|
|
|
),
|
2023-02-07 18:50:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const validEntries = allEntries.filter(
|
|
|
|
([_, playgroundExample]) => playgroundExample !== undefined,
|
|
|
|
)
|
|
|
|
|
|
|
|
return [path.basename(groupDir), Object.fromEntries(validEntries)]
|
|
|
|
}),
|
|
|
|
).then((entries) =>
|
|
|
|
Object.fromEntries(
|
|
|
|
// and then filter it out.
|
|
|
|
entries.filter((entry) => entry[1] !== undefined),
|
2022-07-25 10:37:34 +02:00
|
|
|
),
|
2022-06-15 20:23:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Collect all entry files
|
|
|
|
const entryPoints = Object.values(groups)
|
|
|
|
.flatMap((group) => Object.values(group))
|
2022-06-24 14:13:58 +02:00
|
|
|
.map((module) => module.entryFilePath)
|
2022-06-15 20:23:26 +02:00
|
|
|
|
|
|
|
// Collect all output directories
|
2023-02-07 18:50:57 +01:00
|
|
|
const outModules: PlaygroundExample[] = Object.values(groups).flatMap(
|
|
|
|
(group) => Object.values(group),
|
2022-06-15 20:23:26 +02:00
|
|
|
)
|
|
|
|
|
2022-06-17 21:23:35 +02:00
|
|
|
// Render home page contents
|
|
|
|
const homeHtml = (() => {
|
|
|
|
const sheet = new ServerStyleSheet()
|
|
|
|
try {
|
|
|
|
const html = renderToStaticMarkup(
|
|
|
|
sheet.collectStyles(
|
|
|
|
React.createElement(PlaygroundPage, {
|
|
|
|
groups: mapValues(groups, (group) => Object.keys(group)),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
const styleTags = sheet.getStyleTags() // or sheet.getStyleElement();
|
|
|
|
sheet.seal()
|
|
|
|
return {
|
|
|
|
head: styleTags,
|
|
|
|
html,
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// handle error
|
|
|
|
console.error(error)
|
|
|
|
sheet.seal()
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})()
|
2022-06-15 20:23:26 +02:00
|
|
|
|
|
|
|
const esbuildConfig: BuildOptions = {
|
|
|
|
entryPoints,
|
|
|
|
bundle: true,
|
|
|
|
sourcemap: true,
|
|
|
|
outdir: buildDir,
|
|
|
|
target: ['firefox88'],
|
|
|
|
loader: {
|
|
|
|
'.png': 'file',
|
|
|
|
'.glb': 'file',
|
|
|
|
'.gltf': 'file',
|
2022-09-27 23:23:59 +02:00
|
|
|
'.mp3': 'file',
|
|
|
|
'.ogg': 'file',
|
2022-06-15 20:23:26 +02:00
|
|
|
'.svg': 'dataurl',
|
|
|
|
},
|
|
|
|
define: {
|
|
|
|
...definedGlobals,
|
2022-08-02 15:11:19 +02:00
|
|
|
'window.__IS_VISUAL_REGRESSION_TESTING': JSON.stringify(
|
|
|
|
options.isVisualRegressionTesting,
|
|
|
|
),
|
2023-02-06 12:40:53 +01:00
|
|
|
'process.env.BUILT_FOR_PLAYGROUND': JSON.stringify('true'),
|
2022-06-15 20:23:26 +02:00
|
|
|
},
|
|
|
|
banner: liveReload?.esbuildBanner,
|
2023-02-07 18:50:57 +01:00
|
|
|
// watch: liveReload?.esbuildWatch && {
|
|
|
|
// onRebuild(error, result) {
|
|
|
|
// esbuildWatchStop = result?.stop ?? esbuildWatchStop
|
|
|
|
// liveReload?.esbuildWatch.onRebuild?.(error, result)
|
|
|
|
// },
|
|
|
|
// },
|
2022-06-24 14:21:18 +02:00
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
name: 'watch playground assets',
|
|
|
|
setup(build) {
|
|
|
|
build.onStart(() => {})
|
|
|
|
build.onLoad(
|
|
|
|
{
|
|
|
|
filter: /index\.tsx?$/,
|
|
|
|
},
|
2022-07-25 10:37:34 +02:00
|
|
|
async (loadFile) => {
|
2022-06-24 14:21:18 +02:00
|
|
|
const indexHtmlPath = loadFile.path.replace(
|
|
|
|
/index\.tsx?$/,
|
|
|
|
'index.html',
|
|
|
|
)
|
|
|
|
const relToSrc = path.relative(srcDir, indexHtmlPath)
|
|
|
|
const isInSrcFolder = !relToSrc.startsWith('..')
|
|
|
|
if (isInSrcFolder) {
|
2022-07-25 10:37:34 +02:00
|
|
|
const newHtml = await readFile(indexHtmlPath, 'utf-8').catch(
|
|
|
|
() => undefined,
|
2022-06-24 14:21:18 +02:00
|
|
|
)
|
|
|
|
if (newHtml) {
|
2022-07-25 10:37:34 +02:00
|
|
|
await writeFile(
|
2022-06-24 14:21:18 +02:00
|
|
|
path.resolve(buildDir, relToSrc),
|
|
|
|
newHtml.replace(
|
|
|
|
/<\/body>/,
|
|
|
|
`<script src="${path.join(
|
|
|
|
'/',
|
|
|
|
relToSrc,
|
|
|
|
'../index.js',
|
|
|
|
)}"></script></body>`,
|
|
|
|
),
|
2022-07-25 10:37:34 +02:00
|
|
|
).catch(
|
|
|
|
wrapCatch(
|
|
|
|
`loading index.tsx creates corresponding index.html for ${relToSrc}`,
|
|
|
|
),
|
2022-06-24 14:21:18 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
watchFiles: [indexHtmlPath],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-06-15 20:23:26 +02:00
|
|
|
}
|
2022-06-13 14:47:07 +02:00
|
|
|
|
2023-02-07 18:50:57 +01:00
|
|
|
const ctx = await esbuild.context(esbuildConfig)
|
|
|
|
|
|
|
|
if (liveReload) {
|
|
|
|
await ctx.watch()
|
|
|
|
} else {
|
|
|
|
await ctx.rebuild()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read index.html template
|
|
|
|
const index = await readFile(
|
|
|
|
path.join(__dirname, 'index.html'),
|
|
|
|
'utf8',
|
|
|
|
).catch(wrapCatch('reading index.html template'))
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
// Write home page
|
|
|
|
writeFile(
|
|
|
|
path.join(buildDir, 'index.html'),
|
|
|
|
index
|
|
|
|
.replace(/<\/head>/, `${homeHtml.head}<\/head>`)
|
|
|
|
.replace(/<body>/, `<body>${homeHtml.html}`),
|
|
|
|
'utf-8',
|
|
|
|
).catch(wrapCatch('writing build index.html')),
|
|
|
|
// Write module pages
|
|
|
|
...outModules.map((outModule) =>
|
|
|
|
writeFile(
|
|
|
|
path.join(outModule.outDir, 'index.html'),
|
|
|
|
// Insert the script
|
|
|
|
(outModule.useHtml ?? index).replace(
|
|
|
|
/<\/body>/,
|
|
|
|
`<script src="${path.join(
|
|
|
|
'/',
|
|
|
|
path.relative(buildDir, outModule.outDir),
|
|
|
|
'index.js',
|
|
|
|
)}"></script></body>`,
|
|
|
|
),
|
|
|
|
'utf-8',
|
|
|
|
).catch(
|
|
|
|
wrapCatch(
|
|
|
|
`writing index.html for ${path.relative(buildDir, outModule.outDir)}`,
|
2022-06-13 15:09:32 +02:00
|
|
|
),
|
2023-02-07 18:50:57 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
])
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-07-25 10:37:34 +02:00
|
|
|
// Only start dev server in serve, otherwise just run build and that's it
|
|
|
|
if (!options.serve) {
|
2023-02-07 18:50:57 +01:00
|
|
|
await ctx.dispose()
|
|
|
|
return
|
2022-06-15 20:23:26 +02:00
|
|
|
}
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-07-25 10:37:34 +02:00
|
|
|
const {serve} = options
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
// We start ESBuild serve with no build config because it doesn't need to build
|
|
|
|
// anything, we are already using ESBuild watch.
|
|
|
|
/** See https://esbuild.github.io/api/#serve-return-values */
|
2023-02-07 18:50:57 +01:00
|
|
|
const esbuildServe = await ctx.serve({servedir: buildDir})
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
const proxyServer = createProxyServer(liveReload?.handleRequest, {
|
|
|
|
hostname: '0.0.0.0',
|
|
|
|
port: esbuildServe.port,
|
2022-06-13 15:09:32 +02:00
|
|
|
})
|
|
|
|
|
2023-02-07 18:50:57 +01:00
|
|
|
// const proxyForceExit = createServerForceClose(proxyServer)
|
2022-07-25 10:37:34 +02:00
|
|
|
const portTries = serve.findAvailablePort ? 10 : 1
|
2022-06-15 20:23:26 +02:00
|
|
|
const portChosen = await tryMultiplePorts(defaultPort, portTries, proxyServer)
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
const hostedAt = `http://localhost:${portChosen}`
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
console.log('Playground running at', hostedAt)
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-07-25 10:37:34 +02:00
|
|
|
if (serve.openBrowser) {
|
2022-06-15 20:23:26 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
if (!liveReload?.hasOpenConnections()) openForOS(hostedAt)
|
|
|
|
}, 1000)
|
2022-06-13 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
2023-02-07 18:50:57 +01:00
|
|
|
// return {
|
|
|
|
// async stop() {
|
|
|
|
// esbuildWatchStop?.()
|
|
|
|
// await proxyForceExit()
|
|
|
|
// },
|
|
|
|
// }
|
2022-06-13 15:09:32 +02:00
|
|
|
}
|
2022-06-24 14:13:58 +02:00
|
|
|
|
2022-07-25 10:37:34 +02:00
|
|
|
function wrapCatch(message: string) {
|
|
|
|
return (err: any) => {
|
|
|
|
return Promise.reject(`Rejected "${message}":\n ${err.toString()}`)
|
2022-06-24 14:13:58 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 18:50:57 +01:00
|
|
|
|
|
|
|
const dev = process.argv.find((arg) => ['--dev', '-d'].includes(arg)) != null
|
|
|
|
|
|
|
|
const serve =
|
|
|
|
process.argv.find((arg) => ['--serve'].includes(arg)) != null || undefined
|
|
|
|
|
|
|
|
const isCI = Boolean(process.env.CI)
|
|
|
|
|
|
|
|
start({
|
|
|
|
dev: !isCI && dev,
|
|
|
|
isVisualRegressionTesting: isCI,
|
|
|
|
serve: serve && {
|
|
|
|
findAvailablePort: !isCI,
|
|
|
|
// If not in CI, try to spawn a browser
|
|
|
|
openBrowser: !isCI,
|
|
|
|
// waitBeforeStartingServer: current?.stop(),
|
|
|
|
},
|
|
|
|
}).then(
|
|
|
|
() => {},
|
|
|
|
(err) => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
},
|
|
|
|
)
|