最近,我正在尝试使用webpack创建一个React TS项目,但由于某种原因它无法运行该项目:
--webpack.config.ts
--package.json
--tsconfig-for-webpack-config.json
--src
|
--client
|
--tsconfig.json
--src
|
--app.tsx
--index.tsx
--index.css
--index.html
--tsconfig.json
我的webpack.config.ts包含以下内容:
import path from 'path';
import { Configuration } from 'webpack';
import ManifestPlugin from 'webpack-manifest-plugin';
import cssnano from 'cssnano';
import HtmlWebpackPlugin from 'html-webpack-plugin';
/**
* Server config for webpack
*/
const IS_DEV = process.env.NODE_ENV !== 'production';
const SERVER_PORT = process.env.PORT || 4000;
const WEBPACK_PORT = 9000;
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const targets = IS_DEV ? { chrome: '79', firefox: '72' } : '> 0.25%, not dead';
const config: Configuration = {
mode: IS_DEV ? 'development' : 'production',
devtool: IS_DEV ? 'inline-source-map' : false,
entry: './src/client/src/index.tsx',
target: 'web',
output: {
path: path.join(__dirname, 'dist', 'statics'),
filename: `[name]-[hash:8]-bundle.js`,
chunkFilename: '[name]-[hash:8]-bundle.js',
publicPath: '/statics/',
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.css', '.scss'],
},
optimization: {
minimize: !IS_DEV,
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
priority: 10,
},
},
},
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: [/node_modules/, nodeModulesPath],
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/env', { modules: false, targets }],
'@babel/react',
'@babel/typescript',
],
plugins: [
'@babel/proposal-numeric-separator',
'@babel/plugin-transform-runtime',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-object-rest-spread',
],
},
},
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]___[hash:base64:5]',
},
sourceMap: IS_DEV,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: IS_DEV,
plugins: IS_DEV ? [cssnano()] : [],
},
},
],
},
{
test: /.jpe?g$|.gif$|.png$|.svg$|.woff$|.woff2$|.ttf$|.eot$/,
use: 'url-loader?limit=10000',
},
],
},
devServer: {
port: WEBPACK_PORT,
overlay: IS_DEV,
open: IS_DEV,
},
plugins: [
new ManifestPlugin(),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'client', 'src', 'index.html'),
}),
],
externals: {
react: 'React',
'react-dom': 'ReactDOM',
},
};
export default config;
和我的tsconfig-for-webpack-config.json包含:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"esModuleInterop": true
}
}
当我运行命令时:
"dev": "cross-env NODE_ENV=production TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack -p"
在我的控制台中,我没有收到任何错误,但是当它运行我的网络服务器时,我得到了:
我尝试了几种配置,但是没有得到服务器渲染的结果,所以有想法的人吗?