@darkchest/wck 0.0.16 → 0.0.17

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/README.md CHANGED
@@ -30,6 +30,7 @@
30
30
 
31
31
  - ✅ 支持props属性(注意vue中允许使用大驼峰和小驼峰定义属性, 但是web-component组件属性只允许小写, 所以vue中定义的属性在编译成web-component组件时时会转成小写和中划线连字符形式的属性并进行映射)
32
32
  - ✅ 支持$el, $parent, $children, $root属性
33
+ - ✅ 支持组件内querySelector(selector)查询组件内dom元素(因为web组件内的dom都隔离在shadowDom中)
33
34
  - ✅ 支持$emit事件
34
35
  - ✅ 支持methods方法
35
36
  - ✅ 支持compputed属性
@@ -37,6 +38,7 @@
37
38
  - ✅ 支持slot匿名和具名插槽
38
39
  - ✅ 支持mounted/onMounted, destroyed/unmounted/onUnmounted生命周期
39
40
  - ✅ 支持scss样式
41
+ - [] 支持script setup语法(规划中...)
40
42
 
41
43
  ### 关于props大小写问题
42
44
  由于vue中定义props是允许大小写字母的.而web-component组件的attributes只允许小写字母(包含中划线), 所以当我们在vue组件中定义一个带有大写字母的属性时(例如appTitle), 在@darkchest/wck插件将它转成web-component组件后, 插件会自动在web-component组件中声明apptitle, app-title两种形式(纯小写形式和小写中划线形式)的属性来映射appTitle这个vue组件属性, 所以当我们使用web-component组件时, 可以直接在html上使用全小写的属性名来设置组件的默认属性值:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkchest/wck",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "@darkchest/wck是一个通过将 Vue 单文件组件 (SFC) 转换为通用的web-component组件的解决方案。",
5
5
  "private": false,
6
6
  "main": "src/index.js",
@@ -55,6 +55,9 @@ export default class extends HTMLElement {
55
55
  get $root(){
56
56
  return that.$root;
57
57
  },
58
+ querySelector(selector){
59
+ return that.querySelector(selector);
60
+ },
58
61
  $set(obj, key, value){
59
62
  obj[key] = value;
60
63
  },
@@ -147,6 +150,10 @@ export default class extends HTMLElement {
147
150
  return root;
148
151
  }
149
152
 
153
+ querySelector(selector){
154
+ return this.shadowRoot.querySelector(selector);
155
+ }
156
+
150
157
  _typeChcker(val, type){
151
158
  // todo: 类型检查
152
159
  return val;
@@ -92,7 +92,7 @@ export const compileExportDefault = (ast) => {
92
92
  if(t.isArrayExpression(valueNode)){
93
93
  return {
94
94
  name,
95
- type: valueNode.elements.map(item => t.isIdentifier(item) ? "${item.name}" : null).filter(item => !!item),
95
+ type: valueNode.elements.map(item => t.isIdentifier(item) ? `"${item.name}"` : null).filter(item => !!item),
96
96
  default: null,
97
97
  }
98
98
  }
@@ -144,21 +144,26 @@ export const compileExportDefault = (ast) => {
144
144
  case 'props':
145
145
  if (t.isObjectProperty(propNode)){
146
146
  const toLowerCase = (str) => str.toLowerCase();
147
- const toSnakeCase = (str) => str.replace(/([A-Z])/g, '-$1').replace(/^[-_]*/i, '').toLowerCase();
147
+ const toSnakeCase = (str) => str.replace(/([A-Z])/g, '-$1').toLowerCase();
148
148
  scriptData.attributes = propNode.value.properties?.map?.(item => `"${toLowerCase(item.key.name)}"`).concat(propNode.value.properties?.map?.(item => `"${toSnakeCase(item.key.name)}"`));
149
149
  propNode.value.properties?.forEach?.(node => {
150
150
  const ret = getNodePropData(node);
151
151
  if(ret){
152
- const type = `[${ret.type.join(', ')}]`
153
- scriptData.types.push(`"${ret.name}": { type: ${type}, attr: "${ret.name}" }`);
154
- scriptData.types.push(`"${toLowerCase(ret.name)}": { type: ${type}, attr: "${ret.name}" }`);
155
- scriptData.types.push(`"${toSnakeCase(ret.name)}": { type: ${type}, attr: "${ret.name}" }`);
156
- if(ret.type?.indexOf?.('Object') >= 0 || ret.type?.indexOf?.('Array') >= 0 || ret.type?.indexOf?.('Function') >= 0){
152
+ const type = `${ret.type.join(', ')}`
153
+ scriptData.types.push(`"${ret.name}": { type: [${type}], attr: "${ret.name}" }`);
154
+ if(toLowerCase(ret.name) != ret.name){
155
+ scriptData.types.push(`"${toLowerCase(ret.name)}": { type: [${type}], attr: "${ret.name}" }`);
156
+ }
157
+ if(toSnakeCase(ret.name) != ret.name){
158
+ scriptData.types.push(`"${toSnakeCase(ret.name)}": { type: [${type}], attr: "${ret.name}" }`);
159
+ }
160
+ if(type?.indexOf?.('Object') >= 0 || type?.indexOf?.('Array') >= 0 || type?.indexOf?.('Function') >= 0){
157
161
  // 只要类型中存在复合类型, 则需要通过initializeAttribute函数去赋值.
158
162
  scriptData.config.push(`${ret.name}: null`);
159
163
  if(ret?.default?.indexOf?.('function(){') === 0){
160
164
  // 如果默认值类型是函数, 则放在initializeAttribute中执行.
161
165
  scriptData.initializeAttribute.push(`this.setAttribute("${ret.name}", (${ret.default})());`);
166
+ scriptData.initializeAttribute.push(`this.${ret.name} = (${ret.default})();`);
162
167
  }
163
168
  }else{
164
169
  scriptData.config.push(`${ret.name}: ${ret.default}`);
@@ -236,7 +241,7 @@ export const compileExportDefault = (ast) => {
236
241
  break;
237
242
  case 'watch':
238
243
  if(t.isObjectProperty(propNode)){
239
- const toSnakeCase = (str) => str.replace(/([A-Z])/g, '_$1').replace(/[.\s]+/g, '_').replace(/^[-_]*/i, '').toLowerCase();
244
+ const toSnakeCase = (str) => str.replace(/([A-Z])/g, '_$1').replace(/[.\s]+/g, '_').toLowerCase();
240
245
  propNode.value.properties?.map(node => {
241
246
  const name = t.isStringLiteral(node.key) ? node.key.value : node.key.name;
242
247
  const snaked = toSnakeCase(name);
@@ -10,7 +10,7 @@ export const compileStyles = (styles) => {
10
10
  let content = s.content;
11
11
  try {
12
12
  if (s.lang === 'scss') {
13
- const result = sass.compileString(content, { charset: false });
13
+ const result = sass.compileString(content, { charset: false, style: "compressed" });
14
14
  content = result.css.toString().replace(/\:+\s*root\s*\{/gi, ':host {');
15
15
  }
16
16
  if (s.scoped) {