Przeglądaj źródła

use function and phase constants

Yuki Takei 3 lat temu
rodzic
commit
e543b9958d
2 zmienionych plików z 94 dodań i 93 usunięć
  1. 92 91
      packages/app/next.config.js
  2. 2 2
      packages/app/package.json

+ 92 - 91
packages/app/next.config.js

@@ -5,15 +5,9 @@
  * See: https://github.com/vercel/next.js/discussions/35969#discussioncomment-2522954
  * See: https://github.com/vercel/next.js/discussions/35969#discussioncomment-2522954
  */
  */
 
 
-const eazyLogger = require('eazy-logger');
 const { withSuperjson } = require('next-superjson');
 const { withSuperjson } = require('next-superjson');
+const { PHASE_PRODUCTION_SERVER } = require('next/constants');
 
 
-const isProduction = process.env.NODE_ENV === 'production';
-const isBuildingNext = process.env.BUILDING_NEXT === 'true';
-
-const { i18n, localePath } = isProduction
-  ? require('./src/next-i18next.config')
-  : require('./dist/next-i18next.config');
 
 
 // define additional entries
 // define additional entries
 const additionalWebpackEntries = {
 const additionalWebpackEntries = {
@@ -21,63 +15,8 @@ const additionalWebpackEntries = {
 };
 };
 
 
 
 
-/** @type {import('next').NextConfig} */
-const nextConfig = {
-  // == DOES NOT WORK
-  // see: https://github.com/vercel/next.js/discussions/27876
-  // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
-
-  reactStrictMode: true,
-  typescript: {
-    tsconfigPath: 'tsconfig.build.client.json',
-  },
-  pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
-
-  i18n,
-
-  /** @param config {import('next').NextConfig} */
-  webpack(config, options) {
-    // Avoid "Module not found: Can't resolve 'fs'"
-    // See: https://stackoverflow.com/a/68511591
-    if (!options.isServer) {
-      config.resolve.fallback.fs = false;
-    }
-
-    // See: https://webpack.js.org/configuration/externals/
-    // This provides a way of excluding dependencies from the output bundles
-    config.externals.push('dtrace-provider');
-
-    // configure additional entries
-    const orgEntry = config.entry;
-    config.entry = () => {
-      return orgEntry().then((entry) => {
-        return { ...entry, ...additionalWebpackEntries };
-      });
-    };
-
-    const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
-    config.plugins.push(
-      new WebpackManifestPlugin({
-        fileName: 'custom-manifest.json',
-      }),
-    );
-
-    // setup i18next-hmr
-    if (!options.isServer && options.dev) {
-      const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
-      config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
-    }
-
-    return config;
-  },
-
-};
-
-
-const passThrough = nextConfig => nextConfig;
-let withTM = passThrough;
-
-if (!isProduction || isBuildingNext) {
+const setupTranspileModules = () => {
+  const eazyLogger = require('eazy-logger');
   const { listScopedPackages, listPrefixedPackages } = require('./src/utils/next.config.utils');
   const { listScopedPackages, listPrefixedPackages } = require('./src/utils/next.config.utils');
 
 
   // setup logger
   // setup logger
@@ -86,33 +25,95 @@ if (!isProduction || isBuildingNext) {
     useLevelPrefixes: false,
     useLevelPrefixes: false,
   });
   });
 
 
