Skip to content

Commit

Permalink
fix prependTo so that it is a whitelist and add the ability to pass i…
Browse files Browse the repository at this point in the history
…n a Regex
  • Loading branch information
Jack Brown committed May 14, 2019
1 parent a121da4 commit 0608e84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,37 @@ This project's Git repository comes with a working demo project.
## Options

### `prependTo`
* *Type:* `array|string`
* *Type:* `string` | `regex` | `(regex|string)[]`

By default we prepend the player to the first file with a `.js` extension that is listed as an output. If you only want to prepend to certain file(s) pass an array or string along with the filename of the files you want to prepend the player to.
By default we prepend the player to the first file with a `.js` extension that is listed as an output.

If you only want to prepend to certain file(s) you may pass a string or regex that will be used to match against webpacks output filenames array. You can optionally pass an array in `prependTo`, and the player will be prepended to any file that matches any item in the array (regex or string).

```js
// Example of matching files output by webpack when code splitting
const filenames = ['bundle.js', 'bundle.1.js', 'bundle.2.js', 'playerBundle.js'];

// webpack.config.js
module.exports = {

// ... Additional configuration for entry, output, etc.

plugins: [
new PlayerLoader({
accountId: '12345678910',
// matches bundle.js only
// prependTo: 'bundle.js',

// matches all files starting with bundle
// prependTo: /^bundle/,

// matches both bundle.js and playerBundle.js only
// prependTo: [/^bundle\.js/, /^player/],
})
]
};

```

### `accountId`
* **REQUIRED**
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ class PlayerLoaderPlugin {
assets = [assets[0]];

// if prependTo is specified though, we prepend to anything that is listed
} else {
assets = assets.filter((filename) => this.settings_.prependTo.indexOf(filename) === -1);
} else if (this.settings_.prependTo.length > 0) {
assets = assets.forEach((outputFilename) => {
this.settings_.prependTo.forEach(filter => {
if (filter instanceof RegExp) {
// filter is a regex
return filter.test(outputFilename);
}
// filter is a string
return outputFilename.indexOf(filter) > -1;
});
});
}

if (!assets.length) {
Expand Down

0 comments on commit 0608e84

Please sign in to comment.