@plumeria/compiler 0.14.6 → 0.14.8

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
@@ -29,12 +29,93 @@ function isInComment(code, position) {
29
29
  }
30
30
  return inMultiLineComment;
31
31
  }
32
+ function isInVueAttribute(code, position) {
33
+ let quotePosition = -1;
34
+ for (let i = position - 1; i >= 0; i--) {
35
+ const char = code[i];
36
+ if (char === '"' || char === "'") {
37
+ quotePosition = i;
38
+ break;
39
+ }
40
+ }
41
+ if (quotePosition === -1)
42
+ return false;
43
+ const beforeQuote = code.substring(Math.max(0, quotePosition - 30), quotePosition);
44
+ const vueAttributePattern = /(:|v-bind:)(class|style)\s*=\s*$/;
45
+ return vueAttributePattern.test(beforeQuote);
46
+ }
47
+ function isInString(code, position) {
48
+ let inSingleQuote = false;
49
+ let inDoubleQuote = false;
50
+ let inBacktick = false;
51
+ let escape = false;
52
+ for (let i = 0; i < position; i++) {
53
+ const char = code[i];
54
+ if (escape) {
55
+ escape = false;
56
+ continue;
57
+ }
58
+ if (char === '\\') {
59
+ escape = true;
60
+ continue;
61
+ }
62
+ if (!inSingleQuote && !inDoubleQuote && !inBacktick) {
63
+ if (char === "'") {
64
+ inSingleQuote = true;
65
+ }
66
+ else if (char === '"') {
67
+ inDoubleQuote = true;
68
+ }
69
+ else if (char === '`') {
70
+ inBacktick = true;
71
+ }
72
+ }
73
+ else {
74
+ if (inSingleQuote && char === "'") {
75
+ inSingleQuote = false;
76
+ }
77
+ else if (inDoubleQuote && char === '"') {
78
+ inDoubleQuote = false;
79
+ }
80
+ else if (inBacktick && char === '`') {
81
+ inBacktick = false;
82
+ }
83
+ }
84
+ }
85
+ const inStringLiteral = inSingleQuote || inDoubleQuote || inBacktick;
86
+ if (inStringLiteral && isInVueAttribute(code, position))
87
+ return false;
88
+ return inStringLiteral;
89
+ }
90
+ function isInHtmlText(code, position) {
91
+ let lastOpenTag = -1;
92
+ let lastCloseTag = -1;
93
+ for (let i = position - 1; i >= 0; i--) {
94
+ if (code[i] === '>' && lastCloseTag === -1) {
95
+ lastCloseTag = i;
96
+ }
97
+ if (code[i] === '<') {
98
+ lastOpenTag = i;
99
+ break;
100
+ }
101
+ }
102
+ let nextOpenTag = -1;
103
+ for (let i = position; i < code.length; i++) {
104
+ if (code[i] === '<') {
105
+ nextOpenTag = i;
106
+ break;
107
+ }
108
+ }
109
+ return lastCloseTag > lastOpenTag && nextOpenTag > position;
110
+ }
32
111
  function extractCssProps(code) {
33
112
  const propsMatches = [];
34
113
  const regex = /css\.props\s*\(/g;
35
114
  let match;
36
115
  while ((match = regex.exec(code))) {
37
- if (isInComment(code, match.index)) {
116
+ if (isInComment(code, match.index) ||
117
+ isInString(code, match.index) ||
118
+ isInHtmlText(code, match.index)) {
38
119
  continue;
39
120
  }
40
121
  const startIndex = match.index + match[0].length;
@@ -69,7 +150,9 @@ function extractCssCreate(code) {
69
150
  const regex = /(?:(?:\s*const\s+[a-zA-Z0-9_$]+\s*=\s*css\.create\([\s\S]*?\);\s*))/g;
70
151
  let match;
71
152
  while ((match = regex.exec(code))) {
72
- if (isInComment(code, match.index)) {
153
+ if (isInComment(code, match.index) ||
154
+ isInString(code, match.index) ||
155
+ isInHtmlText(code, match.index)) {
73
156
  continue;
74
157
  }
75
158
  cssCreateMatches.push(match[0]);
@@ -99,7 +182,7 @@ function parseCssPropsArguments(args) {
99
182
  }
100
183
  return results;
101
184
  }
102
- function extractVueAndSvelte(filePath) {
185
+ async function extractVueAndSvelte(filePath) {
103
186
  const ext = path.extname(filePath);
104
187
  if (!(ext === '.svelte' || ext === '.vue'))
105
188
  return filePath;
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ const combineMediaQuery = require('postcss-combine-media-query');
10
10
  const { execute } = require('rscute/execute');
11
11
  const { transform } = require('lightningcss');
12
12
  const { parseSync } = require('@swc/core');
13
- const { buildGlobal, buildCreate } = require('@plumeria/core/processors');
13
+ const { buildGlobal, buildProps } = require('@plumeria/core/processors');
14
14
  const { extractAndInjectStyleProps, 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');
@@ -105,9 +105,26 @@ async function optimizeCSS() {
105
105
  cwd: projectRoot,
106
106
  });
107
107
  const projectName = path.basename(projectRoot);
108
- const filesSupportExtensions = files.map((file) => extractVueAndSvelte(file));
109
- const styleFiles = filesSupportExtensions.filter(isCSS).sort();
110
- const cssPropsFiles = styleFiles.filter((file) => isCSS(file, 'props'));
108
+ const filesSupportExtensions = [];
109
+ for (const file of files) {
110
+ const tsFile = await extractVueAndSvelte(file);
111
+ filesSupportExtensions.push(tsFile);
112
+ }
113
+ const styleFiles = filesSupportExtensions
114
+ .filter((file) => isCSS(file, ''))
115
+ .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
+ }
126
+ return isCSS(file, 'props');
127
+ });
111
128
  for (let i = 0; i < cssPropsFiles.length; i++) {
112
129
  await extractAndInjectStyleProps(path.resolve(cssPropsFiles[i]));
113
130
  }
@@ -120,7 +137,7 @@ async function optimizeCSS() {
120
137
  await buildGlobal(coreFilePath);
121
138
  }
122
139
  for (let i = 0; i < styleFiles.length; i++) {
123
- await buildCreate(coreFilePath);
140
+ await buildProps(coreFilePath);
124
141
  }
125
142
  await optimizeCSS();
126
143
  await restoreAllOriginals();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "0.14.6",
3
+ "version": "0.14.8",
4
4
  "description": "A faster compiler for Plumeria",
5
5
  "keywords": [
6
6
  "css",