@eightshift/frontend-libs-tailwind 3.1.2 → 3.3.0

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.
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Webpack plugin that scans all `manifest.json` files inside a project and
3
+ * emits two class lists (`es-tw-frontend-classes.txt`, `es-tw-editor-classes.txt`)
4
+ * that Tailwind v4 can pick up via `@source`.
5
+ *
6
+ * Classes are collected from `twClasses` (frontend) and `twClasses` +
7
+ * `twClassesEditor` + `twClassesEditorOnly` (editor). Any classes nested under
8
+ * an object with `responsive: true` are expanded into all configured
9
+ * breakpoint variants (`{bp}:cls` and `max-{bp}:cls`).
10
+ */
11
+
12
+ import fs from 'fs';
13
+ import path from 'path';
14
+ import { glob } from 'glob';
15
+
16
+ const DEFAULT_BREAKPOINTS = ['sm', 'md'];
17
+ const DEFAULT_MANIFEST_GLOB = 'src/**/manifest.json';
18
+ const FRONTEND_CLASS_KEYS = new Set(['twClasses']);
19
+ const EDITOR_CLASS_KEYS = new Set(['twClasses', 'twClassesEditor', 'twClassesEditorOnly']);
20
+
21
+ const isResponsiveToken = (token, breakpoints) => breakpoints.some((bp) => token.startsWith(`${bp}:`) || token.startsWith(`max-${bp}:`));
22
+
23
+ const addClasses = (value, breakpoints, output, isResponsive = false) => {
24
+ if (!value) {
25
+ return;
26
+ }
27
+
28
+ if (Array.isArray(value)) {
29
+ value.forEach((item) => addClasses(item, breakpoints, output, isResponsive));
30
+ return;
31
+ }
32
+
33
+ if (typeof value === 'object') {
34
+ const nextResponsive = isResponsive || value.responsive === true;
35
+
36
+ Object.entries(value).forEach(([key, item]) => {
37
+ if (key !== 'responsive') {
38
+ addClasses(item, breakpoints, output, nextResponsive);
39
+ }
40
+ });
41
+
42
+ return;
43
+ }
44
+
45
+ if (typeof value !== 'string') {
46
+ return;
47
+ }
48
+
49
+ value
50
+ .split(/\s+/)
51
+ .filter(Boolean)
52
+ .forEach((token) => {
53
+ if (isResponsiveToken(token, breakpoints)) {
54
+ output.add(token);
55
+ return;
56
+ }
57
+
58
+ if (isResponsive) {
59
+ breakpoints.forEach((bp) => {
60
+ output.add(`${bp}:${token}`);
61
+ output.add(`max-${bp}:${token}`);
62
+ });
63
+ }
64
+ });
65
+ };
66
+
67
+ const collectTailwindClasses = (value, allowedKeys, breakpoints, output) => {
68
+ if (!value || typeof value !== 'object') {
69
+ return;
70
+ }
71
+
72
+ if (Array.isArray(value)) {
73
+ value.forEach((item) => collectTailwindClasses(item, allowedKeys, breakpoints, output));
74
+ return;
75
+ }
76
+
77
+ Object.entries(value).forEach(([key, item]) => {
78
+ if (allowedKeys.has(key)) {
79
+ addClasses(item, breakpoints, output, value.responsive === true);
80
+ return;
81
+ }
82
+
83
+ collectTailwindClasses(item, allowedKeys, breakpoints, output);
84
+ });
85
+ };
86
+
87
+ const processManifest = (jsonPath, breakpoints) => {
88
+ const content = fs.readFileSync(jsonPath, 'utf-8');
89
+ const json = JSON.parse(content);
90
+ const frontend = new Set();
91
+ const editor = new Set();
92
+
93
+ collectTailwindClasses(json, FRONTEND_CLASS_KEYS, breakpoints, frontend);
94
+ collectTailwindClasses(json, EDITOR_CLASS_KEYS, breakpoints, editor);
95
+
96
+ return { frontend, editor };
97
+ };
98
+
99
+ const runPreprocess = ({ projectDir, breakpoints, manifestGlob, outputFrontend, outputEditor }) => {
100
+ const matches = glob.sync(manifestGlob, { cwd: projectDir, absolute: true });
101
+
102
+ const frontend = new Set();
103
+ const editor = new Set();
104
+
105
+ matches.forEach((filePath) => {
106
+ const result = processManifest(filePath, breakpoints);
107
+ result.frontend.forEach((cls) => frontend.add(cls));
108
+ result.editor.forEach((cls) => editor.add(cls));
109
+ });
110
+
111
+ fs.writeFileSync(outputFrontend, [...frontend].join(' '));
112
+ fs.writeFileSync(outputEditor, [...editor].join(' '));
113
+ };
114
+
115
+ /**
116
+ * Factory returning a webpack plugin that regenerates Tailwind class source
117
+ * files before each build and on every manifest change in watch mode.
118
+ *
119
+ * @param {object} options
120
+ * @param {string} options.projectDir Absolute path to the project root.
121
+ * @param {string[]} [options.breakpoints] Tailwind breakpoint prefixes to expand. Defaults to `['sm', 'md']`.
122
+ * @param {string} [options.manifestGlob] Glob (relative to `projectDir`) used to find manifests. Defaults to `src/**\/manifest.json`.
123
+ * @param {{ frontend?: string, editor?: string }} [options.output] Override absolute output paths.
124
+ */
125
+ export const esTwResponsiveCompiler = ({ projectDir, breakpoints = DEFAULT_BREAKPOINTS, manifestGlob = DEFAULT_MANIFEST_GLOB, output = {} } = {}) => {
126
+ if (!projectDir) {
127
+ throw new Error('esTwResponsiveCompiler: `projectDir` is required.');
128
+ }
129
+
130
+ const config = {
131
+ projectDir,
132
+ breakpoints,
133
+ manifestGlob,
134
+ outputFrontend: output.frontend ?? path.resolve(projectDir, 'es-tw-frontend-classes.txt'),
135
+ outputEditor: output.editor ?? path.resolve(projectDir, 'es-tw-editor-classes.txt'),
136
+ };
137
+
138
+ const run = (label) => {
139
+ console.log(`🔄 ${label}`);
140
+ try {
141
+ runPreprocess(config);
142
+ console.log('✅ Done!');
143
+ } catch (error) {
144
+ console.error('❌ Failed. ', error);
145
+ }
146
+ };
147
+
148
+ return {
149
+ apply(compiler) {
150
+ compiler.hooks.beforeRun.tap('EsTwResponsiveCompiler', () => {
151
+ run('Generating Tailwind manifest class sources');
152
+ });
153
+
154
+ compiler.hooks.watchRun.tapAsync('EsTwResponsiveCompiler', ({ modifiedFiles }, callback) => {
155
+ const manifestChanged = modifiedFiles && [...modifiedFiles].some((file) => file.includes('manifest.json'));
156
+
157
+ if (manifestChanged) {
158
+ run('A manifest was modified, re-running Tailwind manifest class generation');
159
+ }
160
+
161
+ callback();
162
+ });
163
+ },
164
+ };
165
+ };