Skip to content

Commit 36c60e4

Browse files
committed
Convert postcss-custom-properties-fallback to esm
1 parent 3d6c0fd commit 36c60e4

File tree

8 files changed

+147
-246
lines changed

8 files changed

+147
-246
lines changed

package-lock.json

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

packages/rollup-package.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
33
import minifyHTML from 'rollup-plugin-minify-html-literals';
44
import { readPackageJson } from '../scripts/modify-pkgjson.mjs';
55
import rollupPostcss from 'rollup-plugin-postcss';
6-
import postcssCustomPropertiesFallback from 'postcss-custom-properties-fallback';
6+
import postcssCustomPropertiesFallback from '../scripts/postcss-custom-properties-fallback/plugin.mjs';
77
import path from 'path';
88
import processLitCSSPlugin from '../scripts/rollup.processLitCSSPlugin.mjs';
99
import importCss from 'rollup-plugin-import-css';

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

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

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

0 commit comments

Comments
 (0)