@plumeria/compiler 6.0.1 → 6.1.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 +85 -35
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -11,6 +11,10 @@ const utils_1 = require("@plumeria/utils");
11
11
  function compileCSS(options) {
12
12
  const { include, exclude, cwd = process.cwd() } = options;
13
13
  const allSheets = new Set();
14
+ const files = fs_1.default.globSync(include, {
15
+ cwd,
16
+ exclude: exclude,
17
+ });
14
18
  const processFile = (filePath) => {
15
19
  const source = fs_1.default.readFileSync(filePath, 'utf-8');
16
20
  const extractedSheets = [];
@@ -30,9 +34,26 @@ function compileCSS(options) {
30
34
  const localConsts = (0, utils_1.collectLocalConsts)(ast);
31
35
  const resourcePath = filePath;
32
36
  const importMap = {};
37
+ const plumeriaAliases = {};
33
38
  (0, utils_1.traverse)(ast, {
34
39
  ImportDeclaration({ node }) {
35
40
  const sourcePath = node.source.value;
41
+ if (sourcePath === '@plumeria/core') {
42
+ node.specifiers.forEach((specifier) => {
43
+ if (specifier.type === 'ImportNamespaceSpecifier') {
44
+ plumeriaAliases[specifier.local.value] = 'NAMESPACE';
45
+ }
46
+ else if (specifier.type === 'ImportDefaultSpecifier') {
47
+ plumeriaAliases[specifier.local.value] = 'NAMESPACE';
48
+ }
49
+ else if (specifier.type === 'ImportSpecifier') {
50
+ const importedName = specifier.imported
51
+ ? specifier.imported.value
52
+ : specifier.local.value;
53
+ plumeriaAliases[specifier.local.value] = importedName;
54
+ }
55
+ });
56
+ }
36
57
  const actualPath = (0, utils_1.resolveImportPath)(sourcePath, resourcePath);
37
58
  if (actualPath) {
38
59
  node.specifiers.forEach((specifier) => {
@@ -119,44 +140,77 @@ function compileCSS(options) {
119
140
  VariableDeclarator({ node }) {
120
141
  if (node.id.type === 'Identifier' &&
121
142
  node.init &&
122
- utils_1.t.isCallExpression(node.init) &&
123
- utils_1.t.isMemberExpression(node.init.callee) &&
124
- utils_1.t.isIdentifier(node.init.callee.object, { name: 'css' }) &&
125
- utils_1.t.isIdentifier(node.init.callee.property) &&
126
- node.init.arguments.length === 1 &&
127
- utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
128
- const resolveVariable = (name) => {
129
- if (localCreateStyles[name]) {
130
- return localCreateStyles[name].obj;
143
+ utils_1.t.isCallExpression(node.init)) {
144
+ let propName;
145
+ const callee = node.init.callee;
146
+ if (utils_1.t.isMemberExpression(callee) &&
147
+ utils_1.t.isIdentifier(callee.object) &&
148
+ utils_1.t.isIdentifier(callee.property)) {
149
+ const objectName = callee.object.value;
150
+ const propertyName = callee.property.value;
151
+ const alias = plumeriaAliases[objectName];
152
+ if (alias === 'NAMESPACE' || objectName === 'css') {
153
+ propName = propertyName;
131
154
  }
132
- };
133
- const propName = node.init.callee.property.value;
134
- if (propName === 'create') {
135
- const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
136
- if (obj) {
137
- localCreateStyles[node.id.value] = { type: 'create', obj };
138
- Object.entries(obj).forEach(([key, style]) => {
139
- const records = (0, utils_1.getStyleRecords)(key, style, 1);
140
- (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
141
- records.forEach((r) => {
142
- extractedSheets.push(r.sheet);
143
- });
144
- });
155
+ }
156
+ else if (utils_1.t.isIdentifier(callee)) {
157
+ const calleeName = callee.value;
158
+ const originalName = plumeriaAliases[calleeName];
159
+ if (originalName) {
160
+ propName = originalName;
145
161
  }
146
162
  }
147
- else if (propName === 'variants') {
148
- const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
149
- if (obj) {
150
- localCreateStyles[node.id.value] = { type: 'variant', obj };
163
+ if (propName &&
164
+ node.init.arguments.length === 1 &&
165
+ utils_1.t.isObjectExpression(node.init.arguments[0].expression)) {
166
+ const resolveVariable = (name) => {
167
+ if (localCreateStyles[name]) {
168
+ return localCreateStyles[name].obj;
169
+ }
170
+ };
171
+ if (propName === 'create') {
172
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
173
+ if (obj) {
174
+ localCreateStyles[node.id.value] = { type: 'create', obj };
175
+ Object.entries(obj).forEach(([key, style]) => {
176
+ const records = (0, utils_1.getStyleRecords)(key, style, 1);
177
+ (0, utils_1.extractOndemandStyles)(style, extractedSheets, scannedTables);
178
+ records.forEach((r) => {
179
+ extractedSheets.push(r.sheet);
180
+ });
181
+ });
182
+ }
183
+ }
184
+ else if (propName === 'variants') {
185
+ const obj = (0, utils_1.objectExpressionToObject)(node.init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable, resolveVariable);
186
+ if (obj) {
187
+ localCreateStyles[node.id.value] = { type: 'variant', obj };
188
+ }
151
189
  }
152
190
  }
153
191
  }
154
192
  },
155
193
  CallExpression({ node }) {
156
194
  const callee = node.callee;
195
+ let propName;
157
196
  if (utils_1.t.isMemberExpression(callee) &&
158
- utils_1.t.isIdentifier(callee.object, { name: 'css' }) &&
197
+ utils_1.t.isIdentifier(callee.object) &&
159
198
  utils_1.t.isIdentifier(callee.property)) {
199
+ const objectName = callee.object.value;
200
+ const propertyName = callee.property.value;
201
+ const alias = plumeriaAliases[objectName];
202
+ if (alias === 'NAMESPACE' || objectName === 'css') {
203
+ propName = propertyName;
204
+ }
205
+ }
206
+ else if (utils_1.t.isIdentifier(callee)) {
207
+ const calleeName = callee.value;
208
+ const originalName = plumeriaAliases[calleeName];
209
+ if (originalName) {
210
+ propName = originalName;
211
+ }
212
+ }
213
+ if (propName) {
160
214
  const args = node.arguments;
161
215
  const extractStylesFromExpression = (expr) => {
162
216
  const results = [];
@@ -225,7 +279,7 @@ function compileCSS(options) {
225
279
  const records = (0, utils_1.getStyleRecords)(hash, style, 1);
226
280
  records.forEach((r) => extractedSheets.push(r.sheet));
227
281
  };
228
- if (callee.property.value === 'props') {
282
+ if (propName === 'props') {
229
283
  const conditionals = [];
230
284
  let groupIdCounter = 0;
231
285
  let baseStyle = {};
@@ -386,14 +440,14 @@ function compileCSS(options) {
386
440
  }
387
441
  }
388
442
  }
389
- else if (callee.property.value === 'keyframes' &&
443
+ else if (propName === 'keyframes' &&
390
444
  args.length > 0 &&
391
445
  utils_1.t.isObjectExpression(args[0].expression)) {
392
446
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
393
447
  const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
394
448
  scannedTables.keyframesObjectTable[hash] = obj;
395
449
  }
396
- else if (callee.property.value === 'viewTransition' &&
450
+ else if (propName === 'viewTransition' &&
397
451
  args.length > 0 &&
398
452
  utils_1.t.isObjectExpression(args[0].expression)) {
399
453
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
@@ -402,7 +456,7 @@ function compileCSS(options) {
402
456
  (0, utils_1.extractOndemandStyles)(obj, extractedSheets, scannedTables);
403
457
  (0, utils_1.extractOndemandStyles)({ vt: `vt-${hash}` }, extractedSheets, scannedTables);
404
458
  }
405
- else if (callee.property.value === 'createTheme' &&
459
+ else if (propName === 'createTheme' &&
406
460
  args.length > 0 &&
407
461
  utils_1.t.isObjectExpression(args[0].expression)) {
408
462
  const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedThemeTable, mergedCreateTable, mergedVariantsTable);
@@ -414,10 +468,6 @@ function compileCSS(options) {
414
468
  });
415
469
  return extractedSheets;
416
470
  };
417
- const files = fs_1.default.globSync(include, {
418
- cwd,
419
- exclude: exclude,
420
- });
421
471
  for (const file of files) {
422
472
  const sheets = processFile(file);
423
473
  for (const sheet of sheets) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "6.0.1",
3
+ "version": "6.1.0",
4
4
  "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@plumeria/utils": "^6.0.1"
24
+ "@plumeria/utils": "^6.1.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@swc/core": "1.15.8",