我在shared
目录中从另一个应用程序中获得了一些共享组件,这些目录已作为软件包添加到package.json
中。
我意识到我需要在Webpack配置中包含此目录,因为组件使用ES6,并且需要babel
进行编译
在谷歌搜索后,我将以下内容添加到了webpack config
module: {
rules: [
{
test: /\.js$|\.jsx$/,
exclude: /node_modules\/(?!shared\/).*/, // added this line
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from',
],
},
},
},
],
},
这应该明确指出要排除shared
目录的node_modules目录 。
我现在的问题是,webpack在unexpected token
目录中为诸如<span>
和<input>
之类的html标签抛出了shared
错误
有人知道为什么会这样吗?我该如何解决?
这是我的webpack配置:
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const htmlPlugin = new HtmlWebPackPlugin({
template: './source/index.html',
filename: 'index.html',
});
const buildPlugin = new webpack.DefinePlugin({
BUILD_INFO: JSON.stringify('BUILD-000'),
});
module.exports = {
entry: './source/client.js',
output: {
path: path.resolve('build'),
filename: 'bundled.js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.js$|\.jsx$/,
exclude: /node_modules\/(?!shared\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-default-from',
],
},
},
},
],
},
resolve: {
alias: {
api: path.resolve(__dirname, './source/js/api'),
config: path.resolve(__dirname, './source/js/config'),
components: path.resolve(__dirname, './source/js/components'),
init: path.resolve(__dirname, './source/js/init'),
context: path.resolve(__dirname, './source/js/context'),
views: path.resolve(__dirname, './source/js/views'),
utilities: path.resolve(__dirname, './source/js/utilities'),
helpers: path.resolve(__dirname, './source/js/helpers'),
store: path.resolve(__dirname, './source/js/store'),
styles: path.resolve(__dirname, './source/styles'),
},
extensions: ['.js', '.jsx'],
},
plugins: [htmlPlugin, buildPlugin],
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
},
devServer: {
disableHostCheck: true,
historyApiFallback: true,
port: 80,
hot: false,
host: '0.0.0.0',
stats: {
assets: true,
children: false,
chunks: false,
hash: false,
modules: false,
publicPath: false,
timings: true,
version: false,
warnings: true,
colors: true,
},
},
};