@eightshift/frontend-libs-tailwind 3.4.0 → 4.0.0-beta.1
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/.github/CODEOWNERS +1 -1
- package/CHANGELOG.md +19 -1
- package/README.md +187 -0
- package/bin/tw-sweep.mjs +281 -0
- package/bun/build.mjs +341 -0
- package/bun/cli.mjs +77 -0
- package/bun/css.mjs +145 -0
- package/bun/discovery.mjs +121 -0
- package/bun/editor.mjs +105 -0
- package/bun/eightshift-css.mjs +66 -0
- package/bun/eightshift-discovery.mjs +135 -0
- package/bun/flat-publication.mjs +343 -0
- package/bun/index.mjs +118 -0
- package/bun/media.mjs +49 -0
- package/bun/preset.mjs +118 -0
- package/bun/publish.mjs +236 -0
- package/bun/release.mjs +182 -0
- package/{webpack/responsive-compiler.mjs → bun/responsive.mjs} +18 -39
- package/bun/ssr.mjs +89 -0
- package/bun/theme-build.mjs +464 -0
- package/bun/theme-discovery.mjs +171 -0
- package/bun/watch.mjs +211 -0
- package/bun/wordpress.mjs +173 -0
- package/linters/oxlint.config.mjs +65 -0
- package/linters/tw-ignore.mjs +45 -0
- package/linters/tw-sweep-extract.mjs +545 -0
- package/package.json +16 -16
- package/test/fixtures/theme/src/Blocks/custom/card/card.php +23 -0
- package/test/fixtures/theme/src/Blocks/custom/card/manifest.json +13 -0
- package/test/fixtures/theme/src/Plugins/Forms.php +15 -0
- package/test/fixtures/theme/src/card.js +2 -0
- package/test/fixtures/theme/tailwind.css +6 -0
- package/test/fixtures/theme/tw-ignore.json +1 -0
- package/test/tw-sweep.test.mjs +119 -0
- package/bun.lock +0 -1433
- package/webpack/base.mjs +0 -126
- package/webpack/helpers.mjs +0 -55
- package/webpack/index.mjs +0 -46
- package/webpack/production.mjs +0 -60
- package/webpack/project.mjs +0 -50
package/bun/watch.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { statSync, watch } from 'node:fs';
|
|
2
|
+
import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Watch source paths under projectDir and run build serially after changes settle.
|
|
6
|
+
* Paths in sources and ignored are relative to projectDir unless absolute.
|
|
7
|
+
* Ignored paths exclude themselves and their descendants, including outputs.
|
|
8
|
+
* `ignore` optionally filters absolute event paths in addition to ignored paths.
|
|
9
|
+
* Returns { stop }, whose promise settles after any running build finishes.
|
|
10
|
+
* Build and watcher errors are passed to onError; watcher errors stop watching.
|
|
11
|
+
*/
|
|
12
|
+
export function watchBuild({ projectDir, sources, ignored = [], ignore = () => false, build, debounce = 200, initial = true, onError = console.error }) {
|
|
13
|
+
if (!projectDir || !Array.isArray(sources) || sources.length === 0 || !Array.isArray(ignored) || typeof ignore !== 'function' || typeof build !== 'function' || typeof onError !== 'function' || !Number.isFinite(debounce) || debounce < 0) {
|
|
14
|
+
throw new TypeError('watchBuild requires projectDir, sources, a build function, and a non-negative debounce.');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const root = resolve(projectDir);
|
|
18
|
+
const within = (parent, path) => {
|
|
19
|
+
const rel = relative(parent, path);
|
|
20
|
+
|
|
21
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
22
|
+
};
|
|
23
|
+
const paths = (items) =>
|
|
24
|
+
items.map((item) => {
|
|
25
|
+
if (typeof item !== 'string' || !item) {
|
|
26
|
+
throw new TypeError('Watch paths must be non-empty strings.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const path = resolve(root, item);
|
|
30
|
+
|
|
31
|
+
if (!within(root, path)) {
|
|
32
|
+
throw new TypeError('Watch paths must be inside projectDir.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return path;
|
|
36
|
+
});
|
|
37
|
+
const inputs = paths(sources);
|
|
38
|
+
const exclusions = paths(ignored);
|
|
39
|
+
let timer;
|
|
40
|
+
let running;
|
|
41
|
+
let pending = false;
|
|
42
|
+
let stopped = false;
|
|
43
|
+
const watchers = new Map();
|
|
44
|
+
|
|
45
|
+
const report = (error) => {
|
|
46
|
+
try {
|
|
47
|
+
onError(error);
|
|
48
|
+
} catch (callbackError) {
|
|
49
|
+
console.error(callbackError);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const run = () => {
|
|
53
|
+
if (stopped || running || !pending) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pending = false;
|
|
58
|
+
running = Promise.resolve()
|
|
59
|
+
.then(build)
|
|
60
|
+
.catch(report)
|
|
61
|
+
.finally(() => {
|
|
62
|
+
running = undefined;
|
|
63
|
+
|
|
64
|
+
if (!timer) {
|
|
65
|
+
run();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
const schedule = () => {
|
|
70
|
+
if (stopped) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
pending = true;
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
timer = setTimeout(() => {
|
|
77
|
+
timer = undefined;
|
|
78
|
+
run();
|
|
79
|
+
}, debounce);
|
|
80
|
+
};
|
|
81
|
+
const stop = async () => {
|
|
82
|
+
if (!stopped) {
|
|
83
|
+
stopped = true;
|
|
84
|
+
pending = false;
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
for (const watcher of watchers.values()) {
|
|
87
|
+
watcher.close();
|
|
88
|
+
}
|
|
89
|
+
watchers.clear();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await running;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const onEvent = (path) => {
|
|
96
|
+
if (stopped) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!within(root, path) || exclusions.some((item) => within(item, path))) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
if (ignore(path)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
report(error);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (inputs.some((item) => within(item, path) || within(path, item))) {
|
|
114
|
+
schedule();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const refresh = () => {
|
|
118
|
+
if (stopped) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const desired = new Map();
|
|
122
|
+
const add = (directory, recursive, name) => {
|
|
123
|
+
const key = `${recursive ? 'recursive' : 'parent'}:${directory}`;
|
|
124
|
+
if (!desired.has(key)) {
|
|
125
|
+
desired.set(key, { directory, recursive, names: new Set() });
|
|
126
|
+
}
|
|
127
|
+
if (name) {
|
|
128
|
+
desired.get(key).names.add(name);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
for (const input of inputs) {
|
|
132
|
+
let current = input;
|
|
133
|
+
let info;
|
|
134
|
+
while (true) {
|
|
135
|
+
try {
|
|
136
|
+
info = statSync(current);
|
|
137
|
+
break;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (error.code !== 'ENOENT' && error.code !== 'ENOTDIR') {
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
if (current === root) {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
current = dirname(current);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (current === input && info.isDirectory()) {
|
|
149
|
+
add(input, true);
|
|
150
|
+
if (input !== root) {
|
|
151
|
+
add(dirname(input), false, basename(input));
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
const directory = current === input ? dirname(input) : current;
|
|
155
|
+
const next = relative(directory, input).split(sep)[0];
|
|
156
|
+
add(directory, false, next);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
for (const [key, config] of desired) {
|
|
160
|
+
const existing = watchers.get(key);
|
|
161
|
+
if (existing) {
|
|
162
|
+
existing.names = config.names;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const watcher = watch(config.directory, { recursive: config.recursive }, (_event, filename) => {
|
|
166
|
+
if (!filename || stopped) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const name = String(filename);
|
|
170
|
+
if (!config.recursive && !watcher.names.has(name)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const path = resolve(config.directory, name);
|
|
174
|
+
onEvent(path);
|
|
175
|
+
try {
|
|
176
|
+
refresh();
|
|
177
|
+
} catch (error) {
|
|
178
|
+
report(error);
|
|
179
|
+
void stop();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
watcher.names = config.names;
|
|
183
|
+
watcher.on('error', (error) => {
|
|
184
|
+
report(error);
|
|
185
|
+
void stop();
|
|
186
|
+
});
|
|
187
|
+
watchers.set(key, watcher);
|
|
188
|
+
}
|
|
189
|
+
for (const [key, watcher] of watchers) {
|
|
190
|
+
if (!desired.has(key)) {
|
|
191
|
+
watcher.close();
|
|
192
|
+
watchers.delete(key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
refresh();
|
|
198
|
+
} catch (error) {
|
|
199
|
+
for (const watcher of watchers.values()) {
|
|
200
|
+
watcher.close();
|
|
201
|
+
}
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (initial) {
|
|
206
|
+
pending = true;
|
|
207
|
+
run();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return { stop };
|
|
211
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { dirname, isAbsolute, resolve } from 'node:path';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
import { execFile } from 'node:child_process';
|
|
6
|
+
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
const externals = {
|
|
9
|
+
moment: ['moment', 'moment', 'commonjs'],
|
|
10
|
+
jquery: ['jQuery', 'jquery', 'commonjs'],
|
|
11
|
+
lodash: ['lodash', 'lodash', 'commonjs'],
|
|
12
|
+
'lodash-es': ['lodash', 'lodash', 'esm'],
|
|
13
|
+
'@babel/runtime/regenerator': ['regeneratorRuntime', 'regenerator-runtime', 'commonjs'],
|
|
14
|
+
react: ['React', 'react', 'react'],
|
|
15
|
+
'react-dom': ['ReactDOM', 'react-dom', 'react'],
|
|
16
|
+
'react-dom/client': ['ReactDOM', 'react-dom', 'react'],
|
|
17
|
+
'react/jsx-runtime': ['ReactJSXRuntime', 'react-jsx-runtime', 'react'],
|
|
18
|
+
'react/jsx-dev-runtime': ['ReactJSXRuntime', 'react-jsx-runtime', 'react'],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const bundledWordPressPackages = new Set(['admin-ui', 'dataviews', 'dataviews/wp', 'fields', 'global-styles-engine', 'global-styles-ui', 'grid', 'icons', 'interface', 'kebab-case', 'react-inert-value', 'style-runtime', 'sync', 'ui', 'undo-manager', 'views']);
|
|
22
|
+
|
|
23
|
+
function externalFor(request) {
|
|
24
|
+
if (Object.hasOwn(externals, request)) {
|
|
25
|
+
return externals[request];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (/^(?:react|react-dom)\//.test(request) || request.includes('react-refresh/runtime')) {
|
|
29
|
+
throw new Error(`Unsupported React external: ${request}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!request.startsWith('@wordpress/')) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const name = request.slice('@wordpress/'.length);
|
|
37
|
+
|
|
38
|
+
if (bundledWordPressPackages.has(name)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)) {
|
|
43
|
+
throw new Error(`Unsupported WordPress external: ${request}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return [['wp', name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())], `wp-${name}`, 'wordpress'];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function coreExports(root, request) {
|
|
50
|
+
if (!root) {
|
|
51
|
+
throw new Error(`WordPress root is required to discover exports for ${request}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const name = request.slice('@wordpress/'.length);
|
|
55
|
+
const script = await readFile(resolve(root, 'wp-includes/js/dist', `${name}.js`), 'utf8');
|
|
56
|
+
const matches = [...script.matchAll(/__export\(index_exports, \{([\s\S]*?)\n\s*\}\);/g)];
|
|
57
|
+
|
|
58
|
+
if (matches.length !== 1 || !script.includes('return __toCommonJS(index_exports);')) {
|
|
59
|
+
throw new Error(`Cannot identify WordPress core exports for ${request}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const names = matches[0][1]
|
|
63
|
+
.trim()
|
|
64
|
+
.split(/,\s*\n/)
|
|
65
|
+
.map((entry) => entry.trim().match(/^([A-Za-z_$][\w$]*):\s*\(\)\s*=>\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/)?.[1]);
|
|
66
|
+
|
|
67
|
+
if (!names.length || names.some((name) => !name) || new Set(names).size !== names.length) {
|
|
68
|
+
throw new Error(`Cannot parse WordPress core exports for ${request}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return names;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Create a fresh adapter for each Bun build. Paths must be absolute. */
|
|
75
|
+
export function wordpressPlugin({ projectRoot = process.cwd(), wpRoot } = {}) {
|
|
76
|
+
if (!isAbsolute(projectRoot) || (wpRoot !== undefined && !isAbsolute(wpRoot))) {
|
|
77
|
+
throw new Error('projectRoot and wpRoot must be absolute paths');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const projectRequire = createRequire(resolve(projectRoot, 'package.json'));
|
|
81
|
+
const dependencies = new Set();
|
|
82
|
+
const modules = new Map();
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
dependencies,
|
|
86
|
+
plugin: {
|
|
87
|
+
name: 'wordpress-globals',
|
|
88
|
+
setup(build) {
|
|
89
|
+
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
90
|
+
const request = args.path;
|
|
91
|
+
const external = externalFor(request);
|
|
92
|
+
|
|
93
|
+
if (!external) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const [global, handle, kind] = external;
|
|
98
|
+
const require = isAbsolute(args.importer) ? createRequire(args.importer) : projectRequire;
|
|
99
|
+
let metadata;
|
|
100
|
+
let entry;
|
|
101
|
+
let fallback = false;
|
|
102
|
+
|
|
103
|
+
if (kind === 'wordpress') {
|
|
104
|
+
try {
|
|
105
|
+
metadata = require(`${request}/package.json`);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error.code !== 'MODULE_NOT_FOUND' || !['@wordpress/plugins', '@wordpress/core-data'].includes(request)) {
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fallback = true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!fallback && metadata.wpScript !== true) {
|
|
115
|
+
throw new Error(`Missing wpScript metadata: ${request}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (kind !== 'commonjs' && kind !== 'wordpress') {
|
|
120
|
+
const importer = isAbsolute(args.importer) ? args.importer : resolve(projectRoot, '__wordpress_plugin__.mjs');
|
|
121
|
+
|
|
122
|
+
entry = kind === 'react' ? require.resolve(request) : Bun.resolveSync(request, dirname(importer));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const key = JSON.stringify([request, entry || '', fallback]);
|
|
126
|
+
|
|
127
|
+
if (!modules.has(key)) {
|
|
128
|
+
let names = [];
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
if (kind === 'wordpress') {
|
|
132
|
+
names = await coreExports(wpRoot, request);
|
|
133
|
+
} else if (kind === 'react') {
|
|
134
|
+
names = ['default', ...Object.keys(require(entry)).filter((name) => name !== 'default')];
|
|
135
|
+
} else if (kind !== 'commonjs') {
|
|
136
|
+
const script = `const r = await Bun.build({ entrypoints: [process.argv[1]], target: 'browser', format: 'esm', write: false }); if (!r.success || r.outputs.length !== 1) { console.error(r.logs.join('\\n')); process.exit(1); } console.log(JSON.stringify(new Bun.Transpiler().scan(await r.outputs[0].text()).exports));`;
|
|
137
|
+
const result = await execFileAsync(process.execPath, ['-e', script, entry], { cwd: projectRoot, maxBuffer: 1024 * 1024 });
|
|
138
|
+
|
|
139
|
+
names = JSON.parse(result.stdout);
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw new Error(`Cannot determine exports for ${request}: ${error.message}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const path = Array.isArray(global) ? global : [global];
|
|
146
|
+
const lines = [`const value = ${path.reduce((expression, part) => `${expression}?.[${JSON.stringify(part)}]`, 'globalThis')};`, `if (value == null) throw new Error(${JSON.stringify(`Missing WordPress global for ${request}`)});`];
|
|
147
|
+
|
|
148
|
+
if (request === 'react/jsx-dev-runtime') {
|
|
149
|
+
lines.push("if (typeof value.jsx !== 'function') throw new Error('Missing ReactJSXRuntime.jsx');", 'module.exports = { Fragment: value.Fragment, jsxDEV: (type, props, key) => value.jsx(type, props, key) };');
|
|
150
|
+
} else if (kind === 'commonjs' || kind === 'react') {
|
|
151
|
+
lines.push('module.exports = value;');
|
|
152
|
+
} else {
|
|
153
|
+
for (const [index, name] of names.entries()) {
|
|
154
|
+
if (name === 'default') {
|
|
155
|
+
lines.push(kind === 'esm' || metadata?.wpScriptDefaultExport === true ? 'export default value;' : 'export default value.default;');
|
|
156
|
+
} else {
|
|
157
|
+
lines.push(`const export${index} = value[${JSON.stringify(name)}];`, `export { export${index} as ${name} };`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
modules.set(key, lines.join('\n'));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
dependencies.add(handle);
|
|
166
|
+
|
|
167
|
+
return { path: key, namespace: 'wordpress-global' };
|
|
168
|
+
});
|
|
169
|
+
build.onLoad({ filter: /.*/, namespace: 'wordpress-global' }, (args) => ({ contents: modules.get(args.path), loader: 'js' }));
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { getDefaultSelectors } from 'eslint-plugin-better-tailwindcss/defaults';
|
|
3
|
+
import { loadTwIgnore, TW_IGNORE_FILE } from './tw-ignore.mjs';
|
|
4
|
+
|
|
5
|
+
// Resolved here, not by name, so projects that depend only on this package
|
|
6
|
+
// load the plugin under isolated linkers (pnpm, bun --linker isolated).
|
|
7
|
+
const tailwindPlugin = fileURLToPath(import.meta.resolve('eslint-plugin-better-tailwindcss'));
|
|
8
|
+
|
|
9
|
+
const defaultIgnorePatterns = ['public/**', 'node_modules/**', 'vendor/**', 'vendor-prefixed/**', 'eightshift/**', 'coverage/**'];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Eightshift oxlint config with Tailwind class checks.
|
|
13
|
+
*
|
|
14
|
+
* Usage in a project's `oxlint.config.ts`:
|
|
15
|
+
*
|
|
16
|
+
* import { defineConfig } from 'oxlint';
|
|
17
|
+
* import { createOxlintConfig } from '@eightshift/frontend-libs-tailwind/linters/oxlint.config.mjs';
|
|
18
|
+
*
|
|
19
|
+
* export default defineConfig(createOxlintConfig({ entryPoint: 'tailwind.css' }));
|
|
20
|
+
*
|
|
21
|
+
* Unknown-class exceptions live in `tw-ignore.json` (shared with `tw-sweep`).
|
|
22
|
+
*
|
|
23
|
+
* @param {object} options
|
|
24
|
+
* @param {string} options.entryPoint Tailwind CSS entry, relative to the config.
|
|
25
|
+
* @param {string} [options.ignoreFile] Unknown-class ignore list (JSON array of regex strings).
|
|
26
|
+
* @param {string[]} [options.ignorePatterns] Extra paths to skip.
|
|
27
|
+
* @param {object} [options.rules] Rule overrides, merged last.
|
|
28
|
+
* @returns {object} oxlint config.
|
|
29
|
+
*/
|
|
30
|
+
export function createOxlintConfig({ entryPoint, ignoreFile = TW_IGNORE_FILE, ignorePatterns = [], rules = {} }) {
|
|
31
|
+
if (!entryPoint) {
|
|
32
|
+
throw new Error('createOxlintConfig: `entryPoint` is required');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
plugins: ['eslint', 'unicorn', 'oxc', 'react'],
|
|
37
|
+
jsPlugins: [tailwindPlugin],
|
|
38
|
+
settings: {
|
|
39
|
+
'better-tailwindcss': {
|
|
40
|
+
entryPoint,
|
|
41
|
+
selectors: [...getDefaultSelectors(), { kind: 'attribute', name: '^.+ClassName$', match: [{ type: 'strings' }] }],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
ignorePatterns: [...defaultIgnorePatterns, ...ignorePatterns],
|
|
45
|
+
rules: {
|
|
46
|
+
'no-array-constructor': 'error',
|
|
47
|
+
'no-constant-binary-expression': 'error',
|
|
48
|
+
'no-implicit-coercion': 'error',
|
|
49
|
+
'no-nested-ternary': 'error',
|
|
50
|
+
'no-unused-expressions': 'error',
|
|
51
|
+
'no-void': 'error',
|
|
52
|
+
'no-param-reassign': 'warn',
|
|
53
|
+
'react/rules-of-hooks': 'error',
|
|
54
|
+
'react/exhaustive-deps': 'error',
|
|
55
|
+
'react/react-in-jsx-scope': 'off',
|
|
56
|
+
'react/self-closing-comp': 'warn',
|
|
57
|
+
'better-tailwindcss/no-duplicate-classes': 'error',
|
|
58
|
+
'better-tailwindcss/no-deprecated-classes': 'error',
|
|
59
|
+
'better-tailwindcss/enforce-canonical-classes': 'error',
|
|
60
|
+
'better-tailwindcss/no-unknown-classes': ['error', { ignore: loadTwIgnore(ignoreFile) }],
|
|
61
|
+
'better-tailwindcss/no-conflicting-classes': 'error',
|
|
62
|
+
...rules,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const TW_IGNORE_FILE = 'tw-ignore.json';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Loads the project's Tailwind unknown-class ignore list: a JSON array of regex
|
|
8
|
+
* source strings, shared by oxlint (`no-unknown-classes`) and `tw-sweep`.
|
|
9
|
+
*
|
|
10
|
+
* A missing file means "ignore nothing". An invalid file or pattern throws,
|
|
11
|
+
* naming the offending entry, so a typo never silently disables the check.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} [file] Path to the JSON file, relative to `cwd`.
|
|
14
|
+
* @param {string} [cwd] Base directory.
|
|
15
|
+
* @returns {string[]} Regex source strings.
|
|
16
|
+
*/
|
|
17
|
+
export function loadTwIgnore(file = TW_IGNORE_FILE, cwd = process.cwd()) {
|
|
18
|
+
const path = resolve(cwd, file);
|
|
19
|
+
|
|
20
|
+
if (!existsSync(path)) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let patterns;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
patterns = JSON.parse(readFileSync(path, 'utf8'));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
throw new Error(`${path}: invalid JSON (${error.message})`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!Array.isArray(patterns) || patterns.some((pattern) => typeof pattern !== 'string')) {
|
|
33
|
+
throw new Error(`${path}: expected a JSON array of regex strings`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (const pattern of patterns) {
|
|
37
|
+
try {
|
|
38
|
+
new RegExp(pattern);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error(`${path}: invalid pattern ${JSON.stringify(pattern)} (${error.message})`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return patterns;
|
|
45
|
+
}
|