Skip to content

Commit

Permalink
refactor: align core-app structure and start-up more closely with studio
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Jan 28, 2025
1 parent 0fe3b4f commit 324407c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const devCommand: CliCommandDefinition = {
const {workDir, cliConfig} = context

// if not studio app, skip all Studio-specific initialization
if (!(cliConfig && 'isStudioApp' in cliConfig)) {
if (cliConfig && 'isStudioApp' in cliConfig && cliConfig.isStudioApp === false) {
// non-studio apps were not possible in v2
const config = cliConfig as CliConfig | undefined
return startDevServer({
Expand Down
5 changes: 1 addition & 4 deletions packages/sanity/src/_internal/cli/server/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export async function startDevServer(options: DevServerOptions): Promise<DevServ

const startTime = Date.now()

debug('Writing Sanity runtime files')
await writeSanityRuntime({cwd, reactStrictMode, watch: true, basePath, isStudioApp})

debug('Resolving vite config')
let viteConfig = await getViteConfig({
basePath,
Expand All @@ -55,7 +52,7 @@ export async function startDevServer(options: DevServerOptions): Promise<DevServ

if (isStudioApp) {
debug('Writing Sanity runtime files')
await writeSanityRuntime({cwd, reactStrictMode, watch: true, basePath})
await writeSanityRuntime({cwd, reactStrictMode, watch: true, basePath, isStudioApp})
} else {
// For non-Studio apps, we need to set the entry point
viteConfig = {
Expand Down
62 changes: 26 additions & 36 deletions packages/sanity/src/_internal/cli/server/getViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,28 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
basePath: rawBasePath = '/',
importMap,
reactCompiler,
isStudioApp = true, // default to true for backwards compatibility
isStudioApp = true, // for backwards compatibility
} = options

// we may eventually load an example sdk app in the monorepo, but there's none right now
const monorepo = isStudioApp ? await loadSanityMonorepo(cwd) : undefined
const monorepo = await loadSanityMonorepo(cwd)
const basePath = normalizeBasePath(rawBasePath)
const runtimeDir = path.join(cwd, '.sanity', 'runtime')

const sanityPkgPath = isStudioApp ? (await readPkgUp({cwd: __dirname}))?.path : null
if (isStudioApp && !sanityPkgPath) {
const sanityPkgPath = (await readPkgUp({cwd: __dirname}))?.path
if (!sanityPkgPath) {
throw new Error('Unable to resolve `sanity` module root')
}

const customFaviconsPath = path.join(cwd, 'static')
const defaultFaviconsPath = path.join(path.dirname(sanityPkgPath), 'static', 'favicons')
const staticPath = `${basePath}static`

const {default: viteReact} = await import('@vitejs/plugin-react')
const viteConfig: InlineConfig = {
root: runtimeDir,
base: basePath,
// Define a custom cache directory so that sanity's vite cache
// does not conflict with any potential local vite projects
cacheDir: 'node_modules/.sanity/vite',
root: cwd,
base: basePath,
build: {
outDir: outputDir || path.resolve(cwd, 'dist'),
sourcemap: sourceMap,
Expand All @@ -103,39 +107,22 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
configFile: false,
mode,
plugins: [
(await import('@vitejs/plugin-react')).default(
viteReact(
reactCompiler ? {babel: {plugins: [['babel-plugin-react-compiler', reactCompiler]]}} : {},
),
],
resolve: {
alias: {
// Map /src to the actual source directory
'/src': path.join(cwd, 'src'),
},
},
appType: 'spa',
}

// Add Studio-specific configuration
if (isStudioApp) {
const customFaviconsPath = path.join(cwd, 'static')
const defaultFaviconsPath = path.join(path.dirname(sanityPkgPath!), 'static', 'favicons')
const staticPath = `${basePath}static`

viteConfig.plugins!.push(
sanityFaviconsPlugin({defaultFaviconsPath, customFaviconsPath, staticUrlPath: staticPath}),
sanityRuntimeRewritePlugin(),
sanityBuildEntries({basePath, cwd, monorepo, importMap}),
)

viteConfig.resolve = {
],
envPrefix: 'SANITY_STUDIO_',
logLevel: mode === 'production' ? 'silent' : 'info',
resolve: {
alias: monorepo?.path
? await getMonorepoAliases(monorepo.path)
: getSanityPkgExportAliases(sanityPkgPath!),
: getSanityPkgExportAliases(sanityPkgPath),
dedupe: ['styled-components'],
}

viteConfig.define = {
},
define: {
// eslint-disable-next-line no-process-env
'__SANITY_STAGING__': process.env.SANITY_INTERNAL_ENV === 'staging',
'process.env.MODE': JSON.stringify(mode),
Expand All @@ -150,20 +137,23 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
*/
'process.env.SC_DISABLE_SPEEDY': JSON.stringify('false'),
...getStudioEnvironmentVariables({prefix: 'process.env.', jsonEncode: true}),
}
},
}

if (mode === 'production') {
viteConfig.build = {
...viteConfig.build,

assetsDir: 'static',
minify: minify ? 'esbuild' : false,
emptyOutDir: false, // Rely on CLI to do this

rollupOptions: {
onwarn: onRollupWarn,
external: isStudioApp ? createExternalFromImportMap(importMap) : undefined,
input: isStudioApp ? {sanity: path.join(cwd, '.sanity', 'runtime', 'app.js')} : undefined,
external: createExternalFromImportMap(importMap),
input: {
sanity: path.join(cwd, '.sanity', 'runtime', 'app.js'),
},
},
}
}
Expand Down
20 changes: 16 additions & 4 deletions packages/sanity/src/_internal/cli/server/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function writeSanityRuntime({
isStudioApp = true,
}: RuntimeOptions): Promise<void> {
debug('Making runtime directory')
console.log('isStudioApp from runtime.ts', isStudioApp)

Check failure on line 41 in packages/sanity/src/_internal/cli/server/runtime.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement.
const runtimeDir = path.join(cwd, '.sanity', 'runtime')
await fs.mkdir(runtimeDir, {recursive: true})

Expand Down Expand Up @@ -76,13 +77,24 @@ export async function writeSanityRuntime({
if (isStudioApp) {
debug('Writing app.js to runtime directory')
const studioConfigPath = await getSanityStudioConfigPath(cwd)
debug('Studio config path: %s', studioConfigPath)
const relativeConfigLocation = studioConfigPath
? path.relative(runtimeDir, studioConfigPath)
: null
debug('Relative config location: %s', relativeConfigLocation)

await fs.writeFile(
path.join(runtimeDir, 'app.js'),
getEntryModule({reactStrictMode, relativeConfigLocation, basePath}),
)
const appJsPath = path.join(runtimeDir, 'app.js')
const appJsContent = getEntryModule({reactStrictMode, relativeConfigLocation, basePath})
debug('Writing app.js to: %s', appJsPath)

Check failure on line 89 in packages/sanity/src/_internal/cli/server/runtime.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `····`
await fs.writeFile(appJsPath, appJsContent)

Check failure on line 91 in packages/sanity/src/_internal/cli/server/runtime.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `····`
// Verify app.js was written
try {
await fs.access(appJsPath)
debug('Successfully wrote and verified app.js')
} catch (err) {
debug('Failed to verify app.js was written: %s', err.message)
}
}
}

0 comments on commit 324407c

Please sign in to comment.