Skip to content

Commit

Permalink
feat: add CLI options to enable auto-updating studios (#6514)
Browse files Browse the repository at this point in the history
* feat: add CLI options to enable auto-updating studios

* refactor(cli): prefix with `unstable_`

* test: Update packages/sanity/src/_internal/cli/server/renderDocument.ts

Co-authored-by: Espen Hovlandsdal <[email protected]>

* chore(core): update import map urls

* feat(cli): build user peer-deps (#6742)

* feat(cli): build user peer-deps

* chore: remove shared-modules build

* test(cli):  verify build config and `exports` subpaths

* text(cli): add depcheck config for fixture projects

* docs: update comments and test name

* feat: create import map w/ installed sanity version (#6784)

* feat: create import map w/ installed sanity version

* fix: use correct URL

* chore(cli): rename auto updates flag

---------

Co-authored-by: Espen Hovlandsdal <[email protected]>
Co-authored-by: Binoy Patel <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent 43711d7 commit 5077c8b
Show file tree
Hide file tree
Showing 28 changed files with 970 additions and 148 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"bootstrap": "pnpm install && pnpm build:cli",
"build": "turbo run build --filter='./packages/*' --filter='./packages/@sanity/*'",
"build:bundle": "turbo run build:bundle --filter='./packages/*' --filter='./packages/@sanity/*' --filter='./packages/@repo/shared-modules.bundle'",
"build:bundle": "turbo run build:bundle --filter='./packages/*' --filter='./packages/@sanity/*'",
"build:clean": "lerna run clean",
"build:cli": "turbo run build --filter=@sanity/cli --filter=sanity",
"build:cli-only": "turbo run --filter=@sanity/cli build",
Expand Down
3 changes: 0 additions & 3 deletions packages/@repo/shared-modules.bundle/.depcheckrc.json

This file was deleted.

9 changes: 0 additions & 9 deletions packages/@repo/shared-modules.bundle/.eslintrc.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions packages/@repo/shared-modules.bundle/.gitignore

This file was deleted.

23 changes: 0 additions & 23 deletions packages/@repo/shared-modules.bundle/package.bundle.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/@repo/shared-modules.bundle/package.json

This file was deleted.

2 changes: 2 additions & 0 deletions packages/@sanity/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ export interface CliConfig {
graphql?: GraphQLAPIConfig[]

vite?: UserViteConfig

autoUpdates?: boolean
}

export type UserViteConfig =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignores": ["react", "react-dom", "styled-components"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "prj-with-react-18",
"private": true,
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"styled-components": "^6.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignores": ["react", "react-dom", "styled-components"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "prj-with-react-19",
"private": true,
"dependencies": {
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"styled-components": "^6.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignores": ["react", "react-dom", "styled-components"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "prj-with-styled-components-5",
"private": true,
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"styled-components": "^5.3.0"
}
}
1 change: 1 addition & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"module-alias": "^2.2.2",
"nano-pubsub": "^3.0.0",
"nanoid": "^3.1.30",
"node-html-parser": "^6.1.13",
"observable-callback": "^1.0.1",
"oneline": "^1.0.3",
"open": "^8.4.0",
Expand Down
36 changes: 35 additions & 1 deletion packages/sanity/src/_internal/cli/actions/build/buildAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'node:path'
import {promisify} from 'node:util'

import chalk from 'chalk'
import {info} from 'log-symbols'
import {noopLogger} from '@sanity/telemetry'
import rimrafCallback from 'rimraf'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -13,6 +14,7 @@ import {checkStudioDependencyVersions} from '../../util/checkStudioDependencyVer
import {checkRequiredDependencies} from '../../util/checkRequiredDependencies'
import {getTimer} from '../../util/timing'
import {BuildTrace} from './build.telemetry'
import {buildVendorDependencies} from '../../server/buildVendorDependencies'

const rimraf = promisify(rimrafCallback)

Expand All @@ -22,6 +24,7 @@ export interface BuildSanityStudioCommandFlags {
'minify'?: boolean
'stats'?: boolean
'source-maps'?: boolean
'auto-updates'?: boolean
}

export default async function buildSanityStudio(
Expand All @@ -46,10 +49,19 @@ export default async function buildSanityStudio(

// If the check resulted in a dependency install, the CLI command will be re-run,
// thus we want to exit early
if ((await checkRequiredDependencies(context)).didInstall) {
const {didInstall, installedSanityVersion} = await checkRequiredDependencies(context)
if (didInstall) {
return {didCompile: false}
}

const autoUpdatesEnabled =
flags['auto-updates'] ||
(cliConfig && 'autoUpdates' in cliConfig && cliConfig.autoUpdates === true)

if (autoUpdatesEnabled) {
output.print(`${info} Building with auto-updates enabled`)
}

const envVarKeys = getSanityEnvVars()
if (envVarKeys.length > 0) {
output.print(
Expand Down Expand Up @@ -105,6 +117,26 @@ export default async function buildSanityStudio(

const trace = telemetry.trace(BuildTrace)
trace.start()

let importMap

if (autoUpdatesEnabled) {
const version = encodeURIComponent(`^${installedSanityVersion}`)
const autoUpdatesImports = {
'sanity': `https://api.sanity.work/v1/modules/sanity/default/${version}`,
'sanity/': `https://api.sanity.work/v1/modules/sanity/default/${version}/`,
'@sanity/vision': `https://api.sanity.work/v1/modules/@sanity__vision/default/${version}`,
'@sanity/vision/': `https://api.sanity.work/v1/modules/@sanity__vision/default/${version}/`,
}

importMap = {
imports: {
...(await buildVendorDependencies({cwd: workDir, outputDir})),
...autoUpdatesImports,
},
}
}

try {
timer.start('bundleStudio')

Expand All @@ -115,7 +147,9 @@ export default async function buildSanityStudio(
sourceMap: Boolean(flags['source-maps']),
minify: Boolean(flags.minify),
vite: cliConfig && 'vite' in cliConfig ? cliConfig.vite : undefined,
importMap,
})

trace.log({
outputSize: bundle.chunks
.flatMap((chunk) => chunk.modules.flatMap((mod) => mod.renderedLength))
Expand Down
Loading

0 comments on commit 5077c8b

Please sign in to comment.