如何使用npm/Yarn链接运行webpack 5的HRM?

xhv8bpkk  于 2023-02-04  发布在  Webpack
关注(0)|答案(1)|浏览(159)

webpack 5+中的hRM不能与npm链接一起工作。我在手表选项中有{ symlink : false }{ followsymlinks : true }
这里有一个针对此特定问题的GitHub问题:https://github.com/webpack/webpack/issues/6845
下面是我的webpack.conf文件的一个片段

{
  mode: 'development',
  watchOptions: {
    followSymlinks: true,
  },
  entry: [
     require.resolve('./polyfills'),
     require.resolve('babel-polyfill'),
     require.resolve('webpack-dev-server/client') + '?http://0.0.0.0:3001',
     require.resolve('webpack/hot/dev-server'),
     paths.appIndexJs,
  ],
  output: {
    // Add /* filename */ comments to generated require()s in the output.
    pathinfo: true,
    // This does not produce a real file. It's just the virtual path that is
    // served by WebpackDevServer in development. This is the JS bundle
    // containing code from all our entry points, and the Webpack runtime.
    filename: 'static/js/bundle.js',
    // There are also additional JS chunk files if you use code splitting.
    chunkFilename: 'static/js/[name].chunk.js',
    // This is the URL that app is served from. We use "/" in development.
    publicPath: '',

    path: path.resolve(__dirname, 'public'),
    // Point sourcemap entries to original disk location (format as URL on Windows)
    devtoolModuleFilenameTemplate: info =>
         path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
    },
    resolve: {
      modules: ['node_modules', paths.appNodeModules].concat(
      // It is guaranteed to exist because we tweak it in `env.js`
      process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
      ),
      symlinks: false,
      extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
    }
    target: ['web', 'es5'],
    alias: {
        'react-native': 'react-native-web',
        'react': path.resolve(__dirname, '../node_modules', 'react')
    },
    
}
6yt4nkrj

6yt4nkrj1#

在我的例子中,经过大量的努力和配置尝试,我发现这是由于缓存的原因。为了防止每次添加更改时重建时间显著增加,请使用以下配置缓存除本地库之外的所有node_modules(来自webpack文档):

snapshot: {
    managedPaths: [
      /^(.+?[\\/]node_modules[\\/](?!(@azure[\\/]msal-browser))(@.+?[\\/])?.+?)[\\/]/,
    ],
  },

在正则表达式中,用你的软件包的名字替换@azure[\\/]msal-browser(保留[\\/]),如果你想排除一个特定的软件包作者,只要保留@author就可以了。
保留symlink: falsefollowSymlinks: true似乎什么也没做。

相关问题