onvno / pokerface

日常技术文章阅读整理
3 stars 0 forks source link

20190506 - Webpack publicPath #21

Open onvno opened 5 years ago

onvno commented 5 years ago

publicPath可以更改加载的资源路径,但是如果需要更改runtime开发环境下的脚本资源加载,就需要用到__webpack_public_path__

then in my main entry script:

window.webpack_public_path = window.resourceBaseUrl; I tried the following as well:

just setting webpack_public_path in a script tag in the generated html both before and after the bundle script tag So how should I be setting `webpack_public_path ?

The only way I've got it working is by setting _webpack_require.p in my main entry file.

// JS脚本 // 入口文件加载之前 //获取 meta 元素 var $path = document.querySelector('meta[name="AppPublic"]'); //如果该 meta 元素存在,则获取其 content 的路径 //如果该 meta 元素不存在,则为本地路径 webpack_public_path = $path ? $path.getAttribute('content')+'/app/js/' : '/'; //从该参数的使用上可以了解到,上面 meta 元素,本地可以不添加 //js获取不到执行本地环境路径,js获取成功则配置具体环境路径

// __webpack_public_path__ 的添加位置需要注意
// 最好是添加在调用按需加载的文件里