简体   繁体   English

如何在 nuxt.config.js 中扩展 webpack > config > externals?

[英]How to extend webpack > config > externals in nuxt.config.js?

So I need to set the alternative to Rollup 's external option ( external: path => /^three/.test( path ) ) in Webpack所以我需要在Webpack设置Rollupexternal选项( external: path => /^three/.test( path ) )的Webpack

As I understand in webpack it's called externals , but I don't know how to set it in extend (config, ctx) {} in nuxt.config.js .正如我在webpack所理解的,它被称为externals ,但我不知道如何在nuxt.config.js中的extend (config, ctx) {}中设置它。

config.module.externals = /^three/ doesn't seem to work, even though in webpack docs I've seen this example: config.module.externals = /^three/似乎不起作用,即使在 webpack 文档中我已经看到了这个例子:

module.exports = {
  //...
  externals: /^(jquery|\$)$/i
};

My config in nuxt.config.js :我在nuxt.config.js配置:

        /*
        ** You can extend webpack config here
        */
        extend (config, ctx) {
            config.module.rules.push(
                {
                    test: /\.(glsl|vs|fs|vert|frag)$/,
                    exclude: /node_modules/,
                    use: 'raw-loader'
                }
            )
        }

I come to this since have same issue a hours ago, and this work for me :D我来这里是因为几个小时前有同样的问题,这对我有用:D

build: {
    extend(config, { isDev, isClient }) {
      config.externals = function (context, request, callback) {
        if (/xlsx|canvg|pdfmake/.test(request)) {
          return callback(null, 'commonjs ' + request)
        }
        callback()
      }
    },
  },

https://webpack.js.org/configuration/externals/ https://webpack.js.org/configuration/externals/

so you don't need add config.module.external because the config is module it self所以你不需要添加 config.module.external 因为配置本身就是模块

    build: {

      extend(config, { isDev, isClient }) {
        config.externals = [
          {
            // String
            react: 'react',
            // Object
            lodash: {
              commonjs: 'lodash',
              amd: 'lodash',
              root: '_', // indicates global variable
            },
            // [string]
            subtract: ['./math', 'subtract'],
          },
          // Function
          function ({ context, request }, callback) {
            if (/^yourregex$/.test(request)) {
              return callback(null, 'commonjs ' + request);
            }
            callback();
          },
          // Regex
          /^(jquery|\$)$/i,
      ];
    },
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM