我在node_modules
中有一个依赖关系,需要通过Babel进行编译。升级堆栈后,我无法使Babel恢复编译。
当前版本:
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 />
答案 0 :(得分:1)
我终于有了这个工作。
package.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');
答案 1 :(得分:0)
我认为这与构建js代码的原理相矛盾。
node_modules中的所有软件包都必须已经构建,因为除软件包作者之外,没有人不知道如何构建此软件包。
如果mydep
是您的软件包,我建议先构建它,然后再发布到npm。否则,您可以将其派生或内置在安装后脚本中。