Webpack 之 output 输出路径

发布于 2025-01-30 04:47:14 字数 3283 浏览 8 评论 0

基本配置

output: {
  filename: '[name].js',
  path:__dirname + '/dist'
}

使用 CDN 和 hash

output: {
  path: '/home/proj/cdn/assets/[fullhash]',
  publicPath: 'https://cdn.example.com/assets/[fullhash]/'
}

webpack4.x 版本及之前为 hash,webpack5.x 中使用 fullhash 和 hash 均可

publicPath

按需加载(on-demand-load) 或加载外部资源(external resources)(如图片、文件等)的相对加载路径前缀 ::: tip 在编译时(compile time) 无法知道输出文件的 publicPath 的情况下,可以留空
然后在入口文件(entry file) 处使用自由变量(free variable) webpack_public_path ,以便在运行时(runtime) 进行动态设置 :::

environment

告诉 webpack 在生成的运行时代码中可以使用哪个版本的 ES 特性。

output: {
  environment: {
    // The environment supports arrow functions ('() => { ... }').
    arrowFunction: true,
    // The environment supports BigInt as literal (123n).
    bigIntLiteral: false,
    // The environment supports const and let for variable declarations.
    const: true,
    // The environment supports destructuring ('{ a, b } = obj').
    destructuring: true,
    // The environment supports an async import() function to import EcmaScript modules.
    dynamicImport: false,
    // The environment supports 'for of' iteration ('for (const x of array) { ... }').
    forOf: true,
    // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
    module: false,
  }
}

其他

output: {
  library: 'someLibName',
  libraryTarget: 'umd',
  // 每个输出 bundle 的名称(不会影响那些「按需加载 chunk 或 loader 创建的文件」的输出文件)
  filename: '[name].[contenthash].[id].bundle.js'
  filename: (pathData) => {
    return pathData.chunk.name === 'main' ? '[name].js': '[name]/[name].js';
  },
  // 添加注释
  auxiliaryComment: 'Test Comment',
  // 为 HTML 的 <script> 标签添加 charset="utf-8" 标识,默认为 true
  charset: true,
  // 定义动态加载(异步加载)的 chunk 的名字
  chunkFilename: [id].js,
  // 请求 chunk 的 timeOut 时间,默认 120000
  chunkLoadTimeout: 120000,
  // 用于加载 chunk 的全局变量(一般为 JSONP 函数)
  chunkLoadingGlobal: 'myCustomFunc',
  // 加载 chunk 的方法.(默认值有 'jsonp' (web),'importScripts' (WebWorker),'require' (sync node.js),'async-node' (async node.js)
  chunkLoading: 'async-node',
  // chunk 的格式.( 默认包含 'array-push' (web/WebWorker),'commonjs' (node.js))
  chunkFormat: 'commonjs',
  // 为入口启用 chunk 加载类型列表
  enabledChunkLoadingTypes: ['jsonp', 'require'],
  // 告诉 webpack 启用 cross-origin 属性 加载 chunk
  crossOriginLoading: 'anonymous',
  // asset Modules 的 filename
  assetModuleFilename: '[hash][ext][query]',
  // 使用哪个全局对象来挂载 library
  globalObject: 'this',
  // 自定义热更新 chunk 的文件名
  hotUpdateChunkFilename: '[id].[fullhash].hot-update.js',
  // 只在 target 设置为 'web' 时使用,用于加载热更新(hot update) 的 JSONP 函数
  hotUpdateGlobal: 'myCustomFunc',
  // 如果一个模块是在 require 时抛出异常,告诉 webpack 从模块实例缓存(require.cache) 中删除这个模块
  strictModuleExceptionHandling: false,
  // 入口点可用的 library 类型列表
  enabledLibraryTypes: ['module'],
  // 告诉 webpack 添加 IIFE 外层包裹生成的代码.
  iife: true
  // 是一个实验性的功能, 想要使用的话,通过设置 experiments.outputModule 为 true
  // 设置 output.iife 为 false, output.libraryTarget 为 'module', output.scriptType 为 'module' 和 terserOptions.module 为 true
  module: true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
29 人气
更多

推荐作者

身边

文章 0 评论 0

qq_oxT0yE

文章 0 评论 0

卷着的草席

文章 0 评论 0

£冰雨忧蓝°

文章 0 评论 0

我还不会笑

文章 0 评论 0

Unbroken

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文