glass-easel-miniprogram-adapter 0.1.0
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 +49 -0
- package/dist/glass_easel_miniprogram_adapter.d.ts +1 -0
- package/dist/glass_easel_miniprogram_adapter.js +2 -0
- package/dist/glass_easel_miniprogram_adapter.js.map +1 -0
- package/dist/types/src/backend.d.ts +51 -0
- package/dist/types/src/backend.d.ts.map +1 -0
- package/dist/types/src/behavior.d.ts +35 -0
- package/dist/types/src/behavior.d.ts.map +1 -0
- package/dist/types/src/component.d.ts +160 -0
- package/dist/types/src/component.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +58 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/selector_query.d.ts +98 -0
- package/dist/types/src/selector_query.d.ts.map +1 -0
- package/dist/types/src/space.d.ts +369 -0
- package/dist/types/src/space.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +77 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/utils.d.ts +2 -0
- package/dist/types/src/utils.d.ts.map +1 -0
- package/dist/types/tests/base/env.d.ts +3 -0
- package/dist/types/tests/base/env.d.ts.map +1 -0
- package/dist/types/tests/data_update.test.d.ts +2 -0
- package/dist/types/tests/data_update.test.d.ts.map +1 -0
- package/dist/types/tests/env.test.d.ts +2 -0
- package/dist/types/tests/env.test.d.ts.map +1 -0
- package/dist/types/tests/selector.test.d.ts +2 -0
- package/dist/types/tests/selector.test.d.ts.map +1 -0
- package/dist/types/tests/space.test.d.ts +2 -0
- package/dist/types/tests/space.test.d.ts.map +1 -0
- package/dist/types/tests/trait_behavior.test.d.ts +2 -0
- package/dist/types/tests/trait_behavior.test.d.ts.map +1 -0
- package/jest.config.js +13 -0
- package/package.json +32 -0
- package/src/backend.ts +106 -0
- package/src/behavior.ts +101 -0
- package/src/component.ts +476 -0
- package/src/index.ts +82 -0
- package/src/selector_query.ts +356 -0
- package/src/space.ts +1514 -0
- package/src/types.ts +85 -0
- package/src/utils.ts +3 -0
- package/tests/base/env.ts +20 -0
- package/tests/data_update.test.ts +92 -0
- package/tests/env.test.ts +141 -0
- package/tests/selector.test.ts +407 -0
- package/tests/space.test.ts +953 -0
- package/tests/trait_behavior.test.ts +141 -0
- package/tsconfig.json +11 -0
- package/webpack.config.js +78 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import * as glassEasel from 'glass-easel'
|
|
2
|
+
import { tmpl } from './base/env'
|
|
3
|
+
import {
|
|
4
|
+
MiniProgramEnv,
|
|
5
|
+
} from '../src'
|
|
6
|
+
|
|
7
|
+
const domHtml = (elem: glassEasel.Element): string => {
|
|
8
|
+
const domElem = elem.getBackendElement() as unknown as Element
|
|
9
|
+
return domElem.innerHTML
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('trait behavior', () => {
|
|
13
|
+
test('implement trait behavior', () => {
|
|
14
|
+
const env = new MiniProgramEnv()
|
|
15
|
+
const codeSpace = env.createCodeSpace('', true)
|
|
16
|
+
|
|
17
|
+
codeSpace.addComponentStaticConfig('path/to/comp', {})
|
|
18
|
+
codeSpace.addCompiledTemplate('path/to/comp', tmpl(`
|
|
19
|
+
<div>{{r}}</div>
|
|
20
|
+
`))
|
|
21
|
+
codeSpace.componentEnv('path/to/comp', ({ Component, Behavior }) => {
|
|
22
|
+
const trait = Behavior.trait<
|
|
23
|
+
{ add(a: number, b: number): number },
|
|
24
|
+
{ minus(a: number, b: number): number }
|
|
25
|
+
>(({ add }) => ({
|
|
26
|
+
minus(a, b) {
|
|
27
|
+
return add(a, -b)
|
|
28
|
+
},
|
|
29
|
+
}))
|
|
30
|
+
Component()
|
|
31
|
+
.data(() => ({
|
|
32
|
+
r: 0,
|
|
33
|
+
}))
|
|
34
|
+
.init(({
|
|
35
|
+
self,
|
|
36
|
+
setData,
|
|
37
|
+
lifetime,
|
|
38
|
+
implement,
|
|
39
|
+
}) => {
|
|
40
|
+
implement(trait, { add: (a, b) => a + b })
|
|
41
|
+
lifetime('attached', () => {
|
|
42
|
+
const r = self.traitBehavior(trait)!.minus(5, 3)
|
|
43
|
+
setData({ r })
|
|
44
|
+
})
|
|
45
|
+
expect(self.hasBehavior(trait)).toBe(true)
|
|
46
|
+
})
|
|
47
|
+
.register()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const ab = env.associateBackend()
|
|
51
|
+
const root = ab.createRoot('body', codeSpace, 'path/to/comp')
|
|
52
|
+
glassEasel.Element.pretendAttached(root.getComponent())
|
|
53
|
+
expect(domHtml(root.getComponent())).toBe('<div>2</div>')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('trait behavior in relations', () => {
|
|
57
|
+
const env = new MiniProgramEnv()
|
|
58
|
+
const codeSpace = env.createCodeSpace('', true)
|
|
59
|
+
|
|
60
|
+
const childTrait = codeSpace.traitBehavior<{
|
|
61
|
+
setA(a: number): void,
|
|
62
|
+
}>()
|
|
63
|
+
const parentTrait = codeSpace.traitBehavior()
|
|
64
|
+
|
|
65
|
+
codeSpace.addComponentStaticConfig('child/list', {
|
|
66
|
+
component: true,
|
|
67
|
+
})
|
|
68
|
+
codeSpace.addCompiledTemplate('child/list', tmpl(`
|
|
69
|
+
<div>{{count}}</div><slot />
|
|
70
|
+
`))
|
|
71
|
+
// eslint-disable-next-line arrow-body-style
|
|
72
|
+
codeSpace.componentEnv('child/list', ({ Component }) => {
|
|
73
|
+
return Component()
|
|
74
|
+
.data(() => ({
|
|
75
|
+
count: 0,
|
|
76
|
+
}))
|
|
77
|
+
.implement(parentTrait, {})
|
|
78
|
+
.init(({ relation, setData }) => {
|
|
79
|
+
const childRel = relation({
|
|
80
|
+
type: 'child',
|
|
81
|
+
target: childTrait,
|
|
82
|
+
linked(child) {
|
|
83
|
+
// eslint-disable-next-line no-use-before-define
|
|
84
|
+
expect(child.asInstanceOf(itemDef)).not.toBe(null)
|
|
85
|
+
setData({ count: this.data.count + 1 })
|
|
86
|
+
childRel.listAsTrait().forEach((c, index) => {
|
|
87
|
+
c.setA(index + 2)
|
|
88
|
+
})
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
.register()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
codeSpace.addComponentStaticConfig('child/item', {
|
|
96
|
+
component: true,
|
|
97
|
+
})
|
|
98
|
+
codeSpace.addCompiledTemplate('child/item', tmpl('{{a}}'))
|
|
99
|
+
// eslint-disable-next-line arrow-body-style
|
|
100
|
+
const itemDef = codeSpace.componentEnv('child/item', ({ Component }) => {
|
|
101
|
+
return Component()
|
|
102
|
+
.data(() => ({
|
|
103
|
+
a: 0,
|
|
104
|
+
}))
|
|
105
|
+
.init(({ implement, relation, setData }) => {
|
|
106
|
+
implement(childTrait, {
|
|
107
|
+
setA(a: number) {
|
|
108
|
+
setData({ a })
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
relation({
|
|
112
|
+
type: 'parent',
|
|
113
|
+
target: parentTrait,
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
.register()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
codeSpace.addComponentStaticConfig('path/to/comp', {
|
|
120
|
+
usingComponents: {
|
|
121
|
+
list: '/child/list',
|
|
122
|
+
item: '/child/item',
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
codeSpace.addCompiledTemplate('path/to/comp', tmpl(`
|
|
126
|
+
<list>
|
|
127
|
+
<item />
|
|
128
|
+
<item />
|
|
129
|
+
</list>
|
|
130
|
+
`))
|
|
131
|
+
codeSpace.componentEnv('path/to/comp', ({ Component }) => {
|
|
132
|
+
Component().register()
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
const ab = env.associateBackend()
|
|
136
|
+
const root = ab.createRoot('body', codeSpace, 'path/to/comp')
|
|
137
|
+
glassEasel.Element.pretendAttached(root.getComponent())
|
|
138
|
+
expect(domHtml(root.getComponent()))
|
|
139
|
+
.toBe('<list><div>2</div><item>2</item><item>3</item></list>')
|
|
140
|
+
})
|
|
141
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const webpack = require('webpack')
|
|
3
|
+
const { RawSource } = require('webpack-sources')
|
|
4
|
+
|
|
5
|
+
const jobs = []
|
|
6
|
+
let minimize = null
|
|
7
|
+
let dev = false
|
|
8
|
+
const args = process.env['GLASS_EASEL_ARGS'] || ''
|
|
9
|
+
args.split(' ').forEach((arg) => {
|
|
10
|
+
if (!arg) return
|
|
11
|
+
if (arg[0] === '-') {
|
|
12
|
+
if (arg === '--minimize') minimize = true
|
|
13
|
+
else if (arg === '--no-minimize') minimize = false
|
|
14
|
+
else if (arg === '--dev') dev = true
|
|
15
|
+
} else {
|
|
16
|
+
jobs.push(arg)
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const mode = dev ? 'development' : 'production'
|
|
21
|
+
|
|
22
|
+
const rules = [
|
|
23
|
+
{
|
|
24
|
+
test: /\.ts$/,
|
|
25
|
+
loader: 'ts-loader',
|
|
26
|
+
exclude: /node_modules/,
|
|
27
|
+
},
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
const optimization = {
|
|
31
|
+
minimize: minimize === null ? undefined : minimize,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const resolve = {
|
|
35
|
+
extensions: [ '.ts', '.js' ],
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const performance = {
|
|
39
|
+
maxEntrypointSize: 512000,
|
|
40
|
+
maxAssetSize: 512000,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class BundleDeclarationEntrancePlugin {
|
|
44
|
+
apply(compiler) {
|
|
45
|
+
compiler.hooks.compilation.tap('BundleDeclarationEntrancePlugin', (compilation, compilationParams) => {
|
|
46
|
+
compilation.hooks.additionalAssets.tapPromise('ExtraAssetPlugin', async () => {
|
|
47
|
+
Object.keys(compilation.assets).forEach((file) => {
|
|
48
|
+
if (file.endsWith('.js')) {
|
|
49
|
+
const dtsFile = file.replace(/\.js$/, '.d.ts')
|
|
50
|
+
compilation.assets[dtsFile] = new RawSource('export * from "./types/src/index"')
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const globalPlugins = [new BundleDeclarationEntrancePlugin()]
|
|
59
|
+
|
|
60
|
+
module.exports = {
|
|
61
|
+
mode,
|
|
62
|
+
output: {
|
|
63
|
+
path: path.resolve(__dirname, 'dist'),
|
|
64
|
+
filename: 'glass_easel_miniprogram_adapter.js',
|
|
65
|
+
libraryTarget: 'commonjs2',
|
|
66
|
+
},
|
|
67
|
+
optimization,
|
|
68
|
+
devtool: 'source-map',
|
|
69
|
+
module: {
|
|
70
|
+
rules,
|
|
71
|
+
},
|
|
72
|
+
resolve,
|
|
73
|
+
externals: 'glass-easel',
|
|
74
|
+
performance,
|
|
75
|
+
plugins: [
|
|
76
|
+
...globalPlugins
|
|
77
|
+
],
|
|
78
|
+
}
|