Skip to content

Commit

Permalink
fix(lwc-compiler): invalid CSS generation when minified is enabled (#552
Browse files Browse the repository at this point in the history
)

* fix(lwc-compiler): invalid CSS generation when minified is enabled

* fix: mistaching types in lwc-compiler

* test: fix test impacted by plugin order change

* Update style.ts

* Update style.ts
  • Loading branch information
pmdartus authored Aug 1, 2018
1 parent d0cf318 commit f19ebb1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions packages/lwc-compiler/src/transformers/__tests__/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ describe('CSS transform', () => {
color: var(--bg-color);
font-size: var(--font-size, 16px);
margin: var(--margin-small, var(--margin-medium, 20px));
border-bottom: 1px solid var(--lwc-border);
}`;

const expected = `
Expand All @@ -274,6 +275,7 @@ describe('CSS transform', () => {
color: \${customProperties(\`--bg-color\`)};
font-size: \${customProperties(\`--font-size\`, \`16px\`)};
margin: \${customProperties(\`--margin-small\`, \`\${customProperties(\`--margin-medium\`, \`20px\`)}\`)};
border-bottom: 1px solid \${customProperties(\`--lwc-border\`)};
}\`;
}
export default style;
Expand All @@ -288,6 +290,35 @@ describe('CSS transform', () => {
expect(pretify(code)).toBe(pretify(expected));
});

it('should transform var functions properly when minification is enabled', async () => {
const actual = `div {
color: var(--bg-color);
font-size: var(--font-size, 16px);
margin: var(--margin-small, var(--margin-medium, 20px));
border-bottom: 1px solid var(--lwc-border);
}`;

const expected = `
import customProperties from '@customProperties';
function style(token) {
return \`div[\${token}]{color:\${customProperties(\`--bg-color\`)};font-size:\${customProperties(\`--font-size\`, \`16px\`)};margin:\${customProperties(\`--margin-small\`, \`\${customProperties(\`--margin-medium\`, \`20px\`)}\`)};border-bottom:1px solid \${customProperties(\`--lwc-border\`)}}\`;
}
export default style;
`;

const { code } = await transform(actual, 'foo.css', {
stylesheetConfig: {
customProperties: { resolution: { type: 'module', name: '@customProperties' } },
},
outputConfig: {
minify: true
}
});

expect(pretify(code)).toBe(pretify(expected));
});

it('should escape grave accents', async () => {
const actual = `/* Comment with grave accents \`#\` */`;
const expected = `
Expand Down
27 changes: 16 additions & 11 deletions packages/lwc-compiler/src/transformers/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,35 @@ export default async function transformStyle(
const { minify } = outputConfig;
const { customProperties } = stylesheetConfig;

const plugins = [
postcssPluginLwc({
token: TOKEN_PLACEHOLDER,
customProperties: {
allowDefinition: customProperties.allowDefinition,
transformVar: transformVar(customProperties.resolution),
}
})
];
const postcssPlugins: postcss.AcceptedPlugin[] = [];

// The LWC plugin produces invalid CSS since it transforms all the var function with actual
// javascript function call. The mification plugin produces invalid CSS when it runs after
// the LWC plugin.
if (minify) {
plugins.push(
postcssPlugins.push(
cssnano({
svgo: false,
preset: ['default']
})
);
}

postcssPlugins.push(
postcssPluginLwc({
token: TOKEN_PLACEHOLDER,
customProperties: {
allowDefinition: customProperties.allowDefinition,
transformVar: transformVar(customProperties.resolution),
}
})
);

const escapedSource = escapeString(src);

let res;
try {
res = await postcss(plugins).process(escapedSource, {
res = await postcss(postcssPlugins).process(escapedSource, {
from: filename,
});
} catch (e) {
Expand Down

0 comments on commit f19ebb1

Please sign in to comment.