diff --git a/src/plugins/source-plugin.js b/src/plugins/source-plugin.js
index 78e66dc2..76ef4877 100644
--- a/src/plugins/source-plugin.js
+++ b/src/plugins/source-plugin.js
@@ -627,16 +627,13 @@ export default (options) =>
const { startIndex, unquoted } = source;
let { value } = source;
const URLObject = parse(value);
+ const { hash } = URLObject;
- if (URLObject.hash) {
- const { hash } = URLObject;
-
+ if (hash) {
URLObject.hash = null;
source.value = URLObject.format();
- if (hash) {
- value = value.slice(0, value.length - hash.length);
- }
+ value = value.slice(0, value.length - hash.length);
}
const importKey = urlToRequest(decodeURIComponent(source.value), root);
@@ -656,7 +653,11 @@ export default (options) =>
});
}
- const replacerKey = JSON.stringify({ importKey, unquoted });
+ const replacerKey = JSON.stringify({
+ importKey,
+ unquoted,
+ hash,
+ });
let replacerName = replacersMap.get(replacerKey);
if (!replacerName) {
@@ -667,6 +668,7 @@ export default (options) =>
type: 'replacer',
value: {
type: 'source',
+ hash,
importName,
replacerName,
unquoted,
@@ -674,13 +676,15 @@ export default (options) =>
});
}
+ const valueLength = hash ? value.length + hash.length : value.length;
+
// eslint-disable-next-line no-param-reassign
html =
html.substr(0, startIndex + offset) +
replacerName +
- html.substr(startIndex + value.length + offset);
+ html.substr(startIndex + valueLength + offset);
- offset += replacerName.length - value.length;
+ offset += replacerName.length - valueLength;
}
return html;
diff --git a/src/runtime/getUrl.js b/src/runtime/getUrl.js
index e1d705d2..b45298bb 100644
--- a/src/runtime/getUrl.js
+++ b/src/runtime/getUrl.js
@@ -1,4 +1,9 @@
-module.exports = (url, maybeNeedQuotes) => {
+module.exports = (url, options) => {
+ if (!options) {
+ // eslint-disable-next-line no-param-reassign
+ options = {};
+ }
+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url && url.__esModule ? url.default : url;
@@ -6,7 +11,12 @@ module.exports = (url, maybeNeedQuotes) => {
return url;
}
- if (maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
+ if (options.hash) {
+ // eslint-disable-next-line no-param-reassign
+ url += options.hash;
+ }
+
+ if (options.maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
return `"${url}"`;
}
diff --git a/src/utils.js b/src/utils.js
index 244791df..ef7a2699 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -74,11 +74,15 @@ export function getModuleCode(html, replaceableMessages) {
let replacersCode = '';
for (const item of replaceableMessages) {
- const { importName, replacerName, unquoted } = item;
+ const { importName, replacerName, unquoted, hash } = item;
- replacersCode += `var ${replacerName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${
- unquoted ? ', true' : ''
- });\n`;
+ const getUrlOptions = []
+ .concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
+ .concat(unquoted ? 'maybeNeedQuotes: true' : []);
+ const preparedOptions =
+ getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(', ')} }` : '';
+
+ replacersCode += `var ${replacerName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
code = code.replace(
new RegExp(replacerName, 'g'),
diff --git a/test/__snapshots__/attributes-option.test.js.snap b/test/__snapshots__/attributes-option.test.js.snap
index bf491a98..c6061e9e 100644
--- a/test/__snapshots__/attributes-option.test.js.snap
+++ b/test/__snapshots__/attributes-option.test.js.snap
@@ -760,7 +760,7 @@ var ___HTML_LOADER_REPLACER_4___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___H
var ___HTML_LOADER_REPLACER_5___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_5___);
var ___HTML_LOADER_REPLACER_6___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_6___);
var ___HTML_LOADER_REPLACER_7___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_7___);
-var code = \\"\\\\n\\\\n
My First Heading
\\\\nMy first paragraph.
\\\\nAn Unordered HTML List
\\\\n\\\\n\\\\n - Coffee
\\\\n - Tea
\\\\n - Milk
\\\\n
\\\\n\\\\nAn Ordered HTML List
\\\\n\\\\n\\\\n - Coffee
\\\\n - Tea
\\\\n - Milk
\\\\n
\\\\n\\\\n\\\\n\\\\n\\\\n\\\\nFoo
\\\\n\\\\n\\\\nBAR
\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n
\\\\n\\\\n\\\\n\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n
\\\\n\\\\n\\\\n \\\\n \\\\n
\\\\n\\\\n\\\\n\\\\n\\\\n