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

feat: better optimize gradients #739

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 72 additions & 2 deletions packages/postcss-minify-gradients/src/__tests__/index.js
Expand Up @@ -14,6 +14,34 @@ test(
'background:linear-gradient(0deg,#ffe500,#121)'
);

test(
'linear: should convert "to top" to 0deg (uppercase)',
processCSS,
'background:LINEAR-GRADIENT(TO TOP,#FFE500,#121)',
'background:LINEAR-GRADIENT(0deg,#FFE500,#121)'
);

test(
'linear: should convert "to top" to 0deg (webkit)',
processCSS,
'background:-webkit-linear-gradient(to top,#ffe500,#121)',
'background:-webkit-linear-gradient(0deg,#ffe500,#121)'
);

test(
'linear: should convert "to top" to 0deg (mozilla)',
processCSS,
'background:-moz-linear-gradient(to top,#ffe500,#121)',
'background:-moz-linear-gradient(0deg,#ffe500,#121)'
);

test(
'linear: should convert "to top" to 0deg (opera)',
processCSS,
'background:-o-linear-gradient(to top,#ffe500,#121)',
'background:-o-linear-gradient(0deg,#ffe500,#121)'
);

test(
'linear: should convert "to top" to 0deg (uppercase property and value)',
processCSS,
Expand Down Expand Up @@ -66,8 +94,29 @@ test(
test(
'repeating-linear: should convert "to top" to 0deg (uppercase)',
processCSS,
'background:REPEATING-LINEAR-GRADIENT(TO TOP,#ffe500,#121)',
'background:REPEATING-LINEAR-GRADIENT(0deg,#ffe500,#121)'
'background:REPEATING-LINEAR-GRADIENT(TO TOP,#FFE500,#121)',
'background:REPEATING-LINEAR-GRADIENT(0deg,#FFE500,#121)'
);

test(
'repeating-linear: should convert "to top" to 0deg (webkit)',
processCSS,
'background:-webkit-repeating-linear-gradient(to top,#ffe500,#121)',
'background:-webkit-repeating-linear-gradient(0deg,#ffe500,#121)'
);

test(
'repeating-linear: should convert "to top" to 0deg (mozilla)',
processCSS,
'background:-moz-repeating-linear-gradient(to top,#ffe500,#121)',
'background:-moz-repeating-linear-gradient(0deg,#ffe500,#121)'
);

test(
'repeating-linear: should convert "to top" to 0deg (opera)',
processCSS,
'background:-o-repeating-linear-gradient(to top,#ffe500,#121)',
'background:-o-repeating-linear-gradient(0deg,#ffe500,#121)'
);

test(
Expand Down Expand Up @@ -204,6 +253,13 @@ test(
'background: -webkit-radial-gradient(50% 26px, circle, #fff, rgba(255, 255, 255, 0) 24%)'
);

test(
'radial: should correctly account with prefix "-webkit" (1) with px',
processCSS,
'background: -webkit-radial-gradient(50% 26px, circle, #fff, rgba(255, 255, 255, 0) 24%)',
'background: -webkit-radial-gradient(50% 26px, circle, #fff, rgba(255, 255, 255, 0) 24%)'
);

test(
'radial: should correctly account with prefix "-webkit" (1) with px (uppercase)',
processCSS,
Expand Down Expand Up @@ -267,6 +323,13 @@ test(
'background: -webkit-radial-gradient(white CALC(30%), black calc(50%))'
);

test(
'radial: should correctly account with prefix "-moz" (1)',
processCSS,
'background: -moz-radial-gradient(50% 26%, circle, #fff, rgba(255, 255, 255, 0) 24%)',
'background: -moz-radial-gradient(50% 26%, circle, #fff, rgba(255, 255, 255, 0) 24%)'
);

test(
'should not mangle floating point numbers',
processCSS,
Expand Down Expand Up @@ -372,6 +435,13 @@ test(
'background:linear-gradient(env(--var))'
);

test(
'linear: should convert "to top" to 0deg with multiple gradients notations',
processCSS,
'background:linear-gradient(env(--var)),linear-gradient(to top,#ffe500,#121),linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%)',
'background:linear-gradient(env(--var)),linear-gradient(0deg,#ffe500,#121),linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%)'
);

test('should not throw error on broken syntax', passthroughCSS, 'background:');

test(
Expand Down
46 changes: 28 additions & 18 deletions packages/postcss-minify-gradients/src/index.js
Expand Up @@ -24,13 +24,7 @@ function optimise(decl) {
return;
}

const normalizedValue = value.toLowerCase();

if (normalizedValue.includes('var(') || normalizedValue.includes('env(')) {
return;
}

if (!normalizedValue.includes('gradient')) {
if (!value.toLowerCase().includes('gradient')) {
return;
}

Expand All @@ -40,13 +34,21 @@ function optimise(decl) {
return false;
}

const lowerCasedValue = node.value.toLowerCase();
const stringifiedFunctionNodes = stringify(node.nodes);

if (
stringifiedFunctionNodes.includes('var(') ||
stringifiedFunctionNodes.includes('env(')
) {
return false;
}

const lowerCasedFunctionName = node.value.toLowerCase();
const functionName = postcss.vendor.unprefixed(lowerCasedFunctionName);

if (
lowerCasedValue === 'linear-gradient' ||
lowerCasedValue === 'repeating-linear-gradient' ||
lowerCasedValue === '-webkit-linear-gradient' ||
lowerCasedValue === '-webkit-repeating-linear-gradient'
functionName === 'linear-gradient' ||
functionName === 'repeating-linear-gradient'
) {
let args = getArguments(node);

Expand Down Expand Up @@ -97,9 +99,13 @@ function optimise(decl) {
return false;
}

const prefix = postcss.vendor.prefix(lowerCasedFunctionName);

// New W3C syntax
if (
lowerCasedValue === 'radial-gradient' ||
lowerCasedValue === 'repeating-radial-gradient'
!prefix &&
(functionName === 'radial-gradient' ||
functionName === 'repeating-radial-gradient')
) {
let args = getArguments(node);
let lastStop;
Expand Down Expand Up @@ -129,9 +135,12 @@ function optimise(decl) {
return false;
}

// Old syntax FF 3.6-15, Chrome 10-25, Safari 5.1-6
// Opera doesn't support `-o` prefix
if (
lowerCasedValue === '-webkit-radial-gradient' ||
lowerCasedValue === '-webkit-repeating-radial-gradient'
prefix &&
(functionName === 'radial-gradient' ||
functionName === 'repeating-radial-gradient')
) {
let args = getArguments(node);
let lastStop;
Expand All @@ -153,11 +162,12 @@ function optimise(decl) {
stop = arg[2].value;
}
} else {
// eslint-disable-next-line no-lonely-if
if (arg[0].type === 'function') {
color = `${arg[0].value}(${stringify(arg[0].nodes)})`;
} else {
color = arg[0].value;
}

color = arg[0].value;
}

color = color.toLowerCase();
Expand Down