Skip to content

Commit

Permalink
Remove if (value) check from HTMLAsset's generate() for script tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
twome committed Mar 31, 2019
1 parent cb975b2 commit 78937be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
4 changes: 1 addition & 3 deletions packages/core/integration-tests/test/html.js
Expand Up @@ -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}
Expand Down Expand Up @@ -831,8 +831,6 @@ describe('html', function() {
'utf8'
);

console.debug(html);

assert(
!/type\s*=/.test(html) // No type="" attribute anywhere
);
Expand Down
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html>

<head>
<title>Script tag type stripping - Parcel</title>
Expand All @@ -9,7 +9,6 @@
</head>

<body>
<h1>Hello world</h1>
</body>

</html>
@@ -1 +0,0 @@
var a = 1
@@ -1 +0,0 @@
var b = 2
58 changes: 28 additions & 30 deletions packages/core/parcel-bundler/src/assets/HTMLAsset.js
Expand Up @@ -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.
Expand Down

0 comments on commit 78937be

Please sign in to comment.