Skip to content

Commit 2e6b646

Browse files
authored
Update build config to output ESM for better code splitting (#183)
Changed to use ESM format for loading scripts to get better code splitting support. This PR is mainly to prepare for adding future features, such as rich text editor. ```release-note None ```
1 parent 8cac6ba commit 2e6b646

File tree

15 files changed

+828
-243
lines changed

15 files changed

+828
-243
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ build {
4848
}
4949

5050
halo {
51-
version = "2.20"
51+
version = "2.21"
5252
debug = true
5353
}

packages/comment-widget/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
"dev": "tsc -w"
3232
},
3333
"dependencies": {
34-
"@emoji-mart/data": "^1.1.2",
35-
"@halo-dev/api-client": "^2.14.0",
36-
"@lit/context": "^1.1.0",
34+
"@emoji-mart/data": "^1.2.1",
35+
"@halo-dev/api-client": "^2.21.1",
36+
"@lit/context": "^1.1.6",
3737
"@lit/localize": "^0.12.2",
38-
"dayjs": "^1.11.10",
39-
"emoji-mart": "^5.5.2",
40-
"es-toolkit": "^1.39.7",
41-
"javascript-time-ago": "^2.5.9",
42-
"lit": "^3.1.2"
38+
"dayjs": "^1.11.13",
39+
"emoji-mart": "^5.6.0",
40+
"es-toolkit": "^1.39.8",
41+
"javascript-time-ago": "^2.5.11",
42+
"lit": "^3.3.1"
4343
},
4444
"devDependencies": {
4545
"@lit/localize-tools": "^0.8.0",

packages/comment-widget/src/comment-widget.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
baseUrlContext,
2424
captchaEnabledContext,
2525
currentUserContext,
26-
emojiDataUrlContext,
2726
groupContext,
2827
kindContext,
2928
nameContext,
@@ -88,10 +87,6 @@ export class CommentWidget extends LitElement {
8887
@property({ type: String, attribute: 'avatar-policy' })
8988
avatarPolicy = '';
9089

91-
@provide({ context: emojiDataUrlContext })
92-
@property({ type: String, attribute: 'emoji-data-url' })
93-
emojiDataUrl = 'https://unpkg.com/@emoji-mart/data';
94-
9590
@provide({ context: currentUserContext })
9691
@state()
9792
currentUser: User | undefined;

packages/comment-widget/src/context/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export const currentUserContext = createContext<User | undefined>(
3434
Symbol('currentUser')
3535
);
3636

37-
export const emojiDataUrlContext = createContext<string>(
38-
Symbol('emojiDataUrl')
39-
);
40-
4137
export const toastContext = createContext<ToastManager | undefined>(
4238
Symbol('toastContext')
4339
);

packages/comment-widget/src/emoji-button.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import es from '@emoji-mart/data/i18n/es.json';
99
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1010
//@ts-ignore
1111
import zh from '@emoji-mart/data/i18n/zh.json';
12-
import { consume } from '@lit/context';
1312
import { msg } from '@lit/localize';
1413
import { Picker } from 'emoji-mart';
1514
import { css, html, LitElement } from 'lit';
1615
import { state } from 'lit/decorators.js';
1716
import { createRef, type Ref, ref } from 'lit/directives/ref.js';
18-
import { emojiDataUrlContext } from './context';
1917
import { getLocale } from './locale';
2018
import baseStyles from './styles/base';
2119
import varStyles from './styles/var';
@@ -37,10 +35,6 @@ export class EmojiButton extends LitElement {
3735
@state()
3836
emojiPicker: Picker | null = null;
3937

40-
@consume({ context: emojiDataUrlContext })
41-
@state()
42-
emojiDataUrl = '';
43-
4438
emojiPickerWrapperRef: Ref<HTMLDivElement> = createRef<HTMLDivElement>();
4539

4640
constructor() {
@@ -78,8 +72,9 @@ export class EmojiButton extends LitElement {
7872

7973
this.emojiLoading = true;
8074

81-
const response = await fetch(this.emojiDataUrl);
82-
const data = await response.json();
75+
const { default: data } = await import(
76+
/* webpackChunkName: "emoji-mart-data" */ '@emoji-mart/data'
77+
);
8378

8479
const emojiPicker = new Picker({
8580
data,
@@ -91,7 +86,6 @@ export class EmojiButton extends LitElement {
9186
i18n: localeMap[getLocale()],
9287
});
9388

94-
// TODO: fix this ts error
9589
this.emojiPickerWrapperRef.value?.appendChild(
9690
emojiPicker as unknown as Node
9791
);
@@ -101,7 +95,11 @@ export class EmojiButton extends LitElement {
10195
}
10296

10397
override render() {
104-
return html`<button class="emoji-button" type="button" aria-label=${msg('Select emoticon')}>
98+
return html`<button
99+
class="emoji-button"
100+
type="button"
101+
aria-label=${msg('Select emoticon')}
102+
>
105103
${
106104
this.emojiLoading
107105
? html`<icon-loading></icon-loading>`

packages/comment-widget/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"noFallthroughCasesInSwitch": true,
1717
"noImplicitAny": true,
1818
"noImplicitThis": true,
19-
"moduleResolution": "node",
19+
"moduleResolution": "bundler",
2020
"allowSyntheticDefaultImports": true,
2121
"experimentalDecorators": true,
2222
"forceConsistentCasingInFileNames": true,

packages/widget/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
2-
"license": "GPL-3.0",
32
"scripts": {
4-
"build": "tsc && vite build",
5-
"dev": "vite"
3+
"build": "tsc && rslib build",
4+
"dev": "rslib build --watch"
65
},
76
"dependencies": {
8-
"@emoji-mart/data": "^1.1.2",
97
"@halo-dev/comment-widget": "workspace:*"
108
},
119
"devDependencies": {
12-
"rollup-plugin-copy-merge": "^1.0.2"
10+
"@rslib/core": "^0.11.1"
1311
}
1412
}

packages/widget/rslib.config.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { defineConfig } from '@rslib/core';
3+
4+
const PLUGIN_NAME = 'PluginCommentWidget';
5+
6+
export default defineConfig({
7+
lib: [
8+
{
9+
format: 'esm',
10+
autoExternal: false,
11+
},
12+
],
13+
output: {
14+
target: 'web',
15+
minify: true,
16+
cleanDistPath: true,
17+
filename: {
18+
js: (pathData) => {
19+
if (pathData.chunk.name === 'index') {
20+
return 'comment-widget.js';
21+
}
22+
return '[name].[contenthash:8].js';
23+
},
24+
},
25+
publicPath: `/plugins/${PLUGIN_NAME}/assets/static/`,
26+
distPath: {
27+
root: fileURLToPath(
28+
new URL('../../src/main/resources/static', import.meta.url)
29+
),
30+
},
31+
},
32+
});

packages/widget/src/env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/// <reference types="vite/client" />
1+
/// <reference types="@rslib/core/types" />

packages/widget/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export function init(el: string, props: Props) {
3737
commentWidget.replySize = props.replySize || 10;
3838
commentWidget.withReplies = props.withReplies || false;
3939
commentWidget.withReplySize = props.withReplySize || 10;
40-
commentWidget.emojiDataUrl =
41-
'/plugins/PluginCommentWidget/assets/static/emoji/native.json';
4240
commentWidget.useAvatarProvider = props.useAvatarProvider || false;
4341
commentWidget.avatarProvider = props.avatarProvider || '';
4442
commentWidget.avatarProviderMirror = props.avatarProviderMirror || '';

0 commit comments

Comments
 (0)