šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

webpack-dynamic-public-path

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-dynamic-public-path

Webpack dynamic public path plugin.

1.0.8
latest
Source
npm
Version published
Weekly downloads
8.2K
7.71%
Maintainers
1
Weekly downloads
Ā 
Created
Source

Dynamic Public Path Plugin

Replace publicPath inside a chunk, or chunks, to a variable.

NPM

Usage

:warning: This plugin is compatible only with webpack 4. If you use webpack 5 - check official guide https://webpack.js.org/guides/public-path/#automatic-publicpath

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

interface Options {
    externalPublicPath: string; // JavaScript code, here you can define some variable or condition.
    chunkNames: string[]; // Chunk names in which `publicPath` will be replaced.
}

new WebpackDynamicPublicPathPlugin(options: Options);

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 = "./";

Keywords

webpack

FAQs

Package last updated on 06 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts