Skip to content

Commit 8e79d68

Browse files
committed
feat(options): added query option that gets appended to the chunk href
Added query option that gets appended to the chunk href. This option can be a string or a synchronous function.
1 parent 359c00f commit 8e79d68

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/CssLoadingRuntimeModule.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ module.exports = class CssLoadingRuntimeModule extends RuntimeModule {
5050

5151
if (!withLoading && !withHmr) return null;
5252

53+
let queryPostfix = '';
54+
if (typeof this.runtimeOptions.query === 'string') {
55+
queryPostfix = ` + ${JSON.stringify(this.runtimeOptions.query)}`;
56+
} else if (typeof this.runtimeOptions.query === 'function') {
57+
queryPostfix = ` + ${JSON.stringify(this.runtimeOptions.query() || '')}`;
58+
}
59+
5360
return Template.asString([
5461
`var createStylesheet = ${runtimeTemplate.basicFunction(
5562
'chunkId, fullhref, resolve, reject',
@@ -174,7 +181,7 @@ module.exports = class CssLoadingRuntimeModule extends RuntimeModule {
174181
[
175182
'applyHandlers.push(applyHandler);',
176183
`chunkIds.forEach(${runtimeTemplate.basicFunction('chunkId', [
177-
`var href = ${RuntimeGlobals.require}.miniCssF(chunkId);`,
184+
`var href = ${RuntimeGlobals.require}.miniCssF(chunkId)${queryPostfix};`,
178185
`var fullhref = ${RuntimeGlobals.publicPath} + href;`,
179186
'const oldTag = findStylesheet(href, fullhref);',
180187
'if(!oldTag) return;',

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class MiniCssExtractPlugin {
6464
this.runtimeOptions = {
6565
insert,
6666
linkType,
67+
query: options.query,
6768
};
6869

6970
this.runtimeOptions.attributes = Template.asString(
@@ -369,6 +370,17 @@ class MiniCssExtractPlugin {
369370
}
370371
);
371372

373+
let queryPostfix = '';
374+
if (typeof this.runtimeOptions.query === 'string') {
375+
queryPostfix = ` + ${JSON.stringify(
376+
this.runtimeOptions.query
377+
)}`;
378+
} else if (typeof this.runtimeOptions.query === 'function') {
379+
queryPostfix = ` + ${JSON.stringify(
380+
this.runtimeOptions.query() || ''
381+
)}`;
382+
}
383+
372384
return Template.asString([
373385
source,
374386
'',
@@ -379,7 +391,7 @@ class MiniCssExtractPlugin {
379391
Template.indent([
380392
'promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {',
381393
Template.indent([
382-
`var href = ${linkHrefPath};`,
394+
`var href = ${linkHrefPath}${queryPostfix};`,
383395
`var fullhref = ${mainTemplate.requireFn}.p + href;`,
384396
'var existingLinkTags = document.getElementsByTagName("link");',
385397
'for(var i = 0; i < existingLinkTags.length; i++) {',

src/plugin-options.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@
4949
"type": "boolean"
5050
}
5151
]
52+
},
53+
"query": {
54+
"description": "A configurable postfix to be appended to the href of the chunk.",
55+
"anyOf": [
56+
{
57+
"type": "string"
58+
},
59+
{
60+
"instanceof": "Function"
61+
}
62+
]
5263
}
5364
}
5465
}

test/__snapshots__/validate-plugin-options.test.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,47 +99,47 @@ exports[`validate options should throw an error on the "linkType" option with "i
9999
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
100100
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
101101
- options has an unknown property 'unknown'. These properties are valid:
102-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
102+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
103103
`;
104104
105105
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
106106
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
107107
- options has an unknown property 'unknown'. These properties are valid:
108-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
108+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
109109
`;
110110
111111
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
112112
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
113113
- options has an unknown property 'unknown'. These properties are valid:
114-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
114+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
115115
`;
116116
117117
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
118118
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
119119
- options has an unknown property 'unknown'. These properties are valid:
120-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
120+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
121121
`;
122122
123123
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
124124
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
125125
- options has an unknown property 'unknown'. These properties are valid:
126-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
126+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
127127
`;
128128
129129
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
130130
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
131131
- options has an unknown property 'unknown'. These properties are valid:
132-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
132+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
133133
`;
134134
135135
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
136136
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
137137
- options has an unknown property 'unknown'. These properties are valid:
138-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
138+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
139139
`;
140140
141141
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
142142
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
143143
- options has an unknown property 'unknown'. These properties are valid:
144-
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType? }"
144+
object { filename?, chunkFilename?, ignoreOrder?, insert?, attributes?, linkType?, query? }"
145145
`;

0 commit comments

Comments
 (0)