简体   繁体   English

如何使用 webpack 加载库源映射?

[英]How to load library source maps using webpack?

I'm building two projects with webpack;我正在用 webpack 构建两个项目; one is a library for the other.一个是另一个的图书馆。

Is it possible to consume the sourcemaps from my library project when building my wrapper project?构建包装器项目时是否可以使用我的库项目中的源映射? I would like the ability to debug my library code from my wrapper UI.我希望能够从我的包装器 UI 调试我的库代码。

My build works correctly in that the library is built in. The only issue is sourcemaps.我的构建工作正常,因为库是内置的。唯一的问题是源映射。 The JavaScript I see in the browser debugger is uglified, because sourcemaps are unavailable.我在浏览器调试器中看到的 JavaScript 是丑化的,因为源映射不可用。

Snippet of my project structure:我的项目结构的片段:

+-- my-ui/
    +-- dist/
        +-- my-ui.js
        +-- my-ui.js.map
    +-- node_modules/
        +-- my-lib/
            +-- dist/
                +-- bundle.js
                +-- bundle.js.map

Snippet from webpack.config.js :来自webpack.config.js的片段:

module.exports = {
    entry: './src/js/main.jsx',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'my-ui.js',
        library: 'my-ui',
        libraryTarget: 'umd'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {test: /\.jsx?$/, loader: 'babel', include: path.join(__dirname, 'src')}
        ]
    },
    plugins: [
        new Clean('dist'),
        new HtmlWebpackPlugin({
            template: 'src/index.html',
            inject: true
        })
    ]
};

I finally figured out my issue...我终于知道我的问题了...

Thanks to @BinaryMuse for the tip on source-map-loader .感谢 @BinaryMuse 提供有关source-map-loader 的提示 This indeed was the right way to go, though it wasn't working for me initially.这确实是正确的方法,尽管它最初对我不起作用。

What I eventually realized is that I need to enable the source-map-loader for webpack in both "my-lib" and "my-ui".我最终意识到我需要在“my-lib”“my-ui”中为 webpack 启用source-map-loader Without source-map-loader in "my-lib" webpack config, the source-map-loader inside "my-ui" errors (with a warning message sadly) because it cannot locate source maps for transitive dependencies of "my-lib".如果没有source-map-loader在“我的-LIB”的WebPack配置时, source-map-loader里面的“我的UI”的错误(与可悲的是一条警告消息),因为它无法找到来源映射为“我的-LIB”的传递依赖. Apparently the source maps are so good that source-map-loader is able to peek at all aspects of the dependency tree.显然,源映射非常好,以至于source-map-loader能够查看依赖树的所有方面。

Also of note, I ran into an issue using source-map-loader in conjunction with react-hot-loader .另外值得注意的是,我在使用source-map-loaderreact-hot-loader遇到了一个问题。 See, react-hot-loader does not include source maps.看, react-hot-loader不包括源映射。 When source-map-loader tries to find them (because it's just scanning everything), it cannot and aborts everything.source-map-loader试图找到它们时(因为它只是扫描所有内容),它无法并中止所有内容。

Ultimately, I'd like source-map-loader to be more fault tolerant, but when set up correctly, it does work!最终,我希望source-map-loader具有更高的容错性,但是如果设置正确,它确实可以工作!

devtool: 'source-map',
module: {
    preLoaders: [
        {test: /\.jsx?$/, loader: 'eslint', exclude: /node_modules/},
        {test: /\.jsx?$/, loader: 'source-map', exclude: /react-hot-loader/}
    ],
    loaders: [
        {test: /\.jsx?$/, loader: 'raect-hot!babel', exclude: /node_modules/}
    ]
}

My answer is similar to @Jeff Fairley's and I had the same directory structure, with the only difference being that I was using module: { rules: [] } instead of his module: { preLoaders: [..], loaders: [...]} .我的回答类似于@Jeff Fairley 的,我有相同的目录结构,唯一的区别是我使用的是module: { rules: [] }而不是他的module: { preLoaders: [..], loaders: [...]} This is what I had to add to my webpack.config.js file:这是我必须添加到我的 webpack.config.js 文件中的内容:

  mode: 'development',
  devtool: 'eval-source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        use: ["source-map-loader"],
      }
    ]
  },

