Webpack 动态设置 __webpack_public_path__ 到底怎么用 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a Javascript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
Javascript 权威指南第 5 版
Closure: The Definitive Guide
Xheldon
V2EX    Javascript

Webpack 动态设置 __webpack_public_path__ 到底怎么用

  •  1
     
  •   Xheldon 2018-09-14 19:34:46 +08:00 8580 次点击
    这是一个创建于 2653 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看了中文英文文档, 说的是可以替代 output.publicPath, 需要将 output.publicPath 设置成'', 然后在入口文件设置 __webpack_public_path__ = 'http:///a.com'; 就行了;

    预期结果是 htmlwebpackplugin 打包后的 html 文件中的 js 引入地址是 'http"//a.com/xx.js'

    我是这么做的:

    webpack.config.js 这么设置:

    output: { filename: '[name].[hash:8].js', publicPath: '' } 

    htmlwebpackplugin 插件这么设置:

    let entries = glob.sync('./src/entries/**/index.js').reduce((prev, curr) => { prev['assets/' + curr.slice(14, -3)] = curr; return prev; }, {}); // entries 同时是 webpack 的 entry js let htmls = Object.keys(entries).map((html) => { return new HWP({ title: html.slice(-5, -1), // 这些个不重要, 不要在意 filename: `${html.slice(7, -6)}.html`, // 这些个不重要, 不要在意 template: './src/tpl/index.html', chunks: [html], // 这些个不重要, 不要在意 inject: 'body', // 这些个不重要, 不要在意 minify: false, data: { build: true } }); }); 

    入口文件这么设置:

     __webpack_public_path__ = 'http://a.com'; import a from './a.js'; import b from './b.js'; blabla... 

    然后满心欢喜的期待生成的 html 中的 script 中的 src 属性是 http://a.com/**/*.js 但是并没有带上 http://a.com

    翻看了 SO, github, 都说这样就 OK 了, 但是我没有得到预期结果 是我遗漏了什么, 还是我对这个 __webpack_public_path__ 理解有误?

    5 条回复    2019-12-11 01:15:43 +08:00
    readonly
        1
    readonly  
       2018-09-15 00:18:09 +08:00 via iPhone
    感觉是 import 语法自动提升了
    Xheldon
        2
    Xheldon  
    OP
       2018-09-17 13:49:38 +08:00
    @readonly 没, 试了下, 跟语法提升没关系 =_=
    surgit
        3
    surgit  
       2018-11-21 20:43:55 +08:00
    publicPath 要删掉,不能为空字符串 ''
    changfenglu
        4
    changfenglu  
       2018-12-01 15:43:31 +08:00
    htmlwebpackplugin 不支持__webpack_public_path__变量,可以使用 webpack-html-independent-public-path 这个插件给 htmlwebpackplugin 插件指定单独的 publicPath 处理
    geekdonie
        5
    geekdonie  
       2019-12-11 01:15:43 +08:00
    官方文档:Be aware that if you use ES6 module imports in your entry file the __webpack_public_path__ assignment will be done after the imports. In such cases, you'll have to move the public path assignment to its own dedicated module and then import it on top of your entry.js

    https://webpack.js.org/guides/public-path/#on-the-fly
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2645 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 40ms UTC 11:54 PVG 19:54 LAX 03:54 JFK 06:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86