@live-codes/browser-compilers 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-codes/browser-compilers",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Compilers that run in the browser, for use in livecodes.io",
5
5
  "author": "Hatem Hosny",
6
6
  "license": "MIT",
@@ -64,6 +64,8 @@
64
64
  "postcss": "8.4.6",
65
65
  "postcss-modules": "6.0.0",
66
66
  "postcss-preset-env": "7.3.1",
67
+ "posthtml": "0.16.6",
68
+ "posthtml-css-modules": "0.1.3",
67
69
  "react": "18.2.0",
68
70
  "react-dom": "18.2.0",
69
71
  "react-native-web": "0.19.1",
@@ -1,3 +1,44 @@
1
1
  import postcssModules from 'postcss-modules';
2
+ import posthtml from 'posthtml';
3
+ import posthtmlCSSModules from 'posthtml-css-modules';
2
4
 
3
- export { postcssModules };
5
+ // based on https://github.com/posthtml/posthtml-class-to-css-module
6
+ const cloneClasses = (options = {}) => (tree) =>
7
+ tree.match({ attrs: { class: /.+/ } }, (node) => {
8
+ const className =
9
+ typeof options.cssModules === 'object'
10
+ ? node.attrs.class
11
+ .split(' ')
12
+ .filter(
13
+ (name) =>
14
+ Object.keys(options.cssModules).includes(name) && options.cssModules[name] !== name,
15
+ )
16
+ .join(' ')
17
+ : node.attrs.class;
18
+
19
+ if (className.trim()) {
20
+ node.attrs = Object.assign(node.attrs, { 'css-module': className });
21
+ }
22
+
23
+ if (options.removeOriginalClasses) {
24
+ delete node.attrs.class;
25
+ }
26
+
27
+ return node;
28
+ });
29
+
30
+ /**
31
+ *
32
+ * @param {string} html
33
+ * @param {Record<String, any>} cssModules
34
+ * @param {boolean} removeOriginalClasses
35
+ * @returns {string}
36
+ */
37
+ const addClassesToHtml = (html, cssModules, removeOriginalClasses = false) =>
38
+ posthtml()
39
+ // @ts-ignore
40
+ .use(cloneClasses({ cssModules, removeOriginalClasses }), posthtmlCSSModules(cssModules))
41
+ // @ts-ignore
42
+ .process(html, { sync: true }).html;
43
+
44
+ export { postcssModules, addClassesToHtml };