Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Your plugin does not support the configuration of exporting next.cofnig.js as a function #65

Open
lovelyJason opened this issue Apr 30, 2024 · 0 comments

Comments

@lovelyJason
Copy link

lovelyJason commented Apr 30, 2024

/** @type {import('next').NextConfig} */
import { error } from "node:console";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import fastSafeStringify from "fast-safe-stringify";
// 不知道为什么,这里必须加上.js
import {
  PHASE_DEVELOPMENT_SERVER,
  PHASE_PRODUCTION_BUILD,
} from "next/dist/shared/lib/constants.js";
import CopyWebpackPlugin from "copy-webpack-plugin";
import webpack from "webpack";
import { merge } from "webpack-merge"; // 不同于lodash.merge,不会更改源对象,且数组会智能合并
import HtmlWebpackPlugin from 'html-webpack-plugin'
import firebase_config from "./firebase.config.mjs";
import util from "node:util";
import withPlugins from "next-compose-plugins";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

console.log(process.env.NODE_ENV);

const nextConfig = (phase, { defaultConfig }) => {
  /**
   * @type {import('next').NextConfig}
   */
  const nextConfig = {
    reactStrictMode: false,
    output: "export",
    distDir: "dist",
    images: {
      unoptimized: true,
    },
    sassOptions: {
      additionalData: `@import "@/scss/variable.scss";`,
    },
    webpack: (
      config,
      { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
    ) => {
      if (phase === PHASE_DEVELOPMENT_SERVER) {
        const configStr = util.inspect(config.module.rules, {
          depth: null,
          colors: false,
        });
        console.log(config.module.rules);
        fs.writeFileSync("webpack.config.output.txt", configStr);

        config = merge(config, {
          plugins: [
            new webpack.DefinePlugin({
              tk_Firebase_Config: JSON.stringify(
                firebase_config.dev.tk_Firebase_Config
              ),
              cb_Firebase_config: JSON.stringify(
                firebase_config.dev.cb_Firebase_config
              ),
            }),
          ],
        });
      } else if (phase === PHASE_PRODUCTION_BUILD) {
        config = merge(config, {
          plugins: [
            new webpack.DefinePlugin({
              tk_Firebase_Config: JSON.stringify(
                firebase_config.prod.tk_Firebase_Config
              ),
              cb_Firebase_config: JSON.stringify(
                firebase_config.prod.cb_Firebase_config
              ),
            }),
          ],
        });
      }
      const originalHtmlWebpackPlugin = config.plugins.find(
        (plugin) => plugin instanceof HtmlWebpackPlugin
      );
      // console.log('originalHtmlWebpackPlugin', originalHtmlWebpackPlugin)
      // console.log(config.plugins)
      return merge(config, {
        // plugins: [
        //   new webpack.ProvidePlugin({
        //     // $: 'jquery',
        //     // Q: path.resolve(__dirname, 'public/js/q.js')
        //   }),
        // ]
      });
    },
  };
  return withPlugins(
    [
      new webpack.DefinePlugin({
        abcd: "hello with plugins",
      }),
    ],
    nextConfig
  );
  // return nextConfig;
};

export default nextConfig;

An error occurs when running the browser.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant