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

Sort shorthand vs. longhand declarations — breaking change behind config #1700

Merged
merged 14 commits into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/chatty-geese-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@compiled/babel-plugin-strip-runtime': minor
'@compiled/parcel-optimizer': minor
'@compiled/webpack-loader': minor
'@compiled/react': minor
'@compiled/utils': minor
'@compiled/ssr-app': minor
'@compiled/css': minor
---

Sort shorthand properties so that they come before longhand properties.

When using Compiled, one of the following will happen:

Option 1. If stylesheet extraction is turned off ("runtime mode"): shorthand properties will be sorted before longhand properties, as long as they are not in a pseudo-selector like `:hover` or `:active`. This is enabled by default and cannot be turned off.

Option 2. If stylesheet extraction is turned on and one of the below is true:

- You are using Webpack
- You are using Parcel AND you are running in production mode

... shorthand properties will only be sorted if `sortShorthand: true` is passed to `CompiledExtractPlugin` (Webpack), or `sortShorthand: true` is passed to your Compiled config file like `.compiledcssrc` (Parcel). When sorting shorthand properties using this method (option 2), shorthand properties will always be sorted before longhand properties, taking precedence over pseudo-selectors like `:hover` or `:active`.
5 changes: 5 additions & 0 deletions .changeset/curvy-scissors-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/webpack-app': patch
---

Enable sortShorthand
5 changes: 5 additions & 0 deletions .changeset/lucky-gorillas-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compiled/utils': minor
---

Remove unused buildSourceMap function
7 changes: 6 additions & 1 deletion examples/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const classNameCompressionMap = require('./class-name-compression-map.json');

const extractCSS = process.env.EXTRACT_TO_CSS === 'true';

console.log('Stylesheet extraction enabled?', extractCSS);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this console.log intentional?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, i added it as a debugging measure but then decided to keep it as it seemed to be useful

happy to remove it if it's not valuable


module.exports = {
entry: './src/index.jsx',
mode: 'development',
Expand Down Expand Up @@ -62,7 +64,10 @@ module.exports = {
},
plugins: [
...(extractCSS
? [new MiniCssExtractPlugin({ filename: '[name].css' }), new CompiledExtractPlugin()]
? [
new MiniCssExtractPlugin({ filename: '[name].css' }),
new CompiledExtractPlugin({ sortShorthand: true }),
]
: []),
new HtmlWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
},
{
"path": "./packages/react/dist/browser/runtime/style.js",
"limit": "482B",
"limit": "1000B",
"import": "CS",
"ignore": [
"react"
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-plugin-strip-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ export default declare<PluginPass>((api) => {
cssFilename
);
mkdirSync(dirname(cssFilePath), { recursive: true });
writeFileSync(
cssFilePath,
sort(this.styleRules.sort().join('\n'), this.opts.sortAtRules)
);

const sortConfig = {
sortAtRulesEnabled: this.opts.sortAtRules,
sortShorthandEnabled: this.opts.sortShorthand,
};

writeFileSync(cssFilePath, sort(this.styleRules.sort().join('\n'), sortConfig));

// Add css import to file
path.unshiftContainer(
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-plugin-strip-runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export interface PluginOptions {
* Defaults to `true`.
*/
sortAtRules?: boolean;

/**
* Whether to sort shorthand and longhand properties,
* eg. `margin` before `margin-top` for enforced determinism.
* Defaults to `false`.
*/
sortShorthand?: boolean;
}

export interface PluginPass extends OriginalPluginPass {
Expand Down
3 changes: 2 additions & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"@types/autoprefixer": "^10.2.0",
"@types/cssnano": "^5.0.0"
"@types/cssnano": "^5.0.0",
"outdent": "^0.8.0"
}
}
4 changes: 3 additions & 1 deletion packages/css/src/plugins/__tests__/sort-at-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import postcss from 'postcss';
import { sortAtomicStyleSheet } from '../sort-atomic-style-sheet';

const transform = (css: string) => {
const result = postcss([sortAtomicStyleSheet(true)]).process(css, {
const result = postcss([
sortAtomicStyleSheet({ sortAtRulesEnabled: undefined, sortShorthandEnabled: undefined }),
]).process(css, {
from: undefined,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import { atomicifyRules } from '../atomicify-rules';
import { sortAtomicStyleSheet } from '../sort-atomic-style-sheet';

const transform = (css: TemplateStringsArray) => {
const result = postcss([sortAtomicStyleSheet(true)]).process(css[0], {
const result = postcss([
sortAtomicStyleSheet({ sortAtRulesEnabled: undefined, sortShorthandEnabled: undefined }),
]).process(css[0], {
from: undefined,
});

return result.css;
};

const transformWithAtomicClasses = (css: TemplateStringsArray) => {
const result = postcss([atomicifyRules(), sortAtomicStyleSheet(true), whitespace()]).process(
css[0],
{
from: undefined,
}
);
const result = postcss([
atomicifyRules(),
sortAtomicStyleSheet({ sortAtRulesEnabled: undefined, sortShorthandEnabled: undefined }),
whitespace(),
]).process(css[0], {
from: undefined,
});

return result.css;
};
Expand Down
Loading
Loading