Skip to content

Commit 2e8ce1c

Browse files
committed
support to export meta in the CSS
1 parent 173dd63 commit 2e8ce1c

File tree

4 files changed

+144
-11
lines changed

4 files changed

+144
-11
lines changed

packages/postcss-container-query/src/containerQuery.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ const walkRules = (root, opts, ruleHandler) => {
5858

5959
/**
6060
* @param {{
61-
* getJSON: function,
62-
* singleContainer: boolean,
61+
* [getJSON]: function,
62+
* [singleContainer]: boolean,
63+
* [exportMetaInCss]: boolean|string,
6364
* }} options
6465
*/
6566
function containerQuery(options = {}) {
6667
const getJSON = options.getJSON || saveMeta;
6768
const singleContainer = options.singleContainer !== false;
69+
const exportMetaInCss =
70+
typeof options.exportMetaInCss !== "undefined"
71+
? options.exportMetaInCss
72+
: "meta";
6873

6974
return function(root, result) {
7075
const containers = {};
@@ -158,7 +163,17 @@ function containerQuery(options = {}) {
158163
filepath
159164
});
160165

161-
getJSON(filepath, meta);
166+
if (typeof getJSON === "function") {
167+
getJSON(filepath, meta);
168+
}
169+
170+
// The following is picked up by css-loader, therefore making importing json
171+
// files obsolete
172+
if (exportMetaInCss) {
173+
root.append(
174+
`\n:export { ${exportMetaInCss}: '${JSON.stringify(meta)}' }`
175+
);
176+
}
162177
};
163178
}
164179

packages/postcss-container-query/src/containerQuery.spec.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import * as missingContainerDelcarationTest from "./test/missing-container-decla
99
import * as missingDeclarationWithRUnitsTest from "./test/missing-declaration-with-r-units";
1010
import * as selfTest from "./test/self";
1111
import * as simpleTest from "./test/simple";
12+
import * as metaExportTest from "./test/meta-export";
13+
import * as metaNamedExportTest from "./test/meta-named-export";
1214
import fs from "fs";
13-
14-
const containerQuery = require("../dist");
15-
const getMetadataFromMessages = require("../getMetadataFromMessages");
16-
const saveMeta = require("../saveMeta");
15+
import containerQuery from "./containerQuery";
16+
import getMetadataFromMessages from "../getMetadataFromMessages";
17+
import saveMeta from "../saveMeta";
1718

1819
jest.mock("fs");
1920

@@ -51,7 +52,10 @@ const processCss = async (rawCSS, options = {}) => {
5152
* @param {{}} [options] plugin options
5253
*/
5354
const assertProcessingResult = async (testObj, options = {}) => {
54-
const { css, meta } = await processCss(testObj.cssInput, options);
55+
const { css, meta } = await processCss(testObj.cssInput, {
56+
exportMetaInCss: false,
57+
...options
58+
});
5559

5660
expect(css).toBe(testObj.cssOutput);
5761
expect(meta).toEqual(testObj.meta);
@@ -67,7 +71,7 @@ test('should avoid accidentally creating ".default" exports', () => {
6771
});
6872

6973
test("should use the default json saving function if none was supplied", () => {
70-
const pluginInstance = containerQuery();
74+
const pluginInstance = containerQuery({ exportMetaInCss: false });
7175

7276
pluginInstance(new Root(), { messages: [] });
7377

@@ -99,10 +103,16 @@ test("should throw on missing container declaration when the container has r-uni
99103
});
100104

101105
test("should ignore unrecognised at-rules, like @keyframes", () =>
102-
assertProcessingResult(unrecognisedAtRulesTest, { singleContainer: false }));
106+
assertProcessingResult(unrecognisedAtRulesTest, {
107+
singleContainer: false,
108+
exportMetaInCss: false
109+
}));
103110

104111
test("should properly process CSS", () =>
105-
assertProcessingResult(regularTest, { singleContainer: false }));
112+
assertProcessingResult(regularTest, {
113+
singleContainer: false,
114+
exportMetaInCss: false
115+
}));
106116

107117
// This also tests that containers are processed even without queries
108118
test("should detect the first class as the container by default", () =>
@@ -126,3 +136,11 @@ test("should handle :self", () => assertProcessingResult(selfTest));
126136

127137
test("should be able to run this simple test", () =>
128138
assertProcessingResult(simpleTest));
139+
140+
test("should export the meta in the css by default", () =>
141+
assertProcessingResult(metaExportTest, { exportMetaInCss: undefined }));
142+
143+
test("should be able to export the meta under a custom export", () =>
144+
assertProcessingResult(metaNamedExportTest, {
145+
exportMetaInCss: "custom-meta"
146+
}));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
SELECTOR,
3+
QUERIES,
4+
ELEMENTS,
5+
VALUES,
6+
CONDITIONS
7+
} from "@zeecoder/container-query-meta-builder";
8+
9+
export const cssInput = `
10+
.Container {
11+
line-height: 1rh;
12+
}
13+
14+
@container (width > 100px) {
15+
:self {
16+
line-height: 2rh;
17+
}
18+
}
19+
`;
20+
21+
export const meta = {
22+
[SELECTOR]: ".Container",
23+
[QUERIES]: [
24+
{
25+
[ELEMENTS]: [
26+
{
27+
[VALUES]: {
28+
"line-height": "1rh"
29+
}
30+
}
31+
]
32+
},
33+
{
34+
[ELEMENTS]: [
35+
{
36+
[VALUES]: {
37+
"line-height": "2rh"
38+
}
39+
}
40+
],
41+
[CONDITIONS]: [[["width", ">", 100]]]
42+
}
43+
]
44+
};
45+
46+
export const cssOutput = `
47+
.Container {
48+
}
49+
:export { meta: '${JSON.stringify(meta)}' }
50+
`;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
SELECTOR,
3+
QUERIES,
4+
ELEMENTS,
5+
VALUES,
6+
CONDITIONS
7+
} from "@zeecoder/container-query-meta-builder";
8+
9+
export const cssInput = `
10+
.Container {
11+
line-height: 1rh;
12+
}
13+
14+
@container (width > 100px) {
15+
:self {
16+
line-height: 2rh;
17+
}
18+
}
19+
`;
20+
21+
export const meta = {
22+
[SELECTOR]: ".Container",
23+
[QUERIES]: [
24+
{
25+
[ELEMENTS]: [
26+
{
27+
[VALUES]: {
28+
"line-height": "1rh"
29+
}
30+
}
31+
]
32+
},
33+
{
34+
[ELEMENTS]: [
35+
{
36+
[VALUES]: {
37+
"line-height": "2rh"
38+
}
39+
}
40+
],
41+
[CONDITIONS]: [[["width", ">", 100]]]
42+
}
43+
]
44+
};
45+
46+
export const cssOutput = `
47+
.Container {
48+
}
49+
:export { custom-meta: '${JSON.stringify(meta)}' }
50+
`;

0 commit comments

Comments
 (0)