From 55c6af6e19aefd6dc22f0a36b9bcfc5cc07011fd Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Mon, 9 May 2022 12:55:24 +0800 Subject: [PATCH] fix(core): make generated files have unambiguous module types --- .../__tests__/__snapshots__/routes.test.ts.snap | 9 +++------ packages/docusaurus/src/server/index.ts | 8 ++++---- packages/docusaurus/src/server/routes.ts | 3 +-- packages/docusaurus/src/webpack/base.ts | 11 ++++++++++- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap b/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap index 391f3f0530c9..fe67fbf1e4f9 100644 --- a/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap +++ b/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap @@ -34,8 +34,7 @@ exports[`loadRoutes loads flat route config 1`] = ` ], }, }, - "routesConfig": "import React from 'react'; -import ComponentCreator from '@docusaurus/ComponentCreator'; + "routesConfig": "import ComponentCreator from '@docusaurus/ComponentCreator'; export default [ { @@ -90,8 +89,7 @@ exports[`loadRoutes loads nested route config 1`] = ` "metadata": "metadata---docs-foo-baz-2-cf-fa7", }, }, - "routesConfig": "import React from 'react'; -import ComponentCreator from '@docusaurus/ComponentCreator'; + "routesConfig": "import ComponentCreator from '@docusaurus/ComponentCreator'; export default [ { @@ -140,8 +138,7 @@ exports[`loadRoutes loads route config with empty (but valid) path string 1`] = "__comp": "__comp---hello-world-jse-0-f-b6c", }, }, - "routesConfig": "import React from 'react'; -import ComponentCreator from '@docusaurus/ComponentCreator'; + "routesConfig": "import ComponentCreator from '@docusaurus/ComponentCreator'; export default [ { diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index 55b2466d11c9..3a9213faf100 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -168,8 +168,8 @@ export default ${JSON.stringify(siteConfig, null, 2)}; const genClientModules = generate( generatedFilesDir, - 'client-modules.js', - `export default [ + 'client-modules.cjs', + `module.exports = [ ${clientModules // Use `require()` because `import()` is async but client modules can have CSS // and the order matters for loading CSS. @@ -181,8 +181,8 @@ ${clientModules const genRegistry = generate( generatedFilesDir, - 'registry.js', - `export default { + 'registry.cjs', + `module.exports = { ${Object.entries(registry) .sort((a, b) => a[0].localeCompare(b[0])) .map( diff --git a/packages/docusaurus/src/server/routes.ts b/packages/docusaurus/src/server/routes.ts index 87cfedf87a82..d99c38bc000b 100644 --- a/packages/docusaurus/src/server/routes.ts +++ b/packages/docusaurus/src/server/routes.ts @@ -314,8 +314,7 @@ export function loadRoutes( .map((r) => genRouteCode(r, res)) .join(',\n'); - res.routesConfig = `import React from 'react'; -import ComponentCreator from '@docusaurus/ComponentCreator'; + res.routesConfig = `import ComponentCreator from '@docusaurus/ComponentCreator'; export default [ ${indent(routeConfigSerialized)}, diff --git a/packages/docusaurus/src/webpack/base.ts b/packages/docusaurus/src/webpack/base.ts index 45a81c1031a4..3f45ada64805 100644 --- a/packages/docusaurus/src/webpack/base.ts +++ b/packages/docusaurus/src/webpack/base.ts @@ -119,7 +119,16 @@ export async function createBaseConfig( devtool: isProd ? undefined : 'eval-cheap-module-source-map', resolve: { unsafeCache: false, // Not enabled, does not seem to improve perf much - extensions: ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'], + extensions: [ + '.wasm', + '.mjs', + '.cjs', + '.js', + '.jsx', + '.ts', + '.tsx', + '.json', + ], symlinks: true, // See https://github.com/facebook/docusaurus/issues/3272 roots: [ // Allow resolution of url("/fonts/xyz.ttf") by webpack