javascript - 在 Babel 7 中包含一些 node_modules 目录

标签 javascript reactjs webpack babeljs jsx

我在 node_modules 中有一个依赖项需要通过 Babel 编译。升级堆栈后,我无法让 Babel 重新编译。

当前版本:

  • @babel/核心 7.5.4
  • webpack 2.7.0

  • webpack.config.js:
    const path = require('path');
    
    module.exports = {
        devtool: 'cheap-module-source-map',
        context: path.resolve('resources/assets/js/'),
        entry: ['./index'],
        output: {
            path: path.resolve('public/js'),
            filename: 'index.js'
        },
        module: {
            rules: [
                {
                    include: [
                        path.resolve('resources/assets/js/'),
                        path.resolve('node_modules/mydep/'),
                    ],
                    exclude: /node_modules\/(?!mydep).+/,
                    test: /\.js|jsx$/,
                    use: { loader: 'babel-loader' }
                }
            ]
        },
        resolve: {
            modules: [
                path.resolve('./resources/assets/js/'),
                'node_modules'
            ]
        },
        watchOptions: {
            aggregateTimeout: 300,
            ignored: [
                /node_modules([\\]+|\/)+(?!mydep)/,
                /\mydep([\\]+|\/)node_modules/
            ]
        }
    };
    

    .babelrc:
    {
      "presets": [
        ["@babel/preset-env", {
          "debug": true,
          "useBuiltIns": "usage"
        }],
        "@babel/preset-react"
      ]
    }
    

    我在第一个 JSX 标记顶部得到的错误:
    ERROR in /var/www/node_modules/mydep/somedir/app/index.js
    Module build failed (from /var/www/node_modules/babel-loader/lib/index.js):
    SyntaxError: /var/www/node_modules/mydep/somedir/app/index.js: Unexpected token (160:15)
    
      158 |         registerReducers();
      159 |         new SomeClass('acquisition');
    > 160 |         return <SomeComponent />
    

    最佳答案

    我终于让这个工作了。

    包.json

    {
      "name": "someproject",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "browserslist": "> 0.25% in DE, not dead",
      "directories": {
        "test": "tests"
      },
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 0"
      },
      "private": true,
      "dependencies": {
        "@babel/core": "^7.5.4",
        "@babel/plugin-proposal-class-properties": "^7.5.0",
        "@babel/preset-env": "^7.5.4",
        "@babel/preset-react": "^7.0.0",
        "babel-loader": "^8.0.6",
        "browserify": "^16.2.0",
        "core-js": "^3.1.4",
        "minify": "^3.0.5",
        "somedeps": "ssh://git@bitbucket.org:somedeps.git",
        "regenerator-runtime": "^0.13.2",
        "webpack": "^4.35.3",
        "webpack-cli": "^3.3.6"
      }
    }
    

    webpack.config.js
    const path = require('path');
    
    
    module.exports = {
        devtool: 'cheap-module-source-map',
        context: path.resolve('resources/assets/js/'),
        entry: ['./index'],
        output: {
            path: path.resolve('public/js'),
            filename: 'index.js'
        },
        module: {
            rules: [
                {
                    test: /\.js|jsx$/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                ["@babel/preset-env", {
                                    useBuiltIns: 'entry',
                                    corejs: 3
                                }],
                                "@babel/preset-react"
                            ],
                            plugins: [
                                "@babel/plugin-proposal-class-properties",
                            ],
                            include: [
                                path.resolve('resources/assets/js/'),
                                path.resolve('node_modules/somedeps/'),
                            ],
                            exclude: /node_modules\/(?!somedeps).+/
                        }
                    }
                }
            ]
        },
        resolve: {
            modules: [
                path.resolve('./resources/assets/js/'),
                'node_modules'
            ]
        },
        watchOptions: {
            aggregateTimeout: 300,
            ignored: [
                /node_modules([\\]+|\/)+(?!somedeps)/,
                /\somedeps([\\]+|\/)node_modules/
            ]
        }
    };
    

    在您的应用程序主文件的顶部,这里是 resources/assets/js/index.js,包括:
    require('core-js');
    require('regenerator-runtime/runtime');
    

    关于javascript - 在 Babel 7 中包含一些 node_modules 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044643/

    相关文章:

    javascript - Angular 如何获取动态创建的 div 的高度并将其发送到兄弟组件?

    javascript - 在元素上使用重复的类名?

    javascript - 如何通过变量中的id获取div的文本

    unit-testing - Jest enzyme 浅测试没有渲染React组件的所有元素

    reactjs - 如何在 React SSR(服务器端渲染)中使用样式加载器?

    javascript - 检查参数中的交替数字和 bool 值

    reactjs - 将属性附加到用 React.forwardRef 包装的功能组件

    javascript - 单击从按钮重定向到文件输入

    javascript - Webpack 文件加载器未加载图像,404 错误

    javascript - 使用 Webpack 5 Boilerplate 构建 Chrome 扩展时如何 chrome.runtime.reload() ?