@dcloudio/uni-cli-shared 2.0.2-4020920240930001 → 2.0.2-4040420241231001
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/components/ad-fullscreen-video.vue +2 -1
- package/components/ad-interstitial.vue +2 -1
- package/components/ad-rewarded-video.vue +4 -3
- package/components/ad.mixin.mp-weixin.js +245 -28
- package/components/uniad.vue +13 -2
- package/components/unicloud-db.vue +1 -1
- package/lib/platform.js +10 -4
- package/lib/preprocess.js +8 -8
- package/lib/uts/dataToEsm.js +154 -0
- package/lib/uts/uni_modules.js +52 -21
- package/lib/uts/uts-loader.js +152 -0
- package/lib/uts/uts-webpack-plugin.js +138 -0
- package/lib/uts/uts.js +240 -41
- package/license.md +1 -1
- package/package.json +3 -2
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
<view @click="_onclick">
|
|
3
3
|
<slot :options="options" :loading="loading" :error="errorMessage" />
|
|
4
4
|
<!-- #ifdef MP-WEIXIN -->
|
|
5
|
-
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
5
|
+
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
|
|
6
|
+
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
|
6
7
|
<!-- #endif -->
|
|
7
8
|
<!-- #ifdef MP-ALIPAY -->
|
|
8
9
|
<uniad-plugin class="uniad-plugin" :adpid="adpid" @create="_handleAdRef" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
<view @click="_onclick">
|
|
3
3
|
<slot :options="options" :loading="loading" :error="errorMessage" />
|
|
4
4
|
<!-- #ifdef MP-WEIXIN -->
|
|
5
|
-
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
5
|
+
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
|
|
6
|
+
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
|
6
7
|
<!-- #endif -->
|
|
7
8
|
<!-- #ifdef MP-ALIPAY -->
|
|
8
9
|
<uniad-plugin class="uniad-plugin" :adpid="adpid" @create="_handleAdRef" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<view @click="_onclick">
|
|
3
|
-
<slot :options="options" :loading="loading" :error="errorMessage" />
|
|
4
|
-
<!-- #ifdef MP-WEIXIN -->
|
|
5
|
-
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
3
|
+
<slot :options="options" :loading="loading" :error="errorMessage" />
|
|
4
|
+
<!-- #ifdef MP-WEIXIN -->
|
|
5
|
+
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
|
|
6
|
+
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
|
6
7
|
<!-- #endif -->
|
|
7
8
|
<!-- #ifdef MP-ALIPAY -->
|
|
8
9
|
<uniad-plugin class="uniad-plugin" :adpid="adpid" @create="_handleAdRef" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
|
@@ -1,9 +1,24 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
const adPlugin = requirePlugin('uni-ad')
|
|
3
|
+
|
|
1
4
|
const EventType = {
|
|
2
5
|
Load: 'load',
|
|
3
6
|
Close: 'close',
|
|
4
7
|
Error: 'error'
|
|
5
8
|
}
|
|
6
9
|
|
|
10
|
+
const AdType = {
|
|
11
|
+
Banner: 'banner',
|
|
12
|
+
RewardedVideo: 'rewardedVideo',
|
|
13
|
+
Interstitial: 'interstitial'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ProviderType = {
|
|
17
|
+
WeChat: 10018,
|
|
18
|
+
UserWeChat: 10017,
|
|
19
|
+
ShanHu: 10020
|
|
20
|
+
}
|
|
21
|
+
|
|
7
22
|
export default {
|
|
8
23
|
props: {
|
|
9
24
|
options: {
|
|
@@ -38,13 +53,21 @@ export default {
|
|
|
38
53
|
data () {
|
|
39
54
|
return {
|
|
40
55
|
loading: false,
|
|
56
|
+
userwx: false,
|
|
57
|
+
userUnitId: '',
|
|
58
|
+
customFullscreen: '',
|
|
59
|
+
wxchannel: false,
|
|
41
60
|
errorMessage: null
|
|
42
61
|
}
|
|
43
62
|
},
|
|
44
63
|
created () {
|
|
45
64
|
this._ad = null
|
|
46
|
-
|
|
47
|
-
|
|
65
|
+
this._loading = false
|
|
66
|
+
this._wxRewardedAd = null
|
|
67
|
+
this._wxInterstitialAd = null
|
|
68
|
+
this._providerType = ProviderType.ShanHu
|
|
69
|
+
if (this.preload && this._canCreateAd()) {
|
|
70
|
+
this.load()
|
|
48
71
|
}
|
|
49
72
|
},
|
|
50
73
|
methods: {
|
|
@@ -53,35 +76,22 @@ export default {
|
|
|
53
76
|
return
|
|
54
77
|
}
|
|
55
78
|
this._startLoading()
|
|
79
|
+
if (this._providerType === ProviderType.ShanHu) {
|
|
80
|
+
} else if (this._providerType === ProviderType.WeChat) {
|
|
81
|
+
this.selectComponent('.uniad-plugin-wx').load()
|
|
82
|
+
} else if (this._providerType === ProviderType.UserWeChat) {
|
|
83
|
+
this._loadWxAd()
|
|
84
|
+
}
|
|
56
85
|
},
|
|
57
86
|
|
|
58
|
-
show () {
|
|
87
|
+
show (e) {
|
|
59
88
|
this.errorMessage = null
|
|
60
|
-
this.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
iv,
|
|
67
|
-
version,
|
|
68
|
-
expireTime
|
|
69
|
-
}) => {
|
|
70
|
-
this._ad.show({
|
|
71
|
-
userId: this.urlCallback.userId || '',
|
|
72
|
-
extra: this.urlCallback.extra || '',
|
|
73
|
-
encryptKey,
|
|
74
|
-
iv,
|
|
75
|
-
version,
|
|
76
|
-
expireTime
|
|
77
|
-
})
|
|
78
|
-
},
|
|
79
|
-
fail: (err) => {
|
|
80
|
-
this._dispatchEvent(EventType.Error, err)
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
} else {
|
|
84
|
-
this._ad.show()
|
|
89
|
+
if (this._providerType === ProviderType.ShanHu) {
|
|
90
|
+
this._showAdInPlugin(this.selectComponent('.uniad-plugin'))
|
|
91
|
+
} else if (this._providerType === ProviderType.WeChat) {
|
|
92
|
+
this._showAdInPlugin(this.selectComponent('.uniad-plugin-wx'))
|
|
93
|
+
} else if (this._providerType === ProviderType.UserWeChat) {
|
|
94
|
+
this._showWxAd(e)
|
|
85
95
|
}
|
|
86
96
|
},
|
|
87
97
|
|
|
@@ -148,10 +158,217 @@ export default {
|
|
|
148
158
|
this._dispatchEvent(EventType.Error, e.detail)
|
|
149
159
|
},
|
|
150
160
|
|
|
161
|
+
_onnextchannel (e) {
|
|
162
|
+
this.wxchannel = true
|
|
163
|
+
const adData = e.detail[0]
|
|
164
|
+
this.$nextTick(() => {
|
|
165
|
+
if (adData.provider === 10017) {
|
|
166
|
+
this._providerType = ProviderType.UserWeChat
|
|
167
|
+
switch (adData._nt_) {
|
|
168
|
+
case 4:
|
|
169
|
+
this.wxAdType = AdType.Banner
|
|
170
|
+
this.userwx = true
|
|
171
|
+
this.userUnitId = adData.posid
|
|
172
|
+
if (adData.tmpl_type === 24) {
|
|
173
|
+
this.customFullscreen = 'uni-ad-custom-fullscreen'
|
|
174
|
+
}
|
|
175
|
+
break
|
|
176
|
+
case 9:
|
|
177
|
+
this.wxAdType = AdType.RewardedVideo
|
|
178
|
+
this._createRewardedAd(adData.posid)
|
|
179
|
+
break
|
|
180
|
+
case 15:
|
|
181
|
+
this.wxAdType = AdType.Interstitial
|
|
182
|
+
this._createInterstitialAd(adData.posid)
|
|
183
|
+
break
|
|
184
|
+
}
|
|
185
|
+
} else if (adData.provider === 10018) {
|
|
186
|
+
this._providerType = ProviderType.WeChat
|
|
187
|
+
this.selectComponent('.uniad-plugin-wx').setConfig(adData)
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
_onwxchannelerror (e) {
|
|
193
|
+
this.wxchannel = false
|
|
194
|
+
this.$nextTick(() => {
|
|
195
|
+
this._providerType = ProviderType.ShanHu
|
|
196
|
+
this.selectComponent('.uniad-plugin').shanhuChannel()
|
|
197
|
+
})
|
|
198
|
+
},
|
|
199
|
+
|
|
151
200
|
_dispatchEvent (type, data) {
|
|
152
201
|
this.$emit(type, {
|
|
153
202
|
detail: data
|
|
154
203
|
})
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
_showAdInPlugin (adComponent) {
|
|
207
|
+
if (this._hasCallback()) {
|
|
208
|
+
const userCryptoManager = wx.getUserCryptoManager()
|
|
209
|
+
userCryptoManager.getLatestUserKey({
|
|
210
|
+
success: ({
|
|
211
|
+
encryptKey,
|
|
212
|
+
iv,
|
|
213
|
+
version,
|
|
214
|
+
expireTime
|
|
215
|
+
}) => {
|
|
216
|
+
adComponent.show({
|
|
217
|
+
userId: this.urlCallback.userId || '',
|
|
218
|
+
extra: this.urlCallback.extra || '',
|
|
219
|
+
encryptKey,
|
|
220
|
+
iv,
|
|
221
|
+
version,
|
|
222
|
+
expireTime
|
|
223
|
+
})
|
|
224
|
+
},
|
|
225
|
+
fail: (err) => {
|
|
226
|
+
this._dispatchEvent(EventType.Error, err)
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
} else {
|
|
230
|
+
adComponent.show({
|
|
231
|
+
userId: this.urlCallback.userId || '',
|
|
232
|
+
extra: this.urlCallback.extra || ''
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
_loadWxAd () {
|
|
238
|
+
switch (this.wxAdType) {
|
|
239
|
+
case AdType.RewardedVideo:
|
|
240
|
+
if (this._wxRewardedAd) {
|
|
241
|
+
this._wxRewardedAd.load()
|
|
242
|
+
}
|
|
243
|
+
break
|
|
244
|
+
case AdType.Interstitial:
|
|
245
|
+
if (this._wxInterstitialAd) {
|
|
246
|
+
this._wxInterstitialAd.load()
|
|
247
|
+
}
|
|
248
|
+
break
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
// 加载/显示广告
|
|
253
|
+
_showWxAd (options) {
|
|
254
|
+
this._urlCallback = options || this.urlCallback
|
|
255
|
+
if (this.loading === true) {
|
|
256
|
+
this._userInvokeShowFlag = true
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
switch (this.wxAdType) {
|
|
260
|
+
case AdType.RewardedVideo:
|
|
261
|
+
if (!this._wxRewardedAd) {
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
// eslint-disable-next-line handle-callback-err
|
|
265
|
+
this._wxRewardedAd.show().catch((err) => {
|
|
266
|
+
this._wxRewardedAd.load().then(() => {
|
|
267
|
+
this._wxRewardedAd.show()
|
|
268
|
+
}).catch((err) => {
|
|
269
|
+
this._dispatchEvent(EventType.Error, err)
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
break
|
|
273
|
+
case AdType.Interstitial:
|
|
274
|
+
if (!this._wxInterstitialAd) {
|
|
275
|
+
return
|
|
276
|
+
}
|
|
277
|
+
// eslint-disable-next-line handle-callback-err
|
|
278
|
+
this._wxInterstitialAd.show().catch((err) => {
|
|
279
|
+
this._wxInterstitialAd.load().then(() => {
|
|
280
|
+
this._wxInterstitialAd.show()
|
|
281
|
+
}).catch((err) => {
|
|
282
|
+
this._dispatchEvent(EventType.Error, err)
|
|
283
|
+
})
|
|
284
|
+
})
|
|
285
|
+
break
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
// 微信激励视频
|
|
290
|
+
_createRewardedAd (adUnitId) {
|
|
291
|
+
if (this._wxRewardedAd) {
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true })
|
|
296
|
+
|
|
297
|
+
this._wxRewardedAd.onLoad(() => {
|
|
298
|
+
this.loading = false
|
|
299
|
+
this._dispatchEvent(EventType.Load, {})
|
|
300
|
+
if (this._userInvokeShowFlag) {
|
|
301
|
+
this._userInvokeShowFlag = false
|
|
302
|
+
this._wxRewardedAd.show()
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
this._wxRewardedAd.onError(err => {
|
|
307
|
+
this.loading = false
|
|
308
|
+
this._dispatchEvent(EventType.Error, err)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
this._wxRewardedAd.onClose(res => {
|
|
312
|
+
if (res.isEnded) {
|
|
313
|
+
this._callServer()
|
|
314
|
+
} else {
|
|
315
|
+
this._dispatchEvent(EventType.Close, res)
|
|
316
|
+
}
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
this.loading = true
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
// 微信插屏
|
|
323
|
+
_createInterstitialAd (adUnitId) {
|
|
324
|
+
if (this._wxInterstitialAd) {
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId })
|
|
329
|
+
|
|
330
|
+
this._wxInterstitialAd.onLoad(() => {
|
|
331
|
+
this.loading = false
|
|
332
|
+
this._dispatchEvent(EventType.Load, {})
|
|
333
|
+
if (this._userInvokeShowFlag) {
|
|
334
|
+
this._userInvokeShowFlag = false
|
|
335
|
+
this._wxInterstitialAd.show()
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
this._wxInterstitialAd.onError(err => {
|
|
340
|
+
this.loading = false
|
|
341
|
+
this._dispatchEvent(EventType.Error, err)
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
this._wxInterstitialAd.onClose(res => {
|
|
345
|
+
this._dispatchEvent(EventType.Close, res)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
this.loading = true
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
_callServer () {
|
|
352
|
+
const userCryptoManager = wx.getUserCryptoManager()
|
|
353
|
+
userCryptoManager.getLatestUserKey({
|
|
354
|
+
success: (encryptConfig) => {
|
|
355
|
+
const callbackData = adPlugin.buildCallbackData(this.adpid, this.urlCallback, {}, encryptConfig)
|
|
356
|
+
uniCloud.callFunction({
|
|
357
|
+
name: 'uniAdCallback',
|
|
358
|
+
data: callbackData,
|
|
359
|
+
secretType: 'both',
|
|
360
|
+
success: (res) => {
|
|
361
|
+
this._dispatchEvent(EventType.Close, res)
|
|
362
|
+
},
|
|
363
|
+
fail: (err) => {
|
|
364
|
+
this._dispatchEvent(EventType.Error, err)
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
},
|
|
368
|
+
fail: (err) => {
|
|
369
|
+
this._dispatchEvent(EventType.Error, err)
|
|
370
|
+
}
|
|
371
|
+
})
|
|
155
372
|
}
|
|
156
373
|
}
|
|
157
374
|
}
|
package/components/uniad.vue
CHANGED
|
@@ -5,8 +5,13 @@
|
|
|
5
5
|
:adpid="adpid"
|
|
6
6
|
@load="_onmpload"
|
|
7
7
|
@close="_onmpclose"
|
|
8
|
-
@error="_onmperror"
|
|
9
|
-
|
|
8
|
+
@error="_onmperror"
|
|
9
|
+
@nextChannel="_onnextchannel"
|
|
10
|
+
/>
|
|
11
|
+
<!-- #ifdef MP-WEIXIN -->
|
|
12
|
+
<ad-custom v-if="userwx" :unit-id="userUnitId" class="uni-ad-custom" :class="[customFullscreen]"></ad-custom>
|
|
13
|
+
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
|
14
|
+
<!-- #endif -->
|
|
10
15
|
</view>
|
|
11
16
|
</template>
|
|
12
17
|
|
|
@@ -26,3 +31,9 @@ export default {
|
|
|
26
31
|
}
|
|
27
32
|
}
|
|
28
33
|
</script>
|
|
34
|
+
|
|
35
|
+
<style>
|
|
36
|
+
.uni-ad-custom-fullscreen {
|
|
37
|
+
height: 100vh;
|
|
38
|
+
}
|
|
39
|
+
</style>
|
|
@@ -393,7 +393,7 @@
|
|
|
393
393
|
needLoading: true,
|
|
394
394
|
loadingTitle: ''
|
|
395
395
|
}, options);
|
|
396
|
-
if (
|
|
396
|
+
if (options.needConfirm) {
|
|
397
397
|
uni.showModal({
|
|
398
398
|
title: options.confirmTitle || t('uniCloud.component.update.showModal.title'),
|
|
399
399
|
content: options.confirmContent || t('uniCloud.component.update.showModal.content'),
|
package/lib/platform.js
CHANGED
|
@@ -46,13 +46,19 @@ function getShadowCdn () {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
let appid
|
|
49
|
+
let platformAppId
|
|
49
50
|
|
|
50
|
-
function createIdent () {
|
|
51
|
+
function createIdent (platform) {
|
|
51
52
|
if (process.env.UNI_INPUT_DIR) {
|
|
52
53
|
if (typeof appid === 'undefined') {
|
|
53
|
-
|
|
54
|
+
const manifestJson = getManifestJson()
|
|
55
|
+
appid = manifestJson.appid || ''
|
|
56
|
+
platformAppId = manifestJson[platform]?.appid
|
|
57
|
+
}
|
|
58
|
+
let id = appid.replace('__UNI__', '')
|
|
59
|
+
if (platformAppId) {
|
|
60
|
+
id += '%%' + platformAppId
|
|
54
61
|
}
|
|
55
|
-
const id = appid.replace('__UNI__', '')
|
|
56
62
|
if (id) {
|
|
57
63
|
return Buffer.from(Buffer.from(id).toString('base64')).toString('hex')
|
|
58
64
|
}
|
|
@@ -63,7 +69,7 @@ function createIdent () {
|
|
|
63
69
|
function createShadowImageUrl (cdn, type = 'grey') {
|
|
64
70
|
let identStr = ''
|
|
65
71
|
if (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'web') {
|
|
66
|
-
const ident = createIdent()
|
|
72
|
+
const ident = createIdent(process.env.UNI_PLATFORM)
|
|
67
73
|
identStr = ident ? `${ident}/` : ''
|
|
68
74
|
}
|
|
69
75
|
return `https://cdn${cdn || ''}.dcloud.net.cn/${identStr}img/shadow-${type}.png`
|
package/lib/preprocess.js
CHANGED
|
@@ -52,14 +52,14 @@ module.exports = function initPreprocess (name, platforms, userDefines = {}) {
|
|
|
52
52
|
nvueContext.APP_NVUE = true
|
|
53
53
|
nvueContext.APP_PLUS_NVUE = true
|
|
54
54
|
|
|
55
|
-
if (process.env.UNI_APP_PLATFORM === 'android') {
|
|
56
|
-
|
|
57
|
-
} else if (process.env.UNI_APP_PLATFORM === 'ios') {
|
|
58
|
-
|
|
59
|
-
} else {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
55
|
+
// if (process.env.UNI_APP_PLATFORM === 'android') {
|
|
56
|
+
// defaultContext.APP_ANDROID = true
|
|
57
|
+
// } else if (process.env.UNI_APP_PLATFORM === 'ios') {
|
|
58
|
+
// defaultContext.APP_IOS = true
|
|
59
|
+
// } else {
|
|
60
|
+
// defaultContext.APP_ANDROID = true
|
|
61
|
+
// defaultContext.APP_IOS = true
|
|
62
|
+
// }
|
|
63
63
|
}
|
|
64
64
|
if (name === 'h5') {
|
|
65
65
|
defaultContext.WEB = true
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const reservedWords =
|
|
2
|
+
'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'
|
|
3
|
+
const builtins =
|
|
4
|
+
'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'
|
|
5
|
+
const forbiddenIdentifiers = new Set(`${reservedWords} ${builtins}`.split(' '))
|
|
6
|
+
forbiddenIdentifiers.add('')
|
|
7
|
+
const makeLegalIdentifier = function makeLegalIdentifier (str) {
|
|
8
|
+
let identifier = str
|
|
9
|
+
.replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
|
|
10
|
+
.replace(/[^$_a-zA-Z0-9]/g, '_')
|
|
11
|
+
if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
|
|
12
|
+
identifier = `_${identifier}`
|
|
13
|
+
}
|
|
14
|
+
return identifier || '_'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function stringify (obj) {
|
|
18
|
+
return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, (char) =>
|
|
19
|
+
`\\u${`000${char.charCodeAt(0).toString(16)}`.slice(-4)}`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function serializeArray (arr, indent, baseIndent) {
|
|
23
|
+
let output = '['
|
|
24
|
+
const separator = indent ? `\n${baseIndent}${indent}` : ''
|
|
25
|
+
for (let i = 0; i < arr.length; i++) {
|
|
26
|
+
const key = arr[i]
|
|
27
|
+
output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`
|
|
28
|
+
}
|
|
29
|
+
return `${output}${indent ? `\n${baseIndent}` : ''}]`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function serializeObject (obj, indent, baseIndent) {
|
|
33
|
+
let output = '{'
|
|
34
|
+
const separator = indent ? `\n${baseIndent}${indent}` : ''
|
|
35
|
+
const entries = Object.entries(obj)
|
|
36
|
+
for (let i = 0; i < entries.length; i++) {
|
|
37
|
+
const [key, value] = entries[i]
|
|
38
|
+
const stringKey = makeLegalIdentifier(key) === key ? key : stringify(key)
|
|
39
|
+
output +=
|
|
40
|
+
`${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize(value, indent, baseIndent + indent)}`
|
|
41
|
+
}
|
|
42
|
+
return `${output}${indent ? `\n${baseIndent}` : ''}}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function serialize (obj, indent, baseIndent) {
|
|
46
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
47
|
+
if (Array.isArray(obj)) {
|
|
48
|
+
return serializeArray(obj, indent, baseIndent)
|
|
49
|
+
}
|
|
50
|
+
if (obj instanceof Date) {
|
|
51
|
+
return `new Date(${obj.getTime()})`
|
|
52
|
+
}
|
|
53
|
+
if (obj instanceof RegExp) {
|
|
54
|
+
return obj.toString()
|
|
55
|
+
}
|
|
56
|
+
return serializeObject(obj, indent, baseIndent)
|
|
57
|
+
}
|
|
58
|
+
if (typeof obj === 'number') {
|
|
59
|
+
if (obj === Infinity) {
|
|
60
|
+
return 'Infinity'
|
|
61
|
+
}
|
|
62
|
+
if (obj === -Infinity) {
|
|
63
|
+
return '-Infinity'
|
|
64
|
+
}
|
|
65
|
+
if (obj === 0) {
|
|
66
|
+
return 1 / obj === Infinity ? '0' : '-0'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isNaN(obj)) {
|
|
70
|
+
return 'NaN'
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (typeof obj === 'symbol') {
|
|
74
|
+
const key = Symbol.keyFor(obj)
|
|
75
|
+
// eslint-disable-next-line no-undefined
|
|
76
|
+
if (key !== undefined) {
|
|
77
|
+
return `Symbol.for(${stringify(key)})`
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (typeof obj === 'bigint') {
|
|
81
|
+
return `${obj}n`
|
|
82
|
+
}
|
|
83
|
+
return stringify(obj)
|
|
84
|
+
}
|
|
85
|
+
// isWellFormed exists from Node.js 20
|
|
86
|
+
const hasStringIsWellFormed = 'isWellFormed' in String.prototype
|
|
87
|
+
|
|
88
|
+
function isWellFormedString (input) {
|
|
89
|
+
// @ts-expect-error String::isWellFormed exists from ES2024. tsconfig lib is set to ES6
|
|
90
|
+
if (hasStringIsWellFormed) {
|
|
91
|
+
return input.isWellFormed()
|
|
92
|
+
}
|
|
93
|
+
// https://github.com/tc39/proposal-is-usv-string/blob/main/README.md#algorithm
|
|
94
|
+
return !/\p{Surrogate}/u.test(input)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function dataToEsm (data, options = {}) {
|
|
98
|
+
var _a, _b
|
|
99
|
+
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t'
|
|
100
|
+
const _ = options.compact ? '' : ' '
|
|
101
|
+
const n = options.compact ? '' : '\n'
|
|
102
|
+
const declarationType = options.preferConst ? 'const' : 'var'
|
|
103
|
+
if (options.namedExports === false ||
|
|
104
|
+
typeof data !== 'object' ||
|
|
105
|
+
Array.isArray(data) ||
|
|
106
|
+
data instanceof Date ||
|
|
107
|
+
data instanceof RegExp ||
|
|
108
|
+
data === null) {
|
|
109
|
+
const code = serialize(data, options.compact ? null : t, '')
|
|
110
|
+
const magic = _ || (/^[{[\-\/]/.test(code) ? '' : ' ') // eslint-disable-line no-useless-escape
|
|
111
|
+
return `export default${magic}${code};`
|
|
112
|
+
}
|
|
113
|
+
let maxUnderbarPrefixLength = 0
|
|
114
|
+
for (const key of Object.keys(data)) {
|
|
115
|
+
const underbarPrefixLength = (_b = (_a = key.match(/^(_+)/)) === null || _a === undefined ? undefined : _a[0]
|
|
116
|
+
.length) !==
|
|
117
|
+
null && _b !== undefined ? _b : 0
|
|
118
|
+
if (underbarPrefixLength > maxUnderbarPrefixLength) {
|
|
119
|
+
maxUnderbarPrefixLength = underbarPrefixLength
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const arbitraryNamePrefix = `${'_'.repeat(maxUnderbarPrefixLength + 1)}arbitrary`
|
|
123
|
+
let namedExportCode = ''
|
|
124
|
+
const defaultExportRows = []
|
|
125
|
+
const arbitraryNameExportRows = []
|
|
126
|
+
for (const [key, value] of Object.entries(data)) {
|
|
127
|
+
if (key === makeLegalIdentifier(key)) {
|
|
128
|
+
if (options.objectShorthand) {
|
|
129
|
+
defaultExportRows.push(key)
|
|
130
|
+
} else {
|
|
131
|
+
defaultExportRows.push(`${key}:${_}${key}`)
|
|
132
|
+
}
|
|
133
|
+
namedExportCode +=
|
|
134
|
+
`export ${declarationType} ${key}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`
|
|
135
|
+
} else {
|
|
136
|
+
defaultExportRows.push(`${stringify(key)}:${_}${serialize(value, options.compact ? null : t, '')}`)
|
|
137
|
+
if (options.includeArbitraryNames && isWellFormedString(key)) {
|
|
138
|
+
const variableName = `${arbitraryNamePrefix}${arbitraryNameExportRows.length}`
|
|
139
|
+
namedExportCode +=
|
|
140
|
+
`${declarationType} ${variableName}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`
|
|
141
|
+
arbitraryNameExportRows.push(`${variableName} as ${JSON.stringify(key)}`)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const arbitraryExportCode = arbitraryNameExportRows.length > 0
|
|
146
|
+
? `export${_}{${n}${t}${arbitraryNameExportRows.join(`,${n}${t}`)}${n}};${n}`
|
|
147
|
+
: ''
|
|
148
|
+
const defaultExportCode = `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`
|
|
149
|
+
return `${namedExportCode}${arbitraryExportCode}${defaultExportCode}`
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
module.exports = {
|
|
153
|
+
dataToEsm
|
|
154
|
+
}
|