# set-webpack-public-path plugin for webpack ## Installation `npm install @rushstack/set-webpack-public-path-plugin --save-dev` ## Mode 1: Using the `document.currentScript` API ### Overview This plugin wraps the entire webpack bundle in an immediately executed function expression (IIFE) that sets a variable to the value of `document.currentScript` and then injects code that extracts the current script's base path from the `src` attribute when setting the `__webpack_public_path__` variable. This is similar to the `output.publicPath = 'auto'` option, but differs in two important ways: 1. It does not contain any fallback logic to look at ` ... ``` In this case, because there is a `scriptRegex` variable defined on the page, the bundle will use its value (`/thescript/i`) to find the script. ###### Case 2 ```HTML ... ``` In this case, because there is not a `scriptRegex` variable defined on the page, the bundle will use the value passed into the `scriptName` option to find the script. ##### `getPostProcessScript = (variableName) => { ... }` A function that returns a snippet of code that manipulates the variable with the name that's specified in the parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified in-place - the processed value should not be returned. This is useful when non-entry assets are deployed to a parent directory or subdirectory of the directory to which the entry assets are deployed. For example, if this parameter is set to this function ```JavaScript getPostProcessScript = (variableName) => { return `${variableName} = ${variableName} + 'assets/';`; }; ``` the public path variable will have `/assets/` appended to the found path. Note that the existing value of the variable already ends in a slash (`/`). ##### `preferLastFoundScript = false` If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script. This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.