@shijiu/jsview-vue 0.9.249 → 0.9.265

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.
@@ -1,274 +0,0 @@
1
- /*
2
- * QCode Added
3
- */
4
- 'use strict';
5
-
6
- Object.defineProperty(exports, '__esModule', { value: true });
7
-
8
- const compilerSfc = require("./compiler-sfc.cjs");
9
-
10
- var findCacheDir = _interopDefaultLegacy(require("find-cache-dir"));
11
- var fs = _interopDefaultLegacy(require('fs'));
12
- var hash = _interopDefaultLegacy(require('hash-sum'));
13
- var path = _interopDefaultLegacy(require('path'));
14
- var postCss = _interopDefaultLegacy(require('postcss'));
15
- var postCssJs = _interopDefaultLegacy(require('postcss-js'));
16
- var postCssImport = _interopDefaultLegacy(require('postcss-import-sync'));
17
-
18
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e['default'] : e; }
19
-
20
- const adaptPath = (path.posix || path);
21
-
22
- var cachedCssStyles = {}
23
-
24
- function ensureSfcDescriptor(descriptor) {
25
- // 如果css-style不存在,不做任何事
26
- if(descriptor.styles.length <= 0) {
27
- return;
28
- }
29
-
30
- // 为了将css-style植入到script中,script或scriptSetup必须存在一个
31
- if(!!descriptor.script
32
- || !!descriptor.scriptSetup) {
33
- return;
34
- }
35
-
36
- // 为了触发compiler-sfc.cjs.compileScript()
37
- // 如果script和scriptSetup都不存在,就做一个空的
38
- // console.log("JsView: insert a empty script.");
39
- descriptor.script = {
40
- type: 'script',
41
- content: "export default {}\n",
42
- loc: {
43
- source: "",
44
- start: { column: 0, line: 0, offset: 0 },
45
- end: { column: 0, line: 0, offset: 0}
46
- },
47
- attrs: {},
48
- map: {
49
- version: 3,
50
- sources: [],
51
- names: [],
52
- mappings: '',
53
- // file: null,
54
- sourceRoot: '',
55
- sourcesContent: []
56
- }
57
- };
58
- }
59
-
60
- function compileCssToJs(sfc, options) {
61
- if(sfc.styles.length <= 0) {
62
- return ""
63
- }
64
-
65
- let compileStyleOptions = {
66
- filename: sfc.filename,
67
- id: `data-v-${options.id}`,
68
- // map: null,
69
- trim: true,
70
- isProd: options.isProd,
71
-
72
- source: null,
73
- scoped: false,
74
- modules: false,
75
- };
76
- // let styleCodeContainer = new Set();
77
- let styleContentContainer = "";
78
- let styleImportContainer = "";
79
- sfc.styles.forEach(style => {
80
- if(!!style.module) {
81
- let errMsg = "JsView: style module is not released by Vue3!\n";
82
- errMsg += style;
83
- console.error(errMsg + " errors =", errors);
84
- throw new Error(errMsg)
85
- }
86
- compileStyleOptions.source = style.content
87
- if(!!style.attrs.src) { // 处理<style [scoped] src="xxx">
88
- const baseDir = path.dirname(sfc.filename);
89
- const cssSrcPath = path.resolve(baseDir, style.attrs.src);
90
- compileStyleOptions.source = fs.readFileSync(cssSrcPath, "utf8");
91
- }
92
- compileStyleOptions.scoped = style.scoped
93
- compileStyleOptions.modules = style.module
94
- const { rawResult, errors } = compilerSfc.compileStyle(compileStyleOptions)
95
- if(errors.length) {
96
- const errMsg = "JsView: Failed to compile style when convert css to js!";
97
- console.error(errMsg + " errors =", errors);
98
- throw new Error(errMsg)
99
- }
100
-
101
- const styleNodes = rawResult.result.root.nodes;
102
- styleNodes.forEach(node => {
103
- // console.log('node=', node.name)
104
-
105
- if(node.name === "import") {
106
- const styleInfo = compileImportNode(node);
107
- // styleCodeContainer.add(styleCode);
108
-
109
- checkSelectors(node, styleInfo.styleFilePath, styleInfo.styleSelectors); // 检测selector是否已经处理过
110
- styleImportContainer += `\nimport "${styleInfo.styleJsFilePath}";`
111
- } else if(node.name === "keyframes") {
112
- const styleFilePath = node.source.input.file;
113
- const styleSelectors = new Set([node.params]);
114
- const styleContent = compileKeyframesNode(node);
115
-
116
- checkSelectors(node, styleFilePath, styleSelectors); // 检测selector是否已经处理过
117
- styleContentContainer += styleContent;
118
- } else if(!!node.selector) {
119
- const styleFilePath = node.source.input.file;
120
- const styleSelectors = new Set([node.selector]);
121
- const styleContent = compileSelectorNode(node);
122
-
123
- checkSelectors(node, styleFilePath, styleSelectors); // 检测selector是否已经处理过
124
- styleContentContainer += styleContent;
125
- } else {
126
- check(false, node.source.input.css, "JsView Error: Unsupported css node.");
127
- }
128
- })
129
- })
130
-
131
- let jsvStyleSheets = styleImportContainer;
132
- jsvStyleSheets += "\nObject.assign(window.JsvCode.Dom.StyleSheets, {";
133
- jsvStyleSheets += styleContentContainer;
134
- jsvStyleSheets += "});\n";
135
-
136
- // 用于@import的css hotload,但是没有生效
137
- // styleCodeContainer.forEach(styleCode => {
138
- // jsvStyleSheets += styleCode + "\n";
139
- // });
140
-
141
- // console.log('jsview-css-to-js.compileCssToJs() jsvStyleSheets=' + jsvStyleSheets)
142
- return jsvStyleSheets;
143
- }
144
-
145
- function checkSelectors(node, styleFilePath, styleSelectors) {
146
- for(let cachedFilePath in cachedCssStyles) {
147
- if(cachedFilePath == styleFilePath) {
148
- continue;
149
- }
150
-
151
- const cachedSelectors = cachedCssStyles[cachedFilePath];
152
- for(let selector of cachedSelectors) {
153
- if(styleSelectors.has(selector)) { // 发现重复的selector
154
- let errMsg = "JsView Error: Multi defined CSS selector '" + selector + "' from " + node.source.input.file + ".\n";
155
- errMsg += "It has been defined in " + cachedFilePath + ".\n";
156
- check(false, node.source.input.css, errMsg);
157
- }
158
- }
159
- }
160
-
161
- // 添加到缓存
162
- let cachedSelectors = cachedCssStyles[styleFilePath];
163
- if(!cachedSelectors) {
164
- cachedSelectors = new Set()
165
- }
166
- cachedSelectors = new Set([...cachedSelectors, ...styleSelectors]);
167
- cachedCssStyles[styleFilePath] = cachedSelectors;
168
- }
169
-
170
- function check(expr, source, errMsg) {
171
- if(!expr) {
172
- errMsg += (" source is: \n" + source);
173
- console.error("\n" + errMsg);
174
- throw new Error(errMsg);
175
- }
176
- }
177
-
178
- function compileImportNode(node) {
179
- const name = node.name;
180
- check(name, node.source.input.css, "JsView Error: name is not found!");
181
-
182
- check(name === "import", node.source.input.css, "JsView Error: @import name is not found!");
183
-
184
- let result = null;
185
- try {
186
- const plugins = [].slice();
187
- plugins.push(postCssImport)
188
-
189
- const content = node.source.input.css.slice(node.source.start.offset, node.source.end.offset + 1);
190
- check(content.endsWith(";"), "JsView Error: Failed to parse @import, please end with ';'")
191
-
192
- const options = {
193
- from: node.source.input.file
194
- };
195
-
196
- result = postCss(plugins).process(content, options);
197
- } catch (e) {
198
- console.log('JsView Error: Failed to compile css import node.', e);
199
- throw e;
200
- }
201
-
202
- const sourceDir = adaptPath.dirname(node.source.input.file);
203
- let styleFileName = node.params.replace(/^["'](.+(?=["']$))["']$/, '$1')
204
- const styleFilePath = adaptPath.resolve(sourceDir, styleFileName);
205
- return compileAndSaveImportedNode(styleFilePath, result.root.nodes);
206
- }
207
-
208
- function compileAndSaveImportedNode(styleFilePath, styleNodes) {
209
- let styleSelectors = new Set();
210
- let styleContent = "";
211
-
212
- styleNodes.forEach(node => {
213
- if(!!node.selector) {
214
- styleContent += compileSelectorNode(node);
215
- styleSelectors.add(node.selector);
216
- } else {
217
- check(false, node.source.input.css, "JsView Error: Unsupported css node from import css file.");
218
- }
219
- })
220
-
221
- // 保存到js文件并import到script中
222
- const cacheDir = findCacheDir({
223
- name: 'jsview-dom', create: true
224
- });
225
- const styleJsFileName = adaptPath.basename(styleFilePath) + `.${hash(styleFilePath)}.js`;
226
- const styleJsFilePath = adaptPath.join(cacheDir, styleJsFileName);
227
-
228
- styleContent = "\nObject.assign(window.JsvCode.Dom.StyleSheets, {" + styleContent;
229
- styleContent += "});\n";
230
- fs.writeFileSync(styleJsFilePath, styleContent, "utf8");
231
-
232
- return {
233
- styleFilePath,
234
- styleSelectors,
235
- styleJsFilePath
236
- };
237
- }
238
-
239
- function compileKeyframesNode(node) {
240
- const name = node.name;
241
- check(name, node.source.input.css, "JsView Error: name is not found!");
242
-
243
- check(name === "keyframes", node.source.input.css, "JsView Error: @keyframes name is not found!");
244
-
245
- const content = node.source.input.css.slice(node.source.start.offset, node.source.end.offset + 1);
246
-
247
- let styleContent = "'" + node.params + "':'";
248
- styleContent += content.replace(/\n/g, " ");
249
- styleContent += "',";
250
-
251
- // console.log('jsview-css-to-js.compileKeyframesNode() return ' + styleContent);
252
- return styleContent
253
- }
254
-
255
- function compileSelectorNode(node) {
256
- const selector = node.selector;
257
- check(selector, node.source.input.css, "JsView Error: Selector is not found!");
258
-
259
- let errMsg = "JsView Error: Only class selector is supported!\n";
260
- errMsg += "JsView Error: Please use css like `.classname { property: value; }`";
261
- check(selector.startsWith('.'), node.source.input.css, errMsg);
262
-
263
- const declarations = postCssJs.objectify(node);
264
- let styleContent = "'" + selector.substr(1) + "':";
265
- styleContent += JSON.stringify(declarations);
266
- styleContent += ",";
267
-
268
- // console.log('jsview-css-to-js.compileSelectorNode() return ', styleContent);
269
- return styleContent;
270
- }
271
-
272
- exports.ensureSfcDescriptor = ensureSfcDescriptor;
273
- exports.compileCssToJs = compileCssToJs;
274
- exports.compileAndSaveImportedNode = compileAndSaveImportedNode;