Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes #6635

Merged
merged 4 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('html', function() {
);

assert(
/^<link rel="stylesheet" href="[/\\]index\.[a-f0-9]+\.css">\s*<script src="[/\\]index\.[a-f0-9]+\.js"><\/script>\s*<h1>Hello/m.test(
/^<link rel="stylesheet" href="[/\\]index\.[a-f0-9]+\.css">\s*<script src="[/\\]index\.[a-f0-9]+\.js" defer=""><\/script>\s*<h1>Hello/m.test(
html,
),
);
Expand Down Expand Up @@ -391,7 +391,8 @@ describe('html', function() {
);

assert.equal(
html.match(/<script src="[/\\]{1}index\.[a-f0-9]+?\.js">/g).length,
html.match(/<script src="[/\\]{1}index\.[a-f0-9]+?\.js" defer="">/g)
.length,
2,
);
});
Expand Down Expand Up @@ -484,7 +485,7 @@ describe('html', function() {
// minifySvg is false
assert(
html.includes(
'<svg version=1.1 width=300 height=200 xmlns=http://www.w3.org/2000/svg baseProfile=full><rect width=100% height=100% fill=red></rect><circle cx=150 cy=100 r=80 fill=green></circle><text x=150 y=125 font-size=60 text-anchor=middle fill=white>SVG</text></svg>',
'<svg version=1.1 baseprofile=full width=300 height=200 xmlns=http://www.w3.org/2000/svg><rect width=100% height=100% fill=red></rect><circle cx=150 cy=100 r=80 fill=green></circle><text x=150 y=125 font-size=60 text-anchor=middle fill=white>SVG</text></svg>',
),
);
});
Expand Down Expand Up @@ -790,6 +791,9 @@ describe('html', function() {
it('should ignore svgs referencing local symbols via <use xlink:href="#">', async function() {
let b = await bundle(
path.join(__dirname, '/integration/html-svg-local-symbol/index.html'),
{
mode: 'production',
},
);

assertBundles(b, [
Expand All @@ -798,6 +802,13 @@ describe('html', function() {
assets: ['index.html'],
},
]);

let contents = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(
contents.includes(
'<svg><symbol id="all"><rect width="100" height="100"/></symbol></svg><svg xmlns:xlink="http://www.w3.org/1999/xlink"><use xlink:href="#all" href="#all"/></svg>',
),
);
});

it('should bundle svg files using <image xlink:href=""> correctly', async function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
<use xlink:href="#all" href="#all"/>
</svg>
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
<svg>
<symbol id="all">
<rect width="100" height="100" x="0" y="0"/>
</symbol>
</svg>
</svg>
<svg>
<use xlink:href="#all" href="#all"/>
</svg>
39 changes: 36 additions & 3 deletions packages/optimizers/htmlnano/src/HTMLNanoOptimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export default (new Optimizer({
keepRoleAttr: true,
},
},
// Do not minify ids or remove unreferenced elements in inline SVGs
// because they could actually be referenced by a separate inline SVG.
{
name: 'cleanupIDs',
active: false,
},
// XML namespaces are not required in HTML.
{
name: 'removeXMLNS',
active: true,
},
]),
},
...(preset || {}),
Expand All @@ -91,10 +102,15 @@ export default (new Optimizer({
// skipConfigLoading: true,
};

let plugins = [htmlnano(htmlNanoConfig)];

// $FlowFixMe
if (htmlNanoConfig.minifySvg !== false) {
plugins.unshift(mapSVG);
}

return {
contents: (
await posthtml([mapSVG, htmlnano(htmlNanoConfig)]).process(contents)
).html,
contents: (await posthtml(plugins).process(contents)).html,
};
},
}): Optimizer);
Expand All @@ -113,6 +129,23 @@ function mapSVG(
}
} else if (node && typeof node === 'object') {
let {tag, attrs} = node;

// SVG in HTML does not require xml namespaces to be declared, but standalone SVG files do.
// If unset, add them here so that SVGO doesn't have parse errors.
if (tag === 'svg') {
if (!attrs) {
node.attrs = attrs = {};
}

if (!attrs.xmlns) {
attrs.xmlns = 'http://www.w3.org/2000/svg';
}

if (!attrs['xmlns:xlink']) {
attrs['xmlns:xlink'] = 'http://www.w3.org/1999/xlink';
}
}

if (inSVG || tag === 'svg') {
if (SVG_TAG_NAMES[tag]) {
node.tag = SVG_TAG_NAMES[tag];
Expand Down
4 changes: 4 additions & 0 deletions packages/transformers/html/src/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ export default function collectDependencies(
if (attrs.type === 'module' && asset.env.shouldScopeHoist) {
outputFormat = 'esmodule';
} else {
if (attrs.type === 'module') {
attrs.defer = '';
}

delete attrs.type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import path from 'path';
import {Transformer} from '@parcel/plugin';
import {relativePath} from '@parcel/utils';

const WRAPPER = path.join(__dirname, 'helpers', 'helpers.js');

function shouldExclude(asset, options) {
return (
Expand All @@ -26,22 +23,22 @@ export default (new Transformer({
return [asset];
}

let wrapperPath = relativePath(path.dirname(asset.filePath), WRAPPER);
if (!wrapperPath.startsWith('.')) {
wrapperPath = './' + wrapperPath;
}
let wrapperPath = `@parcel/transformer-react-refresh-wrap/${path.basename(
__dirname,
)}/helpers/helpers.js`;

let code = await asset.getCode();
let map = await asset.getMap();
let name = `$parcel$ReactRefreshHelpers$${asset.id.slice(-4)}`;

code = `var helpers = require(${JSON.stringify(wrapperPath)});
code = `var ${name} = require(${JSON.stringify(wrapperPath)});
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
helpers.prelude(module);
${name}.prelude(module);

try {
${code}
helpers.postlude(module);
${name}.postlude(module);
} finally {
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
Expand All @@ -57,6 +54,7 @@ ${code}
asset.addDependency({
specifier: wrapperPath,
specifierType: 'esm',
resolveFrom: __filename,
});

return [asset];
Expand Down