Skip to content

Commit 3d6c0fd

Browse files
committed
copying files into project
1 parent 3b7c6cf commit 3d6c0fd

File tree

8 files changed

+13282
-3554
lines changed

8 files changed

+13282
-3554
lines changed

package-lock.json

Lines changed: 13103 additions & 3551 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"postcss-cli": "9.0.1",
9292
"postcss-color-function": "4.1.0",
9393
"postcss-custom-properties": "12.0.0",
94-
"postcss-custom-properties-fallback": "1.0.1",
9594
"postcss-jsx": "0.36.4",
9695
"postcss-load-config": "3.1.0",
9796
"postcss-url": "^10.1.3",
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const fs = require('fs');
2+
3+
const path = require('path');
4+
5+
const postcss = require('postcss');
6+
7+
const {
8+
parse
9+
} = require('postcss-values-parser');
10+
11+
async function getCustomPropertiesFromCSSFile(from) {
12+
const css = await readFile(from); // eslint-disable-next-line no-unused-vars
13+
14+
postcss.parse(css, {
15+
from
16+
});
17+
throw new Error('Importing from CSS files not supported yet'); //return getCustomPropertiesFromRoot(root, { preserve: true });
18+
}
19+
/* Get Custom Properties from Object
20+
/* ========================================================================== */
21+
22+
23+
function getCustomPropertiesFromObject(object) {
24+
const customProperties = Object.assign({}, Object(object).customProperties, Object(object)['custom-properties']);
25+
26+
for (const key in customProperties) {
27+
customProperties[key] = parse(String(customProperties[key])).nodes;
28+
}
29+
30+
return customProperties;
31+
}
32+
/* Get Custom Properties from JSON file
33+
/* ========================================================================== */
34+
35+
36+
async function getCustomPropertiesFromJSONFile(from) {
37+
const object = await readJSON(from);
38+
return getCustomPropertiesFromObject(object);
39+
}
40+
/* Get Custom Properties from JS file
41+
/* ========================================================================== */
42+
43+
44+
async function getCustomPropertiesFromJSFile(from) {
45+
const object = require(from);
46+
47+
return getCustomPropertiesFromObject(object);
48+
}
49+
50+
module.exports = function getCustomPropertiesFromImports(sources) {
51+
return sources.map(source => {
52+
if (source instanceof Promise) {
53+
return source;
54+
} else if (source instanceof Function) {
55+
return source();
56+
} // read the source as an object
57+
58+
59+
const opts = source === Object(source) ? source : {
60+
from: String(source)
61+
}; // skip objects with Custom Properties
62+
63+
if (opts.customProperties || opts['custom-properties']) {
64+
return opts;
65+
} // source pathname
66+
67+
68+
const from = path.resolve(String(opts.from || '')); // type of file being read from
69+
70+
const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
71+
return {
72+
type,
73+
from
74+
};
75+
}).reduce(async (customProperties, source) => {
76+
const {
77+
type,
78+
from
79+
} = await source;
80+
81+
if (type === 'css') {
82+
return Object.assign(await customProperties, await getCustomPropertiesFromCSSFile(from));
83+
}
84+
85+
if (type === 'js') {
86+
return Object.assign(await customProperties, await getCustomPropertiesFromJSFile(from));
87+
}
88+
89+
if (type === 'json') {
90+
return Object.assign(await customProperties, await getCustomPropertiesFromJSONFile(from));
91+
}
92+
93+
return Object.assign(await customProperties, await getCustomPropertiesFromObject(await source));
94+
}, {});
95+
};
96+
97+
const readFile = from => new Promise((resolve, reject) => {
98+
fs.readFile(from, 'utf8', (error, result) => {
99+
if (error) {
100+
reject(error);
101+
} else {
102+
resolve(result);
103+
}
104+
});
105+
});
106+
107+
const readJSON = async from => JSON.parse(await readFile(from));
108+
//# sourceMappingURL=import-from.js.map

scripts/postcss-custom-properties-fallback/import-from.cjs.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const getCustomPropertiesFromImports = require('./import-from.cjs');
2+
3+
const valueParser = require('postcss-value-parser'); // match custom property inclusions
4+
// @TODO optimize to skip vars with fallbacks already
5+
6+
7+
const customPropertiesRegExp = /(^|[^\w-])var\([\W\w]+\)/; // var\([\W\w]+,[\W\w]+\) template for detecting if skippable?
8+
// whether the declaration should be potentially transformed
9+
10+
const isTransformableDecl = decl => customPropertiesRegExp.test(decl.value);
11+
12+
const {
13+
nodeToString
14+
} = require('postcss-values-parser'); // eslint-disable-next-line no-empty-pattern
15+
16+
17+
module.exports = opts => ({
18+
postcssPlugin: 'postcss-custom-properties-fallback',
19+
20+
prepare() {
21+
// sources to import custom selectors from
22+
const importFrom = [].concat(Object(opts).importFrom || []); // promise any custom selectors are imported
23+
24+
const customPropertiesPromise = getCustomPropertiesFromImports(importFrom);
25+
return {
26+
async Declaration(node) {
27+
if (isTransformableDecl(node)) {
28+
const customProperties = await customPropertiesPromise;
29+
const parsed = valueParser(node.value);
30+
parsed.walk(node => {
31+
// Only deal with vars without a fallback
32+
if (node.type !== 'function' || node.nodes.length !== 1) {
33+
return;
34+
}
35+
36+
const fallback = customProperties[node.nodes[0].value];
37+
38+
if (fallback && fallback.length === 1) {
39+
node.nodes.push({
40+
type: 'divider',
41+
value: ','
42+
}, {
43+
type: 'word',
44+
value: fallback
45+
});
46+
} //when fallback value contains more then one node, stringify them with value parser used to parse the customProperties object and add as one node type word.
47+
48+
49+
if (fallback && fallback.length > 1) {
50+
node.nodes.push({
51+
type: 'divider',
52+
value: ','
53+
}, {
54+
type: 'word',
55+
value: fallback.map(fallbackNode => nodeToString(fallbackNode)).join(' ')
56+
});
57+
}
58+
});
59+
node.value = parsed.toString();
60+
}
61+
}
62+
63+
};
64+
}
65+
66+
});
67+
//# sourceMappingURL=plugin.js.map

scripts/postcss-custom-properties-fallback/plugin.cjs.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/rollup.processLitCSSPlugin.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import postcss from 'postcss';
22
import syntax from 'postcss-jsx';
3-
import postcssCustomPropertiesFallback from 'postcss-custom-properties-fallback';
3+
import postcssCustomPropertiesFallback from './postcss-custom-properties-fallback/plugin.cjs';
44
import { createFilter } from '@rollup/pluginutils';
55
import postcssConfig from 'postcss-load-config';
66

scripts/vite.processLitCSSPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TODO: Find solution to use same code in CommonJS and ESM.
66
const createFilter = require('@rollup/pluginutils').createFilter;
77
const postcss = require('postcss');
88
const syntax = require('postcss-jsx');
9-
const postcssCustomPropertiesFallback = require('postcss-custom-properties-fallback');
9+
const postcssCustomPropertiesFallback = require('./postcss-custom-properties-fallback/plugin.cjs');
1010
const postcssConfig = require('postcss-load-config');
1111

1212
// @ts-ignore-start

0 commit comments

Comments
 (0)