@stylexjs/unplugin 0.17.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.
- package/README.md +66 -0
- package/lib/consts.js +144 -0
- package/lib/dev-inject-middleware.js +18 -0
- package/lib/es/consts.mjs +138 -0
- package/lib/es/dev-inject-middleware.mjs +11 -0
- package/lib/es/index.mjs +484 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +490 -0
- package/package.json +48 -0
package/lib/es/index.mjs
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { createUnplugin } from 'unplugin';
|
|
2
|
+
import { transformAsync } from '@babel/core';
|
|
3
|
+
import stylexBabelPlugin from '@stylexjs/babel-plugin';
|
|
4
|
+
import flowSyntaxPlugin from '@babel/plugin-syntax-flow';
|
|
5
|
+
import jsxSyntaxPlugin from '@babel/plugin-syntax-jsx';
|
|
6
|
+
import typescriptSyntaxPlugin from '@babel/plugin-syntax-typescript';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import fsp from 'node:fs/promises';
|
|
10
|
+
import { transform as lightningTransform } from 'lightningcss';
|
|
11
|
+
import browserslist from 'browserslist';
|
|
12
|
+
import { browserslistToTargets } from 'lightningcss';
|
|
13
|
+
import { devInjectMiddleware } from "./dev-inject-middleware.mjs";
|
|
14
|
+
import { DEV_CSS_PATH, VIRTUAL_STYLEX_RUNTIME_SCRIPT, VIRTUAL_STYLEX_CSS_ONLY_SCRIPT } from "./consts.mjs";
|
|
15
|
+
function pickCssAssetFromRollupBundle(bundle, choose) {
|
|
16
|
+
const assets = Object.values(bundle).filter(a => a && a.type === 'asset' && typeof a.fileName === 'string' && a.fileName.endsWith('.css'));
|
|
17
|
+
if (assets.length === 0) return null;
|
|
18
|
+
if (typeof choose === 'function') {
|
|
19
|
+
const chosen = assets.find(a => choose(a.fileName));
|
|
20
|
+
if (chosen) return chosen;
|
|
21
|
+
}
|
|
22
|
+
const best = assets.find(a => /(^|\/)index\.css$/.test(a.fileName)) || assets.find(a => /(^|\/)style\.css$/.test(a.fileName));
|
|
23
|
+
return best || assets[0];
|
|
24
|
+
}
|
|
25
|
+
function processCollectedRulesToCSS(rules, options) {
|
|
26
|
+
if (!rules || rules.length === 0) return '';
|
|
27
|
+
const collectedCSS = stylexBabelPlugin.processStylexRules(rules, {
|
|
28
|
+
useLayers: !!options.useCSSLayers,
|
|
29
|
+
enableLTRRTLComments: options?.enableLTRRTLComments
|
|
30
|
+
});
|
|
31
|
+
const {
|
|
32
|
+
code
|
|
33
|
+
} = lightningTransform({
|
|
34
|
+
targets: browserslistToTargets(browserslist('>= 1%')),
|
|
35
|
+
...options.lightningcssOptions,
|
|
36
|
+
filename: 'stylex.css',
|
|
37
|
+
code: Buffer.from(collectedCSS)
|
|
38
|
+
});
|
|
39
|
+
return code.toString();
|
|
40
|
+
}
|
|
41
|
+
const unpluginInstance = createUnplugin((userOptions = {}) => {
|
|
42
|
+
const {
|
|
43
|
+
dev = process.env.NODE_ENV === 'development' || process.env.BABEL_ENV === 'development',
|
|
44
|
+
unstable_moduleResolution = {
|
|
45
|
+
type: 'commonJS',
|
|
46
|
+
rootDir: process.cwd()
|
|
47
|
+
},
|
|
48
|
+
babelConfig: {
|
|
49
|
+
plugins = [],
|
|
50
|
+
presets = []
|
|
51
|
+
} = {},
|
|
52
|
+
importSources = ['stylex', '@stylexjs/stylex'],
|
|
53
|
+
useCSSLayers = false,
|
|
54
|
+
lightningcssOptions,
|
|
55
|
+
cssInjectionTarget,
|
|
56
|
+
devPersistToDisk = false,
|
|
57
|
+
devMode = 'full',
|
|
58
|
+
...stylexOptions
|
|
59
|
+
} = userOptions;
|
|
60
|
+
const stylexRulesById = new Map();
|
|
61
|
+
function getSharedStore() {
|
|
62
|
+
try {
|
|
63
|
+
const g = globalThis;
|
|
64
|
+
if (!g.__stylex_unplugin_store) {
|
|
65
|
+
g.__stylex_unplugin_store = {
|
|
66
|
+
rulesById: new Map(),
|
|
67
|
+
version: 0
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return g.__stylex_unplugin_store;
|
|
71
|
+
} catch {
|
|
72
|
+
return {
|
|
73
|
+
rulesById: stylexRulesById,
|
|
74
|
+
version: 0
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let viteServer = null;
|
|
79
|
+
let viteOutDir = null;
|
|
80
|
+
function findNearestNodeModules(startDir) {
|
|
81
|
+
let dir = startDir;
|
|
82
|
+
for (;;) {
|
|
83
|
+
const candidate = path.join(dir, 'node_modules');
|
|
84
|
+
if (fs.existsSync(candidate)) {
|
|
85
|
+
const stat = fs.statSync(candidate);
|
|
86
|
+
if (stat.isDirectory()) return candidate;
|
|
87
|
+
}
|
|
88
|
+
const parent = path.dirname(dir);
|
|
89
|
+
if (parent === dir) break;
|
|
90
|
+
dir = parent;
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
const NEAREST_NODE_MODULES = findNearestNodeModules(process.cwd());
|
|
95
|
+
const DISK_RULES_DIR = NEAREST_NODE_MODULES ? path.join(NEAREST_NODE_MODULES, '.stylex') : path.join(process.cwd(), 'node_modules', '.stylex');
|
|
96
|
+
const DISK_RULES_PATH = path.join(DISK_RULES_DIR, 'rules.json');
|
|
97
|
+
async function runBabelTransform(inputCode, id, callerName) {
|
|
98
|
+
const result = await transformAsync(inputCode, {
|
|
99
|
+
babelrc: false,
|
|
100
|
+
filename: id,
|
|
101
|
+
presets,
|
|
102
|
+
plugins: [...plugins, /\.jsx?/.test(path.extname(id)) ? flowSyntaxPlugin : [typescriptSyntaxPlugin, {
|
|
103
|
+
isTSX: true
|
|
104
|
+
}], jsxSyntaxPlugin, stylexBabelPlugin.withOptions({
|
|
105
|
+
...stylexOptions,
|
|
106
|
+
dev,
|
|
107
|
+
unstable_moduleResolution
|
|
108
|
+
})],
|
|
109
|
+
caller: {
|
|
110
|
+
name: callerName,
|
|
111
|
+
supportsStaticESM: true,
|
|
112
|
+
supportsDynamicImport: true,
|
|
113
|
+
supportsTopLevelAwait: !inputCode.includes('require('),
|
|
114
|
+
supportsExportNamespaceFrom: true
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
if (!result || result.code == null) {
|
|
118
|
+
return {
|
|
119
|
+
code: inputCode,
|
|
120
|
+
map: null,
|
|
121
|
+
metadata: {}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
function escapeReg(src) {
|
|
127
|
+
return src.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
128
|
+
}
|
|
129
|
+
function containsStylexImport(code, source) {
|
|
130
|
+
const s = escapeReg(typeof source === 'string' ? source : source.from);
|
|
131
|
+
const re = new RegExp(`(?:from\\s*['"]${s}['"]|import\\s*\\(\\s*['"]${s}['"]\\s*\\)|require\\s*\\(\\s*['"]${s}['"]\\s*\\)|^\\s*import\\s*['"]${s}['"])`, 'm');
|
|
132
|
+
return re.test(code);
|
|
133
|
+
}
|
|
134
|
+
function shouldHandle(code) {
|
|
135
|
+
if (!code) return false;
|
|
136
|
+
return importSources.some(src => containsStylexImport(code, src));
|
|
137
|
+
}
|
|
138
|
+
function resetState() {
|
|
139
|
+
stylexRulesById.clear();
|
|
140
|
+
if (devPersistToDisk) {
|
|
141
|
+
try {
|
|
142
|
+
fs.rmSync(DISK_RULES_PATH, {
|
|
143
|
+
force: true
|
|
144
|
+
});
|
|
145
|
+
} catch {}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function collectCss() {
|
|
149
|
+
const merged = new Map();
|
|
150
|
+
if (devPersistToDisk) {
|
|
151
|
+
try {
|
|
152
|
+
if (fs.existsSync(DISK_RULES_PATH)) {
|
|
153
|
+
const json = JSON.parse(fs.readFileSync(DISK_RULES_PATH, 'utf8'));
|
|
154
|
+
for (const [k, v] of Object.entries(json)) merged.set(k, v);
|
|
155
|
+
}
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const shared = getSharedStore().rulesById;
|
|
160
|
+
for (const [k, v] of shared.entries()) merged.set(k, v);
|
|
161
|
+
} catch {}
|
|
162
|
+
for (const [k, v] of stylexRulesById.entries()) merged.set(k, v);
|
|
163
|
+
const allRules = Array.from(merged.values()).flat();
|
|
164
|
+
return processCollectedRulesToCSS(allRules, {
|
|
165
|
+
useCSSLayers,
|
|
166
|
+
lightningcssOptions,
|
|
167
|
+
enableLTRRTLComments: stylexOptions?.enableLTRRTLComments
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async function persistRulesToDisk(id, rules) {
|
|
171
|
+
if (!devPersistToDisk) return;
|
|
172
|
+
try {
|
|
173
|
+
let current = {};
|
|
174
|
+
try {
|
|
175
|
+
const txt = await fsp.readFile(DISK_RULES_PATH, 'utf8');
|
|
176
|
+
current = JSON.parse(txt);
|
|
177
|
+
} catch {}
|
|
178
|
+
if (rules && Array.isArray(rules) && rules.length > 0) {
|
|
179
|
+
current[id] = rules;
|
|
180
|
+
} else if (current[id]) {
|
|
181
|
+
delete current[id];
|
|
182
|
+
}
|
|
183
|
+
await fsp.writeFile(DISK_RULES_PATH, JSON.stringify(current), 'utf8');
|
|
184
|
+
} catch {}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
name: '@stylexjs/unplugin',
|
|
188
|
+
apply: (config, env) => {
|
|
189
|
+
try {
|
|
190
|
+
const command = env?.command || (typeof config === 'string' ? undefined : undefined);
|
|
191
|
+
if (devMode === 'off' && command === 'serve') return false;
|
|
192
|
+
} catch {}
|
|
193
|
+
return true;
|
|
194
|
+
},
|
|
195
|
+
enforce: 'pre',
|
|
196
|
+
buildStart() {
|
|
197
|
+
resetState();
|
|
198
|
+
},
|
|
199
|
+
buildEnd() {},
|
|
200
|
+
async transform(code, id) {
|
|
201
|
+
const JS_LIKE_RE = /\.[cm]?[jt]sx?(\?|$)/;
|
|
202
|
+
if (!JS_LIKE_RE.test(id)) return null;
|
|
203
|
+
if (!shouldHandle(code)) return null;
|
|
204
|
+
const result = await runBabelTransform(code, id, '@stylexjs/unplugin');
|
|
205
|
+
const {
|
|
206
|
+
metadata
|
|
207
|
+
} = result;
|
|
208
|
+
if (!stylexOptions.runtimeInjection) {
|
|
209
|
+
const hasRules = metadata && Array.isArray(metadata.stylex) && metadata.stylex.length > 0;
|
|
210
|
+
const shared = getSharedStore();
|
|
211
|
+
if (hasRules) {
|
|
212
|
+
stylexRulesById.set(id, metadata.stylex);
|
|
213
|
+
shared.rulesById.set(id, metadata.stylex);
|
|
214
|
+
shared.version++;
|
|
215
|
+
await persistRulesToDisk(id, metadata.stylex);
|
|
216
|
+
} else {
|
|
217
|
+
stylexRulesById.delete(id);
|
|
218
|
+
if (shared.rulesById.has(id)) {
|
|
219
|
+
shared.rulesById.delete(id);
|
|
220
|
+
shared.version++;
|
|
221
|
+
}
|
|
222
|
+
await persistRulesToDisk(id, []);
|
|
223
|
+
}
|
|
224
|
+
if (viteServer) {
|
|
225
|
+
try {
|
|
226
|
+
viteServer.ws.send({
|
|
227
|
+
type: 'custom',
|
|
228
|
+
event: 'stylex:css-update'
|
|
229
|
+
});
|
|
230
|
+
} catch {}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const ctx = this;
|
|
234
|
+
if (ctx && ctx.meta && ctx.meta.watchMode && typeof ctx.parse === 'function') {
|
|
235
|
+
try {
|
|
236
|
+
const ast = ctx.parse(result.code);
|
|
237
|
+
for (const stmt of ast.body) {
|
|
238
|
+
if (stmt.type === 'ImportDeclaration') {
|
|
239
|
+
const resolved = await ctx.resolve(stmt.source.value, id);
|
|
240
|
+
if (resolved && !resolved.external) {
|
|
241
|
+
const loaded = await ctx.load(resolved);
|
|
242
|
+
if (loaded && loaded.meta && 'stylex' in loaded.meta) {
|
|
243
|
+
stylexRulesById.set(resolved.id, loaded.meta.stylex);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch {}
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
code: result.code,
|
|
252
|
+
map: result.map
|
|
253
|
+
};
|
|
254
|
+
},
|
|
255
|
+
shouldTransformCachedModule({
|
|
256
|
+
id,
|
|
257
|
+
meta
|
|
258
|
+
}) {
|
|
259
|
+
if (meta && 'stylex' in meta) {
|
|
260
|
+
stylexRulesById.set(id, meta.stylex);
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
},
|
|
264
|
+
generateBundle(_opts, bundle) {
|
|
265
|
+
const css = collectCss();
|
|
266
|
+
if (!css) return;
|
|
267
|
+
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
|
|
268
|
+
if (target) {
|
|
269
|
+
const current = typeof target.source === 'string' ? target.source : target.source?.toString() || '';
|
|
270
|
+
target.source = current ? current + '\n' + css : css;
|
|
271
|
+
} else {}
|
|
272
|
+
},
|
|
273
|
+
vite: devMode === 'off' ? undefined : {
|
|
274
|
+
configResolved(config) {
|
|
275
|
+
try {
|
|
276
|
+
viteOutDir = config.build?.outDir || viteOutDir;
|
|
277
|
+
} catch {}
|
|
278
|
+
},
|
|
279
|
+
configureServer(server) {
|
|
280
|
+
viteServer = server;
|
|
281
|
+
if (devMode === 'full') {
|
|
282
|
+
server.middlewares.use(devInjectMiddleware);
|
|
283
|
+
}
|
|
284
|
+
server.middlewares.use((req, res, next) => {
|
|
285
|
+
if (!req.url) return next();
|
|
286
|
+
if (req.url.startsWith(DEV_CSS_PATH)) {
|
|
287
|
+
res.statusCode = 200;
|
|
288
|
+
res.setHeader('Content-Type', 'text/css');
|
|
289
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
290
|
+
const css = collectCss();
|
|
291
|
+
res.end(css || '');
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
next();
|
|
295
|
+
});
|
|
296
|
+
const shared = getSharedStore();
|
|
297
|
+
let lastVersion = shared.version;
|
|
298
|
+
const interval = setInterval(() => {
|
|
299
|
+
const curr = shared.version;
|
|
300
|
+
if (curr !== lastVersion) {
|
|
301
|
+
lastVersion = curr;
|
|
302
|
+
try {
|
|
303
|
+
server.ws.send({
|
|
304
|
+
type: 'custom',
|
|
305
|
+
event: 'stylex:css-update'
|
|
306
|
+
});
|
|
307
|
+
} catch {}
|
|
308
|
+
}
|
|
309
|
+
}, 150);
|
|
310
|
+
server.httpServer?.once('close', () => clearInterval(interval));
|
|
311
|
+
},
|
|
312
|
+
resolveId(id) {
|
|
313
|
+
if (devMode === 'full' && id === 'virtual:stylex:runtime') return id;
|
|
314
|
+
if (devMode === 'css-only' && id === 'virtual:stylex:css-only') return id;
|
|
315
|
+
return null;
|
|
316
|
+
},
|
|
317
|
+
load(id) {
|
|
318
|
+
if (devMode === 'full' && id === 'virtual:stylex:runtime') {
|
|
319
|
+
return VIRTUAL_STYLEX_RUNTIME_SCRIPT;
|
|
320
|
+
}
|
|
321
|
+
if (devMode === 'css-only' && id === 'virtual:stylex:css-only') {
|
|
322
|
+
return VIRTUAL_STYLEX_CSS_ONLY_SCRIPT;
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
},
|
|
326
|
+
transformIndexHtml() {
|
|
327
|
+
if (devMode !== 'full') return null;
|
|
328
|
+
if (!viteServer) return null;
|
|
329
|
+
return [{
|
|
330
|
+
tag: 'script',
|
|
331
|
+
attrs: {
|
|
332
|
+
type: 'module',
|
|
333
|
+
src: '/@id/virtual:stylex:runtime'
|
|
334
|
+
},
|
|
335
|
+
injectTo: 'head'
|
|
336
|
+
}, {
|
|
337
|
+
tag: 'link',
|
|
338
|
+
attrs: {
|
|
339
|
+
rel: 'stylesheet',
|
|
340
|
+
href: DEV_CSS_PATH
|
|
341
|
+
},
|
|
342
|
+
injectTo: 'head'
|
|
343
|
+
}];
|
|
344
|
+
},
|
|
345
|
+
handleHotUpdate(ctx) {
|
|
346
|
+
const cssMod = ctx.server.moduleGraph.getModuleById('virtual:stylex:css-module');
|
|
347
|
+
if (cssMod) {
|
|
348
|
+
ctx.server.moduleGraph.invalidateModule(cssMod);
|
|
349
|
+
}
|
|
350
|
+
try {
|
|
351
|
+
ctx.server.ws.send({
|
|
352
|
+
type: 'custom',
|
|
353
|
+
event: 'stylex:css-update'
|
|
354
|
+
});
|
|
355
|
+
} catch {}
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
webpack(compiler) {
|
|
359
|
+
const PLUGIN_NAME = '@stylexjs/unplugin';
|
|
360
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
|
|
361
|
+
resetState();
|
|
362
|
+
const wp = compiler.webpack || compiler.rspack || undefined;
|
|
363
|
+
const stage = wp?.Compilation?.PROCESS_ASSETS_STAGE_SUMMARIZE;
|
|
364
|
+
const tapOptions = stage != null ? {
|
|
365
|
+
name: PLUGIN_NAME,
|
|
366
|
+
stage
|
|
367
|
+
} : PLUGIN_NAME;
|
|
368
|
+
const toRawSource = content => {
|
|
369
|
+
const RawSource = wp?.sources?.RawSource;
|
|
370
|
+
return RawSource ? new RawSource(content) : {
|
|
371
|
+
source: () => content,
|
|
372
|
+
size: () => Buffer.byteLength(content)
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
compilation.hooks.processAssets.tap(tapOptions, assets => {
|
|
376
|
+
const css = collectCss();
|
|
377
|
+
if (!css) return;
|
|
378
|
+
const cssAssets = Object.keys(assets).filter(f => f.endsWith('.css'));
|
|
379
|
+
if (cssAssets.length === 0) {
|
|
380
|
+
compilation.warnings.push(new Error('[stylex] No CSS asset found to inject into. Skipping.'));
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const pickName = typeof cssInjectionTarget === 'function' && cssAssets.find(f => cssInjectionTarget(f)) || cssAssets.find(f => /(^|\/)index\.css$/.test(f)) || cssAssets.find(f => /(^|\/)style\.css$/.test(f)) || cssAssets[0];
|
|
384
|
+
const asset = compilation.getAsset(pickName);
|
|
385
|
+
if (!asset) return;
|
|
386
|
+
const existing = asset.source.source().toString();
|
|
387
|
+
const next = existing ? existing + '\n' + css : css;
|
|
388
|
+
compilation.updateAsset(pickName, toRawSource(next));
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
},
|
|
392
|
+
rspack(compiler) {
|
|
393
|
+
this.webpack?.(compiler);
|
|
394
|
+
},
|
|
395
|
+
esbuild: {
|
|
396
|
+
name: '@stylexjs/unplugin',
|
|
397
|
+
setup(build) {
|
|
398
|
+
build.onEnd(async result => {
|
|
399
|
+
try {
|
|
400
|
+
const css = collectCss();
|
|
401
|
+
if (!css) return;
|
|
402
|
+
const initial = build.initialOptions;
|
|
403
|
+
const outDir = initial.outdir || (initial.outfile ? path.dirname(initial.outfile) : null);
|
|
404
|
+
if (!outDir) return;
|
|
405
|
+
let outfile = null;
|
|
406
|
+
const meta = result && result.metafile;
|
|
407
|
+
if (meta && meta.outputs) {
|
|
408
|
+
const outputs = Object.keys(meta.outputs);
|
|
409
|
+
const cssOutputs = outputs.filter(f => f.endsWith('.css'));
|
|
410
|
+
const pick = cssOutputs.find(f => /(^|\/)index\.css$/.test(f)) || cssOutputs.find(f => /(^|\/)style\.css$/.test(f)) || cssOutputs[0];
|
|
411
|
+
if (pick) outfile = path.isAbsolute(pick) ? pick : path.join(process.cwd(), pick);
|
|
412
|
+
} else {
|
|
413
|
+
try {
|
|
414
|
+
const files = fs.readdirSync(outDir).filter(f => f.endsWith('.css'));
|
|
415
|
+
const pick = files.find(f => /(^|\/)index\.css$/.test(f)) || files.find(f => /(^|\/)style\.css$/.test(f)) || files[0];
|
|
416
|
+
if (pick) outfile = path.join(outDir, pick);
|
|
417
|
+
} catch {}
|
|
418
|
+
}
|
|
419
|
+
if (!outfile) {
|
|
420
|
+
const fallback = path.join(outDir, 'stylex.css');
|
|
421
|
+
await fsp.mkdir(path.dirname(fallback), {
|
|
422
|
+
recursive: true
|
|
423
|
+
});
|
|
424
|
+
await fsp.writeFile(fallback, css, 'utf8');
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
try {
|
|
428
|
+
const current = fs.readFileSync(outfile, 'utf8');
|
|
429
|
+
if (!current.includes(css)) {
|
|
430
|
+
await fsp.writeFile(outfile, current ? current + '\n' + css : css, 'utf8');
|
|
431
|
+
}
|
|
432
|
+
} catch {}
|
|
433
|
+
} catch {}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
async writeBundle(options, bundle) {
|
|
438
|
+
try {
|
|
439
|
+
const css = collectCss();
|
|
440
|
+
if (!css) return;
|
|
441
|
+
const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
|
|
442
|
+
const outDir = options?.dir || (options?.file ? path.dirname(options.file) : viteOutDir);
|
|
443
|
+
if (!outDir) return;
|
|
444
|
+
try {
|
|
445
|
+
await fsp.mkdir(outDir, {
|
|
446
|
+
recursive: true
|
|
447
|
+
});
|
|
448
|
+
} catch {}
|
|
449
|
+
let outfile;
|
|
450
|
+
if (!target) {
|
|
451
|
+
try {
|
|
452
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
453
|
+
if (fs.existsSync(assetsDir)) {
|
|
454
|
+
const files = fs.readdirSync(assetsDir).filter(f => f.endsWith('.css'));
|
|
455
|
+
const pick = files.find(f => /(^|\/)index\.css$/.test(f)) || files.find(f => /(^|\/)style\.css$/.test(f)) || files[0];
|
|
456
|
+
if (pick) outfile = path.join(assetsDir, pick);
|
|
457
|
+
}
|
|
458
|
+
} catch {}
|
|
459
|
+
if (!outfile) {
|
|
460
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
461
|
+
try {
|
|
462
|
+
await fsp.mkdir(assetsDir, {
|
|
463
|
+
recursive: true
|
|
464
|
+
});
|
|
465
|
+
} catch {}
|
|
466
|
+
const fallback = path.join(assetsDir, 'stylex.css');
|
|
467
|
+
await fsp.writeFile(fallback, css, 'utf8');
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
outfile = path.join(outDir, target.fileName);
|
|
472
|
+
}
|
|
473
|
+
try {
|
|
474
|
+
const current = fs.readFileSync(outfile, 'utf8');
|
|
475
|
+
if (!current.includes(css)) {
|
|
476
|
+
await fsp.writeFile(outfile, current ? current + '\n' + css : css, 'utf8');
|
|
477
|
+
}
|
|
478
|
+
} catch {}
|
|
479
|
+
} catch {}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
});
|
|
483
|
+
export default unpluginInstance;
|
|
484
|
+
export const unplugin = unpluginInstance;
|
package/lib/index.d.ts
ADDED