【问题标题】:React Typescript with Webpack webpack.config.ts file already exists, mkdirReact Typescript 与 Webpack webpack.config.ts 文件已经存在,mkdir
【发布时间】:2020-10-17 20:45:08
【问题描述】:

我正在尝试使用 Typescript 为 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/**/*"
    ]
}

package.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

提前致谢

【问题讨论】:

    标签: node.js reactjs typescript webpack


    【解决方案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
    

    【讨论】:

    • 您好,感谢您的快速回复。但不幸的是,这并不能解决问题。我尝试将文件名更改为 ts 但每当我尝试构建它时都会出现相同的错误。
    • 不管我怎么称呼它,错误总是:错误:EEXIST:文件已经存在,mkdir '\Some\Path\WebWorkspace\reacttest\whateverNameIUse'
    【解决方案2】:

    发现错误:

    替换:

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

    与:

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

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 2020-01-23
      • 2019-01-11
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      相关资源
      最近更新 更多