@stylexjs/unplugin 0.17.3 → 0.17.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/lib/es/index.mjs CHANGED
@@ -39,6 +39,60 @@ function processCollectedRulesToCSS(rules, options) {
39
39
  });
40
40
  return code.toString();
41
41
  }
42
+ function getAssetBaseName(asset) {
43
+ if (asset?.name && typeof asset.name === 'string') return asset.name;
44
+ const fallback = asset?.fileName ? path.basename(asset.fileName) : 'stylex.css';
45
+ const match = /^(.*?)(-[a-z0-9]{8,})?\.css$/i.exec(fallback);
46
+ if (match) return `${match[1]}.css`;
47
+ return fallback || 'stylex.css';
48
+ }
49
+ function replaceBundleReferences(bundle, oldFileName, newFileName) {
50
+ for (const item of Object.values(bundle)) {
51
+ if (!item) continue;
52
+ if (item.type === 'chunk') {
53
+ if (typeof item.code === 'string' && item.code.includes(oldFileName)) {
54
+ item.code = item.code.split(oldFileName).join(newFileName);
55
+ }
56
+ const importedCss = item.viteMetadata?.importedCss;
57
+ if (importedCss instanceof Set && importedCss.has(oldFileName)) {
58
+ importedCss.delete(oldFileName);
59
+ importedCss.add(newFileName);
60
+ } else if (Array.isArray(importedCss)) {
61
+ const next = importedCss.map(name => name === oldFileName ? newFileName : name);
62
+ item.viteMetadata.importedCss = next;
63
+ }
64
+ } else if (item.type === 'asset' && typeof item.source === 'string') {
65
+ if (item.source.includes(oldFileName)) {
66
+ item.source = item.source.split(oldFileName).join(newFileName);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ function replaceCssAssetWithHashedCopy(ctx, bundle, asset, nextSource) {
72
+ const baseName = getAssetBaseName(asset);
73
+ const referenceId = ctx.emitFile({
74
+ type: 'asset',
75
+ name: baseName,
76
+ source: nextSource
77
+ });
78
+ const nextFileName = ctx.getFileName(referenceId);
79
+ const oldFileName = asset.fileName;
80
+ if (!nextFileName || !oldFileName || nextFileName === oldFileName) {
81
+ asset.source = nextSource;
82
+ return;
83
+ }
84
+ replaceBundleReferences(bundle, oldFileName, nextFileName);
85
+ const emitted = bundle[nextFileName];
86
+ if (emitted && emitted !== asset) {
87
+ delete bundle[nextFileName];
88
+ }
89
+ asset.fileName = nextFileName;
90
+ asset.source = nextSource;
91
+ if (bundle[oldFileName] === asset) {
92
+ delete bundle[oldFileName];
93
+ }
94
+ bundle[nextFileName] = asset;
95
+ }
42
96
  function readJSON(file) {
43
97
  try {
44
98
  const content = fs.readFileSync(file, 'utf8');
@@ -360,7 +414,8 @@ const unpluginInstance = createUnplugin((userOptions = {}, metaOptions) => {
360
414
  const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
361
415
  if (target) {
362
416
  const current = typeof target.source === 'string' ? target.source : target.source?.toString() || '';
363
- target.source = current ? current + '\n' + css : css;
417
+ const nextSource = current ? current + '\n' + css : css;
418
+ replaceCssAssetWithHashedCopy(this, bundle, target, nextSource);
364
419
  } else {}
365
420
  },
366
421
  vite: devMode === 'off' ? undefined : {
package/lib/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
1
7
  import { type UnpluginInstance } from 'unplugin';
2
8
  import type { Options as StyleXOptions } from '@stylexjs/babel-plugin';
3
9
  import type { TransformOptions as LightningcssOptions } from 'lightningcss';
package/lib/index.js CHANGED
@@ -45,6 +45,60 @@ function processCollectedRulesToCSS(rules, options) {
45
45
  });
46
46
  return code.toString();
47
47
  }
48
+ function getAssetBaseName(asset) {
49
+ if (asset?.name && typeof asset.name === 'string') return asset.name;
50
+ const fallback = asset?.fileName ? _nodePath.default.basename(asset.fileName) : 'stylex.css';
51
+ const match = /^(.*?)(-[a-z0-9]{8,})?\.css$/i.exec(fallback);
52
+ if (match) return `${match[1]}.css`;
53
+ return fallback || 'stylex.css';
54
+ }
55
+ function replaceBundleReferences(bundle, oldFileName, newFileName) {
56
+ for (const item of Object.values(bundle)) {
57
+ if (!item) continue;
58
+ if (item.type === 'chunk') {
59
+ if (typeof item.code === 'string' && item.code.includes(oldFileName)) {
60
+ item.code = item.code.split(oldFileName).join(newFileName);
61
+ }
62
+ const importedCss = item.viteMetadata?.importedCss;
63
+ if (importedCss instanceof Set && importedCss.has(oldFileName)) {
64
+ importedCss.delete(oldFileName);
65
+ importedCss.add(newFileName);
66
+ } else if (Array.isArray(importedCss)) {
67
+ const next = importedCss.map(name => name === oldFileName ? newFileName : name);
68
+ item.viteMetadata.importedCss = next;
69
+ }
70
+ } else if (item.type === 'asset' && typeof item.source === 'string') {
71
+ if (item.source.includes(oldFileName)) {
72
+ item.source = item.source.split(oldFileName).join(newFileName);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ function replaceCssAssetWithHashedCopy(ctx, bundle, asset, nextSource) {
78
+ const baseName = getAssetBaseName(asset);
79
+ const referenceId = ctx.emitFile({
80
+ type: 'asset',
81
+ name: baseName,
82
+ source: nextSource
83
+ });
84
+ const nextFileName = ctx.getFileName(referenceId);
85
+ const oldFileName = asset.fileName;
86
+ if (!nextFileName || !oldFileName || nextFileName === oldFileName) {
87
+ asset.source = nextSource;
88
+ return;
89
+ }
90
+ replaceBundleReferences(bundle, oldFileName, nextFileName);
91
+ const emitted = bundle[nextFileName];
92
+ if (emitted && emitted !== asset) {
93
+ delete bundle[nextFileName];
94
+ }
95
+ asset.fileName = nextFileName;
96
+ asset.source = nextSource;
97
+ if (bundle[oldFileName] === asset) {
98
+ delete bundle[oldFileName];
99
+ }
100
+ bundle[nextFileName] = asset;
101
+ }
48
102
  function readJSON(file) {
49
103
  try {
50
104
  const content = _nodeFs.default.readFileSync(file, 'utf8');
@@ -366,7 +420,8 @@ const unpluginInstance = (0, _unplugin.createUnplugin)((userOptions = {}, metaOp
366
420
  const target = pickCssAssetFromRollupBundle(bundle, cssInjectionTarget);
367
421
  if (target) {
368
422
  const current = typeof target.source === 'string' ? target.source : target.source?.toString() || '';
369
- target.source = current ? current + '\n' + css : css;
423
+ const nextSource = current ? current + '\n' + css : css;
424
+ replaceCssAssetWithHashedCopy(this, bundle, target, nextSource);
370
425
  } else {}
371
426
  },
372
427
  vite: devMode === 'off' ? undefined : {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/unplugin",
3
- "version": "0.17.3",
3
+ "version": "0.17.5",
4
4
  "private": false,
5
5
  "description": "Universal bundler plugin for StyleX using unplugin",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "@babel/plugin-syntax-flow": "^7.26.0",
36
36
  "@babel/plugin-syntax-jsx": "^7.25.9",
37
37
  "@babel/plugin-syntax-typescript": "^7.25.9",
38
- "@stylexjs/babel-plugin": "0.17.3",
38
+ "@stylexjs/babel-plugin": "0.17.5",
39
39
  "browserslist": "^4.24.0",
40
40
  "lightningcss": "^1.29.1"
41
41
  },