【问题标题】:Webpack generates JavaScript source maps instead of TypeScriptWebpack 生成 JavaScript 源映射而不是 TypeScript
【发布时间】:2021-10-02 11:37:25
【问题描述】:

我正在开发一个 TypeScript 应用程序(Angular 11,物有所值),使用 Webpack 捆绑代码。我已经设置了 webpack 来生成源映射,它确实如此,但是源映射是为 JavaScript 代码生成的,而不是为原始的 TypeScript 生成的,好像执行顺序被弄乱了一样。 sourcemap 标记在 JS 文件的末尾正确添加,并且地图被正确加载,所以这似乎是我这边的配置错误而不是其他任何东西。

我对 webpackish 不太流利,下面的文件是从我之前为其他项目制作的其他配置编译而来的,并且在那里运行良好,所以我想这次我做错了。任何人都可以看到任何东西吗?非常感谢。

Webpack 4.44.2
打字稿 4.0.5
Nodejs 12.20.1
角 11.0.6
通天塔 7.12.10

const {AngularCompilerPlugin} = require('@ngtools/webpack');
const HashedModuleIdsPlugin = require('webpack/lib/HashedModuleIdsPlugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const SuppressChunksPlugin = require('suppress-chunks-webpack-plugin').default;
const TerserPlugin = require('terser-webpack-plugin');

const dev = !!process.argv.includes('--watch');

module.exports = function() {
    const srcDir = path.resolve(__dirname);
    const buildDir = path.resolve(__dirname, 'build');
    const outputDir = process.env.npm_config_out ? path.resolve(process.cwd(), outDirArg) : buildDir;

    const sassLoader = {
        loader: 'sass-loader',
        options: {sourceMap: dev}
    };

    return [{
        mode: 'production',
        devtool: dev ? 'source-map' : undefined,
        target: 'web',
        watch: dev,
        entry: {
            something: path.resolve(srcDir, 'something.module.ts'),
        },
        module: {
            rules: [{
                test: /\.ts$/,
                include: [srcDir],
                exclude: /\.spec\.ts$|\.test\.ts$/,
                use: ['@ngtools/webpack'],
                parser: {
                    amd: false,
                    commonjs: true,
                    system: false,
                    harmony: true,
                    requireInclude: false,
                    requireEnsure: false,
                    requireContext: false,
                    browserify: false,
                    requireJs: false,
                    node: false
                }
            }, {
                test: /\.js$/,
                include: [srcDir],
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [['@babel/preset-env', {
                                'targets': '> 0.25%, not dead, ie 11',
                                'useBuiltIns': 'entry',
                                'corejs': 3
                            }]],
                        }
                    }
                ]
            }, {
                test: /\.component\.scss$/,
                include: [srcDir],
                use: ['raw-loader', sassLoader]
            }, {
                test: /\.html$/,
                include: [srcDir],
                use: [{
                    loader: 'html-loader',
                    options: {
                        minimize: {
                            removeComments: true,
                            collapseWhitespace: true,
                            conservativeCollapse: true,
                            caseSensitive: true,
                            minifyJS: false,
                            minifyCSS: false,
                            removeAttributeQuotes: false,
                        },
                        attributes: false
                    }
                }]
            }, {
                test: /\.bdf$/,
                include: [srcDir],
                use: ['raw-loader']
            }]
        },
        output: {
            path: outputDir,
            filename: '[name].js',
            sourceMapFilename: '[file].map'
        },
        plugins: [
            new AngularCompilerPlugin({
                skipCodeGeneration: false,
                typeChecking: true,
                tsConfigPath: path.resolve(srcDir, 'tsconfig.json'),
                compilerOptions: {
                    entryModule: 'something.module#SomethingModule',
                }
            }),
            new HashedModuleIdsPlugin(),
            new MiniCssExtractPlugin({
                filename: '[name].[hash].css',
                chunkFilename: '[name].[hash].css',
            }),
            new SuppressChunksPlugin(['css']),
        ],
        optimization: {
            minimize: true,
            minimizer: [
                new OptimizeCSSAssetsPlugin({}),
                new TerserPlugin({
                    sourceMap: true,
                    parallel: true,
                    terserOptions: {
                        mangle: {
                            toplevel: true
                        },
                        module: true,
                        compress: {
                            booleans_as_integers: false,
                            toplevel: true,
                            keep_fargs: false,
                            drop_debugger: false,
                        },
                        parse: {},
                        output: {
                            comments: false
                        }
                    }
                })
            ],
        }
    }];
};```

【问题讨论】:

  • 您看到什么症状让您相信源地图生成不正确?
  • @Monkpit 当我在 DevTools Sources 部分打开我的一个源文件时,我看到的是编译后的代码,而不是原始代码。而且文件名不是whatever.component.ts,而是whatever.component.js。如果我检查源映射文件本身,我会看到相同的结果。
  • webpack.js.org/guides/typescript 这有什么启示吗?很抱歉,我无论如何都不是 webpack 大师。

标签: typescript webpack source-maps


【解决方案1】:

我的问题已经解决了,我只需将它添加到配置中。

resolve: {
    extensions: ['.ts', '.js'],
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 2020-08-03
    • 1970-01-01
    • 2019-01-11
    • 2020-06-30
    • 2015-11-05
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多