Skip to content

Commit 9fba393

Browse files
committed
wip
1 parent 7917bab commit 9fba393

File tree

4 files changed

+1137
-120
lines changed

4 files changed

+1137
-120
lines changed

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,30 @@
1010
"bugs": {
1111
"url": "https://github.com/ZeeCoder/container-query/issues"
1212
},
13-
"dependencies": {},
1413
"devDependencies": {
1514
"coveralls": "^3.0.0",
16-
"husky": "^0.14.3",
15+
"husky": "^1.1.3",
1716
"jest": "^22.4.2",
1817
"lerna": "^2.9.0",
19-
"lint-staged": "^7.0.0",
20-
"prettier": "^1.10.2"
18+
"lint-staged": "^8.0.4",
19+
"prettier": "^1.14.3"
2120
},
2221
"scripts": {
2322
"test": "jest --coverage",
2423
"coveralls": "cat ./coverage/lcov.info | coveralls",
2524
"watch:test": "jest --watch",
26-
"precommit": "lint-staged",
2725
"pretest": "lerna run flow && yarn run build",
2826
"prettify": "prettier --write 'packages/**/*.js'",
2927
"build": "lerna run build",
3028
"bootstrap": "lerna bootstrap"
3129
},
30+
"husky": {
31+
"hooks": {
32+
"pre-commit": "lint-staged"
33+
}
34+
},
3235
"lint-staged": {
33-
"*.js": [
36+
"*.{js,md}": [
3437
"prettier --write",
3538
"git add"
3639
]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Runs through all selectors of a meta object.
3+
* @param {{}} meta
4+
* @param {function} cb
5+
*/
6+
const mapMetaSelectors = (meta, cb) => {
7+
if (typeof meta[SELECTOR] === "string") {
8+
meta[SELECTOR] = cb(meta[SELECTOR]);
9+
}
10+
11+
// Patching the elements' selectors
12+
if (Array.isArray(meta[QUERIES])) {
13+
meta[QUERIES].forEach(query => {
14+
if (Array.isArray(query[ELEMENTS])) {
15+
query[ELEMENTS].forEach(element => {
16+
if (typeof element[SELECTOR] === "string") {
17+
element[SELECTOR] = cb(element[SELECTOR]);
18+
}
19+
});
20+
}
21+
});
22+
}
23+
};
24+
25+
/**
26+
* Rewrites the meta selectors according to the styles map object.
27+
* @param {{}} rawMeta
28+
* @param {{}} styles A classname => classname map, like the one CSS Modules provides.
29+
* Ex: "{ App: "App_ehyfd" }"
30+
* @return {{}}
31+
*/
32+
const remapMetaSelectors = (rawMeta, styles) => {
33+
// If meta is a string, then assume it's from a css-loader export
34+
// todo trim quotations instead of just slicing
35+
const meta =
36+
typeof rawMeta === "string" ? JSON.parse(rawMeta.slice(1, -1)) : rawMeta;
37+
38+
/**
39+
* Checks if the given css selector has a hashed css class in the given `styles`
40+
* object
41+
* @param {string} selector Ex: ".App"
42+
* @return {boolean}
43+
*/
44+
const hasSelectorInStyles = selector =>
45+
typeof styles[selector.slice(1)] === "string";
46+
47+
/**
48+
* Returns a hashed css class to the given selector, from the styles object.
49+
* @param {string} selector Ex: ".App"
50+
* @return {string} Ex: ".App_wCjtv"
51+
*/
52+
const getMappedCssClass = selector => `.${styles[selector.slice(1)]}`;
53+
54+
mapMetaSelectors(meta, selector => {
55+
if (!hasSelectorInStyles(selector)) {
56+
return selector;
57+
}
58+
59+
return getMappedCssClass(selector);
60+
});
61+
62+
return meta;
63+
};
64+
65+
export default remapMetaSelectors;

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ const walkRules = (root, opts, ruleHandler) => {
6363
* }} options
6464
*/
6565
function containerQuery(options = {}) {
66-
const getJSON = options.getJSON || saveMeta;
66+
const getJSON =
67+
typeof options.getJSON !== "undefined" ? options.getJSON : saveMeta;
6768
const singleContainer = options.singleContainer !== false;
6869

6970
return function(root, result) {
@@ -145,7 +146,9 @@ function containerQuery(options = {}) {
145146

146147
const meta = !singleContainer
147148
? containers
148-
: currentContainerSelector ? containers[currentContainerSelector] : {};
149+
: currentContainerSelector
150+
? containers[currentContainerSelector]
151+
: {};
149152

150153
const filepath = root.source.input.file;
151154

@@ -156,7 +159,14 @@ function containerQuery(options = {}) {
156159
filepath
157160
});
158161

159-
getJSON(filepath, meta);
162+
if (typeof getJSON === "function") {
163+
getJSON(filepath, meta);
164+
}
165+
166+
// todo if option is set
167+
root.append(`
168+
:export { meta: '${JSON.stringify(meta)}' }
169+
`);
160170
};
161171
}
162172

0 commit comments

Comments
 (0)