gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:18:39 25 4
gpt4 key购买 nike

我在 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com