You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将全栈TypeScript应用打包为单HTML、JS、CSS文件?Webpack报错求助

Webpack打包TypeScript应用时模块找不到问题

我最初开发了一个简单的TypeScript应用,用tsc将TypeScript编译为JavaScript。随着应用规模扩大,我需要把所有资源打包成单个HTML、JS和CSS文件,尝试使用Webpack时遇到了模块找不到的错误。

我的webpack.config.js配置文件如下:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.ts',
    mode: 'development',
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './src',
    },
    module: {
        rules: [
            {
                test: /\.html$/,
                use: ['html-loader'],
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
                include: [path.resolve(__dirname, 'src')],
            },
            {
                test: /\.[jt]sx?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-env',
                            '@babel/preset-typescript',
                        ],
                    },
                },
            },
        ],
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx'],
    },
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'public'),
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/public/pages/report/index.html',
            favicon: './src/public/media/favicon-review.ico',
        }),
    ],
};

项目文件夹结构参考本地目录(原截图链接已移除)

编译时出现的错误信息:

Module not found: Error: Can't resolve '/static/pages/report/styles/index.css' in 'C:\Users\User\Desktop\review\src\public\pages\report' 
at C:\Users\User\Desktop\review\node_modules\webpack\lib\Compilation.js:2094:28
at C:\Users\User\Desktop\review\node_modules\webpack\lib\NormalModuleFactory.js:895:13

内容的提问来源于stack exchange,提问作者Fahmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:22:15