Webpack 4:如何使用LESS获取CSS源地图?

时间:2018-03-16 23:50:39

标签: css webpack less webpack-4

我多年来一直试图让CSS源图在webpack中工作无济于事。我不确定链条中的哪些地方出了问题。我希望有人能指出我正确的方向。

这里发生了什么:

enter image description here

行号错误,实际上文件名也是错误的。 main.less只包含一堆@import个,实际的CSS位于不同的文件中。

以下是我的webpack.config.js。我用util.inspect打印了它,因为它是由很多不同的块组合而成的,我认为这样更容易阅读:

{ target: 'web',
  context: '/home/me/Projects/myproject',
  mode: 'production',
  entry: 
   { main: 
      [ '/home/me/Projects/myproject/assets/scripts/lib/autofocus-polyfill',
        '/home/me/Projects/myproject/assets/scripts/lib/matches-polyfill',
        'whatwg-fetch',
        'core-js/es6',
        'core-js/stage/4',
        '/home/me/Projects/myproject/assets/main' ],
     print: 
      [ '/home/me/Projects/myproject/assets/stylesheets/print' ] },
  output: 
   { path: '/home/me/Projects/myproject/www/assets',
     pathinfo: false,
     publicPath: '/assets/',
     crossOriginLoading: 'anonymous',
     filename: '[name].[chunkhash].js',
     chunkFilename: 'chunk.[chunkhash].js' },
  resolve: 
   { modules: 
      [ '/home/me/Projects/myproject/assets',
        '/home/me/Projects/myproject/node_modules/',
        '/home/me/Projects/myproject/vendor/' ],
     alias: 
      { 'jquery-ui/ui/widget': '/home/me/Projects/myproject/node_modules/jquery-ui/widget.js' },
     extensions: [ '.jsx', '.js', '.less', '.css' ] },
  node: { __filename: true, __dirname: true, fs: 'empty' },
  amd: { jQuery: true },
  module: 
   { rules: 
      [ { enforce: 'pre',
          test: /\.jsx?$/,
          loader: 'eslint-loader',
          include: '/home/me/Projects/myproject/assets',
          options: { cache: false, quiet: true, failOnError: true } },
        { test: /\.jsx?$/,
          loader: 'strip-loader',
          options: 
           { strip: 
              [ 'console.assert',
                'console.count',
                'console.dir',
                'console.dirxml',
                'console.group',
                'console.groupCollapsed',
                'console.groupEnd',
                'console.info',
                'console.log',
                'console.profile',
                'console.profileEnd',
                'console.table',
                'console.time',
                'console.timeEnd',
                'console.timeStamp' ] } },
        { test: /\.less$/,
          use: 
           [ '/home/me/Projects/myproject/node_modules/mini-css-extract-plugin/dist/loader.js',
             { loader: 'css-loader',
               options: 
                { sourceMap: true,
                  root: '/home/me/Projects/myproject/www',
                  localIdentName: '[name]_[local]--[hash:base62:5]',
                  importLoaders: 1 } },
             { loader: 'postcss-loader',
               options: 
                { ident: 'postcss',
                  sourceMap: true,
                  plugins: [Function: plugins] } },
             { loader: 'less-loader',
               options: { sourceMap: true, strictMath: true, strictUnits: true } } ] },
        { test: /\.css$/,
          use: 
           [ '/home/me/Projects/myproject/node_modules/mini-css-extract-plugin/dist/loader.js',
             { loader: 'css-loader',
               options: 
                { sourceMap: true,
                  root: '/home/me/Projects/myproject/www',
                  localIdentName: '[name]_[local]--[hash:base62:5]',
                  importLoaders: 1 } },
             { loader: 'postcss-loader',
               options: 
                { ident: 'postcss',
                  sourceMap: true,
                  plugins: [Function: plugins] } } ] },
        { test: /\.jsx?$/,
          include: 
           [ '/home/me/Projects/myproject/assets',
             '/home/me/Projects/myproject/node_modules/react-ajax-loader' ],
          loader: 'babel-loader',
          options: 
           { cacheDirectory: '/home/me/Projects/myproject/cache/babel',
             forceEnv: 'production' } },
        { test: /\.(jpe?g|png|gif|svg)($|\?)/i,
          loader: 'url-loader',
          options: { limit: 2048, name: '[name].[md5:hash:base32:10].[ext]' } },
        { test: /\.(eot|ttf|woff2?|htc)($|\?)/i,
          loader: 'file-loader',
          options: { name: '[name].[md5:hash:base32:10].[ext]' } } ] },
  plugins: 
   [ NoEmitOnErrorsPlugin {},
     HashedModuleIdsPlugin {
       options: 
        { context: null,
          hashFunction: 'md4',
          hashDigest: 'base64',
          hashDigestLength: 4 } },
     LoaderOptionsPlugin { options: { options: {}, test: { test: [Function: test] } } },
     MiniCssExtractPlugin {
       options: 
        { filename: '[name].[hash].css',
          chunkFilename: 'chunk.[chunkhash].css' } },
     ProvidePlugin {
       definitions: 
        { '$': 'jquery',
          jQuery: 'jquery',
          timezoneJS: 'timezone-js',
          React: 'react',
          _: 'lodash' } },
     DefinePlugin {
       definitions: { 'process.env': { NODE_ENV: '"production"' } } },
     { apply: [Function: apply] } ],
  bail: true,
  devtool: 'source-map',
  optimization: 
   { minimize: true,
     minimizer: 
      [ UglifyJsPlugin {
          options: 
           { test: /\.(js?)(\?.+)?$/i,
             warningsFilter: [Function],
             extractComments: /^!|\b(copyright|license)\b|@(preserve|license|cc_on)\b/i,
             sourceMap: true,
             cache: true,
             parallel: true,
             include: undefined,
             exclude: /\.min\.js$/,
             uglifyOptions: 
              { output: { beautify: false },
                compress: { warnings: false, drop_console: true },
                mangle: true,
                sourceMap: true } } } ],
     splitChunks: { chunks: 'all' },
     runtimeChunk: true } }

在中间某处你可以看到.less的加载器。扩大了,它看起来像这样:

[
    {
        loader: 'css-loader',
        options: {
            sourceMap: true,
            root: publicDir,
            localIdentName: '[name]_[local]--[hash:base62:5]',
            importLoaders: 1,
        }
    },
    {
        loader: 'postcss-loader',
        options: {
            ident: 'postcss',
            sourceMap: true,
            plugins: loader => [
                require('autoprefixer')({
                    browsers: ['> 1%', 'last 2 Firefox versions', 'last 2 Chrome versions', 'last 2 Edge versions', 'last 2 Safari versions', 'Firefox ESR', 'IE >= 8'],
                }),
                require('postcss-opacity'),
                require('cssnano')({
                    discardComments: {
                        remove: comment => !copyrightPatt.test(comment),
                    }
                })
            ],
        }
    },
    {
        loader: 'less-loader',
        options: {
            sourceMap: true,
            strictMath: true,
            strictUnits: true,
        }
    }
]

正如您所看到的,我有3个不同的加载器(css​​-loader,postcss-loader和less-loader)。他们都sourceMap设置为true,所以源图应该返回原始来源,对吗?

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

删除cssnano时,地图似乎合理,但准确性不高,而且我的配置非常简单。

.patent {        // line1
    .child {}    // map to line1
    .childs {}   // map to line1
}