Then I ran然后我跑了

npm i -D source-map-loader

and I saw the TypeScript source code of the dependency I was using when clicking through tracebacks in Chrome's devtools.我在 Chrome 的开发工具中点击回溯时看到了我使用的依赖项的 TypeScript 源代码。 See the Webpack docs for source-map-loader .有关source-map-loader的信息,请参阅 Webpack 文档

You should be able to use any of the eval source map options that Webpack provides.您应该能够使用 Webpack 提供的任何eval源映射选项。

Really that just amounts to setting the right devtool option in your webpack.config.js for the my-lib project.实际上,这相当于在您的webpack.config.jsmy-lib项目设置正确的devtool选项。

devtool: 'eval',

eval and eval-source-maps should both work. evaleval-source-maps应该都可以工作。

See the Webpack source map documentation for the various options.有关各种选项,请参阅Webpack 源映射文档

I am using create-react-app and this is how I Fixed it (without running eject cmd)我正在使用create-react-app ,这就是我修复它的方式(不运行eject cmd)

Note : If your app is already overriding webpack config using react-app-rewired you can ignore first three steps.注意:如果您的应用程序已经使用react-app-rewired webpack config覆盖了webpack config您可以忽略前三个步骤。

  • npm i react-app-rewired -D - This will help you to override webpack configuration. npm i react-app-rewired -D - 这将帮助您覆盖webpack配置。
  • package.json - change your scripts, replace react-scripts with react-app-rewired package.json - 改变你的脚本,用react-app-rewired替换react-scripts
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject"
  }

  • config-overrides.js - create this file in the parent level of the app. config-overrides.js - 在应用程序的父级创建此文件。

  • npm i source-map-loader -D - To load source maps (assuming that your lib's dist has source map file). npm i source-map-loader -D - 加载源映射(假设你的 lib 的 dist 有源映射文件)。 It doesn't matter which build tool(ex: Rollup , webpack or parcel ) you use to generate sourcemap .不要紧,它的构建工具(如: Rollupwebpackparcel )用来生成sourcemap

  • Copy below code in config-overrides.jsconfig-overrides.js复制以下代码

module.exports = {
  webpack: (config, env) => {
    // Load source maps in dev mode
    if (env === 'development') {
      config.module.rules.push({
        test: /\.(js|mjs|jsx|ts|tsx)$/,
        use: ['source-map-loader'],
        enforce: 'pre',
      });

      // For `babel-loader` make sure that sourceMap is true.
      config.module.rules = config.module.rules.map(rule => {
        // `create-react-app` uses `babel-loader` in oneOf
        if (rule.oneOf) {
          rule.oneOf.map(oneOfRule => {
            if (
              oneOfRule.loader &&
              oneOfRule.loader.indexOf('babel-loader') !== -1
            ) {
              if (oneOfRule.hasOwnProperty('options')) {
                if (oneOfRule.options.hasOwnProperty('sourceMaps')) {
                  // eslint-disable-next-line no-param-reassign
                  oneOfRule.options.sourceMaps = true;
                }
              }
            }
          });
        }
        return rule;
      });
    }

    return config;
  },
};


  • Restart your app (if it's already running).重新启动您的应用程序(如果它已经在运行)。 source files get loaded in different locations, based on path in map file. source files根据地图文件中的路径加载到不同的位置。 Check all folders patiently :)耐心检查所有文件夹:)

Note : 1. Your source maps get loaded in one of the folder(ex : localhost:3000 or webpack:/// ) based on path it reads from xxx.js.map file.注意:1.您的源映射根据它从 xxx.js.map 文件读取的路径加载到文件夹之一(例如: localhost:3000webpack:/// )。 2. If you are using rollup for your libs, please make sure you give proper path in the configuration file (output.sourcemapPathTransform ), This will help to load sourcemaps in the proper location. 2. 如果你正在为你的库使用rollup ,请确保你在配置文件(output.sourcemapPathTransform)中提供了正确的路径,这将有助于在正确的位置加载sourcemaps

Great solution! 很好的解决方案! It works in our complicated working environment. 它可以在我们复杂的工作环境中工作。 Thanks for figuring it out. 感谢您解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM