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

Commit 98c801e

Browse files
committed
Fixing output paths for glob supplementals
This requires copy-webpack-plugin 4.6.0 for the transformPath property, which also requires working around a yet-unfixed regression in c-w-p that was causing a test failure - the "nobraces" option no longer works properly when the input is an explicit file or directory. So, if there's a cwpPatternConfig.fromArgs option, we explicitly tell c-w-p to treat this as a glob. We could probably safely do this no matter what, but better safe than sorry.
1 parent 0ab55ea commit 98c801e

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
"dependencies": {
6666
"ajv": "^6.1.1",
67-
"copy-webpack-plugin": "^4.4.1",
67+
"copy-webpack-plugin": "^4.6.0",
6868
"html-webpack-include-assets-plugin": "^1.0.2"
6969
}
7070
}

src/HtmlWebpackExternalsPlugin.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import CopyWebpackPlugin from 'copy-webpack-plugin'
22
import HtmlWebpackIncludeAssetsPlugin from 'html-webpack-include-assets-plugin'
33
import Ajv from 'ajv'
44
import configSchema from './configSchema.json'
5+
import path from 'path'
56

67
export default class HtmlWebpackExternalsPlugin {
78
static validateArguments = (() => {
@@ -94,14 +95,22 @@ export default class HtmlWebpackExternalsPlugin {
9495

9596
const pluginsToApply = []
9697

98+
const context = path.isAbsolute(this.cwpOptions.context)
99+
? this.cwpOptions.context
100+
: path.resolve(compiler.options.context, this.cwpOptions.context || ".")
101+
97102
pluginsToApply.push(
98103
new CopyWebpackPlugin(
99-
this.assetsToCopy.map(({ dist, ...entry }) => ({
104+
this.assetsToCopy.map(({ dist, cwpPatternConfig, ...entry }) => ({
100105
dist: dist ? `/${dist}` : '',
106+
cwpPatternConfig: cwpPatternConfig || {},
101107
...entry
102108
})).map(({ module, path, dist, cwpPatternConfig }) => ({
103-
from: `${module}${dist}/${path}`,
104-
to: `${this.outputPath}/${module}/${path}`,
109+
from: cwpPatternConfig.fromArgs
110+
? {...cwpPatternConfig.fromArgs, glob:`${module}${dist}/${path}`}
111+
: `${module}${dist}/${path}`,
112+
to: 'UNEXPECTED_ERROR/', // We're going to be overwriting this below
113+
transformPath: (_,absolutePath) => (absolutePath.replace(`${cwpPatternConfig.context||context}/${module}${dist}`,`${this.outputPath}/${module}`)),
105114
...cwpPatternConfig,
106115
})),
107116
this.cwpOptions

test/HtmlWebpackExternalsPlugin.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,62 @@ describe('HtmlWebpackExternalsPlugin', function() {
402402
.then(() => checkHtmlIncludes('vendor/context_test/dist/contextTest.css', 'css'))
403403
})
404404

405+
it('works with glob filename supplements, matching single', function() {
406+
const externals = [
407+
{
408+
module: 'bootstrap',
409+
entry: ['dist/js/bootstrap.min.js'],
410+
supplements: [
411+
'dist/css/bootstrap.min.c*s',
412+
]
413+
},
414+
]
415+
416+
return runWebpack(
417+
new HtmlWebpackPlugin(),
418+
new HtmlWebpackExternalsPlugin({ externals })
419+
)
420+
.then(() => checkCopied('vendor/bootstrap/dist/css/bootstrap.min.css'))
421+
})
422+
423+
it('works with glob filename supplements, matching multiple', function() {
424+
const externals = [
425+
{
426+
module: 'bootstrap',
427+
entry: ['dist/js/bootstrap.min.js'],
428+
supplements: [
429+
'dist/css/*.min.css',
430+
]
431+
},
432+
]
433+
434+
return runWebpack(
435+
new HtmlWebpackPlugin(),
436+
new HtmlWebpackExternalsPlugin({ externals })
437+
)
438+
.then(() => checkCopied('vendor/bootstrap/dist/css/bootstrap.min.css'))
439+
.then(() => checkCopied('vendor/bootstrap/dist/css/bootstrap-reboot.min.css'))
440+
})
441+
442+
it('works with recursive glob supplements', function() {
443+
const externals = [
444+
{
445+
module: 'bootstrap',
446+
entry: ['dist/js/bootstrap.min.js'],
447+
supplements: [
448+
'**/dropdown.js',
449+
]
450+
},
451+
]
452+
453+
return runWebpack(
454+
new HtmlWebpackPlugin(),
455+
new HtmlWebpackExternalsPlugin({ externals })
456+
)
457+
.then(() => checkCopied('vendor/bootstrap/js/dist/dropdown.js'))
458+
.then(() => checkCopied('vendor/bootstrap/js/src/dropdown.js'))
459+
})
460+
405461
it('Specifying which HTML files to affect example', function() {
406462
return runWebpack(
407463
new HtmlWebpackPlugin({

0 commit comments

Comments
 (0)