@plumeria/compiler 0.14.7 → 0.14.9

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/dist/extract.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
- const originalCodeMap = new Map();
6
5
  const generatedTsMap = new Map();
7
6
  function isInComment(code, position) {
8
7
  const beforePosition = code.substring(0, position);
@@ -29,6 +28,21 @@ function isInComment(code, position) {
29
28
  }
30
29
  return inMultiLineComment;
31
30
  }
31
+ function isInVueAttribute(code, position) {
32
+ let quotePosition = -1;
33
+ for (let i = position - 1; i >= 0; i--) {
34
+ const char = code[i];
35
+ if (char === '"' || char === "'") {
36
+ quotePosition = i;
37
+ break;
38
+ }
39
+ }
40
+ if (quotePosition === -1)
41
+ return false;
42
+ const beforeQuote = code.substring(Math.max(0, quotePosition - 30), quotePosition);
43
+ const vueAttributePattern = /(:|v-bind:)(class|style)\s*=\s*$/;
44
+ return vueAttributePattern.test(beforeQuote);
45
+ }
32
46
  function isInString(code, position) {
33
47
  let inSingleQuote = false;
34
48
  let inDoubleQuote = false;
@@ -67,7 +81,10 @@ function isInString(code, position) {
67
81
  }
68
82
  }
69
83
  }
70
- return inSingleQuote || inDoubleQuote || inBacktick;
84
+ const inStringLiteral = inSingleQuote || inDoubleQuote || inBacktick;
85
+ if (inStringLiteral && isInVueAttribute(code, position))
86
+ return false;
87
+ return inStringLiteral;
71
88
  }
72
89
  function isInHtmlText(code, position) {
73
90
  let lastOpenTag = -1;
@@ -141,6 +158,20 @@ function extractCssCreate(code) {
141
158
  }
142
159
  return cssCreateMatches.join('\n');
143
160
  }
