@plumeria/vite-plugin 4.1.3 → 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 +47 -22
  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, scanForCreateStatic, scanForCreateTheme, scanForKeyframes, scanForViewTransition, t, extractOndemandStyles, deepMerge, } 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 = {}) {
@@ -69,23 +70,51 @@ export function plumeria(options = {}) {
69
70
  dependencies.push(depPath);
70
71
  this.addWatchFile(depPath);
71
72
  };
72
- tables.staticTable = scanForCreateStatic(addDependency);
73
- const { keyframesHashTableLocal, keyframesObjectTableLocal } = scanForKeyframes(addDependency);
74
- tables.keyframesHashTable = keyframesHashTableLocal;
75
- tables.keyframesObjectTable = keyframesObjectTableLocal;
76
- const { viewTransitionHashTableLocal, viewTransitionObjectTableLocal } = scanForViewTransition(addDependency);
77
- tables.viewTransitionHashTable = viewTransitionHashTableLocal;
78
- tables.viewTransitionObjectTable = viewTransitionObjectTableLocal;
79
- const { themeTableLocal, createThemeObjectTableLocal } = scanForCreateTheme(addDependency);
80
- tables.themeTable = themeTableLocal;
81
- tables.createThemeObjectTable = createThemeObjectTableLocal;
73
+ scanAll(addDependency);
82
74
  const ast = parseSync(source, {
83
75
  syntax: 'typescript',
84
76
  tsx: true,
85
77
  target: 'es2022',
86
78
  });
87
79
  const localConsts = collectLocalConsts(ast);
88
- Object.assign(tables.staticTable, localConsts);
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);
89
118
  const localCreateStyles = {};
90
119
  const replacements = [];
91
120
  const extractedSheets = [];
@@ -103,7 +132,7 @@ export function plumeria(options = {}) {
103
132
  const propName = node.init.callee.property.value;
104
133
  if (propName === 'create' &&
105
134
  t.isObjectExpression(node.init.arguments[0].expression)) {
106
- 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);
107
136
  if (obj) {
108
137
  const hashMap = {};
109
138
  Object.entries(obj).forEach(([key, style]) => {
@@ -201,7 +230,7 @@ export function plumeria(options = {}) {
201
230
  if (propName === 'keyframes' &&
202
231
  args.length > 0 &&
203
232
  t.isObjectExpression(args[0].expression)) {
204
- 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);
205
234
  const hash = genBase36Hash(obj, 1, 8);
206
235
  tables.keyframesObjectTable[hash] = obj;
207
236
  replacements.push({
@@ -213,7 +242,7 @@ export function plumeria(options = {}) {
213
242
  else if (propName === 'viewTransition' &&
214
243
  args.length > 0 &&
215
244
  t.isObjectExpression(args[0].expression)) {
216
- 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);
217
246
  const hash = genBase36Hash(obj, 1, 8);
218
247
  tables.viewTransitionObjectTable[hash] = obj;
219
248
  extractOndemandStyles(obj, extractedSheets);
@@ -227,7 +256,7 @@ export function plumeria(options = {}) {
227
256
  else if (propName === 'createTheme' &&
228
257
  args.length > 0 &&
229
258
  t.isObjectExpression(args[0].expression)) {
230
- 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);
231
260
  const hash = genBase36Hash(obj, 1, 8);
232
261
  tables.createThemeObjectTable[hash] = obj;
233
262
  }
@@ -259,14 +288,10 @@ export function plumeria(options = {}) {
259
288
  return;
260
289
  const styleInfo = localCreateStyles[node.value];
261
290
  if (styleInfo && !styleInfo.hasDynamicAccess) {
262
- const fullHashMap = {};
263
- Object.entries(styleInfo.hashMap).forEach(([key, atomMap]) => {
264
- fullHashMap[key] = Object.values(atomMap).join(' ');
265
- });
266
291
  replacements.push({
267
292
  start: node.span.start - ast.span.start,
268
293
  end: node.span.end - ast.span.start,
269
- content: JSON.stringify(fullHashMap),
294
+ content: JSON.stringify(styleInfo.hashMap),
270
295
  });
271
296
  }
272
297
  },
@@ -326,7 +351,7 @@ export function plumeria(options = {}) {
326
351
  const merged = args.reduce((acc, arg) => {
327
352
  const expr = arg.expression;
328
353
  if (t.isObjectExpression(expr)) {
329
- const obj = objectExpressionToObject(expr, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
354
+ const obj = objectExpressionToObject(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
330
355
  return obj ? deepMerge(acc, obj) : acc;
331
356
  }
332
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.1.3",
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.1.3"
25
+ "@plumeria/utils": "^4.2.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",