简体   繁体   English

为我的 React typescript 项目配置 Webpack(Webpack 版本 4.39.2)

[英]Configuring Webpack for my React typescript project (Webpack Version 4.39.2)

I have been tasked by fixing the Webpack build in a project I work on.我的任务是在我从事的项目中修复 Webpack 构建。 I'm no Webpack expert and am struggling somewhat to make this work.我不是 Webpack 专家,并且正在努力完成这项工作。 The project has a untraditional react frontend with typescript.该项目有一个带有打字稿的非传统反应前端。 I have been trying to create this webpack.common.js/dev.js/prod.js setup.我一直在尝试创建这个 webpack.common.js/dev.js/prod.js 设置。 But nothing seems to work.但似乎没有任何效果。 Since I have the entry set in webpack.common.js I would assume that the entry would be set, but Webpack still tries to find it as src/index.js.因为我在 webpack.common.js 中设置了条目,所以我假设该条目将被设置,但 Webpack 仍然尝试将其作为 src/index.js 查找。 Here is my webpack.dev.js:这是我的 webpack.dev.js:

const merge = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
    mode: 'development',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: '../FrontEnd/dist/base'
    }
});

Webpack.prod.js: Webpack.prod.js:

const merge = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
    mode: 'production'
});

webpack.common.js: webpack.common.js:

const merge = require('webpack-merge');
const common = require('./webpack.common.js');
const webpack = require('webpack');
const path = require('path');
const BitBarWebpackProgressPlugin = require("bitbar-webpack-progress-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = merge(common, {
    mode: 'production',

    entry: {
        app: ["babel-polyfill", "./src/NordicBase/NKportal/portal.tsx", "core-js/fn/promise"],
        vendor: ['react', 'react-dom']
    },

    plugins: [
        new CleanWebpackPlugin(),
        new BitBarWebpackProgressPlugin(),
        new webpack.ProvidePlugin({
            'Promise': 'es6-promise',
            'fetch': 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch'
        }),
        new HtmlWebpackPlugin({
            title: 'Production'
        })
    ],

    node: {
        fs: "empty"
    },

    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            {
                test: /\.tsx?$/,
                loader: "awesome-typescript-loader"
            },

            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader?name=fonts/[name].[ext]"
            },
            {
                test: /\.css$/,
                use: [{
                    loader: "style-loader"
                },
                {
                    loader: "css-loader"
                }
                ]
            },
            {
                test: /\.scss$/,
                // include: [
                //     path.resolve(__dirname, "src/css")
                // ],
                use: [
                    "style-loader", // creates style nodes from JS strings
                    "css-loader", // translates CSS into CommonJS
                    "sass-loader" // compiles Sass to CSS
                ]
            },
            {
                test: /\.(png|jp(e*)g|svg)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                    //limit: 8000, // Convert images < 8kb to base64 strings
                    name: 'images/[hash]-[name].[ext]'
                }
                }]
            } 
        ],

    },

    output: {
        filename: "[name].bundle.js",
        path: path.resolve("../FrontEnd/dist/Base")
    }
});

I have been using Webpack guide for production setup in combination with trying to use different parts of the configuration of the old setup.我一直在使用 Webpack 指南进行生产设置,并尝试使用旧设置的不同配置部分。 My scripts from package.json:我来自 package.json 的脚本:

"start": "webpack-dev-server --open --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"

when I run the the build script i get:当我运行构建脚本时,我得到:

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.

ERROR in Entry module not found: Error: Can't resolve './src' in 'C:\Users.......'

Webpack version : 4.39.2. Webpack 版本:4.39.2。 If I move common setup from Webpack.Common to Webpack.prod the entry is recognized but it crashes because none of the modules being imported in the entry files are found.如果我将通用设置从 Webpack.Common 移动到 Webpack.prod 条目被识别但它崩溃,因为没有找到条目文件中导入的模块。 I don't now why this happens since the project is building fine with the old setup.我现在不知道为什么会发生这种情况,因为该项目在旧设置下构建良好。

Solved this issue, problem here was that I pasted the wrong code for webpack.common.js.解决了这个问题,这里的问题是我为 webpack.common.js 粘贴了错误的代码。 Actually pasted the content for a version of webpack.prod.js as I was trying to debug the issue.当我试图调试问题时,实际上粘贴了 webpack.prod.js 版本的内容。 in the original webpack.common.js I had modules.export instead of module.exports.在原来的 webpack.common.js 中,我有 modules.export 而不是 module.exports。 I was also missing resovle argument:我也缺少 resovle 参数:

resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json", ".css"]
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM