@shijiu/jsview 1.9.914 → 1.9.921

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.
@@ -11,32 +11,87 @@ const path = require('path');
11
11
  const postCssJs = require('postcss-js');
12
12
  const styleFormat = require("./jsview-style-format");
13
13
 
14
+ const compilerSfc = require("./compiler-sfc.cjs");
15
+ const processorLangList = [
16
+ 'less',
17
+ 'sass',
18
+ 'scss',
19
+ 'styl',
20
+ 'stylus',
21
+ ];
22
+
14
23
  const cachedCssStyles = {};
15
24
 
16
25
  function ensureSfcDescriptor(descriptor) {
17
26
  try {
18
27
  ensureSfcDescriptorImpl(descriptor)
28
+
19
29
  } catch (exception) {
20
- console.error("Failed to call ensureSfcDescriptor()! Exception:\n", exception);
30
+ console.error("JsView Error: Failed to call ensureSfcDescriptor()! Exception:\n", exception);
21
31
  }
22
32
  }
23
33
 
24
34
  function compileCssToJs(sfc, options) {
25
35
  try {
26
- const styleJsFilePath = getStyleJsFilePath(sfc.filename);
27
- return `\nimport "${styleJsFilePath}";`
28
- // return compileCssToJsImpl(sfc, options)
36
+ // 必须在此处parse css,否则npm run build时进入不到js代码中。
37
+ // const styleJsFilePath = getStyleJsFilePath(sfc.filename);
38
+ // return `\nimport "${styleJsFilePath}";`
39
+
40
+ const compiledJsContent = compileCssToJsImpl(sfc, options)
41
+ return compiledJsContent;
29
42
  } catch (exception) {
30
- console.error("Failed to call compileCssToJs()! Exception:\n", exception);
43
+ console.error("JsView Error: Failed to call compileCssToJs()! Exception:\n", exception);
31
44
  }
32
45
  }
33
46
 
