简体   繁体   English

Webpack + Babel 正在转译 Webpack 加载器,我该如何防止?

[英]Webpack + Babel is transpiling Webpack loaders, how do I prevent this?

I thought Webpack + Babel would only transpile application code, not code used for the Webpack build.我认为 Webpack + Babel 只会转译应用程序代码,而不是用于 Webpack 构建的代码。 However, it seems to be transpiling code from css-loader , mini-css-extract-plugin , etc, which is causing the following error:但是,它似乎是从css-loadermini-css-extract-plugin等转译代码,这导致了以下错误:

ERROR in ./styles/main.scss
Module build failed (from ../node_modules/mini-css-extract-plugin/dist/loader.js):
TypeError: __webpack_require__(...) is not a function

If I add the following to exclude , this error is fixed:如果我将以下内容添加到exclude ,则此错误已修复:

  module: {
    rules: [
      {
        test: /\.(j|t)sx?$/,
        include: [APP_ROOT, path.resolve('./node_modules')],
        exclude: [
          path.resolve('./node_modules/mini-css-extract-plugin'),
          path.resolve('./node_modules/css-loader'),
        ],
        use: [
          'babel-loader',
        ],
      },
      ...

I thought I wouldn't need this because Babel shouldn't be transpiling packages used for Webpack's build process.我想我不需要这个,因为 Babel 不应该转译用于 Webpack 构建过程的包。 I added node_modules to include because some packages use code that wouldn't work in older browsers.我添加了node_modulesinclude因为一些包使用的代码在旧浏览器中不起作用。

The reason for that could be related to the include option on your rules, why dont you try to put the reference of node_modules in the resolve option, this way webpack looks through the declared entry and parses what it needs to resolve but excluding from transpiling the declared exclusions.原因可能与规则中的 include 选项有关,为什么不尝试将 node_modules 的引用放在 resolve 选项中,这样 webpack 会查看声明的条目并解析它需要解析的内容,但不包括转译宣布排除。

entry: SRC_DIR + '/main.js',
resolve: {
    extensions: ['.js'],
    modules: [SRC_DIR, 'node_modules']
},
module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /node_modules/,
            use: ['eslint-loader']
        }
    ]
}

Try excluding all of node_modules , and only include the node_module resources you actually need to be transpiled.尝试排除所有node_modules ,只包含您实际需要转译的 node_module 资源。 I'm betting this will make your problem go away.我打赌这会让你的问题消失。

For transpiling special files like, css or png or jpg or svg, etc from your project folders, certain loaders are required, but during transpiling it also picks up loader files so that format of your file (like css) can be matched and analysed for errors.为了从您的项目文件夹中转译特殊文件,如 css 或 png 或 jpg 或 svg 等,需要某些加载器,但在转译过程中,它还会选取加载器文件,以便可以匹配和分析您的文件格式(如 css)错误。 This is then transpiled to give you the final build files.然后将其转换为最终构建文件。

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

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