Dynamic Public Path Plugin
Replace publicPath inside a chunk, or chunks, to a variable.
Install
npm install --save-dev webpack-dynamic-public-path
Usage
⚠️ This plugin is compatible only with webpack 4.
webpack.config.js
const WebpackDynamicPublicPathPlugin = ; moduleexports = output: publicPath: "publicPathPlaceholder" plugins: externalPublicPath: "window.externalPublicPath"
bundle.js
// before__webpack_require__p = "publicPathPlaceholder"; // after__webpack_require__p = windowexternalPublicPath;
Options
options: object
Name | Type | Description |
---|---|---|
externalPublicPath |
{String} |
JavaScript code, here you can define some variable or condition. |
chunkNames |
{Array} |
Chunk names in which publicPath will be replaced. |
chunkNames
In case if you have several entries you should define plugin instance for each of them. Check example.
webpack.config.js
const WebpackDynamicPublicPathPlugin = ; moduleexports = entry: 'index': path 'second-chunk': path output: filename: '[name].bundle.js' publicPath: "publicPathPlaceholder" plugins: externalPublicPath: "window.externalPublicPathForMainChunk" chunkNames: 'index' externalPublicPath: '"./"' chunkNames: 'second-chunk'
index.bundle.js
__webpack_require__p = windowexternalPublicPathForMainChunk;
second-chunk.bundle.js
__webpack_require__p = "./";