34
- function compileAndSaveImportedNode(styleFilePath, styleNodes) {
35
- try {
36
- return compileAndSaveImportedNodeImpl(styleFilePath, styleNodes)
37
- } catch (exception) {
38
- console.error("Failed to call compileAndSaveImportedNode()! Exception:\n", exception);
47
+ function compileCssToJsImpl(sfc, options) {
48
+ let cachedStyleImports = [];
49
+ let cachedStyleSheets = {};
50
+ for (const style of sfc.styles) {
51
+ if(!!style.module) {
52
+ let errMsg = "JsView Error: style module is not released by Vue3!\n";
53
+ errMsg += style;
54
+ console.error(errMsg + " errors =", errors);
55
+ throw new Error(errMsg)
56
+ }
57
+
58
+ const compileStyleOptions = {
59
+ filename: sfc.filename,
60
+ id: `data-v-${options.id}`,
61
+ // map: null,
62
+ trim: true,
63
+ isProd: options.isProd,
64
+
65
+ source: style.content,
66
+ scoped: style.scoped,
67
+ modules: style.module,
68
+
69
+ preprocessLang: style.attrs.lang,
70
+ };
71
+
72
+ if(!!style.attrs.src) { // 处理<style [scoped] src="xxx">
73
+ const baseDir = path.dirname(sfc.filename);
74
+ const cssSrcPath = path.resolve(baseDir, style.attrs.src);
75
+ compileStyleOptions.source = fs.readFileSync(cssSrcPath, "utf8");
76
+ }
77
+
78
+ const { rawResult, errors } = compilerSfc.compileStyle(compileStyleOptions)
79
+ if(errors.length) {
80
+ const errMsg = "JsView Error: Failed to compile style when convert css to js!";
81
+ console.error(errMsg + " errors =", errors);
82
+ throw new Error(errMsg)
83
+ }
84
+
85
+ const {styleImports, styleSheets } = compileAndSaveImportedNode(rawResult.result.root.nodes);
86
+ cachedStyleImports = [...cachedStyleImports, styleImports];
87
+ cachedStyleSheets = { ...cachedStyleSheets, ...styleSheets };
39
88
  }
89
+
90
+ const styleJsContents = serializeStyleJsFile(cachedStyleImports, cachedStyleSheets);
91
+
92
+ // console.log("jsview-css-to-js.compileCssToJsImpl() styleJsContents=", styleJsContents);
93
+
94
+ return styleJsContents;
40
95
  }
41
96
 
42
97
  function ensureSfcDescriptorImpl(descriptor) {
@@ -113,8 +168,9 @@ function check(expr, source, errMsg) {
113
168
  }
114
169
  }
115
170
 
116
- function compileAndSaveImportedNodeImpl(styleFilePath, styleNodes) {
117
- const { styleImports, styleSheets } = deserializeStyleJsFile(styleFilePath);
171
+ function compileAndSaveImportedNode(styleNodes) {
172
+ let styleImports = [];
173
+ let styleSheets = {};
118
174
 
119
175
  styleNodes.forEach(node => {
120
176
  if (node.type === "comment"
@@ -169,61 +225,34 @@ function compileAndSaveImportedNodeImpl(styleFilePath, styleNodes) {
169
225
  }
170
226
  })
171
227
 
172
- // console.log('jsview-css-to-js.compileAndSaveImportedNodeImpl() styleFilePath=', styleFilePath, ', styleSheets=', styleSheets);
173
- serializeStyleJsFile(styleFilePath, { styleImports, styleSheets });
174
- }
175
-
176
- function deserializeStyleJsFile(styleFilePath) {
177
- const styleJsFilePath = getStyleJsFilePath(styleFilePath);
178
-
179
- let styleImports = [];
180
- let styleSheets = {};
181
-
182
- let styleJsContent = fs.readFileSync(styleJsFilePath, "utf8");
183
- if (styleJsContent) {
184
- const styleSheetsFlags = getStyleSheetsDeclaredFlags();
185
- const styleImportsContent = styleJsContent.substring(0, styleJsContent.indexOf(styleSheetsFlags.start));
186
- let styleSheetsContent = styleJsContent.substring(0, styleJsContent.indexOf(styleSheetsFlags.end));
187
- styleSheetsContent = styleSheetsContent.substring(styleJsContent.indexOf(styleSheetsFlags.start) + styleSheetsFlags.start.length);
188
-
189
- styleImports = styleImportsContent.split('\n');
190
- styleSheets = JSON.parse(styleSheetsContent.length != 0 ? styleSheetsContent : '{}');
191
- }
192
-
193
-
228
+ // console.log('jsview-css-to-js.compileAndSaveImportedNode() styleSheets=', styleImports, styleSheets);
194
229
  return {
195
230
  styleImports,
196
231
  styleSheets,
197
232
  }
198
233
  }
199
234
 
200
- function serializeStyleJsFile(styleFilePath, { styleImports, styleSheets }) {
201
- const styleJsFilePath = getStyleJsFilePath(styleFilePath);
235
+ function serializeStyleJsFile(styleImports, styleSheets) {
236
+ if (styleImports.length == 0
237
+ && Object.keys(styleSheets).length === 0) {
238
+ return "";
239
+ }
202
240
 
203
- const styleSheetsFlags = getStyleSheetsDeclaredFlags();
204
241
  const styleImportsContent = styleImports.join('\n');
205
242
  const styleSheetsContent = JSON.stringify(styleSheets);
206
243
 
207
- let styleJsContent = "";
244
+ let styleJsContent = "\n";
208
245
  styleJsContent += styleImportsContent;
209
- styleJsContent += styleSheetsFlags.start;
246
+ styleJsContent += "\nconst styleSheetsContent = ";
210
247
  styleJsContent += styleSheetsContent;
211
- styleJsContent += styleSheetsFlags.end;
248
+ styleJsContent += ";\nwindow.JsvCode.Dom.DeclareStyleSheets(styleSheetsContent);";
212
249
 
213
- // 保存到js文件并import到script中
214
- fs.writeFileSync(styleJsFilePath, styleJsContent, "utf8");
215
- }
216
-
217
- function getStyleSheetsDeclaredFlags() {
218
- return {
219
- start: "\nconst styleSheetsContent = ",
220
- end: ";\nwindow.JsvCode.Dom.DeclareStyleSheets(styleSheetsContent);",
221
- }
250
+ return styleJsContent;
222
251
  }
223
252
 
224
253
  function getStyleJsCacheDir() {
225
254
  const nodeModulesDir = path.resolve(process.cwd(), 'node_modules');
226
- const cacheDir = path.resolve(nodeModulesDir, '@shijiu', '.cache', 'css-to-js'); // 不能放在.vite文件夹,否则有网络缓存,文件改变后不更新
255
+ const cacheDir = path.resolve(nodeModulesDir, '.vite', 'jsview', 'css-to-js'); // 不能放在.vite文件夹,否则有网络缓存,文件改变后不更新
227
256
  if (!fs.existsSync(cacheDir)) {
228
257
  fs.mkdirSync(cacheDir, { recursive: true });
229
258
  }
@@ -236,7 +265,7 @@ function getStyleJsFilePath(styleFilePath) {
236
265
  const cacheDir = getStyleJsCacheDir()
237
266
 
238
267
  const styleFilePathHash = crypto.createHash('md5').update(styleFilePath).digest('hex').substring(0, 8);
239
- const styleJsFileName = styleFilePathHash + '.' + path.basename(styleFilePath) + '.js';
268
+ const styleJsFileName = path.basename(styleFilePath) + '.' + styleFilePathHash + '.js';
240
269
  const styleJsFilePath = path.join(cacheDir, styleJsFileName);
241
270
  if (!fs.existsSync(styleJsFilePath)) {
242
271
  fs.closeSync(fs.openSync(styleJsFilePath, 'w'));
@@ -345,7 +374,12 @@ function parsePostCssJs(node, selector, declarations) {
345
374
  function parseUnscopedStyleSheets(node, selector, declarations, scopedId) {
346
375
  let styleRules = {};
347
376
 
348
- for (const [name, value] of Object.entries(declarations)) {
377
+ for (let [name, value] of Object.entries(declarations)) {
378
+ // 处理var变量中如果存在'a.b'的写法,被翻译成'a\.b'的问题。
379
+ if (typeof (value) == 'string' && value.startsWith('var(--')) {
380
+ value = value.replaceAll('\\', '');
381
+ }
382
+
349
383
  if (name.startsWith('@keyframes')) { // keyframes解嵌套处理
350
384
  check(false, node.source.input.css, "JsView Error: Nested keyframes is not supported!");
351
385
  delete declarations[name];
@@ -370,4 +404,4 @@ function parseUnscopedStyleSheets(node, selector, declarations, scopedId) {
370
404
 
371
405
  exports.ensureSfcDescriptor = ensureSfcDescriptor;
372
406
  exports.compileCssToJs = compileCssToJs;
373
- exports.compileAndSaveImportedNode = compileAndSaveImportedNode;
407
+ // exports.compileAndSaveImportedNode = compileAndSaveImportedNode;
@@ -116,21 +116,21 @@ class StyleFormatter {
116
116
  case StyleName.Text.TextAlign:
117
117
  case StyleName.Text.TextShadow:
118
118
  case StyleName.Text.TextOverflow:
119
+ case StyleName.Text.Transform:
120
+ case StyleName.Text.TransformOrigin:
121
+ case StyleName.Text.TransformStyle:
119
122
  case StyleName.Text.WebkitTextStroke:
120
123
  case StyleName.Text.WhiteSpace:
121
124
  case StyleName.Text.WordWrap:
122
125
  formattedValue = StyleFormatter.#GetTextValue(propName, propValue);
123
126
  break;
124
127
  case StyleName.Motion.Animation:
125
- case StyleName.Motion.Transform:
126
- case StyleName.Motion.TransformOrigin:
127
- case StyleName.Motion.TransformStyle:
128
128
  case StyleName.Motion.Transition:
129
129
  formattedValue = StyleFormatter.#GetMotionValue(propName, propValue);
130
130
  break;
131
131
  default:
132
132
  // 返回未处理的值
133
- console.warn("JsView Warn: Unimplemented style property " + propName);
133
+ console.warn("JsView Warn: Unimplemented style property [" + propName + "]");
134
134
  break;
135
135
  }
136
136
 
@@ -47,6 +47,9 @@ class StyleName {
47
47
  static TextAlign = "textAlign";
48
48
  static TextOverflow = "textOverflow";
49
49
  static TextShadow = "textShadow";
50
+ static Transform = "transform";
51
+ static TransformOrigin = "transformOrigin";
52
+ static TransformStyle = "transformStyle";
50
53
  static WebkitTextStroke = "WebkitTextStroke";
51
54
  static WhiteSpace = "whiteSpace";
52
55
  static WordWrap = "wordWrap";