2022-06-24 14:13:58 +02:00
|
|
|
import {readdirSync, readFileSync, statSync} from 'fs'
|
2022-06-13 14:47:07 +02:00
|
|
|
import {writeFile, readFile} from 'fs/promises'
|
|
|
|
import path from 'path'
|
|
|
|
import type {BuildOptions} from 'esbuild'
|
|
|
|
import esbuild from 'esbuild'
|
2022-06-15 20:23:26 +02:00
|
|
|
import {definedGlobals} from '../../../theatre/devEnv/definedGlobals'
|
2022-06-13 14:47:07 +02:00
|
|
|
import {mapValues} from 'lodash-es'
|
|
|
|
import React from 'react'
|
|
|
|
import {renderToStaticMarkup} from 'react-dom/server'
|
2022-06-17 21:23:35 +02:00
|
|
|
import {ServerStyleSheet} from 'styled-components'
|
|
|
|
import {PlaygroundPage} from './home/PlaygroundPage'
|
2022-06-15 20:23:26 +02:00
|
|
|
import {timer} from './timer'
|
|
|
|
import {openForOS} from './openForOS'
|
|
|
|
import {tryMultiplePorts} from './tryMultiplePorts'
|
|
|
|
import {createProxyServer} from './createProxyServer'
|
|
|
|
import {createEsbuildLiveReloadTools} from './createEsbuildLiveReloadTools'
|
|
|
|
import {createServerForceClose} from './createServerForceClose'
|
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
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
export async function start(options: {
|
|
|
|
dev: boolean
|
|
|
|
findAvailablePort: boolean
|
|
|
|
openBrowser: boolean
|
|
|
|
waitBeforeStartingServer?: Promise<void>
|
|
|
|
/** defaults to 8080 */
|
|
|
|
defaultPort?: number
|
|
|
|
}): Promise<{stop(): Promise<void>}> {
|
|
|
|
const defaultPort = options.defaultPort ?? 8080
|
|
|
|
|
|
|
|
const liveReload = options.dev ? createEsbuildLiveReloadTools() : undefined
|
|
|
|
|
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
|
|
|
|
const groups: Groups = Object.fromEntries(
|
|
|
|
[sharedDir, personalDir, testDir]
|
|
|
|
.map((groupDir) => {
|
|
|
|
try {
|
|
|
|
return [
|
|
|
|
path.basename(groupDir),
|
|
|
|
Object.fromEntries(
|
2022-06-24 14:13:58 +02:00
|
|
|
readdirSync(groupDir)
|
|
|
|
.map(
|
|
|
|
(moduleDirName): [string, PlaygroundExample | undefined] => {
|
|
|
|
const entryKey = path.basename(moduleDirName)
|
|
|
|
const entryFilePath = path.join(
|
|
|
|
groupDir,
|
|
|
|
moduleDirName,
|
|
|
|
'index.tsx',
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!tryOrUndefined(() => statSync(entryFilePath).isFile()))
|
|
|
|
return [entryKey, undefined]
|
|
|
|
|
|
|
|
return [
|
|
|
|
entryKey,
|
|
|
|
{
|
|
|
|
// Including your own html file for playground is an experimental feature,
|
|
|
|
// it's not quite ready for "prime time" and advertising to the masses until
|
|
|
|
// it properly handles file watching.
|
|
|
|
// It's good for now, since we can use it for some demos, just make sure that
|
|
|
|
// you add a comment to the custom index.html file saying that you have to
|
|
|
|
// restart playground server entirely to see changes.
|
|
|
|
useHtml: tryOrUndefined(() =>
|
|
|
|
readFileSync(
|
|
|
|
path.join(groupDir, moduleDirName, 'index.html'),
|
|
|
|
'utf-8',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
entryFilePath,
|
|
|
|
outDir: path.join(
|
|
|
|
buildDir,
|
|
|
|
path.basename(groupDir),
|
|
|
|
moduleDirName,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.filter((entry) => entry[1] !== undefined),
|
2022-06-15 20:23:26 +02:00
|
|
|
),
|
|
|
|
]
|
|
|
|
} catch (e) {
|
|
|
|
// If the group dir doesn't exist, we just set its entry to undefined
|
|
|
|
return [path.basename(groupDir), undefined]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// and then filter it out.
|
|
|
|
.filter((entry) => entry[1] !== undefined),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-06-24 14:13:58 +02:00
|
|
|
const outModules = 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 _initialBuild = timer('esbuild initial playground entry point builds')
|
|
|
|
|
|
|
|
const esbuildConfig: BuildOptions = {
|
|
|
|
entryPoints,
|
|
|
|
bundle: true,
|
|
|
|
sourcemap: true,
|
|
|
|
outdir: buildDir,
|
|
|
|
target: ['firefox88'],
|
|
|
|
loader: {
|
|
|
|
'.png': 'file',
|
|
|
|
'.glb': 'file',
|
|
|
|
'.gltf': 'file',
|
|
|
|
'.svg': 'dataurl',
|
|
|
|
},
|
|
|
|
define: {
|
|
|
|
...definedGlobals,
|
|
|
|
'window.__IS_VISUAL_REGRESSION_TESTING': 'true',
|
|
|
|
},
|
|
|
|
banner: liveReload?.esbuildBanner,
|
2022-06-17 21:23:35 +02:00
|
|
|
watch: liveReload?.esbuildWatch && {
|
|
|
|
onRebuild(error, result) {
|
|
|
|
esbuildWatchStop = result?.stop ?? esbuildWatchStop
|
|
|
|
liveReload?.esbuildWatch.onRebuild?.(error, result)
|
|
|
|
},
|
|
|
|
},
|
2022-06-15 20:23:26 +02:00
|
|
|
}
|
2022-06-13 14:47:07 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
let esbuildWatchStop: undefined | (() => void)
|
2022-06-13 14:47:07 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
await esbuild
|
|
|
|
.build(esbuildConfig)
|
|
|
|
.finally(() => _initialBuild.stop())
|
|
|
|
.catch((err) => {
|
|
|
|
// if in dev mode, permit continuing to watch even if there was an error
|
|
|
|
return options.dev ? Promise.resolve() : Promise.reject(err)
|
|
|
|
})
|
|
|
|
.then(async (buildResult) => {
|
|
|
|
esbuildWatchStop = buildResult?.stop
|
|
|
|
// Read index.html template
|
|
|
|
const index = await readFile(path.join(__dirname, 'index.html'), 'utf8')
|
|
|
|
await Promise.all([
|
|
|
|
// Write home page
|
2022-06-13 14:47:07 +02:00
|
|
|
writeFile(
|
2022-06-15 20:23:26 +02:00
|
|
|
path.join(buildDir, 'index.html'),
|
2022-06-17 21:23:35 +02:00
|
|
|
index
|
|
|
|
.replace(/<\/head>/, `${homeHtml.head}<\/head>`)
|
|
|
|
.replace(/<body>/, `<body>${homeHtml.html}`),
|
2022-06-15 20:23:26 +02:00
|
|
|
'utf-8',
|
2022-06-13 14:47:07 +02:00
|
|
|
),
|
2022-06-15 20:23:26 +02:00
|
|
|
// Write module pages
|
2022-06-24 14:13:58 +02:00
|
|
|
...outModules.map((outModule) =>
|
2022-06-15 20:23:26 +02:00
|
|
|
writeFile(
|
2022-06-24 14:13:58 +02:00
|
|
|
path.join(outModule.outDir, 'index.html'),
|
2022-06-17 21:23:35 +02:00
|
|
|
// Insert the script
|
2022-06-24 14:13:58 +02:00
|
|
|
(outModule.useHtml ?? index).replace(
|
2022-06-17 21:23:35 +02:00
|
|
|
/<\/body>/,
|
|
|
|
`<script src="${path.join(
|
|
|
|
'/',
|
2022-06-24 14:13:58 +02:00
|
|
|
path.relative(buildDir, outModule.outDir),
|
2022-06-17 21:23:35 +02:00
|
|
|
'index.js',
|
|
|
|
)}"></script></body>`,
|
2022-06-15 20:23:26 +02:00
|
|
|
),
|
|
|
|
'utf-8',
|
|
|
|
),
|
2022-06-13 15:09:32 +02:00
|
|
|
),
|
2022-06-15 20:23:26 +02:00
|
|
|
])
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
return process.exit(1)
|
2022-06-13 15:09:32 +02:00
|
|
|
})
|
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
// Only start dev server in dev, otherwise just run build and that's it
|
|
|
|
if (!options.dev) {
|
|
|
|
return {
|
|
|
|
stop() {
|
|
|
|
esbuildWatchStop?.()
|
|
|
|
return Promise.resolve()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 15:09:32 +02:00
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
await options.waitBeforeStartingServer
|
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 */
|
|
|
|
const esbuildServe = await esbuild.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
|
|
|
})
|
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
const proxyForceExit = createServerForceClose(proxyServer)
|
|
|
|
const portTries = options.findAvailablePort ? 10 : 1
|
|
|
|
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-06-15 20:23:26 +02:00
|
|
|
if (options.openBrowser) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!liveReload?.hasOpenConnections()) openForOS(hostedAt)
|
|
|
|
}, 1000)
|
2022-06-13 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
2022-06-15 20:23:26 +02:00
|
|
|
return {
|
|
|
|
stop() {
|
|
|
|
esbuildServe.stop()
|
|
|
|
esbuildWatchStop?.()
|
|
|
|
return Promise.all([proxyForceExit(), esbuildServe.wait]).then(() => {
|
|
|
|
// map to void for type defs
|
|
|
|
})
|
|
|
|
},
|
2022-06-13 15:09:32 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 14:13:58 +02:00
|
|
|
|
|
|
|
function tryOrUndefined<T>(fn: () => T): T | undefined {
|
|
|
|
try {
|
|
|
|
return fn()
|
|
|
|
} catch (err) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|