带有 Webpack webpack.config.ts 文件的 React Typescript 已经存在,mkdir



我正在尝试用打字稿为 React 创建一个模板。

为了在生产环境中使用它,我想将其与 webpack 捆绑在一起。

就我的研究而言,我应该正确配置所有内容。

如下

webpack.config.ts

const path = require('path');
module.exports = {
mode: "development",
entry: {
app: path.join(__dirname, 'src', 'index.tsx')
},
target: 'web',
module: {
rules: [
{
test: /.tsx?$/,
use: 'ts-loader',
include: [
path.resolve(__dirname, 'ts')
],
},
{
enforce: "pre",
test: "/.js$/",
loader: "source-map-loader"
}
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
filename: 'index.js',
path: path.resolve(__filename, 'js')
},
devtool: 'source-map',
};

tsconfig.json

{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./js/",
"preserveConstEnums": true,
"moduleResolution": "Node",
"sourceMap": true,
"removeComments": true,
"jsx": "react"
},
"include": [
"./ts/**/*"
]
}

包.json

{
"name": "reacttest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^14.0.14",
"@types/react": "^16.9.41",
"@types/react-dom": "^16.9.8",
"html-webpack-plugin": "^4.3.0",
"source-map-loader": "^1.0.0",
"ts-loader": "^7.0.5",
"typescript": "^3.9.5",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}

但每次我跑步时

webpack 

webpack --config webpack.config.ts

webpack webpack.config.ts

我收到以下错误消息

错误:EEXIST:文件已存在,mkdir '\Some\Path\WebWorkspace\reacttest\webpack.config.ts'

我做错了什么?

两者都尝试过

NodeJs 13.7

NodeJs v12.18.1

提前致谢

问题的根本原因是由于使用 .ts 作为 webpack.config文件的扩展名。

tsconfig.json 文件指示 Webpack 将所有 .ts 文件包含在处理范围内。这无意中也包括了 webpack.config.ts 文件。

这个问题可以通过将 webpack.config.ts 文件重命名为 webpack.config.js 来解决。

以下是在 Typescript 中实现 React JS 项目所需的所有文件的工作示例:

1. 项目的文件夹结构

react-ui
react-ui/package.json
react-ui/tsconfig.json
react-ui/webpack.config.json
react-ui/src
react-ui/src/index.tsx  ---> Main program
react-ui/www   ---> For static html file and images
react-ui/www/index.html
react-ui/www/images

2. package.json (包括 webpack、webpack-cli 和 webpack-dev-server(

{
"name": "react-ui",
"version": "1.0.0",
"description": "UI with React and Typescript",
"main": "index.tsx",
"scripts": {
"start": "webpack-dev-server --port 3000 --mode development --hot",
"build": "webpack --config webpack.config.js"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@types/node": "^14.0.13",
"@types/react": "^16.9.38",
"@types/react-dom": "^16.9.8",
"@types/webpack": "^4.41.17",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.0.2",
"css-loader": "^3.6.0",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"ignore-emit-webpack-plugin": "^2.0.2",
"mini-css-extract-plugin": "^0.9.0",
"ts-loader": "^7.0.5",
"typescript": "^3.9.5",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
}
}

3. tsconfig.json

{
"compilerOptions": {
"outDir": "./dist",
"target": "es5",
"module": "es6",
"jsx": "react",
"noImplicitAny": true,
"allowSyntheticDefaultImports": true
}
}

4. webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
mode: 'none',
entry: {
app: path.join(__dirname, 'src', 'index.tsx')
},
target: 'web',
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({ filename: '[name].css' }),
new HtmlWebpackPlugin({ template: path.join(__dirname, 'www', 'index.html') }),
new CopyPlugin({ patterns: [{ from: 'www/images', to: 'www/images' }] })
],
module: {
rules: [
{
test: /.tsx?$/,
use: 'ts-loader',
exclude: '/node_modules/'
},
{
test: /.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
},
{
test: /.(svg|png|jpg|gif|eot|ttf|woff|woff2)$/,
use: ["file-loader"]
}          
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /.css$/,
chunks: 'all',
enforce: true,
},
},
},
}
}

怎么用?

启动开发服务器:

> npm start   ---> Starts dev server at port # 3000

生产版本:

> npm run build   --> Creates production ready package in react-ui/dist folder

发现错误:

取代:

path: path.resolve(__filename, 'js')

跟:

path: path.resolve(__dirname, 'js')

相关内容

最新更新