From 78937beaa45bcbe71dc55420a29714cfb0d8c915 Mon Sep 17 00:00:00 2001 From: Tom Kenny Date: Sun, 31 Mar 2019 16:02:01 +0800 Subject: [PATCH] Remove `if (value)` check from HTMLAsset's generate() for script tags Script tags don't need to have inner content, and so this check was excluding non-inline script tags from being included in the HTMLAsset's ast for transforming script tags. This meant they were all being output the same as in the source HTML. This should now strip `type` attributes for 'application/javascript', 'text/javascript', and 'module'. Also remove superfluous static content & dev code for this test. --- packages/core/integration-tests/test/html.js | 4 +- .../html-module-type-scripts/index.html | 3 +- .../html-module-type-scripts/module.js | 1 - .../text-javascript.js | 1 - .../parcel-bundler/src/assets/HTMLAsset.js | 58 +++++++++---------- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index b0876b7bc4c..397eb7e6570 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -795,7 +795,7 @@ describe('html', function() { assert(html.includes('alert("Hello, World!")')); }); - it('should strip `type` attribute from ES-module and "text/javascript" script tags', async function() { + it('should strip `type` attribute from ES-module and plain script tags', async function() { let b = await bundle( path.join(__dirname, '/integration/html-module-type-scripts/index.html'), {production: true} @@ -831,8 +831,6 @@ describe('html', function() { 'utf8' ); - console.debug(html); - assert( !/type\s*=/.test(html) // No type="" attribute anywhere ); diff --git a/packages/core/integration-tests/test/integration/html-module-type-scripts/index.html b/packages/core/integration-tests/test/integration/html-module-type-scripts/index.html index dd26abe51ab..7345fe89d48 100644 --- a/packages/core/integration-tests/test/integration/html-module-type-scripts/index.html +++ b/packages/core/integration-tests/test/integration/html-module-type-scripts/index.html @@ -1,5 +1,5 @@ - + Script tag type stripping - Parcel @@ -9,7 +9,6 @@ -

Hello world

diff --git a/packages/core/integration-tests/test/integration/html-module-type-scripts/module.js b/packages/core/integration-tests/test/integration/html-module-type-scripts/module.js index eabcd42093c..e69de29bb2d 100644 --- a/packages/core/integration-tests/test/integration/html-module-type-scripts/module.js +++ b/packages/core/integration-tests/test/integration/html-module-type-scripts/module.js @@ -1 +0,0 @@ -var a = 1 \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/html-module-type-scripts/text-javascript.js b/packages/core/integration-tests/test/integration/html-module-type-scripts/text-javascript.js index dab2671734f..e69de29bb2d 100644 --- a/packages/core/integration-tests/test/integration/html-module-type-scripts/text-javascript.js +++ b/packages/core/integration-tests/test/integration/html-module-type-scripts/text-javascript.js @@ -1 +0,0 @@ -var b = 2 \ No newline at end of file diff --git a/packages/core/parcel-bundler/src/assets/HTMLAsset.js b/packages/core/parcel-bundler/src/assets/HTMLAsset.js index f2768ae17c8..b58f9fffdeb 100644 --- a/packages/core/parcel-bundler/src/assets/HTMLAsset.js +++ b/packages/core/parcel-bundler/src/assets/HTMLAsset.js @@ -216,40 +216,38 @@ class HTMLAsset extends Asset { this.ast.walk(node => { if (node.tag === 'script' || node.tag === 'style') { let value = node.content && node.content.join('').trim(); - if (value) { - let type; - - if (node.tag === 'style') { - if (node.attrs && node.attrs.type) { - type = node.attrs.type.split('/')[1]; - } else { - type = 'css'; - } - } else if (node.attrs && node.attrs.type) { - // Skip JSON - if (SCRIPT_TYPES[node.attrs.type] === false) { - return node; - } - - if (SCRIPT_TYPES[node.attrs.type]) { - type = SCRIPT_TYPES[node.attrs.type]; - } else { - type = node.attrs.type.split('/')[1]; - } + let type; + + if (node.tag === 'style') { + if (node.attrs && node.attrs.type) { + type = node.attrs.type.split('/')[1]; } else { - type = 'js'; + type = 'css'; + } + } else if (node.attrs && node.attrs.type) { + // Skip JSON + if (SCRIPT_TYPES[node.attrs.type] === false) { + return node; } - parts.push({ - type, - value, - inlineHTML: true, - meta: { - type: 'tag', - node - } - }); + if (SCRIPT_TYPES[node.attrs.type]) { + type = SCRIPT_TYPES[node.attrs.type]; + } else { + type = node.attrs.type.split('/')[1]; + } + } else { + type = 'js'; } + + parts.push({ + type, + value, + inlineHTML: true, + meta: { + type: 'tag', + node + } + }); } // Process inline style attributes.