@plumeria/vite-plugin 4.2.0 → 5.0.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.
Files changed (2) hide show
  1. package/dist/index.js +61 -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,60 @@ 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 = { ...tables.staticTable };
115
+ for (const key of Object.keys(localConsts)) {
116
+ mergedStaticTable[key] = localConsts[key];
117
+ }
118
+ for (const key of Object.keys(importMap)) {
119
+ mergedStaticTable[key] = importMap[key];
120
+ }
121
+ const mergedKeyframesTable = { ...tables.keyframesHashTable };
122
+ for (const key of Object.keys(importMap)) {
123
+ mergedKeyframesTable[key] = importMap[key];
124
+ }
125
+ const mergedViewTransitionTable = { ...tables.viewTransitionHashTable };
126
+ for (const key of Object.keys(importMap)) {
127
+ mergedViewTransitionTable[key] = importMap[key];
128
+ }
129
+ const mergedThemeTable = { ...tables.themeTable };
130
+ for (const key of Object.keys(importMap)) {
131
+ mergedThemeTable[key] = importMap[key];
132
+ }
80
133
  const localCreateStyles = {};
81
134
  const replacements = [];
82
135
  const extractedSheets = [];
@@ -94,7 +147,7 @@ export function plumeria(options = {}) {
94
147
  const propName = node.init.callee.property.value;
95
148
  if (propName === 'create' &&
96
149
  t.isObjectExpression(node.init.arguments[0].expression)) {
97
- const obj = objectExpressionToObject(node.init.arguments[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
150
+ const obj = objectExpressionToObject(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
98
151
  if (obj) {
99
152
  const hashMap = {};
100
153
  Object.entries(obj).forEach(([key, style]) => {
@@ -192,7 +245,7 @@ export function plumeria(options = {}) {
192
245
  if (propName === 'keyframes' &&
193
246
  args.length > 0 &&
194
247
  t.isObjectExpression(args[0].expression)) {
195
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
248
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
196
249
  const hash = genBase36Hash(obj, 1, 8);
197
250
  tables.keyframesObjectTable[hash] = obj;
198
251
  replacements.push({
@@ -204,7 +257,7 @@ export function plumeria(options = {}) {
204
257
  else if (propName === 'viewTransition' &&
205
258
  args.length > 0 &&
206
259
  t.isObjectExpression(args[0].expression)) {
207
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
260
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
208
261
  const hash = genBase36Hash(obj, 1, 8);
209
262
  tables.viewTransitionObjectTable[hash] = obj;
210
263
  extractOndemandStyles(obj, extractedSheets);
@@ -218,7 +271,7 @@ export function plumeria(options = {}) {
218
271
  else if (propName === 'createTheme' &&
219
272
  args.length > 0 &&
220
273
  t.isObjectExpression(args[0].expression)) {
221
- const obj = objectExpressionToObject(args[0].expression, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
274
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
222
275
  const hash = genBase36Hash(obj, 1, 8);
223
276
  tables.createThemeObjectTable[hash] = obj;
224
277
  }
@@ -313,7 +366,7 @@ export function plumeria(options = {}) {
313
366
  const merged = args.reduce((acc, arg) => {
314
367
  const expr = arg.expression;
315
368
  if (t.isObjectExpression(expr)) {
316
- const obj = objectExpressionToObject(expr, tables.staticTable, tables.keyframesHashTable, tables.viewTransitionHashTable, tables.themeTable);
369
+ const obj = objectExpressionToObject(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable);
317
370
  return obj ? deepMerge(acc, obj) : acc;
318
371
  }
319
372
  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": "5.0.0",
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": "^5.0.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@swc/core": "1.15.8",