JSPM

webpack-dynamic-public-path

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5462
  • Score
    100M100P100Q182865F
  • License MIT

Webpack dynamic public path plugin.

Package Exports

  • webpack-dynamic-public-path

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (webpack-dynamic-public-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 = require("webpack-dynamic-public-path");

module.exports = {
    output: {
        publicPath: "publicPathPlaceholder",
    },
    plugins: [
        new WebpackDynamicPublicPathPlugin({
            externalPublicPath: "window.externalPublicPath"
        }),
    ]
}

bundle.js

// before
__webpack_require__.p = "publicPathPlaceholder";

// after
__webpack_require__.p = window.externalPublicPath;

Options

new WebpackDynamicPublicPathPlugin(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 = require("webpack-dynamic-public-path");

module.exports = {
    entry: {
        'index': path.resolve(__dirname, 'src', 'index.js'),
        'second-chunk': path.resolve(__dirname, 'src', 'second-chunk.js')
    },
    output: {
        filename: '[name].bundle.js',
        publicPath: "publicPathPlaceholder"
    },
    plugins: [
        new WebpackDynamicPublicPathPlugin({
            externalPublicPath: "window.externalPublicPathForMainChunk",
            chunkNames: ['index']
        }),
        new WebpackDynamicPublicPathPlugin({
            externalPublicPath: '"./"',
            chunkNames: ['second-chunk']
        }),
    ]
}

index.bundle.js

__webpack_require__.p = window.externalPublicPathForMainChunk;

second-chunk.bundle.js

__webpack_require__.p = "./";