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

Load svgo version from project #9969

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
fsFixture,
} from '@parcel/test-utils';
import path from 'path';
import Logger from '@parcel/logger';

describe('html', function () {
beforeEach(async () => {
Expand Down Expand Up @@ -679,6 +680,77 @@ describe('html', function () {
);
});

it('should detect the version of SVGO to use', async function () {
// Test is outside parcel so that svgo is not already installed.
await fsFixture(overlayFS, '/')`
htmlnano-svgo-version
index.html:
<!DOCTYPE html>
<html>
<body>
<svg><rect id="test" /></svg>
</body>
</html>

.htmlnanorc:
{
"minifySvg": {
"full": true
}
}

yarn.lock:
`;

let messages = [];
let loggerDisposable = Logger.onLog(message => {
if (message.level !== 'verbose') {
messages.push(message);
}
});

try {
await bundle(path.join('/htmlnano-svgo-version/index.html'), {
inputFS: overlayFS,
defaultTargetOptions: {
shouldOptimize: true,
},
shouldAutoinstall: false,
});
} catch (err) {
// autoinstall is disabled
assert.equal(
err.diagnostics[0].message,
'Could not resolve module "svgo" from "/htmlnano-svgo-version/index"',
);
}

loggerDisposable.dispose();
assert(
messages[0].diagnostics[0].message.startsWith(
'Detected deprecated SVGO v2 options in',
),
);
assert.deepEqual(messages[0].diagnostics[0].codeFrames, [
{
filePath: path.normalize('/htmlnano-svgo-version/.htmlnanorc'),
codeHighlights: [
{
message: undefined,
start: {
line: 3,
column: 5,
},
end: {
line: 3,
column: 16,
},
},
],
},
]);
});

it('should not minify default values inside HTML in production mode', async function () {
let inputFile = path.join(
__dirname,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { extendDefaultPlugins } = require('svgo');

module.exports = {
plugins: extendDefaultPlugins([
plugins: [
{
name: 'removeComments',
active: false
name: 'preset-default',
params: {
overrides: {
removeComments: false
}
}
}
])
]
}
79 changes: 77 additions & 2 deletions packages/core/integration-tests/test/svg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import assert from 'assert';
import {assertBundles, bundle, distDir, outputFS} from '@parcel/test-utils';
import {
assertBundles,
bundle,
distDir,
outputFS,
overlayFS,
fsFixture,
} from '@parcel/test-utils';
import path from 'path';
import Logger from '@parcel/logger';

describe('svg', function () {
it('should support bundling SVG', async () => {
Expand Down Expand Up @@ -126,6 +134,73 @@ describe('svg', function () {
assert(file.includes('comment'));
});

it('should detect the version of SVGO to use', async function () {
// Test is outside parcel so that svgo is not already installed.
await fsFixture(overlayFS, '/')`
svgo-version
icon.svg:
<svg></svg>

index.html:
<img src="icon.svg" />

svgo.config.json:
{
"full": true
}

yarn.lock:
`;

let messages = [];
let loggerDisposable = Logger.onLog(message => {
if (message.level !== 'verbose') {
messages.push(message);
}
});

try {
await bundle(path.join('/svgo-version/index.html'), {
inputFS: overlayFS,
defaultTargetOptions: {
shouldOptimize: true,
},
shouldAutoinstall: false,
});
} catch (err) {
// autoinstall is disabled
assert.equal(
err.diagnostics[0].message,
'Could not resolve module "svgo" from "/svgo-version/index"',
);
}

loggerDisposable.dispose();
assert(
messages[0].diagnostics[0].message.startsWith(
'Detected deprecated SVGO v2 options in',
),
);
assert.deepEqual(messages[0].diagnostics[0].codeFrames, [
{
filePath: path.normalize('/svgo-version/svgo.config.json'),
codeHighlights: [
{
message: undefined,
start: {
line: 2,
column: 3,
},
end: {
line: 2,
column: 14,
},
},
],
},
]);
});

it('should detect xml-stylesheet processing instructions', async function () {
let b = await bundle(
path.join(__dirname, '/integration/svg-xml-stylesheet/img.svg'),
Expand Down Expand Up @@ -222,7 +297,7 @@ describe('svg', function () {

const svg = await outputFS.readFile(path.join(distDir, 'img.svg'), 'utf8');

assert(svg.includes('<style>:root{fill:red}</style>'));
assert(svg.includes('style="fill:red"'));
});

it('should be in separate bundles', async function () {
Expand Down
1 change: 1 addition & 0 deletions packages/core/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ export {
remapSourceLocation,
} from './sourcemap';
export {default as stripAnsi} from 'strip-ansi';
export {detectSVGOVersion} from './svgo';
50 changes: 50 additions & 0 deletions packages/core/utils/src/svgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @flow
export function detectSVGOVersion(
config: any,
): {|version: 3|} | {|version: 2, path: string|} {
if (!config) {
return {version: 3};
}

// These options were removed in v2.
if (config.full != null || config.svg2js != null) {
return {version: 2, path: config.full != null ? '/full' : '/svg2js'};
}

if (Array.isArray(config.plugins)) {
// Custom plugins in v2 had additional (required) fields that don't exist anymore.
let v2Plugin = config.plugins.findIndex(
p => p?.type != null || (p?.fn && p?.params != null),
);
if (v2Plugin !== -1) {
let field = config.plugins[v2Plugin].type != null ? 'type' : 'params';
return {version: 2, path: `/plugins/${v2Plugin}/${field}`};
}

// the cleanupIDs plugin lost the prefix option in v3.
let cleanupIdsIndex = config.plugins.findIndex(
p => p?.name === 'cleanupIDs',
);
let cleanupIDs =
cleanupIdsIndex !== -1 ? config.plugins[cleanupIdsIndex] : null;
if (cleanupIDs?.params?.prefix != null) {
return {version: 2, path: `/plugins/${cleanupIdsIndex}/params/prefix`};
}

// Automatically migrate some options from SVGO 2 config files.
config.plugins = config.plugins.filter(p => p?.active !== false);

for (let i = 0; i < config.plugins.length; i++) {
let p = config.plugins[i];
if (p === 'cleanupIDs') {
config.plugins[i] = 'cleanupIds';
}

if (p?.name === 'cleanupIDs') {
config.plugins[i].name = 'cleanupIds';
}
}
}

return {version: 3};
}
8 changes: 6 additions & 2 deletions packages/optimizers/htmlnano/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
"parcel": "^2.12.0"
},
"dependencies": {
"@parcel/diagnostic": "2.12.0",
"@parcel/plugin": "2.12.0",
"@parcel/utils": "2.12.0",
"htmlnano": "^2.0.0",
"nullthrows": "^1.1.1",
"posthtml": "^0.16.5",
"svgo": "^2.4.0"
"posthtml": "^0.16.5"
},
"devDependencies": {
"svgo": "^3.3.2"
}
}
Loading
Loading