|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +import { Listr } from 'listr2' |
| 5 | + |
| 6 | +import { getConfig, getConfigPath } from '@redwoodjs/project-config' |
| 7 | +import { errorTelemetry } from '@redwoodjs/telemetry' |
| 8 | + |
| 9 | +import { getPaths, writeFile } from '../../lib' |
| 10 | +import c from '../../lib/colors' |
| 11 | +import { isTypeScriptProject } from '../../lib/project' |
| 12 | + |
| 13 | +import { |
| 14 | + command, |
| 15 | + description, |
| 16 | + EXPERIMENTAL_TOPIC_ID, |
| 17 | +} from './setupStreamingSsr' |
| 18 | +import { printTaskEpilogue } from './util' |
| 19 | + |
| 20 | +export const handler = async ({ force, verbose }) => { |
| 21 | + const rwPaths = getPaths() |
| 22 | + const redwoodTomlPath = getConfigPath() |
| 23 | + const configContent = fs.readFileSync(redwoodTomlPath, 'utf-8') |
| 24 | + |
| 25 | + const tasks = new Listr( |
| 26 | + [ |
| 27 | + { |
| 28 | + title: 'Check prerequisites', |
| 29 | + task: () => { |
| 30 | + if (!rwPaths.web.entryClient || !rwPaths.web.viteConfig) { |
| 31 | + throw new Error('Vite needs to be setup before you can enable RSCs') |
| 32 | + } |
| 33 | + |
| 34 | + if (!getConfig().experimental?.streamingSsr?.enabled) { |
| 35 | + throw new Error( |
| 36 | + 'The Streaming SSR experimental feature must be enabled before you can enable RSCs' |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + if (!isTypeScriptProject()) { |
| 41 | + throw new Error( |
| 42 | + 'RSCs are only supported in TypeScript projects at this time' |
| 43 | + ) |
| 44 | + } |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + title: 'Adding config to redwood.toml...', |
| 49 | + task: (_ctx, task) => { |
| 50 | + if (!configContent.includes('[experimental.rsc]')) { |
| 51 | + writeFile( |
| 52 | + redwoodTomlPath, |
| 53 | + configContent.concat('\n[experimental.rsc]\n enabled = true\n'), |
| 54 | + { |
| 55 | + overwriteExisting: true, // redwood.toml always exists |
| 56 | + } |
| 57 | + ) |
| 58 | + } else { |
| 59 | + if (force) { |
| 60 | + task.output = 'Overwriting config in redwood.toml' |
| 61 | + |
| 62 | + writeFile( |
| 63 | + redwoodTomlPath, |
| 64 | + configContent.replace( |
| 65 | + // Enable if it's currently disabled |
| 66 | + '\n[experimental.rsc]\n enabled = false\n', |
| 67 | + '\n[experimental.rsc]\n enabled = true\n' |
| 68 | + ), |
| 69 | + { |
| 70 | + overwriteExisting: true, // redwood.toml always exists |
| 71 | + } |
| 72 | + ) |
| 73 | + } else { |
| 74 | + task.skip( |
| 75 | + 'The [experimental.rsc] config block already exists in your `redwood.toml` file.' |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + }, |
| 80 | + options: { persistentOutput: true }, |
| 81 | + }, |
| 82 | + { |
| 83 | + title: 'Adding entries.ts...', |
| 84 | + task: async () => { |
| 85 | + const entriesTemplate = fs.readFileSync( |
| 86 | + path.resolve(__dirname, 'templates', 'rsc', 'entries.ts.template'), |
| 87 | + 'utf-8' |
| 88 | + ) |
| 89 | + const entriesPath = path.join(rwPaths.web.src, 'entries.ts') |
| 90 | + |
| 91 | + writeFile(entriesPath, entriesTemplate, { |
| 92 | + overwriteExisting: force, |
| 93 | + }) |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + title: 'Updating App.tsx...', |
| 98 | + task: async () => { |
| 99 | + const appTemplate = fs.readFileSync( |
| 100 | + path.resolve(__dirname, 'templates', 'rsc', 'App.tsx.template'), |
| 101 | + 'utf-8' |
| 102 | + ) |
| 103 | + const appPath = rwPaths.web.app |
| 104 | + |
| 105 | + writeFile(appPath, appTemplate, { |
| 106 | + overwriteExisting: true, |
| 107 | + }) |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + title: 'Adding Counter.tsx...', |
| 112 | + task: async () => { |
| 113 | + const counterTemplate = fs.readFileSync( |
| 114 | + path.resolve(__dirname, 'templates', 'rsc', 'Counter.tsx.template'), |
| 115 | + 'utf-8' |
| 116 | + ) |
| 117 | + const counterPath = path.join(rwPaths.web.src, 'Counter.tsx') |
| 118 | + |
| 119 | + writeFile(counterPath, counterTemplate, { |
| 120 | + overwriteExisting: force, |
| 121 | + }) |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + title: 'Updating index.html...', |
| 126 | + task: async () => { |
| 127 | + let indexHtml = fs.readFileSync(rwPaths.web.html, 'utf-8') |
| 128 | + indexHtml = indexHtml.replace( |
| 129 | + 'href="/favicon.png" />', |
| 130 | + 'href="/favicon.png" />\n <script type="module" src="entry.client.tsx"></script>' |
| 131 | + ) |
| 132 | + |
| 133 | + writeFile(rwPaths.web.html, indexHtml, { |
| 134 | + overwriteExisting: true, |
| 135 | + }) |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + task: () => { |
| 140 | + printTaskEpilogue(command, description, EXPERIMENTAL_TOPIC_ID) |
| 141 | + }, |
| 142 | + }, |
| 143 | + ], |
| 144 | + { |
| 145 | + rendererOptions: { collapseSubtasks: false, persistentOutput: true }, |
| 146 | + renderer: verbose ? 'verbose' : 'default', |
| 147 | + } |
| 148 | + ) |
| 149 | + |
| 150 | + try { |
| 151 | + await tasks.run() |
| 152 | + } catch (e) { |
| 153 | + errorTelemetry(process.argv, e.message) |
| 154 | + console.error(c.error(e.message)) |
| 155 | + process.exit(e?.exitCode || 1) |
| 156 | + } |
| 157 | +} |
0 commit comments