Skip to content
This repository was archived by the owner on Apr 5, 2021. It is now read-only.

Commit cd77258

Browse files
author
Matt Miller
committed
use externals array so that external modules that do not contain globals don't throw webpack config errors
1 parent aaf3060 commit cd77258

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

src/HtmlWebpackExternalsPlugin.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class HtmlWebpackExternalsPlugin {
2323
this.assetsToPrepend = []
2424
this.assetsToAppend = []
2525
this.assetsToCopy = []
26-
this.externals = {}
26+
this.externals = []
2727

2828
const { externals, hash, outputPath, publicPath, files, enabled, cwpOptions } = config
2929
this.hash = hash
@@ -34,7 +34,7 @@ export default class HtmlWebpackExternalsPlugin {
3434
this.cwpOptions = cwpOptions
3535

3636
externals.forEach(({ module, entry, global, supplements, append }) => {
37-
this.externals[module] = global
37+
this.externals.push(global ? { [module]: global } : module)
3838

3939
const localEntries = []
4040

@@ -77,12 +77,9 @@ export default class HtmlWebpackExternalsPlugin {
7777
if (!compiler.options.externals) {
7878
compiler.options.externals = this.externals
7979
} else if (Array.isArray(compiler.options.externals)) {
80-
compiler.options.externals.push(this.externals)
81-
} else if (typeof compiler.options.externals === 'object') {
82-
compiler.options.externals = {
83-
...compiler.options.externals,
84-
...this.externals,
85-
}
80+
compiler.options.externals = [...compiler.options.externals, ...this.externals]
81+
} else {
82+
compiler.options.externals = [compiler.options.externals, ...this.externals]
8683
}
8784

8885
const publicPath = (() => {

test/HtmlWebpackExternalsPlugin.spec.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
cleanUp,
88
runWebpack,
99
checkBundleExcludes,
10+
checkConfigExternals,
1011
checkCopied,
1112
checkHtmlIncludes,
1213
} from './utils'
@@ -22,35 +23,37 @@ describe('HtmlWebpackExternalsPlugin', function() {
2223
})
2324

2425
it('Local JS external example', function() {
26+
const externals = [
27+
{
28+
module: 'jquery',
29+
entry: 'dist/jquery.min.js',
30+
global: 'jQuery',
31+
},
32+
]
33+
2534
return runWebpack(
2635
new HtmlWebpackPlugin(),
27-
new HtmlWebpackExternalsPlugin({
28-
externals: [
29-
{
30-
module: 'jquery',
31-
entry: 'dist/jquery.min.js',
32-
global: 'jQuery',
33-
},
34-
],
35-
})
36+
new HtmlWebpackExternalsPlugin({ externals })
3637
)
38+
.then(({ config }) => checkConfigExternals(config, externals))
3739
.then(() => checkBundleExcludes('jQuery'))
3840
.then(() => checkCopied('vendor/jquery/dist/jquery.min.js'))
3941
.then(() => checkHtmlIncludes('vendor/jquery/dist/jquery.min.js', 'js'))
4042
})
4143

4244
it('Local CSS external example', function() {
45+
const externals = [
46+
{
47+
module: 'bootstrap',
48+
entry: 'dist/css/bootstrap.min.css',
49+
},
50+
]
51+
4352
return runWebpack(
4453
new HtmlWebpackPlugin(),
45-
new HtmlWebpackExternalsPlugin({
46-
externals: [
47-
{
48-
module: 'bootstrap',
49-
entry: 'dist/css/bootstrap.min.css',
50-
},
51-
],
52-
})
54+
new HtmlWebpackExternalsPlugin({ externals })
5355
)
56+
.then(({ config }) => checkConfigExternals(config, externals))
5457
.then(() => checkCopied('vendor/bootstrap/dist/css/bootstrap.min.css'))
5558
.then(() =>
5659
checkHtmlIncludes('vendor/bootstrap/dist/css/bootstrap.min.css', 'css')
@@ -195,7 +198,7 @@ describe('HtmlWebpackExternalsPlugin', function() {
195198
hash: true,
196199
})
197200
)
198-
.then(stats => {
201+
.then(({ stats }) => {
199202
hash = stats.toJson().hash
200203
})
201204
.then(() => checkCopied('vendor/bootstrap/dist/css/bootstrap.min.css'))

test/utils/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function cleanUp() {
1313
}
1414

1515
export function runWebpack(...plugins) {
16-
return promisify(webpack, {
16+
const config = {
1717
entry: {
1818
app: path.resolve(__dirname, '..', 'fixtures', 'app.js'),
1919
style: path.resolve(__dirname, '..', 'fixtures', 'style.css'),
@@ -31,13 +31,15 @@ export function runWebpack(...plugins) {
3131
],
3232
},
3333
plugins: [new ExtractTextPlugin({ filename: '[name].css' }), ...plugins],
34-
}).then(stats => {
34+
}
35+
36+
return promisify(webpack, config).then(stats => {
3537
assert.strictEqual(
3638
stats.hasErrors(),
3739
false,
3840
stats.toJson().errors.toString()
3941
)
40-
return stats
42+
return { stats, config }
4143
})
4244
}
4345

@@ -58,6 +60,26 @@ export function checkCopied(file) {
5860
return promisify(fs.access, path.join(OUTPUT_PATH, file))
5961
}
6062

63+
export function checkConfigExternals(config, externals) {
64+
assert.ok(Array.isArray(config.externals), 'webpackConfig.externals is not an array');
65+
66+
externals.forEach(({ module, global }) => {
67+
if (global) {
68+
const expected = { [module]: global }
69+
assert.deepEqual(
70+
config.externals.find(external => external[module]),
71+
expected,
72+
`webpackConfig.externals did not contain ${JSON.stringify(expected)}`
73+
)
74+
} else {
75+
assert.ok(
76+
config.externals.indexOf(module) > -1,
77+
`webpackConfig.externals did not contain ${module}`
78+
)
79+
}
80+
})
81+
}
82+
6183
export function checkHtmlIncludes(
6284
file,
6385
type,

0 commit comments

Comments
 (0)