下一个JS配置多个插件配置

时间:2019-01-14 14:11:58

标签: reactjs webpack apollo next next.js

const {
  DEVELOPMENT_SERVER,
  PRODUCTION_BUILD
} = require("next/constants");

require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

const nextConfig = {
  webpack: config => ({ ...config, node: { fs: "empty" } })
};
module.exports = phase => {
  if (phase === DEVELOPMENT_SERVER || phase === PRODUCTION_BUILD) {
    const withCSS = require("@zeit/next-css");
    return withCSS(nextConfig);
  }
  return nextConfig;
};
*module.exports =  {
  webpack: (config) => {
    config.plugins = config.plugins || []
    config.plugins = [
      ...config.plugins,
      // Read the .env file
      new Dotenv({
        path: path.join(__dirname, '.env'),
        systemvars: true
      })
    ]
    return config
  }
}*

let prefix;
switch (process.env.NODE_ENV) {
  case "test":
    prefix = "https://test.domain.com/providers";
    break;
  case "stage":
    prefix = "https://state.domain.com/providers";
    break;
  case "production":
    prefix = "https://production.domain.com/providers";
    break;
  default:
    prefix = "";
    break;
}
module.exports = {
  distDir: "build",
  assetPrefix: prefix
};

这是我的next.config.js配置。 但是当我尝试运行时,会收到类似 错误!网络错误:JSON在位置0处出现意外的令牌N

但是,当我尝试将任何内容运行到粗体(*)并将其仅保留到next.config.js中时,则工作正常。 如何将多个插件配置到module.export

3 个答案:

答案 0 :(得分:1)

next-compose-plugins插件提供了更简洁的API,用于为next.js启用和配置插件。

安装npm install --save next-compose-plugins

next.config.js中使用它:

// next.config.js
const withPlugins = require('next-compose-plugins');
const images = require('next-images');
const sass = require('@zeit/next-sass');
const typescript = require('@zeit/next-typescript');

// optional next.js configuration
const nextConfig = {
  useFileSystemPublicRoutes: false,
  distDir: 'build',
};

module.exports = withPlugins([

  // add a plugin with specific configuration
  [sass, {
    cssModules: true,
    cssLoaderOptions: {
      localIdentName: '[local]___[hash:base64:5]',
    },
  }],

  // add a plugin without a configuration
  images,

  // another plugin with a configuration
  [typescript, {
    typescriptLoaderOptions: {
      transpileOnly: false,
    },
  }],

], nextConfig);

答案 1 :(得分:0)

这是在Next.js中使用多个嵌套插件的简单方法

const withImages = require('next-images');
const withCSS = require('@zeit/next-css');

module.exports = withCSS(withImages({
    webpack(config, options) {
      return config
    }
  }))

如果要使用单个插件,请执行以下操作:

const withImages = require('next-images');

module.export = withImages();

有关Next.js插件及其文档的更多信息,请单击此处: https://github.com/zeit/next-plugins/tree/master/packages

答案 2 :(得分:0)

下一个JS版本> 10.0.3

图像已成为内置对象,因此可以在下面的代码中添加插件和图像。

const withPlugins = require('next-compose-plugins');
const withCSS = require('@zeit/next-css');

const nextConfig = {
    images: {
        domains: ['sd.domain.com'], //you domain
    },
};

module.exports = withPlugins([
    [withCSS]
], nextConfig);