161
+ function extractCssGlobal(code) {
162
+ const cssCreateMatches = [];
163
+ const regex = /\bcss\.global\(\s*([\s\S]*?)\s*\);/g;
164
+ let match;
165
+ while ((match = regex.exec(code))) {
166
+ if (isInComment(code, match.index) ||
167
+ isInString(code, match.index) ||
168
+ isInHtmlText(code, match.index)) {
169
+ continue;
170
+ }
171
+ cssCreateMatches.push(match[0]);
172
+ }
173
+ return cssCreateMatches.join('\n');
174
+ }
144
175
  function parseCssPropsArguments(args) {
145
176
  const results = [];
146
177
  const splitArgs = args.split(/\s*,\s*(?![^(]*\))/);
@@ -169,7 +200,6 @@ async function extractVueAndSvelte(filePath) {
169
200
  if (!(ext === '.svelte' || ext === '.vue'))
170
201
  return filePath;
171
202
  const code = fs.readFileSync(filePath, 'utf8');
172
- originalCodeMap.set(filePath, code);
173
203
  const lines = code.split(/\r?\n/);
174
204
  let inScript = false;
175
205
  const contentLines = [];
@@ -196,13 +226,17 @@ async function extractVueAndSvelte(filePath) {
196
226
  const importRegex = /^(\s*import\s[^;]+;\s*)+/m;
197
227
  const importMatch = tsCode.match(importRegex);
198
228
  const importSection = importMatch ? importMatch[0] : '';
199
- const stylesSection = extractCssCreate(tsCode);
229
+ const cssCreateSection = extractCssCreate(code);
230
+ const cssGlobalSection = extractCssGlobal(code);
200
231
  let finalCode = '';
201
232
  if (importSection) {
202
233
  finalCode += importSection + '\n';
203
234
  }
204
- if (stylesSection) {
205
- finalCode += stylesSection + '\n';
235
+ if (cssGlobalSection) {
236
+ finalCode += cssGlobalSection + '\n';
237
+ }
238
+ if (cssCreateSection) {
239
+ finalCode += cssCreateSection + '\n';
206
240
  }
207
241
  if (calls) {
208
242
  finalCode += calls + '\n';
@@ -212,14 +246,14 @@ async function extractVueAndSvelte(filePath) {
212
246
  generatedTsMap.set(filePath, tsPath);
213
247
  return tsPath;
214
248
  }
215
- async function extractAndInjectStyleProps(filePath) {
216
- const original = fs.readFileSync(filePath, 'utf8');
217
- originalCodeMap.set(filePath, original);
249
+ async function extractTSFile(filePath) {
250
+ const code = fs.readFileSync(filePath, 'utf8');
218
251
  const importRegex = /^(?:\s*import\s[^;]+;\s*)+/m;
219
- const importMatch = original.match(importRegex);
252
+ const importMatch = code.match(importRegex);
220
253
  const importSection = importMatch ? importMatch[0] : '';
221
- const cssCreateSection = extractCssCreate(original);
222
- const propsMatches = extractCssProps(original);
254
+ const cssCreateSection = extractCssCreate(code);
255
+ const cssGlobalSection = extractCssGlobal(code);
256
+ const propsMatches = extractCssProps(code);
223
257
  const calls = propsMatches
224
258
  .filter(Boolean)
225
259
  .map((call) => `${call};`)
@@ -227,10 +261,16 @@ async function extractAndInjectStyleProps(filePath) {
227
261
  let finalCode = '';
228
262
  if (importSection)
229
263
  finalCode += importSection + '\n';
264
+ if (cssGlobalSection)
265
+ finalCode += cssGlobalSection + '\n';
230
266
  if (cssCreateSection)
231
267
  finalCode += cssCreateSection + '\n';
232
268
  finalCode += calls;
233
- fs.writeFileSync(filePath, finalCode, 'utf8');
269
+ const ext = path.extname(filePath);
270
+ const tempFilePath = filePath.replace(ext, '-temp.ts');
271
+ fs.writeFileSync(tempFilePath, finalCode, 'utf8');
272
+ generatedTsMap.set(filePath, tempFilePath);
273
+ return tempFilePath;
234
274
  }
235
275
  async function restoreAllOriginals() {
236
276
  for (const [originalPath, genPath] of generatedTsMap.entries()) {
@@ -239,10 +279,6 @@ async function restoreAllOriginals() {
239
279
  }
240
280
  }
241
281
  generatedTsMap.clear();
242
- for (const [filePath, backup] of originalCodeMap.entries()) {
243
- fs.writeFileSync(filePath, backup, 'utf8');
244
- }
245
- originalCodeMap.clear();
246
282
  }
247
283
  process.on('uncaughtException', async (error) => {
248
284
  console.error('Uncaught Exception:', error);
@@ -255,8 +291,7 @@ process.on('unhandledRejection', async (reason, promise) => {
255
291
  process.exit(1);
256
292
  });
257
293
  module.exports = {
258
- extractAndInjectStyleProps,
294
+ extractTSFile,
259
295
  restoreAllOriginals,
260
296
  extractVueAndSvelte,
261
- originalCodeMap,
262
297
  };
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ const { execute } = require('rscute/execute');
11
11
  const { transform } = require('lightningcss');
12
12
  const { parseSync } = require('@swc/core');
13
13
  const { buildGlobal, buildProps } = require('@plumeria/core/processors');
14
- const { extractAndInjectStyleProps, restoreAllOriginals, extractVueAndSvelte, } = require('./extract');
14
+ const { extractTSFile, restoreAllOriginals, extractVueAndSvelte, } = require('./extract');
15
15
  const projectRoot = process.cwd().split('node_modules')[0];
16
16
  const directPath = path.join(projectRoot, 'node_modules/@plumeria/core');
17
17
  const coreFilePath = path.join(directPath, 'stylesheet.css');
@@ -107,27 +107,22 @@ async function optimizeCSS() {
107
107
  const projectName = path.basename(projectRoot);
108
108
  const filesSupportExtensions = [];
109
109
  for (const file of files) {
110
- const tsFile = await extractVueAndSvelte(file);
111
- filesSupportExtensions.push(tsFile);
110
+ const ext = path.extname(file);
111
+ if (ext === '.vue' || ext === '.svelte') {
112
+ const tsFile = await extractVueAndSvelte(file);
113
+ filesSupportExtensions.push(tsFile);
114
+ }
115
+ else {
116
+ const tempFile = await extractTSFile(file);
117
+ filesSupportExtensions.push(tempFile);
118
+ }
112
119
  }
113
120
  const styleFiles = filesSupportExtensions
114
121
  .filter((file) => isCSS(file, ''))
115
122
  .sort();
116
- const cssPropsFiles = styleFiles.filter((file) => {
117
- if (file.endsWith('.ts')) {
118
- const vueFile = file.replace('.ts', '.vue');
119
- const svelteFile = file.replace('.ts', '.svelte');
120
- const isGeneratedFromVue = existsSync(vueFile);
121
- const isGeneratedFromSvelte = existsSync(svelteFile);
122
- if (isGeneratedFromVue || isGeneratedFromSvelte) {
123
- return false;
124
- }
125
- }
123
+ const propsFiles = styleFiles.filter((file) => {
126
124
  return isCSS(file, 'props');
127
125
  });
128
- for (let i = 0; i < cssPropsFiles.length; i++) {
129
- await extractAndInjectStyleProps(path.resolve(cssPropsFiles[i]));
130
- }
131
126
  for (let i = 0; i < styleFiles.length; i++) {
132
127
  await execute(path.resolve(styleFiles[i]));
133
128
  if (process.argv.includes('--paths'))
@@ -136,7 +131,7 @@ async function optimizeCSS() {
136
131
  for (let i = 0; i < styleFiles.length; i++) {
137
132
  await buildGlobal(coreFilePath);
138
133
  }
139
- for (let i = 0; i < styleFiles.length; i++) {
134
+ for (let i = 0; i < propsFiles.length; i++) {
140
135
  await buildProps(coreFilePath);
141
136
  }
142
137
  await optimizeCSS();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "0.14.7",
3
+ "version": "0.14.9",
4
4
  "description": "A faster compiler for Plumeria",
5
5
  "keywords": [
6
6
  "css",