diff --git a/README.md b/README.md index 9372de53..e662ee1b 100644 --- a/README.md +++ b/README.md @@ -587,6 +587,32 @@ module.exports = { ## Examples +### Disable url resolving using the `` comment + +With `` comment, can to disable sources handling for next tag. + +```html + + + + + + + + + + + + + + + +``` + ### roots With [`resolve.roots`](https://webpack.js.org/configuration/resolve/#resolveroots) can specify a list of directories where requests of server-relative URLs (starting with '/') are resolved. diff --git a/src/plugins/sources-plugin.js b/src/plugins/sources-plugin.js index cd2c532e..3464b6e8 100644 --- a/src/plugins/sources-plugin.js +++ b/src/plugins/sources-plugin.js @@ -7,6 +7,7 @@ import { normalizeUrl, requestify, stringifyRequest, + isWebpackIgnoreComment, } from '../utils'; export default (options) => @@ -14,13 +15,25 @@ export default (options) => const sources = []; const document = parse5.parse(html, { sourceCodeLocationInfo: true }); + let webpackIgnore = false; + traverse(document, (node) => { const { tagName, attrs: attributes, sourceCodeLocation } = node; + if (isWebpackIgnoreComment(node)) { + webpackIgnore = true; + return; + } + if (!tagName) { return; } + if (webpackIgnore) { + webpackIgnore = false; + return; + } + attributes.forEach((attribute) => { let { name } = attribute; diff --git a/src/utils.js b/src/utils.js index b4e7c9a8..cc0d0643 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1184,3 +1184,13 @@ export function c0ControlCodesExclude(source) { return { value, startOffset }; } + +const webpackIgnoreCommentRegexp = /webpackIgnore:(\s+)?true/; + +export function isWebpackIgnoreComment(node) { + if (node.nodeName !== '#comment') { + return false; + } + + return webpackIgnoreCommentRegexp.test(node.data); +} diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 6db7fc90..ec5a1f4c 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -152,6 +152,93 @@ exports[`loader should work with server-relative url: result 1`] = ` exports[`loader should work with server-relative url: warnings 1`] = `Array []`; +exports[`loader should work with webpackIgnore comment: errors 1`] = `Array []`; + +exports[`loader should work with webpackIgnore comment: module 1`] = ` +"// Module +var code = \\"\\\\n\\\\n
\\\\n \\\\n \\\\n \\\\n