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 + + + + + + +Elva dressed as a fairy + + + + + + + + +``` + ### 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 Document\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n\\\\n \\\\n\\\\n\\\\\\"Elva\\\\n\\\\n\\\\\\"Elva\\\\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 \\\\\\"Flowers\\\\\\"\\\\n\\\\n\\\\n\\\\n\\\\n\\"; +// Exports +export default code;" +`; + +exports[`loader should work with webpackIgnore comment: result 1`] = ` +" + + + + + + Document + + + + + + + + +\\"Elva + +\\"Elva + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + \\"Flowers\\" + + + + +" +`; + +exports[`loader should work with webpackIgnore comment: warnings 1`] = `Array []`; + exports[`loader should work: errors 1`] = `Array []`; exports[`loader should work: module 1`] = ` diff --git a/test/fixtures/webpackIgnore.html b/test/fixtures/webpackIgnore.html new file mode 100644 index 00000000..8d54011c --- /dev/null +++ b/test/fixtures/webpackIgnore.html @@ -0,0 +1,73 @@ + + + + + + + Document + + + + + + + + +Elva dressed as a fairy + +Elva dressed as a fairy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + Flowers + + + + + \ No newline at end of file diff --git a/test/fixtures/webpackIgnore.js b/test/fixtures/webpackIgnore.js new file mode 100644 index 00000000..e17250d0 --- /dev/null +++ b/test/fixtures/webpackIgnore.js @@ -0,0 +1,3 @@ +import html from './webpackIgnore.html'; + +export default html; diff --git a/test/loader.test.js b/test/loader.test.js index 011e5d56..67287ffa 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -190,4 +190,18 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); expect(getErrors(stats)).toMatchSnapshot('errors'); }); + + it('should work with webpackIgnore comment', async () => { + const compiler = getCompiler('webpackIgnore.js'); + const stats = await compile(compiler); + + expect(getModuleSource('./webpackIgnore.html', stats)).toMatchSnapshot( + 'module' + ); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); });