javascript - Webpack-开发工具 : source-map for CSS and eval-source-map for JS?

标签 javascript css webpack source-maps

如果我使用 devtool: 'source-map' 它与 CSS 配合得很好:

I'm getting the file, row and everything is awesome

但是,我的 JavaScript 变量名并不有趣:

Mangeled JavaScript vars

所以,如果我使用 devtool:eval-source-maps。生活是美好的——调试 JS。但是我的 CSS 然后指向大 bundle-css 而不是单独的文件:

Great times debugging JS!

This is no fun...

我怎么吃我的蛋糕呢?! 我想在同一构建期间对 JS 使用 eval-source-map,对 CSS 使用 source-map。

这是我的 webpack 配置(使用撰写本文时的最新版本):

/* eslint no-console:"off" */
const {resolve} = require('path');
const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineManifestWebpackPlugin = require('inline-manifest-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// const OfflinePlugin = require('offline-plugin/runtime').install();

module.exports = (env) => {
    const {ifProd, ifNotProd} = getIfUtils(env);
    const config = {
        context: resolve('src'),
        entry: './js/index/index.js',
        output: {
            filename: 'bundle.[name].[hash].js',
            path: resolve('dist'),
            pathinfo: ifNotProd()
        },
        // devtool: 'source-map' //This works for CSS but not JS,
        devtool: 'eval-source-map' //This works for JS but not css
        module: {
            rules: [
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /(node_modules)/
                },
                {
                    test: /\.js$/,
                    loader: 'eslint-loader',
                    exclude: /(node_modules)/
                },
                {
                    test: /\.css$/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [
                            {
                                loader: 'css-loader',
                                options: {importLoaders: 1, minimize: false, sourceMap: true}
                            }
                        ]
                    })
                },
                {
                    test: /\.html$/,
                    use: {
                        loader: 'html-loader'
                    }
                },
                {
                    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    loader: 'url-loader?limit=10000&mimetype=application/font-woff',
                    query: {
                        name: 'static/media/files/[name].[hash:8].[ext]'
                    }
                }, {
                    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    loader: 'file-loader',
                    query: {
                        name: 'static/media/fonts/[name].[hash:8].[ext]'
                    }
                },
                {
                    test: /\.(gif|jpe?g|png)$/,
                    loader: 'url-loader?limit=25000',
                    query: {
                        limit: 10000,
                        name: 'static/media/images/[name].[hash:8].[ext]'
                    }
                }
            ]
        },
        plugins: removeEmpty([
            // ifProd(new InlineManifestWebpackPlugin()),
            // ifProd(new webpack.optimize.CommonsChunkPlugin({
            //     names: ['manifest']
            // })),
            new HtmlWebpackPlugin({
                template: './index.html'
                // inject: 'head'
            }),
            // new OfflinePlugin(),
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: ifProd('"production"', '"development"')
                }
            }),
            new UglifyJSPlugin({
                    parallel: {
                        cache: true
                    },
                    sourceMap: true
                }
            ),
            new OptimizeCssAssetsPlugin({
                cssProcessorOptions: {
                    preset: 'default',
                    map: {inline: false}
                }
            }),
            new ExtractTextPlugin('styles.[name].[hash].css'),
            new BundleAnalyzerPlugin(),
            new ProgressBarPlugin(),
        ])
    };
    if (env.debug) {
        console.log(config);
        debugger // eslint-disable-line
    }
    return config;
};

最佳答案

这似乎可行:

{
  ...webpackConfig,
  devtool: false,
  plugins: [
    new webpack.SourceMapDevToolPlugin({
      test: /\.s?[ac]ss$/
    }),
    new webpack.EvalSourceMapDevToolPlugin({
      test: /\.(vue|[jt]sx?)$/
    }),
  ]
}

这将为您提供用于 css、scss 和 sass 的 inline-source-map 以及用于 vue、js、jsx、ts 和 tsx 的 eval-source-map

关于javascript - Webpack-开发工具 : source-map for CSS and eval-source-map for JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903805/

相关文章:

php - 将图像上传到数据库不起作用

css - 验证 CSS 是有效语法

javascript - 如何在使用 webpack 编译期间将文件文本导入到 javascript 中的变量中?

javascript - 如何让 jQuery UI Slider 小部件自动播放多年的文本时间轴?

javascript - Jquery 平滑滚动 Onclick

javascript - var 语句应该放在函数的顶部吗?

html - 将背景颜色扩展到整个 html 电子邮件

javascript - 如何使用Howler JS实现Seek功能

javascript - 我的新 Reactjs 应用程序出现 404 错误,它是否正在寻找 main.js 文件?

javascript - Typescript Webpack 模块解析仅在运行时失败