為 Remotion 專案準備 remocn
專案根目錄:$ARGUMENTS(若為空則為當前資料夾)
shadcn init 會拒絕 Remotion 專案(「無法偵測到支援的框架」),而且 Remotion 的打包器不讀取 tsconfig 路徑。因此一個全新的專案在安裝第一個元件之前需要進行三處小修改。請使用您的檔案工具自行依序完成這些修改。若專案已有對應設定,則跳過該步驟。
0. 檢查專案
讀取專案根目錄下的 package.json。
- 若無
package.json:停止並告知使用者。 - 若
dependencies或devDependencies中沒有remotion:則這不是 Remotion 專案,停止並告知使用者。remocn-video技能會透過npx create-video@latest --yes --blank <folder>建立此類專案。
若使用者已有一個專案,請列出接下來步驟中將觸及的檔案,並在編輯前徵求同意。
1. components.json
-
檔案缺失: 建立並填入以下內容:
{ "$schema": "https://ui.shadcn.com/schema.json", "style": "new-york", "rsc": false, "tsx": true, "tailwind": { "config": "", "css": "", "baseColor": "neutral", "cssVariables": false }, "aliases": { "components": "@/components", "utils": "@/lib/utils", "ui": "@/components/ui", "lib": "@/lib", "hooks": "@/hooks" }, "registries": { "@remocn": "https://remocn.dev/r/{name}.json" } } -
檔案存在但缺少
registries["@remocn"]: 在registries中加入"@remocn": "https://remocn.dev/r/{name}.json",必要時建立該物件。其餘欄位保持不變。 -
已包含
@remocn: 略過。
2. tsconfig.json 中的 @/* 路徑
remocn 元件之間透過 @/ 互相匯入,因此 TypeScript 必須能夠解析此路徑。tsconfig.json 可能包含註解與結尾逗號:請就地編輯並保留它們。
找出 別名資料夾,即 @/ 所指向的相對專案根目錄的資料夾。步驟 3 會用到它。
- 若
compilerOptions.paths["@/*"]已存在: 略過該檔案。別名資料夾為其第一個不含結尾/*的條目(./src/*→src),如果設有compilerOptions.baseUrl,則相對於baseUrl解析,否則相對於tsconfig.json所在的資料夾。 - 若檔案有
extends且自身沒有paths: 本地的paths將會取代繼承的對應,而非合併。開啟它擴展的設定(僅一層,僅限相對路徑)。如果該設定有@/*,則略過兩個檔案,並從該處取得別名資料夾,相對於父級資料夾解析。如果沒有,則不要編輯:告訴使用者在其paths定義處加入"@/*": ["./src/*"],並使用src作為別名資料夾。 - 其他情況: 在
compilerOptions.paths中加入"@/*": ["./src/*"],必要時建立paths並保留現有條目。如果baseUrl不是.,則改為撰寫相對於baseUrl的src路徑。別名資料夾為src。 - 若無
tsconfig.json: 不要建立它。別名資料夾為src。
3. remotion.config.ts 中的 @ 別名
打包器需要同樣的別名。在專案根目錄尋找 remotion.config.ts,然後再找 remotion.config.js。如果檔案中已包含 withRemocnAlias,則略過。
別名目標為 path.join(process.cwd(), "<別名資料夾>"),如果別名資料夾就是專案根目錄本身,則為 process.cwd()。
- 無設定檔: 建立
remotion.config.ts,包含以下的匯入陳述式與程式碼區塊。 remotion.config.ts已存在: 在檔案頂部加入import path from "node:path";和import { Config } from "@remotion/cli/config";(若尚未匯入),然後在檔案結尾附加以下區塊。remotion.config.js已存在: 附加相同的區塊,但不含型別標註(const withRemocnAlias = (config) => ({ ... })和config.resolve?.alias ?? {}),以及相同兩個匯入陳述式(若缺失)。
// remocn 元件之間透過 components.json 中的 "@/" 別名互相匯入。
// 打包器不讀取 tsconfig 路徑,因此在此處也解析別名。
// 兩個覆寫都是刻意的:Remotion CLI 會同時對 webpack 和 rspack 套用 overrideBundlerConfig,
// 而 Remocn Studio 的預覽僅讀取 overrideWebpackConfig。
// 兩者都從專案根目錄執行,也就是 process.cwd() 所指的位置。
const withRemocnAlias = <C extends { resolve?: object }>(config: C): C => ({
...config,
resolve: {
...config.resolve,
alias: {
...((config.resolve as { alias?: object } | undefined)?.alias ?? {}),
"@": path.join(process.cwd(), "src"),
},
},
});
// overrideBundlerConfig 僅存在於較新的 Remotion 4 版本中。
if (typeof Config.overrideBundlerConfig === "function") {
Config.overrideBundlerConfig(withRemocnAlias);
}
Config.overrideWebpackConfig(withRemocnAlias);
如果別名目標與上述不同,請將 path.join(process.cwd(), "src") 替換為步驟 2 得出的別名目標。
4. 回報
用簡短幾行告知使用者每個步驟的執行結果:建立、修改或保持不變,以及任何需要手動添加的項目。之後 npx shadcn@latest add @remocn/<name> 即可在該專案中正常運作。