{"id":309,"date":"2026-11-07T11:43:15","date_gmt":"2026-11-07T03:43:15","guid":{"rendered":"https:\/\/yykcj.com\/?p=309"},"modified":"2026-11-07T11:43:15","modified_gmt":"2026-11-07T03:43:15","slug":"monorepo-%e5%b7%a5%e7%a8%8b%e5%8c%96%e5%ae%9e%e6%88%98%ef%bc%9aturborepo-%e4%b8%8e-nx-%e7%9a%84%e6%b7%b1%e5%ba%a6%e9%80%89%e5%9e%8b%e4%b8%8e%e9%85%8d%e7%bd%ae%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=309","title":{"rendered":"Monorepo \u5de5\u7a0b\u5316\u5b9e\u6218\uff1aTurborepo \u4e0e Nx \u7684\u6df1\u5ea6\u9009\u578b\u4e0e\u914d\u7f6e\u6307\u5357"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9700\u8981 Monorepo\uff1f<\/h2>\n<p>\u968f\u7740\u9879\u76ee\u89c4\u6a21\u6269\u5927\uff0c\u591a\u4ed3\u5e93\uff08Polyrepo\uff09\u6a21\u5f0f\u9010\u6e10\u66b4\u9732\u51fa\u4f9d\u8d56\u7ba1\u7406\u590d\u6742\u3001\u4ee3\u7801\u590d\u7528\u56f0\u96be\u3001\u6784\u5efa\u90e8\u7f72\u4e0d\u4e00\u81f4\u7b49\u95ee\u9898\u3002Monorepo \u5c06\u591a\u4e2a\u76f8\u5173\u9879\u76ee\u96c6\u4e2d\u5728\u4e00\u4e2a\u4ed3\u5e93\u4e2d\u7ba1\u7406\uff0c\u914d\u5408\u73b0\u4ee3\u5de5\u5177\u94fe\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u4f46 Monorepo \u4e5f\u5e26\u6765\u4e86\u65b0\u7684\u6311\u6218\uff1a\u5982\u4f55\u9ad8\u6548\u5730\u8fd0\u884c\u4efb\u52a1\u3001\u7ba1\u7406\u4f9d\u8d56\u3001\u4fdd\u8bc1\u6784\u5efa\u7f13\u5b58\u548c\u589e\u91cf\u6784\u5efa\uff1f\u8fd9\u6b63\u662f Turborepo \u548c Nx \u8981\u89e3\u51b3\u7684\u95ee\u9898\u3002<\/p>\n<p>\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\uff0c\u5bf9\u6bd4 Turborepo \u548c Nx \u7684\u6838\u5fc3\u7279\u6027\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u914d\u7f6e\u793a\u4f8b\uff0c\u52a9\u4f60\u505a\u51fa\u9002\u5408\u56e2\u961f\u7684\u6280\u672f\u9009\u578b\u3002<\/p>\n<h2>\u4e00\u3001Turborepo vs Nx\uff1a\u6838\u5fc3\u5dee\u5f02<\/h2>\n<p>\u4e24\u8005\u90fd\u662f\u4f18\u79c0\u7684 Monorepo \u5de5\u5177\uff0c\u4f46\u8bbe\u8ba1\u54f2\u5b66\u4e0d\u540c\uff1a<\/p>\n<ul>\n<li><strong>Turborepo<\/strong>\uff1a\u8f7b\u91cf\u7ea7\uff0c\u4e13\u6ce8\u4e8e\u4efb\u52a1\u7f16\u6392\u548c\u7f13\u5b58\uff0c\u914d\u7f6e\u7b80\u5355\uff0c\u4e0e\u73b0\u6709\u5de5\u5177\uff08\u5982 npm\/yarn\/pnpm\uff09\u65e0\u7f1d\u96c6\u6210\uff0c\u5b66\u4e60\u66f2\u7ebf\u5e73\u7f13\u3002<\/li>\n<li><strong>Nx<\/strong>\uff1a\u529f\u80fd\u5168\u9762\uff0c\u63d0\u4f9b\u63d2\u4ef6\u7cfb\u7edf\u3001\u4ee3\u7801\u751f\u6210\u3001\u4f9d\u8d56\u56fe\u53ef\u89c6\u5316\u3001\u5206\u5e03\u5f0f\u4efb\u52a1\u6267\u884c\u7b49\uff0c\u9002\u5408\u5927\u578b\u56e2\u961f\u548c\u590d\u6742\u9879\u76ee\uff0c\u4f46\u914d\u7f6e\u76f8\u5bf9\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u578b\u5efa\u8bae<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e2d\u5c0f\u578b\u56e2\u961f\u6216\u521a\u63a5\u89e6 Monorepo\uff1a\u4f18\u5148\u9009\u62e9 Turborepo\uff0c\u5feb\u901f\u4e0a\u624b\u3002<\/li>\n<li>\u5927\u578b\u4f01\u4e1a\u7ea7\u9879\u76ee\uff0c\u9700\u8981\u4ee3\u7801\u751f\u6210\u3001\u6a21\u5757\u8fb9\u754c\u7ea6\u675f\u3001\u9ad8\u7ea7\u7f13\u5b58\u7b56\u7565\uff1a\u9009\u62e9 Nx\u3002<\/li>\n<\/ul>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5206\u522b\u7528\u4e24\u8005\u642d\u5efa\u4e00\u4e2a\u5305\u542b Next.js \u5e94\u7528\u548c\u5171\u4eab UI \u5e93\u7684 Monorepo\u3002<\/p>\n<h2>\u4e8c\u3001\u4f7f\u7528 Turborepo \u642d\u5efa Monorepo<\/h2>\n<h3>1. \u521d\u59cb\u5316\u9879\u76ee<\/h3>\n<p>\u4f7f\u7528 pnpm \u4f5c\u4e3a\u5305\u7ba1\u7406\u5668\uff08\u63a8\u8350\uff09\uff0c\u521b\u5efa\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-bash\">\nmkdir my-turborepo &amp;&amp; cd my-turborepo\npnpm init\npnpm add -D turbo\n<\/code><\/pre>\n<p>\u521b\u5efa <code>pnpm-workspace.yaml<\/code>\uff1a<\/p>\n<pre><code class=\"language-yaml\">\npackages:\n  - &quot;apps\/*&quot;\n  - &quot;packages\/*&quot;\n<\/code><\/pre>\n<h3>2. \u521b\u5efa\u5e94\u7528\u548c\u5171\u4eab\u5e93<\/h3>\n<pre><code class=\"language-bash\">\nmkdir -p apps\/web packages\/ui\ncd apps\/web &amp;&amp; pnpm init\ncd ..\/..\/packages\/ui &amp;&amp; pnpm init\n<\/code><\/pre>\n<p>\u5728 <code>apps\/web\/package.json<\/code> \u4e2d\u6dfb\u52a0 Next.js \u4f9d\u8d56\u548c\u811a\u672c\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;name&quot;: &quot;web&quot;,\n  &quot;scripts&quot;: {\n    &quot;dev&quot;: &quot;next dev&quot;,\n    &quot;build&quot;: &quot;next build&quot;,\n    &quot;lint&quot;: &quot;next lint&quot;\n  },\n  &quot;dependencies&quot;: {\n    &quot;next&quot;: &quot;latest&quot;,\n    &quot;react&quot;: &quot;latest&quot;,\n    &quot;react-dom&quot;: &quot;latest&quot;,\n    &quot;ui&quot;: &quot;workspace:*&quot;\n  }\n}\n<\/code><\/pre>\n<p>\u5728 <code>packages\/ui\/package.json<\/code> \u4e2d\u5b9a\u4e49\u5171\u4eab\u7ec4\u4ef6\u5e93\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;name&quot;: &quot;ui&quot;,\n  &quot;version&quot;: &quot;0.0.0&quot;,\n  &quot;main&quot;: &quot;index.js&quot;,\n  &quot;scripts&quot;: {\n    &quot;lint&quot;: &quot;eslint .&quot;\n  }\n}\n<\/code><\/pre>\n<p>\u521b\u5efa <code>packages\/ui\/index.js<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nexport const Button = () =&gt; &lt;button&gt;Click me&lt;\/button&gt;;\n<\/code><\/pre>\n<h3>3. \u914d\u7f6e Turborepo \u7ba1\u9053<\/h3>\n<p>\u5728\u6839\u76ee\u5f55\u521b\u5efa <code>turbo.json<\/code>\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;$schema&quot;: &quot;https:\/\/turbo.build\/schema.json&quot;,\n  &quot;pipeline&quot;: {\n    &quot;build&quot;: {\n      &quot;dependsOn&quot;: [&quot;^build&quot;],\n      &quot;outputs&quot;: [&quot;.next\/**&quot;, &quot;dist\/**&quot;]\n    },\n    &quot;lint&quot;: {\n      &quot;dependsOn&quot;: [&quot;^lint&quot;]\n    },\n    &quot;dev&quot;: {\n      &quot;cache&quot;: false,\n      &quot;persistent&quot;: true\n    }\n  }\n}\n<\/code><\/pre>\n<h3>4. \u8fd0\u884c\u4efb\u52a1<\/h3>\n<p>\u5728\u6839 <code>package.json<\/code> \u4e2d\u6dfb\u52a0\u811a\u672c\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;scripts&quot;: {\n    &quot;build&quot;: &quot;turbo run build&quot;,\n    &quot;lint&quot;: &quot;turbo run lint&quot;,\n    &quot;dev&quot;: &quot;turbo run dev&quot;\n  }\n}\n<\/code><\/pre>\n<p>\u6267\u884c <code>pnpm build<\/code>\uff0cTurborepo \u4f1a\u5e76\u884c\u6784\u5efa\u6240\u6709\u5305\uff0c\u5e76\u5229\u7528\u7f13\u5b58\u52a0\u901f\u540e\u7eed\u6784\u5efa\u3002<\/p>\n<p><strong>\ud83d\udca1 \u6ce8\u610f<\/strong>\uff1aTurborepo \u7684\u7f13\u5b58\u57fa\u4e8e\u6587\u4ef6\u54c8\u5e0c\uff0c\u786e\u4fdd <code>outputs<\/code> \u914d\u7f6e\u6b63\u786e\uff0c\u5426\u5219\u7f13\u5b58\u53ef\u80fd\u5931\u6548\u3002<\/p>\n<h2>\u4e09\u3001\u4f7f\u7528 Nx \u642d\u5efa Monorepo<\/h2>\n<h3>1. \u521b\u5efa Nx \u5de5\u4f5c\u533a<\/h3>\n<pre><code class=\"language-bash\">\nnpx create-nx-workspace@latest my-nx-repo --preset=ts\ncd my-nx-repo\n<\/code><\/pre>\n<p>\u9009\u62e9 pnpm \u4f5c\u4e3a\u5305\u7ba1\u7406\u5668\u3002<\/p>\n<h3>2. \u751f\u6210\u5e94\u7528\u548c\u5e93<\/h3>\n<p>\u4f7f\u7528 Nx \u63d2\u4ef6\u751f\u6210 Next.js \u5e94\u7528\uff1a<\/p>\n<pre><code class=\"language-bash\">\npnpm add -D @nx\/next\nnx g @nx\/next:app apps\/web\n<\/code><\/pre>\n<p>\u751f\u6210\u5171\u4eab UI \u5e93\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnx g @nx\/react:lib packages\/ui\n<\/code><\/pre>\n<p>Nx \u4f1a\u81ea\u52a8\u914d\u7f6e\u8def\u5f84\u522b\u540d\u548c\u6784\u5efa\u811a\u672c\u3002<\/p>\n<h3>3. \u914d\u7f6e\u4efb\u52a1\u7ba1\u9053<\/h3>\n<p>Nx \u4f7f\u7528 <code>nx.json<\/code> \u5b9a\u4e49\u4efb\u52a1\u7ba1\u9053\u548c\u7f13\u5b58\uff1a<\/p>\n<pre><code class=\"language-json\">\n{\n  &quot;targetDefaults&quot;: {\n    &quot;build&quot;: {\n      &quot;dependsOn&quot;: [&quot;^build&quot;],\n      &quot;outputs&quot;: [&quot;{projectRoot}\/.next&quot;, &quot;{projectRoot}\/dist&quot;]\n    },\n    &quot;lint&quot;: {\n      &quot;dependsOn&quot;: [&quot;^lint&quot;]\n    }\n  }\n}\n<\/code><\/pre>\n<h3>4. \u8fd0\u884c\u4efb\u52a1<\/h3>\n<pre><code class=\"language-bash\">\nnx run-many --target=build --all\nnx run-many --target=lint --all\n<\/code><\/pre>\n<p>Nx \u63d0\u4f9b\u4f9d\u8d56\u56fe\u53ef\u89c6\u5316\uff1a<code>nx graph<\/code>\uff0c\u65b9\u4fbf\u5206\u6790\u9879\u76ee\u7ed3\u6784\u3002<\/p>\n<p><strong>\ud83d\udca1 \u6ce8\u610f<\/strong>\uff1aNx \u7684\u7f13\u5b58\u9ed8\u8ba4\u5b58\u50a8\u5728\u672c\u5730\uff0c\u53ef\u901a\u8fc7 <code>nx cloud<\/code> \u542f\u7528\u5206\u5e03\u5f0f\u7f13\u5b58\u3002<\/p>\n<h2>\u56db\u3001\u5e38\u89c1\u5751\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ol>\n<li><strong>\u4f9d\u8d56\u7248\u672c\u4e00\u81f4\u6027<\/strong>\uff1a\u4f7f\u7528 pnpm \u7684 <code>overrides<\/code> \u6216 Nx \u7684 <code>sync generators<\/code> \u786e\u4fdd\u4f9d\u8d56\u7248\u672c\u7edf\u4e00\u3002<\/li>\n<li><strong>\u7f13\u5b58\u5931\u6548<\/strong>\uff1a\u786e\u4fdd <code>outputs<\/code> \u548c <code>inputs<\/code> \u914d\u7f6e\u51c6\u786e\uff0c\u907f\u514d\u7f13\u5b58\u6c61\u67d3\u3002<\/li>\n<li><strong>\u4efb\u52a1\u7f16\u6392<\/strong>\uff1a\u5408\u7406\u4f7f\u7528 <code>dependsOn<\/code> \u548c <code>persistent<\/code> \u6807\u5fd7\uff0c\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u589e\u91cf\u6784\u5efa<\/strong>\uff1aTurborepo \u548c Nx \u90fd\u652f\u6301\u589e\u91cf\u6784\u5efa\uff0c\u4f46\u9700\u8981\u6b63\u786e\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u548c\u6587\u4ef6\u76d1\u542c\u3002<\/li>\n<li><strong>CI\/CD \u96c6\u6210<\/strong>\uff1a\u5728 CI \u4e2d\u542f\u7528\u8fdc\u7a0b\u7f13\u5b58\uff08Turborepo \u7684 Remote Cache \u6216 Nx Cloud\uff09\uff0c\u5927\u5e45\u63d0\u5347\u6784\u5efa\u901f\u5ea6\u3002<\/li>\n<\/ol>\n<h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<p>Turborepo \u548c Nx \u90fd\u662f\u5f3a\u5927\u7684 Monorepo \u5de5\u5177\uff0c\u9009\u62e9\u53d6\u51b3\u4e8e\u56e2\u961f\u89c4\u6a21\u548c\u9700\u6c42\u590d\u6742\u5ea6\u3002Turborepo \u9002\u5408\u8ffd\u6c42\u7b80\u5355\u9ad8\u6548\u7684\u56e2\u961f\uff0cNx \u9002\u5408\u9700\u8981\u5168\u9762\u89e3\u51b3\u65b9\u6848\u7684\u5927\u578b\u9879\u76ee\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u4e2a\uff0c\u5408\u7406\u914d\u7f6e\u7f13\u5b58\u548c\u4efb\u52a1\u7ba1\u9053\u662f\u5173\u952e\u3002\u5efa\u8bae\u4ece\u4e00\u4e2a\u5c0f\u578b\u9879\u76ee\u5f00\u59cb\u5b9e\u8df5\uff0c\u9010\u6b65\u4f18\u5316\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong>\uff1a<\/p>\n<ul>\n<li>Turborepo \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/turbo.build\/repo<\/li>\n<li>Nx \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/nx.dev<\/li>\n<li>pnpm workspaces\uff1ahttps:\/\/pnpm.io\/workspaces<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u5bf9\u6bd4 Turborepo \u548c Nx \u5728 Monorepo \u5de5\u7a0b\u5316\u4e2d\u7684\u4f18\u52a3\uff0c\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u914d\u7f6e\u4e24\u8005\uff0c\u5e76\u5206\u4eab\u9009\u578b\u51b3\u7b56\u4e0e\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u56e2\u961f\u9ad8\u6548\u6784\u5efa\u53ef\u7ef4\u62a4\u7684 Monorepo \u67b6\u6784\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[273,275,274,171,276],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-monorepo","tag-nx","tag-turborepo","tag-171","tag-276"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}