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

标签 javascript reactjs typescript webpack

我的任务是修复我所从事的项目中的 Webpack 构建。我不是 Webpack 专家,并且在完成这项工作方面有些困难。该项目有一个带有 typescript 的非传统 react 前端。我一直在尝试创建此 webpack.common.js/dev.js/prod.js 设置。但似乎没有任何作用。 由于我在 webpack.common.js 中设置了条目,因此我假设该条目已设置,但 Webpack 仍尝试将其查找为 src/index.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:

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

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

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")
    }
});

我一直在使用 Webpack 指南进行生产设置,并尝试使用旧设置配置的不同部分。我的 package.json 脚本:

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

当我运行构建脚本时,我得到:

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 版本:4.39.2。 如果我将通用设置从 Webpack.Common 移动到 Webpack.prod,该条目会被识别,但它会崩溃,因为找不到条目文件中导入的任何模块。我现在不明白为什么会发生这种情况,因为该项目在旧设置下构建得很好。

最佳答案

解决了这个问题,这里的问题是我为 webpack.common.js 粘贴了错误的代码。当我尝试调试问题时,实际上粘贴了 webpack.prod.js 版本的内容。在原来的 webpack.common.js 中,我使用了 module.export 而不是 module.exports。我还缺少 resovle 参数:

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

关于javascript - 为我的 React typescript 项目配置 Webpack(Webpack 版本 4.39.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292723/

相关文章:

javascript - 获取数组中元素的数量(JS)

javascript - 在 React 中从父组件到子组件共享状态

angular - 如何在angular2 RC5中获取路由参数

angular - Angular 2 @Component 语法是 ES6 的一部分吗?

angular - AllowJS 不适用于 Angular

javascript - 选择元素的子集,并将它们包装在 div 中

javascript - 对唯一的有界数组进行排序的最有效方法?

javascript - 读取标签之间的文本

javascript - React 不会在状态改变时重新渲染

reactjs - 如何使用父组件的redux store