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

Bundle PostCSS Global Data Plugin with default configuration #389

Merged
merged 12 commits into from
Apr 29, 2024
5 changes: 5 additions & 0 deletions .changeset/thin-geckos-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"10up-toolkit": minor
---

Bundle PostCSS Global Data Plugin with default configuration
36 changes: 29 additions & 7 deletions package-lock.json

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
html {
background: #f5f5f5;
padding: 20px;

@mixin margin-trim;
Copy link
Member

Choose a reason for hiding this comment

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

@fabiankaegy I set up this test and for some reason, this line breaks building this file! It doesn't even create the css file.

Did I miss something? I'm wondering if this mixins is undefined and therefore breaking everyting. On the other hand I'd like to understand why this test doesn't even generate the css file with this line and it also doesn't give any error.

To run tests locally just do npm run test -w=packages/toolkit -- --watch you can run the tests then inspect the dist folder for each test to see what toolkit is outputting

Copy link
Member Author

Choose a reason for hiding this comment

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

@nicholasio it appears that the issue was due to the paths not being set correctly for the __fixtures__ path. See 52e5b67

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense! Thanks!


@media (--bp-small) {
padding: 40px;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Media Queries
*/
@custom-media --bp-tiny ( min-width: 25em ); /* 400px */
@custom-media --bp-small ( min-width: 30em ); /* 480px */
@custom-media --bp-medium ( min-width: 48em ); /* 768px */
@custom-media --bp-large ( min-width: 64em ); /* 1024px */
@custom-media --bp-xlarge ( min-width: 80em ); /* 1280px */
@custom-media --bp-xxlarge ( min-width: 90em ); /* 1440px */

/* WP Core Breakpoints (used for the admin bar for example) */
@custom-media --wp-small ( min-width: 600px );
@custom-media --wp-medium-max (max-width: 782px);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@define-mixin margin-trim {
margin-trim: block;

/* Fallback for browsers that don't support margin-trim */
@supports not (margin-trim: block) {

& > *:first-child {
margin-top: 0;
}

& > *:last-child {
margin-bottom: 0;
}
}
}
15 changes: 15 additions & 0 deletions packages/toolkit/__tests__/build-project-global-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test-build-project-global-css",
"10up-toolkit": {
"useBlockAssets": false,
"entry": {
"frontend-css": "./__fixtures__/assets/css/frontend/styles.css"
},
"paths": {
"srcDir": "./__fixtures__/assets/",
"cssLoaderPaths": ["./__fixtures__/assets/css", "./includes/blocks"],
"globalStylesDir": "./__fixtures__/assets/css/globals/",
"globalMixinsDir": "./__fixtures__/assets/css/mixins/"
}
}
}
24 changes: 24 additions & 0 deletions packages/toolkit/__tests__/build-project-global-css/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable import/no-extraneous-dependencies */
import spawn from 'cross-spawn';
import fs from 'fs';
import path from 'path';

describe('build a project', () => {
it('builds and compiles css with global css', async () => {
spawn.sync('node', ['../../scripts/build'], {
cwd: __dirname,
});

const frontendCss = path.join(__dirname, 'dist', 'css', 'frontend-css.css');

expect(fs.existsSync(frontendCss)).toBeTruthy();
expect(
fs.existsSync(path.join(__dirname, 'dist', 'css', 'frontend-css.asset.php')),
).toBeTruthy();

const compiledCSS = fs.readFileSync(frontendCss).toString();

// expect the compiled CSS to contain "min-width: 30em"
expect(compiledCSS).toMatch('min-width: 30em');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,14 @@ a c, b c, a d, b d {
exports[`postcss.config.js build a proper postcss config file for non-editor styles: env=development 1`] = `
{
"plugins": {
"@csstools/postcss-global-data": {
"files": [],
},
"cssnano": false,
"postcss-import": {},
"postcss-mixins": {},
"postcss-mixins": {
"mixinsFiles": [],
},
"postcss-preset-env": {
"features": {
"custom-properties": false,
Expand All @@ -268,6 +273,9 @@ exports[`postcss.config.js build a proper postcss config file for non-editor sty
exports[`postcss.config.js build a proper postcss config file for non-editor styles: env=production 1`] = `
{
"plugins": {
"@csstools/postcss-global-data": {
"files": [],
},
"cssnano": {
"preset": [
"default",
Expand All @@ -286,7 +294,9 @@ exports[`postcss.config.js build a proper postcss config file for non-editor sty
],
},
"postcss-import": {},
"postcss-mixins": {},
"postcss-mixins": {
"mixinsFiles": [],
},
"postcss-preset-env": {
"features": {
"custom-properties": false,
Expand All @@ -300,6 +310,9 @@ exports[`postcss.config.js build a proper postcss config file for non-editor sty
exports[`postcss.config.js builds a proper postcss config file for editor styles: env=development 1`] = `
{
"plugins": {
"@csstools/postcss-global-data": {
"files": [],
},
"cssnano": false,
"postcss-editor-styles-wrapper": {
"ignore": [
Expand All @@ -324,7 +337,9 @@ exports[`postcss.config.js builds a proper postcss config file for editor styles
],
},
"postcss-import": {},
"postcss-mixins": {},
"postcss-mixins": {
"mixinsFiles": [],
},
"postcss-preset-env": {
"features": {
"custom-properties": false,
Expand All @@ -338,6 +353,9 @@ exports[`postcss.config.js builds a proper postcss config file for editor styles
exports[`postcss.config.js builds a proper postcss config file for editor styles: env=production 1`] = `
{
"plugins": {
"@csstools/postcss-global-data": {
"files": [],
},
"cssnano": {
"preset": [
"default",
Expand Down Expand Up @@ -378,7 +396,9 @@ exports[`postcss.config.js builds a proper postcss config file for editor styles
],
},
"postcss-import": {},
"postcss-mixins": {},
"postcss-mixins": {
"mixinsFiles": [],
},
"postcss-preset-env": {
"features": {
"custom-properties": false,
Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/config/paths.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ module.exports = {
cssLoaderPaths: ['./assets/css', './includes/blocks'],
copyAssetsDir: './assets/',
blocksDir: './includes/blocks/',
globalStylesDir: './assets/css/globals/',
globalMixinsDir: './assets/css/mixins/',
};
16 changes: 15 additions & 1 deletion packages/toolkit/config/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
const path = require('path');
const glob = require('fast-glob');

const { getTenUpScriptsConfig } = require('../utils');

module.exports = ({ file, env }) => {
const projectConfig = getTenUpScriptsConfig();
const { globalStylesDir, globalMixinsDir } = projectConfig.paths;

const globalCssFiles = glob.sync(`${globalStylesDir}**/*.css`);
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
const globalMixinFiles = glob.sync(`${globalMixinsDir}**/*.css`);

const config = {
plugins: {
'postcss-import': {},
'postcss-mixins': {},
'@csstools/postcss-global-data': {
files: globalCssFiles,
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
},
'postcss-mixins': {
mixinsFiles: globalMixinFiles,
},
'postcss-preset-env': {
stage: 0,
features: {
Expand Down
13 changes: 7 additions & 6 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@
},
"dependencies": {
"@babel/eslint-parser": "^7.23.3",
"@csstools/postcss-global-data": "^2.1.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
"@svgr/webpack": "^8.1.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@wordpress/dependency-extraction-webpack-plugin": "^5.4.0",
"@wordpress/jest-console": "^7.19.0",
"@wordpress/eslint-plugin": "^17.5.0",
"@wordpress/jest-console": "^7.19.0",
"babel-jest": "^29.7.0",
"babel-loader": "^9.1.3",
"camelcase": "^6.3.0",
"copy-webpack-plugin": "^11.0.0",
"core-js": "^3.35.0",
"core-js-pure": "^3.35.0",
"cross-spawn": "^7.0.3",
"css-loader": "^6.8.1",
"cssnano": "^6.0.3",
"error-stack-parser": "^2.1.4",
"eslint-webpack-plugin": "^4.0.1",
"fast-glob": "^3.3.2",
"html-webpack-plugin": "^5.6.0",
Expand Down Expand Up @@ -63,22 +66,20 @@
"webpack-bundle-analyzer": "^4.10.1",
"webpack-dev-server": "^4.15.1",
"webpack-sources": "^3.2.3",
"webpackbar": "^6.0.0",
"error-stack-parser": "^2.1.4",
"core-js-pure": "^3.35.0"
"webpackbar": "^6.0.0"
},
"devDependencies": {
"@10up/babel-preset-default": ">=2.1.1",
"@10up/eslint-config": ">=4.0.0",
"@10up/stylelint-config": ">=3.0.0"
},
"peerDependencies": {
"typescript": ">=5.0.0",
"@10up/babel-preset-default": "^2.1.1",
"@10up/eslint-config": "^4.0.0",
"@10up/stylelint-config": "^3.0.0",
"@linaria/babel-preset": ">=4.3.3",
"@linaria/webpack-loader": ">=4.1.11",
"@linaria/babel-preset": ">=4.3.3"
"typescript": ">=5.0.0"
},
"peerDependenciesMeta": {
"@linaria/webpack-loader": {
Expand Down
1 change: 0 additions & 1 deletion projects/10up-theme/assets/css/frontend/global/index.css
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
@import url("colors.css");
@import url("media-queries.css");
6 changes: 6 additions & 0 deletions projects/10up-theme/assets/css/frontend/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
html {
background: #f5f5f5;
padding: 20px;

@mixin margin-trim;

@media (--bp-small) {
padding: 40px;
}
}

p {
Expand Down
15 changes: 15 additions & 0 deletions projects/10up-theme/assets/css/mixins/margin-trim.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@define-mixin margin-trim {
margin-trim: block;

/* Fallback for browsers that don't support margin-trim */
@supports not (margin-trim: block) {

& > *:first-child {
margin-top: 0;
}

& > *:last-child {
margin-bottom: 0;
}
}
}
6 changes: 6 additions & 0 deletions projects/10up-theme/includes/blocks/example/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
.wp-block-example-block__title {
border: 3px dashed #ccc;
padding: 1em;

@mixin margin-trim;

@media (--bp-small) {
padding: 2em;
}
}
Loading