From c2851f937498ae939e40960b521d927870390133 Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Fri, 19 Feb 2021 16:41:13 +0300 Subject: [PATCH 1/4] feat: added support webpackIgnore comment --- test/fixtures/webpackIgnore.html | 8 ++++++++ test/fixtures/webpackIgnore.js | 3 +++ test/loader.test.js | 12 ++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 test/fixtures/webpackIgnore.html create mode 100644 test/fixtures/webpackIgnore.js diff --git a/test/fixtures/webpackIgnore.html b/test/fixtures/webpackIgnore.html new file mode 100644 index 00000000..e26da6ab --- /dev/null +++ b/test/fixtures/webpackIgnore.html @@ -0,0 +1,8 @@ + + + + +Elva dressed as a fairy + +Elva dressed as a fairy + 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..5fb94464 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -190,4 +190,16 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); expect(getErrors(stats)).toMatchSnapshot('errors'); }); + + // it.only('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'); + // }); }); From a127257cea965b15f4ca554b00488b8332d5715a Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Fri, 19 Feb 2021 19:34:07 +0300 Subject: [PATCH 2/4] feat: added support webpackIgnore comment --- README.md | 26 ++++++++ src/plugins/sources-plugin.js | 13 ++++ src/utils.js | 10 +++ test/__snapshots__/loader.test.js.snap | 87 ++++++++++++++++++++++++++ test/fixtures/webpackIgnore.html | 71 ++++++++++++++++++++- test/loader.test.js | 24 +++---- 6 files changed, 217 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9372de53..436765a4 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..b98a223f 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/i; + +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 index e26da6ab..8d54011c 100644 --- a/test/fixtures/webpackIgnore.html +++ b/test/fixtures/webpackIgnore.html @@ -1,8 +1,73 @@ - + + + + + + + Document + + + + -Elva dressed as a fairy -Elva dressed as a fairy +Elva dressed as a fairy + +Elva dressed as a fairy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + Flowers + + + + \ No newline at end of file diff --git a/test/loader.test.js b/test/loader.test.js index 5fb94464..67287ffa 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -191,15 +191,17 @@ describe('loader', () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); - // it.only('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'); - // }); + 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'); + }); }); From 95caf72c203046a8fe0e3921d079ce755c4081ab Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Fri, 19 Feb 2021 19:54:57 +0300 Subject: [PATCH 3/4] feat: added support webpackIgnore comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 436765a4..e662ee1b 100644 --- a/README.md +++ b/README.md @@ -589,7 +589,7 @@ module.exports = { ### Disable url resolving using the `` comment -With ``comment, can to disable sources handling for next tag. +With `` comment, can to disable sources handling for next tag. ```html From 565e1bda47ffe2749dd0893725819ed02ed8ca44 Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Fri, 19 Feb 2021 19:59:55 +0300 Subject: [PATCH 4/4] feat: added support webpackIgnore comment --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index b98a223f..cc0d0643 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1185,7 +1185,7 @@ export function c0ControlCodesExclude(source) { return { value, startOffset }; } -const webpackIgnoreCommentRegexp = /webpackIgnore:(\s+)?true/i; +const webpackIgnoreCommentRegexp = /webpackIgnore:(\s+)?true/; export function isWebpackIgnoreComment(node) { if (node.nodeName !== '#comment') {