Skip to content

Commit b5617e6

Browse files
sagrawal31brandonocasey
authored andcommitted
feat: Make this plugin backwards compatible with webpack 3 (#16)
1 parent 05472ba commit b5617e6

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The official webpack plugin for the Brightcove Player.
1212
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
1313

1414
- [Installation](#installation)
15+
- [Compatibility](#compatibility)
1516
- [Basic Usage](#basic-usage)
1617
- [How it Works](#how-it-works)
1718
- [Limitations](#limitations)
@@ -36,6 +37,10 @@ To install, use:
3637
npm install --save-dev @brightcove/player-loader-webpack-plugin
3738
```
3839

40+
## Compatibility
41+
42+
This webpack plugin supports webpack 3.x & webpack 4.x
43+
3944
## Basic Usage
4045
First, require the plugin at the top of your `webpack.config.js`:
4146

src/index.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,48 @@ class PlayerLoaderPlugin {
3333
}
3434

3535
apply(compiler) {
36-
compiler.hooks.emit.tapAsync('PlayerLoaderPlugin', (compilation, callback) => {
37-
request.get(this.playerUrl).then((playerjs) => {
38-
let assets = Object.keys(compilation.assets);
36+
if (compiler.hooks && compiler.hooks.emit) {
37+
// webpack@4 syntax
38+
compiler.hooks.emit.tapAsync('PlayerLoaderPlugin', this.downloadAndAppendPlayer.bind(this));
39+
} else {
40+
// webpack@3 syntax
41+
compiler.plugin('emit', this.downloadAndAppendPlayer.bind(this));
42+
}
43+
}
3944

40-
// normally we filter out all non .js outputs, and choose prepend to
41-
// only the first output
42-
if (typeof this.settings_.prependTo === 'undefined') {
43-
// filter out non js files
44-
assets = assets.filter((filename) => path.extname(filename) === '.js');
45+
downloadAndAppendPlayer(compilation, callback) {
46+
request.get(this.playerUrl).then((playerjs) => {
47+
let assets = Object.keys(compilation.assets);
4548

46-
// only prepend to the first one
47-
assets = [assets[0]];
49+
// normally we filter out all non .js outputs, and choose prepend to
50+
// only the first output
51+
if (typeof this.settings_.prependTo === 'undefined') {
52+
// filter out non js files
53+
assets = assets.filter((filename) => path.extname(filename) === '.js');
4854

49-
// if prependTo is specified though, we prepend to anything that is listed
50-
} else {
51-
assets = assets.filter((filename) => this.settings_.prependTo.indexOf(filename) === -1);
52-
}
55+
// only prepend to the first one
56+
assets = [assets[0]];
5357

54-
if (!assets.length) {
55-
console.error('webpack-player-loader-plugin: did not find anything to prepend the player to!');
56-
console.error();
57-
process.exit(1);
58-
}
58+
// if prependTo is specified though, we prepend to anything that is listed
59+
} else {
60+
assets = assets.filter((filename) => this.settings_.prependTo.indexOf(filename) !== -1);
61+
}
5962

60-
assets.forEach(function(file) {
61-
compilation.assets[file] = new ConcatSource(playerjs, compilation.assets[file]);
62-
});
63-
callback();
64-
}).catch(function(err) {
65-
console.error('Failed to get a player at ' + this.playerUrl + ' double check your options');
66-
console.error(err);
63+
if (!assets.length) {
64+
console.error('webpack-player-loader-plugin: did not find anything to prepend the player to!');
6765
console.error();
6866
process.exit(1);
67+
}
68+
69+
assets.forEach(function(file) {
70+
compilation.assets[file] = new ConcatSource(playerjs, compilation.assets[file]);
6971
});
72+
callback();
73+
}).catch(function(err) {
74+
console.error('Failed to get a player at ' + this.playerUrl + ' double check your options');
75+
console.error(err);
76+
console.error();
77+
process.exit(1);
7078
});
7179
}
7280
}

0 commit comments

Comments
 (0)