【问题标题】:Use chunkhash only for some output files in webpack仅对 webpack 中的某些输出文件使用 chunkhash
【发布时间】:2018-12-06 10:48:52
【问题描述】:

我正在构建一个作为插件加载到其他应用程序中的应用程序。以语法或对讲方式思考。
因此,我将有一个客户需要在其应用程序中引用的 javascript 文件(加载程序)。
该文件又添加了一个 iFrame 并加载了我的 index.html,它引用了我的主包。

现在,我想让加载器的文件名中没有 chunkhash,但 bundle 应该有 chunkhash。

可能吗?如果有,怎么做?

我的 webpack 配置(从create-react-app-typescript 弹出):

entry: {
    app: [require.resolve('./polyfills'), paths.appIndexJs],
    loader: "./src/integration/loader.ts"
},
output: {
    // The build folder.
    path: paths.appBuild,
    // Generated JS file names (with nested folders).
    // There will be one main bundle, and one file per asynchronous chunk.
    // We don't currently advertise code splitting but Webpack supports it.
    filename: '[name].[chunkhash:8].js',
    chunkFilename: '[name].[chunkhash:8].chunk.js',
    // We inferred the "public path" (such as / or /my-project) from homepage.
    publicPath: publicPath,
    // Point sourcemap entries to original disk location (format as URL on Windows)
    devtoolModuleFilenameTemplate: info =>
        path
            .relative(paths.appSrc, info.absoluteResourcePath)
            .replace(/\\/g, '/'),
},

【问题讨论】:

  • 你能补充一点关于你的项目结构的信息吗?那真的很有帮助。
  • @AravindanVe 不确定您到底需要知道什么。你能澄清一下吗?

标签: javascript typescript webpack


【解决方案1】:

为什么不使用filename 的函数。

...
output: {
    ...
    filename: (chunkData) => {
        return chunkData.chunk.name === 'loader'
            ? '[name].js'
            : '[name].[chunkhash:8].js';
    },
}

https://webpack.js.org/configuration/output/#output-filename

【讨论】:

  • 这无济于事,因为这也会从应用入口点的构建结果中删除哈希。
  • 如果你的加载器只是加载 iframe 的几行代码,你真的需要使用 webpack 吗?你可以用普通的 es5 编写加载器吗?它只会加载你的 index.html,它使用正确的块哈希引用主包
  • 加载器比较多。它还集成了不同的编辑字段和所见即所得的编辑器,因此它确实需要 webpack 构建。生成的包大约是 iFrame 中加载的逻辑包大小的 1/3
  • @DanielHilgarth 我已经编辑了我的答案,这对你有用吗?
  • 看起来这正是我想要的。但是,我目前使用的是 webpack 3.x。需要先更新到 webpack 4.x
猜你喜欢
  • 2020-02-06
  • 2020-05-11
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 2012-11-22
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多