unplugin-stylex 0.5.3 → 0.5.5

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # unplugin-stylex
2
2
 
3
+ ## 0.5.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [#119](https://github.com/eryue0220/unplugin-stylex/pull/119) [`9bb6f3c`](https://github.com/eryue0220/unplugin-stylex/commit/9bb6f3c5936fb50e950bb84e7d614f5e9665f817) Thanks [@sfc-gh-alisowski](https://github.com/sfc-gh-alisowski)! - Upgrade stylex
8
+
9
+ ## 0.5.4
10
+
11
+ ### Patch Changes
12
+
13
+ - [#116](https://github.com/eryue0220/unplugin-stylex/pull/116) [`5986904`](https://github.com/eryue0220/unplugin-stylex/commit/5986904215a04fa5774167a5031c7ddc5de000f8) Thanks [@eryue0220](https://github.com/eryue0220)! - upgrade deps and add alias feature
14
+
3
15
  ## 0.5.3
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,434 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/index.ts
9
+ var _path = require('path'); var path2 = _interopRequireWildcard(_path);
10
+ var _unplugin = require('unplugin');
11
+
12
+ // src/core/build.ts
13
+ var _babelplugin = require('@stylexjs/babel-plugin'); var _babelplugin2 = _interopRequireDefault(_babelplugin);
14
+ function buildStylexRules(stylexRules, useCSSLayers) {
15
+ const rules = Object.values(stylexRules).flat();
16
+ if (rules.length === 0) return "";
17
+ return _babelplugin2.default.processStylexRules(rules, useCSSLayers);
18
+ }
19
+
20
+ // src/utils/constants.ts
21
+ var PLUGIN_NAME = "unplugin-stylex";
22
+ var isDevelopment = process.env.NODE_ENV !== "production" || process.env.BABEL_ENV !== "production";
23
+ var EXTENSIONS = [".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"];
24
+
25
+ // src/utils/get-aliases.ts
26
+ var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
27
+
28
+ function makePathAbsolute(value) {
29
+ return path2.default.isAbsolute(value) ? value : path2.default.resolve(process.cwd(), value);
30
+ }
31
+ function isTSProject() {
32
+ const tsConfigPath = path2.default.resolve(process.cwd(), "tsconfig.json");
33
+ return _fs2.default.existsSync(tsConfigPath);
34
+ }
35
+ function getAliasesFromTSConfig() {
36
+ var _a;
37
+ if (!isTSProject()) {
38
+ return void 0;
39
+ }
40
+ try {
41
+ const tsConfigPath = path2.default.resolve(process.cwd(), "tsconfig.json");
42
+ const tsConfigContent = _fs2.default.readFileSync(tsConfigPath, "utf8");
43
+ const tsConfig = JSON.parse(tsConfigContent);
44
+ const paths = ((_a = tsConfig == null ? void 0 : tsConfig.compilerOptions) == null ? void 0 : _a.paths) || {};
45
+ const aliases = Object.entries(paths).reduce(
46
+ (acc, [key, value]) => {
47
+ acc[key] = Array.isArray(value) ? value.map(makePathAbsolute) : makePathAbsolute(value);
48
+ return acc;
49
+ },
50
+ {}
51
+ );
52
+ return Object.keys(aliases).length > 0 ? aliases : void 0;
53
+ } catch (error) {
54
+ return void 0;
55
+ }
56
+ }
57
+ function getAliasesFromViteConfig() {
58
+ var _a;
59
+ const configFiles = EXTENSIONS.map((ext) => `vite.config${ext}`);
60
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
61
+ if (!configPath) {
62
+ return {};
63
+ }
64
+ try {
65
+ const configModule = __require(path2.default.resolve(process.cwd(), configPath));
66
+ const config = typeof configModule === "function" ? configModule() : configModule.default || configModule;
67
+ const aliases = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) || {};
68
+ return Object.entries(aliases).reduce(
69
+ (acc, [key, value]) => {
70
+ acc[key] = makePathAbsolute(value);
71
+ return acc;
72
+ },
73
+ {}
74
+ );
75
+ } catch (error) {
76
+ return {};
77
+ }
78
+ }
79
+ function getAliasesFromWebpackConfig() {
80
+ var _a;
81
+ const configFiles = EXTENSIONS.map((ext) => `webpack.config${ext}`);
82
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
83
+ if (!configPath) {
84
+ return {};
85
+ }
86
+ try {
87
+ const configContent = _fs2.default.readFileSync(path2.default.resolve(process.cwd(), configPath), "utf8");
88
+ const config = JSON.parse(configContent);
89
+ const aliases = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) || {};
90
+ return Object.entries(aliases).reduce(
91
+ (acc, [key, value]) => {
92
+ acc[key] = makePathAbsolute(value);
93
+ return acc;
94
+ },
95
+ {}
96
+ );
97
+ } catch (error) {
98
+ return {};
99
+ }
100
+ }
101
+ function getAliasesFromRollupConfig() {
102
+ var _a, _b;
103
+ const configFiles = EXTENSIONS.map((ext) => `rollup.config${ext}`);
104
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
105
+ if (!configPath) {
106
+ return {};
107
+ }
108
+ try {
109
+ const configModule = __require(path2.default.resolve(process.cwd(), configPath));
110
+ const config = typeof configModule === "function" ? configModule() : configModule.default || configModule;
111
+ const aliasPlugin = (_a = config == null ? void 0 : config.plugins) == null ? void 0 : _a.find((plugin) => {
112
+ if (typeof plugin === "object" && plugin !== null) {
113
+ return plugin.name === "@rollup/plugin-alias";
114
+ }
115
+ return false;
116
+ });
117
+ if (!aliasPlugin) {
118
+ return {};
119
+ }
120
+ const aliases = ((_b = aliasPlugin == null ? void 0 : aliasPlugin.options) == null ? void 0 : _b.entries) || {};
121
+ return Object.entries(aliases).reduce(
122
+ (acc, [key, value]) => {
123
+ acc[key] = makePathAbsolute(value);
124
+ return acc;
125
+ },
126
+ {}
127
+ );
128
+ } catch (error) {
129
+ return {};
130
+ }
131
+ }
132
+ function getAliasesFromRolldownConfig() {
133
+ var _a;
134
+ const configFiles = EXTENSIONS.map((ext) => `vite.config${ext}`);
135
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
136
+ if (!configPath) {
137
+ return {};
138
+ }
139
+ try {
140
+ const configModule = __require(path2.default.resolve(process.cwd(), configPath));
141
+ const config = typeof configModule === "function" ? configModule() : configModule.default || configModule;
142
+ const aliases = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) || {};
143
+ return Object.entries(aliases).reduce(
144
+ (acc, [key, value]) => {
145
+ acc[key] = makePathAbsolute(value);
146
+ return acc;
147
+ },
148
+ {}
149
+ );
150
+ } catch (error) {
151
+ return {};
152
+ }
153
+ }
154
+ function getAliasesFromEsbuildConfig() {
155
+ return {};
156
+ }
157
+ function getAliasesFromRspackConfig() {
158
+ var _a;
159
+ const configFiles = EXTENSIONS.map((ext) => `rspack.config${ext}`);
160
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
161
+ if (!configPath) {
162
+ return {};
163
+ }
164
+ try {
165
+ const configContent = _fs2.default.readFileSync(path2.default.resolve(process.cwd(), configPath), "utf8");
166
+ const config = JSON.parse(configContent);
167
+ const aliases = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) || {};
168
+ return Object.entries(aliases).reduce(
169
+ (acc, [key, value]) => {
170
+ acc[key] = makePathAbsolute(value);
171
+ return acc;
172
+ },
173
+ {}
174
+ );
175
+ } catch (error) {
176
+ return {};
177
+ }
178
+ }
179
+ function getAliasesFromFarmConfig() {
180
+ var _a;
181
+ const configFiles = EXTENSIONS.map((ext) => `farm.config${ext}`);
182
+ const configPath = configFiles.find((file) => _fs2.default.existsSync(path2.default.resolve(process.cwd(), file)));
183
+ if (!configPath) {
184
+ return {};
185
+ }
186
+ try {
187
+ const configModule = __require(path2.default.resolve(process.cwd(), configPath));
188
+ const config = typeof configModule === "function" ? configModule() : configModule.default || configModule;
189
+ const aliases = ((_a = config == null ? void 0 : config.resolve) == null ? void 0 : _a.alias) || {};
190
+ return Object.entries(aliases).reduce(
191
+ (acc, [key, value]) => {
192
+ acc[key] = makePathAbsolute(value);
193
+ return acc;
194
+ },
195
+ {}
196
+ );
197
+ } catch (error) {
198
+ return {};
199
+ }
200
+ }
201
+ function getAliases(framework) {
202
+ const tsAliases = getAliasesFromTSConfig();
203
+ if (framework === "vite") {
204
+ const viteAliases = getAliasesFromViteConfig();
205
+ return {
206
+ ...tsAliases,
207
+ ...viteAliases
208
+ };
209
+ }
210
+ if (framework === "webpack") {
211
+ const webpackAliases = getAliasesFromWebpackConfig();
212
+ return {
213
+ ...tsAliases,
214
+ ...webpackAliases
215
+ };
216
+ }
217
+ if (framework === "rollup") {
218
+ const rollupAliases = getAliasesFromRollupConfig();
219
+ return {
220
+ ...tsAliases,
221
+ ...rollupAliases
222
+ };
223
+ }
224
+ if (framework === "rolldown") {
225
+ const rolldownAliases = getAliasesFromRolldownConfig();
226
+ return {
227
+ ...tsAliases,
228
+ ...rolldownAliases
229
+ };
230
+ }
231
+ if (framework === "esbuild") {
232
+ const esbuildAliases = getAliasesFromEsbuildConfig();
233
+ return {
234
+ ...tsAliases,
235
+ ...esbuildAliases
236
+ };
237
+ }
238
+ if (framework === "rspack") {
239
+ const rspackAliases = getAliasesFromRspackConfig();
240
+ return {
241
+ ...tsAliases,
242
+ ...rspackAliases
243
+ };
244
+ }
245
+ if (framework === "farm") {
246
+ const farmAliases = getAliasesFromFarmConfig();
247
+ return {
248
+ ...tsAliases,
249
+ ...farmAliases
250
+ };
251
+ }
252
+ }
253
+
254
+ // src/core/options.ts
255
+ function getOptions(options) {
256
+ var _a, _b, _c;
257
+ const projectAliases = getAliases(options.framework);
258
+ const stylex2 = options.stylex || {};
259
+ const isDev = options.dev || isDevelopment;
260
+ return {
261
+ ...options,
262
+ dev: isDev,
263
+ // .js, .jsx, .mjs, .cjs, .ts, .tsx, .mts, .cts
264
+ validExts: _nullishCoalesce(options.validExts, () => ( /\.[mc]?[jt]sx?$/)),
265
+ stylex: {
266
+ filename: stylex2.filename || "stylex.css",
267
+ stylexImports: stylex2.stylexImports || ["@stylexjs/stylex"],
268
+ runtimeInjection: _nullishCoalesce(stylex2.runtimeInjection, () => ( isDev)),
269
+ aliases: _nullishCoalesce(_nullishCoalesce(stylex2.aliases, () => ( projectAliases)), () => ( {})),
270
+ useCSSLayers: stylex2.useCSSLayers || false,
271
+ unstable_moduleResolution: stylex2.unstable_moduleResolution || { type: "commonJS", rootDir: process.cwd() },
272
+ babelConfig: {
273
+ babelrc: _nullishCoalesce(((_a = stylex2.babelConfig) == null ? void 0 : _a.babelrc), () => ( false)),
274
+ plugins: _nullishCoalesce(((_b = stylex2.babelConfig) == null ? void 0 : _b.plugins), () => ( [])),
275
+ presets: _nullishCoalesce(((_c = stylex2.babelConfig) == null ? void 0 : _c.presets), () => ( []))
276
+ },
277
+ ...stylex2
278
+ }
279
+ };
280
+ }
281
+
282
+ // src/core/transformer.ts
283
+
284
+ var _core = require('@babel/core');
285
+ var _pluginsyntaxjsx = require('@babel/plugin-syntax-jsx'); var _pluginsyntaxjsx2 = _interopRequireDefault(_pluginsyntaxjsx);
286
+
287
+
288
+ // src/core/plugins.ts
289
+ var _pluginsyntaxflow = require('@babel/plugin-syntax-flow'); var _pluginsyntaxflow2 = _interopRequireDefault(_pluginsyntaxflow);
290
+ var _pluginsyntaxtypescript = require('@babel/plugin-syntax-typescript'); var _pluginsyntaxtypescript2 = _interopRequireDefault(_pluginsyntaxtypescript);
291
+ function getSyntaxPlugins(extname2) {
292
+ const TSPlugin = extname2 === ".tsx" ? [[_pluginsyntaxtypescript2.default, { isTSX: true }]] : [_pluginsyntaxtypescript2.default];
293
+ return [".js", ".jsx"].includes(extname2) ? [_pluginsyntaxflow2.default] : TSPlugin;
294
+ }
295
+
296
+ // src/core/transformer.ts
297
+ async function transformer(context) {
298
+ var _a, _b, _c, _d;
299
+ const { id, inputCode, options } = context;
300
+ const stylex2 = options.stylex;
301
+ const extname2 = _path.extname.call(void 0, id);
302
+ const stylexRules = {};
303
+ const stylexBabelPluginOptions = {
304
+ dev: options.dev,
305
+ importSources: stylex2.stylexImports,
306
+ ...stylex2
307
+ };
308
+ const plugins = [
309
+ ...((_a = stylex2.babelConfig) == null ? void 0 : _a.plugins) || [],
310
+ ...getSyntaxPlugins(extname2),
311
+ _pluginsyntaxjsx2.default,
312
+ _babelplugin2.default.withOptions(stylexBabelPluginOptions)
313
+ ];
314
+ const { code, map, metadata } = await _core.transformAsync.call(void 0, inputCode, {
315
+ babelrc: (_b = stylex2.babelConfig) == null ? void 0 : _b.babelrc,
316
+ filename: id,
317
+ presets: (_c = stylex2.babelConfig) == null ? void 0 : _c.presets,
318
+ plugins
319
+ });
320
+ if (metadata.stylex && metadata.stylex.length > 0) {
321
+ stylexRules[id] = metadata.stylex;
322
+ }
323
+ if (!((_d = stylex2.babelConfig) == null ? void 0 : _d.babelrc)) {
324
+ return {
325
+ code,
326
+ // compatible for farm, null will occur an error
327
+ map: map || void 0,
328
+ stylexRules
329
+ };
330
+ }
331
+ return { code, stylexRules };
332
+ }
333
+
334
+ // src/index.ts
335
+ var unpluginFactory = (rawOptions, meta) => {
336
+ const options = getOptions({ ...rawOptions || {}, framework: meta.framework });
337
+ const stylexRules = {};
338
+ let viteConfig = null;
339
+ return {
340
+ name: PLUGIN_NAME,
341
+ transformInclude(id) {
342
+ const validExts = options.validExts;
343
+ const extname2 = path2.extname(id);
344
+ const questionMarkIndex = extname2.indexOf("?");
345
+ const validExtName = questionMarkIndex > -1 ? extname2.slice(0, questionMarkIndex) : extname2;
346
+ return validExts instanceof RegExp ? validExts.test(validExtName) : validExts.includes(validExtName);
347
+ },
348
+ async transform(code, id) {
349
+ var _a;
350
+ const dir = path2.dirname(id);
351
+ const basename2 = path2.basename(id);
352
+ const file = path2.join(dir, basename2.includes("?") ? basename2.split("?")[0] : basename2);
353
+ if (!options.stylex.stylexImports.some((importName) => code.includes(importName))) {
354
+ return;
355
+ }
356
+ const context = {
357
+ id: file,
358
+ inputCode: code,
359
+ pluginContext: this,
360
+ options
361
+ };
362
+ try {
363
+ const result = await transformer(context);
364
+ if ((_a = result.stylexRules) == null ? void 0 : _a[id]) {
365
+ stylexRules[id] = result.stylexRules[id];
366
+ }
367
+ return result;
368
+ } catch (error) {
369
+ console.error("transform::error::", error);
370
+ this.error(error);
371
+ }
372
+ },
373
+ buildEnd() {
374
+ const fileName = options.stylex.filename;
375
+ const collectedCSS = buildStylexRules(stylexRules, options.stylex.useCSSLayers);
376
+ if (!collectedCSS) return;
377
+ this.emitFile({
378
+ fileName,
379
+ source: collectedCSS,
380
+ type: "asset"
381
+ });
382
+ },
383
+ vite: {
384
+ config(config) {
385
+ viteConfig = {
386
+ build: config.build,
387
+ base: config.base
388
+ };
389
+ },
390
+ configResolved(config) {
391
+ config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
392
+ config.optimizeDeps.exclude.push("@stylexjs/open-props");
393
+ },
394
+ buildEnd() {
395
+ var _a;
396
+ const fileName = `${_nullishCoalesce(((_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir), () => ( "assets"))}/${options.stylex.filename}`;
397
+ const collectedCSS = buildStylexRules(stylexRules, options.stylex.useCSSLayers);
398
+ if (!collectedCSS) return;
399
+ this.emitFile({
400
+ fileName,
401
+ source: collectedCSS,
402
+ type: "asset"
403
+ });
404
+ },
405
+ transformIndexHtml(html, ctx) {
406
+ var _a, _b;
407
+ const fileName = `${_nullishCoalesce(((_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir), () => ( "assets"))}/${options.stylex.filename}`;
408
+ const css = (_b = ctx.bundle) == null ? void 0 : _b[fileName];
409
+ if (!css) {
410
+ return html;
411
+ }
412
+ const publicPath = path2.posix.join(_nullishCoalesce((viteConfig == null ? void 0 : viteConfig.base), () => ( "/")), fileName.replace(/\\/g, "/"));
413
+ return [
414
+ {
415
+ tag: "link",
416
+ attrs: {
417
+ rel: "stylesheet",
418
+ href: publicPath
419
+ },
420
+ injectTo: "head"
421
+ }
422
+ ];
423
+ }
424
+ }
425
+ };
426
+ };
427
+ var unplugin = _unplugin.createUnplugin.call(void 0, unpluginFactory);
428
+ var index_default = unplugin;
429
+
430
+
431
+
432
+
433
+
434
+ exports.unpluginFactory = unpluginFactory; exports.unplugin = unplugin; exports.index_default = index_default;