-  const setupWithTM = () => {
-    // define transpiled packages for '@growi/*'
-    const packages = [
-      ...listScopedPackages(['@growi'], { ignorePackageNames: ['@growi/app'] }),
-      // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
-      'react-markdown',
-      'unified',
-      'comma-separated-tokens',
-      'decode-named-character-reference',
-      'html-void-elements',
-      'property-information',
-      'space-separated-tokens',
-      'trim-lines',
-      'web-namespaces',
-      'vfile',
-      'zwitch',
-      'emoticon',
-      ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'micromark-', 'unist-']),
-    ];
-
-    logger.info('{bold:Listing scoped packages for transpiling:}');
-    logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
-
-    return require('next-transpile-modules')(packages);
+  // define transpiled packages for '@growi/*'
+  const packages = [
+    ...listScopedPackages(['@growi'], { ignorePackageNames: ['@growi/app'] }),
+    // listing ESM packages until experimental.esmExternals works correctly to avoid ERR_REQUIRE_ESM
+    'react-markdown',
+    'unified',
+    'comma-separated-tokens',
+    'decode-named-character-reference',
+    'html-void-elements',
+    'property-information',
+    'space-separated-tokens',
+    'trim-lines',
+    'web-namespaces',
+    'vfile',
+    'zwitch',
+    'emoticon',
+    ...listPrefixedPackages(['remark-', 'rehype-', 'hast-', 'mdast-', 'micromark-', 'micromark-', 'unist-']),
+  ];
+
+  logger.info('{bold:Listing scoped packages for transpiling:}');
+  logger.unprefixed('info', `{grey:${JSON.stringify(packages, null, 2)}}`);
+
+  return require('next-transpile-modules')(packages);
+};
+
+
+module.exports = async(phase, { defaultConfig }) => {
+
+  const { i18n, localePath } = phase === PHASE_PRODUCTION_SERVER
+    ? require('./dist/next-i18next.config')
+    : require('./src/next-i18next.config');
+
+  /** @type {import('next').NextConfig} */
+  const nextConfig = {
+    // == DOES NOT WORK
+    // see: https://github.com/vercel/next.js/discussions/27876
+    // experimental: { esmExternals: true }, // Prefer loading of ES Modules over CommonJS
+
+    reactStrictMode: true,
+    typescript: {
+      tsconfigPath: 'tsconfig.build.client.json',
+    },
+    pageExtensions: ['page.tsx', 'page.ts', 'page.jsx', 'page.js'],
+
+    i18n,
+
+    /** @param config {import('next').NextConfig} */
+    webpack(config, options) {
+      // Avoid "Module not found: Can't resolve 'fs'"
+      // See: https://stackoverflow.com/a/68511591
+      if (!options.isServer) {
+        config.resolve.fallback.fs = false;
+      }
+
+      // See: https://webpack.js.org/configuration/externals/
+      // This provides a way of excluding dependencies from the output bundles
+      config.externals.push('dtrace-provider');
+
+      // configure additional entries
+      const orgEntry = config.entry;
+      config.entry = () => {
+        return orgEntry().then((entry) => {
+          return { ...entry, ...additionalWebpackEntries };
+        });
+      };
+
+      const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
+      config.plugins.push(
+        new WebpackManifestPlugin({
+          fileName: 'custom-manifest.json',
+        }),
+      );
+
+      // setup i18next-hmr
+      if (!options.isServer && options.dev) {
+        const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
+        config.plugins.push(new I18NextHMRPlugin({ localesDir: localePath }));
+      }
+
+      return config;
+    },
+
   };
   };
 
 
-  withTM = setupWithTM();
-}
+  // production server
+  if (phase === PHASE_PRODUCTION_SERVER) {
+    return withSuperjson()(nextConfig);
+  }
 
 
-module.exports = withSuperjson()(withTM(nextConfig));
+  const withTM = setupTranspileModules();
+  return withSuperjson()(withTM(nextConfig));
+};

+ 2 - 2
packages/app/package.json

@@ -6,7 +6,7 @@
     "//// for production": "",
     "//// for production": "",
     "build": "run-p build:*",
     "build": "run-p build:*",
     "start": "yarn next start",
     "start": "yarn next start",
-    "build:client": "yarn cross-env BUILDING_NEXT=true yarn next build",
+    "build:client": "yarn next build",
     "prebuild:client": "tsc -p tsconfig.build.next.config.json",
     "prebuild:client": "tsc -p tsconfig.build.next.config.json",
     "build:server": "yarn cross-env NODE_ENV=production tsc -p tsconfig.build.server.json && tsc-alias -p tsconfig.build.server.json",
     "build:server": "yarn cross-env NODE_ENV=production tsc -p tsconfig.build.server.json && tsc-alias -p tsconfig.build.server.json",
     "postbuild:server": "npx -y shx mv transpiled/src dist && npx -y shx cp -r transpiled/config/* config && npx -y shx cp -r src/server/views dist/server/ && npx -y shx rm -rf transpiled",
     "postbuild:server": "npx -y shx mv transpiled/src dist && npx -y shx cp -r transpiled/config/* config && npx -y shx cp -r src/server/views dist/server/ && npx -y shx rm -rf transpiled",
@@ -93,7 +93,6 @@
     "detect-indent": "^7.0.0",
     "detect-indent": "^7.0.0",
     "diff": "^5.0.0",
     "diff": "^5.0.0",
     "diff_match_patch": "^0.1.1",
     "diff_match_patch": "^0.1.1",
-    "eazy-logger": "^3.1.0",
     "entities": "^2.0.0",
     "entities": "^2.0.0",
     "esa-node": "^0.2.2",
     "esa-node": "^0.2.2",
     "escape-string-regexp": "=4.0.0",
     "escape-string-regexp": "=4.0.0",
@@ -200,6 +199,7 @@
     "core-js": "=2.6.9",
     "core-js": "=2.6.9",
     "csv-to-markdown-table": "^1.0.1",
     "csv-to-markdown-table": "^1.0.1",
     "diff2html": "^3.1.2",
     "diff2html": "^3.1.2",
+    "eazy-logger": "^3.1.0",
     "emoji-mart": "npm:panta82-emoji-mart@^3.0.1",
     "emoji-mart": "npm:panta82-emoji-mart@^3.0.1",
     "eslint-plugin-cypress": "^2.12.1",
     "eslint-plugin-cypress": "^2.12.1",
     "eslint-plugin-regex": "^1.8.0",
     "eslint-plugin-regex": "^1.8.0",