@plumeria/vite-plugin 4.2.0 → 4.2.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.
Files changed (2) hide show
  1. package/dist/index.js +46 -8
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { createFilter } from 'vite';
2
2
  import { parseSync } from '@swc/core';
3
3
  import path from 'path';
4
+ import fs from 'fs';
4
5
  import { genBase36Hash } from 'zss-engine';
5
- import { tables, traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, extractOndemandStyles, deepMerge, scanAll, } from '@plumeria/utils';
6
+ import { tables, traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, } from '@plumeria/utils';
6
7
  const TARGET_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
7
8
  const EXTENSION_PATTERN = /\.(ts|tsx|js|jsx)$/;
8
9
  export function plumeria(options = {}) {
@@ -75,8 +76,45 @@ export function plumeria(options = {}) {
75
76
  tsx: true,
76
77
  target: 'es2022',
77
78
  });
78
- const localConsts = collectLocalConsts(ast, id);
79
- Object.assign(tables.staticTable, localConsts);
79
+ const localConsts = collectLocalConsts(ast);
80
+ const resourcePath = id;
81
+ const importMap = {};
82
+ traverse(ast, {
83
+ ImportDeclaration({ node }) {
84
+ const sourcePath = node.source.value;
85
+ const actualPath = resolveImportPath(sourcePath, resourcePath);
86
+ if (actualPath && fs.existsSync(actualPath)) {
87
+ if (fs.existsSync(actualPath)) {
88
+ node.specifiers.forEach((specifier) => {
89
+ if (specifier.type === 'ImportSpecifier') {
90
+ const importedName = specifier.imported
91
+ ? specifier.imported.value
92
+ : specifier.local.value;
93
+ const localName = specifier.local.value;
94
+ const uniqueKey = `${actualPath}-${importedName}`;
95
+ if (tables.staticTable[uniqueKey]) {
96
+ importMap[localName] = tables.staticTable[uniqueKey];
97
+ }
98
+ if (tables.keyframesHashTable[uniqueKey]) {
99
+ importMap[localName] = tables.keyframesHashTable[uniqueKey];
100
+ }
101
+ if (tables.viewTransitionHashTable[uniqueKey]) {
102
+ importMap[localName] =
103
+ tables.viewTransitionHashTable[uniqueKey];
104
+ }
105
+ if (tables.themeTable[uniqueKey]) {
106
+ importMap[localName] = tables.themeTable[uniqueKey];
107
+ }
108
+ }
109
+ });
110
+ }
111
+ }
112
+ },
113
+ });
114
+ const mergedStaticTable = Object.assign(Object.create(tables.staticTable), localConsts, importMap);
115
+ const mergedKeyframesTable = Object.assign(Object.create(tables.keyframesHashTable), importMap);
116
+ const mergedViewTransitionTable = Object.assign(Object.create(tables.viewTransitionHashTable), importMap);
117
+ const mergedThemeTable = Object.assign(Object.create(tables.themeTable), importMap);
80
118
  const localCreateStyles = {};
81
119
  const replacements = [];
82
120
  const extractedSheets = [];
@@ -94,7 +132,7 @@ export function plumeria(options = {}) {
94
132
  const propName = node.init.callee.property.value;
95
133
  if (propName === 'create' &&
96
134
  t.isObjectExpression(node.init.arguments[0].expression)) {
97
- const obj = objectExpressionToObject(node.init.arguments[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
135
+ const obj = objectExpressionToObject(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
98
136
  if (obj) {
99
137
  const hashMap = {};
100
138
  Object.entries(obj).forEach(([key, style]) => {
@@ -192,7 +230,7 @@ export function plumeria(options = {}) {
192
230
  if (propName === 'keyframes' &&
193
231
  args.length > 0 &&
194
232
  t.isObjectExpression(args[0].expression)) {
195
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
233
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
196
234
  const hash = genBase36Hash(obj, 1, 8);
197
235
  tables.keyframesObjectTable[hash] = obj;
198
236
  replacements.push({
@@ -204,7 +242,7 @@ export function plumeria(options = {}) {
204
242
  else if (propName === 'viewTransition' &&
205
243
  args.length > 0 &&
206
244
  t.isObjectExpression(args[0].expression)) {
207
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
245
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
208
246
  const hash = genBase36Hash(obj, 1, 8);
209
247
  tables.viewTransitionObjectTable[hash] = obj;
210
248
  extractOndemandStyles(obj, extractedSheets);
@@ -218,7 +256,7 @@ export function plumeria(options = {}) {
218
256
  else if (propName === 'createTheme' &&
219
257
  args.length > 0 &&
220
258
  t.isObjectExpression(args[0].expression)) {
221
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
259
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
222
260
  const hash = genBase36Hash(obj, 1, 8);
223
261
  tables.createThemeObjectTable[hash] = obj;
224
262
  }
@@ -313,7 +351,7 @@ export function plumeria(options = {}) {
313
351
  const merged = args.reduce((acc, arg) => {
314
352
  const expr = arg.expression;
315
353
  if (t.isObjectExpression(expr)) {
316
- const obj = objectExpressionToObject(expr, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
354
+ const obj = objectExpressionToObject(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
317
355
  return obj ? deepMerge(acc, obj) : acc;
318
356
  }
319
357
  else if (t.isMemberExpression(expr) &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/vite-plugin",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "type": "module",
5
5
  "description": "Plumeria Vite plugin",
6
6
  "author": "Refirst 11",
@@ -22,7 +22,7 @@
22
22
  "dist/"
23
23
  ],
24
24
  "dependencies": {
25
- "@plumeria/utils": "^4.2.0"
25
+ "@plumeria/utils": "^4.2.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",