@plumeria/compiler 0.14.8 → 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);
@@ -159,6 +158,20 @@ function extractCssCreate(code) {
159
158
  }
160
159
  return cssCreateMatches.join('\n');
161
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
+ }
162
175
  function parseCssPropsArguments(args) {
163
176
  const results = [];
164
177
  const splitArgs = args.split(/\s*,\s*(?![^(]*\))/);
@@ -187,7 +200,6 @@ async function extractVueAndSvelte(filePath) {
187
200
  if (!(ext === '.svelte' || ext === '.vue'))
188
201
  return filePath;
189
202
  const code = fs.readFileSync(filePath, 'utf8');
190
- originalCodeMap.set(filePath, code);
191
203
  const lines = code.split(/\r?\n/);
192
204
  let inScript = false;
193
205
  const contentLines = [];
@@ -214,13 +226,17 @@ async function extractVueAndSvelte(filePath) {
214
226
  const importRegex = /^(\s*import\s[^;]+;\s*)+/m;
215
227
  const importMatch = tsCode.match(importRegex);
216
228
  const importSection = importMatch ? importMatch[0] : '';
217
- const stylesSection = extractCssCreate(tsCode);
229
+ const cssCreateSection = extractCssCreate(code);
230
+ const cssGlobalSection = extractCssGlobal(code);
218
231
  let finalCode = '';
219
232
  if (importSection) {
220
233
  finalCode += importSection + '\n';
221
234
  }
222
- if (stylesSection) {
223
- finalCode += stylesSection + '\n';
235
+ if (cssGlobalSection) {
236
+ finalCode += cssGlobalSection + '\n';
237
+ }
238
+ if (cssCreateSection) {
239
+ finalCode += cssCreateSection + '\n';
224
240
  }
225
241
  if (calls) {
226
242
  finalCode += calls + '\n';
@@ -230,14 +246,14 @@ async function extractVueAndSvelte(filePath) {
230
246
  generatedTsMap.set(filePath, tsPath);
231
247
  return tsPath;
232
248
  }
233
- async function extractAndInjectStyleProps(filePath) {
234
- const original = fs.readFileSync(filePath, 'utf8');
235
- originalCodeMap.set(filePath, original);
249
+ async function extractTSFile(filePath) {
250
+ const code = fs.readFileSync(filePath, 'utf8');
236
251
  const importRegex = /^(?:\s*import\s[^;]+;\s*)+/m;
237
- const importMatch = original.match(importRegex);
252
+ const importMatch = code.match(importRegex);
238
253
  const importSection = importMatch ? importMatch[0] : '';
239
- const cssCreateSection = extractCssCreate(original);
240
- const propsMatches = extractCssProps(original);
254
+ const cssCreateSection = extractCssCreate(code);
255
+ const cssGlobalSection = extractCssGlobal(code);
256
+ const propsMatches = extractCssProps(code);
241
257
  const calls = propsMatches
242
258
  .filter(Boolean)
243
259
  .map((call) => `${call};`)
@@ -245,10 +261,16 @@ async function extractAndInjectStyleProps(filePath) {
245
261
  let finalCode = '';
246
262
  if (importSection)
247
263
  finalCode += importSection + '\n';
264
+ if (cssGlobalSection)
265
+ finalCode += cssGlobalSection + '\n';
248
266
  if (cssCreateSection)
249
267
  finalCode += cssCreateSection + '\n';
250
268
  finalCode += calls;
251
- 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;
252
274
  }
253
275
  async function restoreAllOriginals() {
254
276
  for (const [originalPath, genPath] of generatedTsMap.entries()) {
@@ -257,10 +279,6 @@ async function restoreAllOriginals() {
257
279
  }
258
280
  }
259
281
  generatedTsMap.clear();
260
- for (const [filePath, backup] of originalCodeMap.entries()) {
261
- fs.writeFileSync(filePath, backup, 'utf8');
262
- }
263
- originalCodeMap.clear();
264
282
  }
265
283
  process.on('uncaughtException', async (error) => {
266
284
  console.error('Uncaught Exception:', error);
@@ -273,8 +291,7 @@ process.on('unhandledRejection', async (reason, promise) => {
273
291
  process.exit(1);
274
292
  });
275
293
  module.exports = {
276
- extractAndInjectStyleProps,
294
+ extractTSFile,
277
295
  restoreAllOriginals,
278
296
  extractVueAndSvelte,
279
- originalCodeMap,
280
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.8",
3
+ "version": "0.14.9",
4
4
  "description": "A faster compiler for Plumeria",
5
5
  "keywords": [
6
6
  "css",