Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix prependTo and allow regex to be passed in #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 19 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,25 @@ 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) {
const matches = [];

assets.forEach((outputFilename) => {
this.settings_.prependTo.forEach(filter => {
if (filter instanceof RegExp) {
// filter is a regex
if (filter.test(outputFilename)) {
matches.push(outputFilename);
}
}
// filter is a string
if (outputFilename.indexOf(filter) > -1) {
matches.push(outputFilename);
}
});
});

assets = matches;
}

if (!assets.length) {
Expand Down