javascript - 为什么 webpack 说它已经生成了源映射,而实际上却没有?

标签 javascript node.js webpack httpserver source-maps

在下面的屏幕截图中,您可以看到我们的网站加载了两个主要的 .js 文件 - app 和 lib。我们的 .js 文件由 webpack 构建并输出用于生产,底部没有 //# sourceMappingURL=/path/to/script.js 。也没有返回像 X-SourceMap:/path/to/script.js.map 这样的 header 。

那么为什么 Chrome 会在尝试获取源映射时抛出控制台错误?

index.js 是什么?我们的网站上甚至没有该文件。

我们的网站是由 Docker 容器中的 http-server Node 模块组成的服务器,由 nginx 提供服务。

enter image description here

<小时/>

更新

Derek 下面的回答表明,webpack 实际上已在我们的输出文件中添加了 #sourcemap 注释,即使它没有生成源映射,也没有要求生成源映射。

那么为什么 webpack 在我们编译的 app.js 文件中放置了对不存在的源映射的引用?

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');
var rootPath = __dirname; // e.g.  ~/projects/ekaya
var srcPath = path.join(rootPath, 'src');
var distPath = path.join(rootPath, '../dist/client_gumtree/app');
var shared

Path = path.resolve('../shared');

module.exports =
{
  bail: true,
  cache: false,
  context: rootPath,
  debug: false,
  //devtool: 'source-map', //inline-source-map, https://webpack.github.io/docs/configuration.html#devtool
  target: 'web', //node, web
  // devServer:
  // {
  //   contentBase: distPath,
  //   historyApiFallback: true,
  //   outputPath: path.join(distPath, 'devServer'),
  //   hot : true,
  // },
  entry:
  {
    app: ['babel-polyfill', path.join(srcPath, 'core/index.ts')],
    lib: ['babel-polyfill', 'react', 'react-router', 'react-dom', 'lodash', 'history',
          'react-redux', 'redux-thunk', 'redux-api-middleware', 'redux']
  },
  output:
  {
    path: distPath,
    publicPath: '',
    filename: '/[name].js',
    pathInfo: true
  },
  resolve:
  {
    root: srcPath,
    extensions: ['', '.js',  '.jsx', '.ts', '.tsx'],
    modulesDirectories: ['node_modules', srcPath, 'typings']
  },
  module:
  {
    loaders:
    [
      {test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [srcPath, sharedPath]},
      {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', include: [srcPath, sharedPath]},
      {test: /\.ts$/, loader: 'babel-loader!ts-loader?cacheDirectory', include: [srcPath, sharedPath]},
      {test: /\.tsx$/, loader: 'babel-loader!ts-loader?cacheDirectory', include: [srcPath, sharedPath]},
      {test: /\.json$/, loader: 'json-loader'},
      {test: /\.scss$/, loaders: [
        'style?sourceMap',
        'css?modules&importLoaders=1&localIdentName=[name]-[local]---[hash:base64:5]',
        'cssnext',
        'resolve-url',
        'sass?sourceMap'
      ]},
      {test: /\.png$/, loader: 'file-loader'},
      {test: /\.jpg$/, loader: 'file-loader'},
      {test: /\.jpeg$/, loader: 'file-loader'},
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml&name=/[name].[ext]'},
      {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff&name=/[name].[ext]"},
      {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff&name=/[name].[ext]"},
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream&name=/[name].[ext]"},
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?name=/[name].[ext]"}
    ]
  },
  plugins:
  [
    new webpack.DefinePlugin({}) //these are our config settings & are injected in the build script when calling webpack using --define
    ,new CopyWebpackPlugin([ { from: path.join(srcPath, 'images'), to: 'images' } ]) //copy images to the build folder unchanged
    ,new HtmlWebpackPlugin({ inject: true, template: path.join(srcPath, 'index.html')  }) // this puts our script file into the main html page
    ,new webpack.NoErrorsPlugin() // don't emit bundles with errors
    ,new webpack.optimize.CommonsChunkPlugin('lib', '/lib.js')  // share common files
    ,new webpack.optimize.DedupePlugin() // share common files
    ,new webpack.optimize.AggressiveMergingPlugin()
  //  ,new webpack.optimize.UglifyJsPlugin({ sourceMap: false, mangle: false, minimize: true, beautify: false, comments: false,}) //Can't get this to work without error, so instead we uglify manually in the build script after webpack has run
  ]
};

最佳答案

它正在加载,因为您的文件包含 sourceMappingURL:

enter image description here

关于javascript - 为什么 webpack 说它已经生成了源映射,而实际上却没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39393551/

相关文章:

javascript - mongodb 保存在嵌套对象(node.js)应用程序中

node.js - npm 错误! cb() 从未调用过

webstorm - Webpack watch 无法在 Windows 上的 Webstorm 上运行?

node.js - 使用 sass 和 babel 的 Webpack 4 生产设置

javascript - dom 加载后包含外部脚本

javascript - pikachoose 轮播的不同 img src

node.js - 带有 etags 的 s3 流式传输( Node )

angularjs - 带有 Angular 1.x 和 ES5 的 Webpack

javascript - 全部展开或全部折叠

javascript - 如何根据计数值 : javascript 对单词进行排序