@cloudbase/oauth 1.2.1-beta → 2.0.0-beta.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.
Files changed (125) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +26 -0
  3. package/CHANGELOG.md +30 -0
  4. package/Dockerfile +15 -0
  5. package/README.md +183 -0
  6. package/_exmaple/assets/scripts/function/function.ts +99 -0
  7. package/_exmaple/assets/scripts/index.ts +101 -0
  8. package/_exmaple/assets/scripts/request.ts +11 -0
  9. package/_exmaple/index.html +15 -0
  10. package/_exmaple/package.json +33 -0
  11. package/_exmaple/tsconfig.json +71 -0
  12. package/_exmaple/typings.d.ts +0 -0
  13. package/_exmaple/webpack.config.js +42 -0
  14. package/dist/cjs/auth/apis.d.ts +55 -0
  15. package/dist/cjs/auth/apis.js +568 -0
  16. package/{auth → dist/cjs/auth}/consts.d.ts +9 -21
  17. package/dist/cjs/auth/consts.js +61 -0
  18. package/dist/cjs/auth/models.d.ts +354 -0
  19. package/dist/cjs/auth/models.js +3 -0
  20. package/dist/cjs/captcha/captcha.d.ts +35 -0
  21. package/dist/cjs/captcha/captcha.js +267 -0
  22. package/dist/cjs/index.d.ts +14 -0
  23. package/dist/cjs/index.js +63 -0
  24. package/{oauthclient → dist/cjs/oauth2client}/consts.d.ts +22 -1
  25. package/dist/cjs/oauth2client/consts.js +73 -0
  26. package/dist/cjs/oauth2client/interface.d.ts +15 -0
  27. package/dist/cjs/oauth2client/interface.js +10 -0
  28. package/dist/cjs/oauth2client/models.d.ts +47 -0
  29. package/dist/cjs/oauth2client/models.js +3 -0
  30. package/dist/cjs/oauth2client/oauth2client.d.ts +71 -0
  31. package/dist/cjs/oauth2client/oauth2client.js +640 -0
  32. package/dist/cjs/utils/function/single-promise.d.ts +5 -0
  33. package/dist/cjs/utils/function/single-promise.js +89 -0
  34. package/dist/cjs/utils/uuid.d.ts +1 -0
  35. package/dist/cjs/utils/uuid.js +12 -0
  36. package/dist/esm/auth/apis.d.ts +55 -0
  37. package/dist/esm/auth/apis.js +566 -0
  38. package/dist/esm/auth/consts.d.ts +54 -0
  39. package/dist/esm/auth/consts.js +58 -0
  40. package/dist/esm/auth/models.d.ts +354 -0
  41. package/dist/esm/auth/models.js +1 -0
  42. package/dist/esm/captcha/captcha.d.ts +35 -0
  43. package/dist/esm/captcha/captcha.js +264 -0
  44. package/dist/esm/index.d.ts +14 -0
  45. package/dist/esm/index.js +32 -0
  46. package/dist/esm/oauth2client/consts.d.ts +67 -0
  47. package/dist/esm/oauth2client/consts.js +70 -0
  48. package/dist/esm/oauth2client/interface.d.ts +15 -0
  49. package/dist/esm/oauth2client/interface.js +7 -0
  50. package/dist/esm/oauth2client/models.d.ts +47 -0
  51. package/dist/esm/oauth2client/models.js +1 -0
  52. package/dist/esm/oauth2client/oauth2client.d.ts +71 -0
  53. package/dist/esm/oauth2client/oauth2client.js +636 -0
  54. package/dist/esm/utils/function/single-promise.d.ts +5 -0
  55. package/dist/esm/utils/function/single-promise.js +86 -0
  56. package/dist/esm/utils/uuid.d.ts +1 -0
  57. package/dist/esm/utils/uuid.js +8 -0
  58. package/package.json +27 -14
  59. package/publish.sh +2 -0
  60. package/src/auth/apis.ts +642 -0
  61. package/src/auth/consts.ts +57 -0
  62. package/src/auth/models.ts +444 -0
  63. package/src/captcha/captcha.ts +222 -0
  64. package/src/index.ts +49 -0
  65. package/src/oauth2client/consts.ts +69 -0
  66. package/src/oauth2client/interface.ts +57 -0
  67. package/src/oauth2client/models.ts +61 -0
  68. package/src/oauth2client/oauth2client.ts +620 -0
  69. package/src/utils/function/single-promise.ts +40 -0
  70. package/src/utils/uuid.ts +11 -0
  71. package/tsconfig.esm.json +44 -0
  72. package/tsconfig.json +44 -0
  73. package/wiki/README.md +75 -0
  74. package/app/index.d.ts +0 -29
  75. package/app/index.js +0 -50
  76. package/app/index.js.map +0 -1
  77. package/app/internal.d.ts +0 -12
  78. package/app/internal.js +0 -25
  79. package/app/internal.js.map +0 -1
  80. package/app/openuri.d.ts +0 -20
  81. package/app/openuri.js +0 -106
  82. package/app/openuri.js.map +0 -1
  83. package/app/request.d.ts +0 -19
  84. package/app/request.js +0 -52
  85. package/app/request.js.map +0 -1
  86. package/app/storage.d.ts +0 -41
  87. package/app/storage.js +0 -35
  88. package/app/storage.js.map +0 -1
  89. package/auth/consts.js +0 -79
  90. package/auth/consts.js.map +0 -1
  91. package/auth/index.d.ts +0 -242
  92. package/auth/index.js +0 -550
  93. package/auth/index.js.map +0 -1
  94. package/auth/models.d.ts +0 -284
  95. package/auth/models.js +0 -4
  96. package/auth/models.js.map +0 -1
  97. package/captcha/index.d.ts +0 -45
  98. package/captcha/index.js +0 -135
  99. package/captcha/index.js.map +0 -1
  100. package/function/index.d.ts +0 -38
  101. package/function/index.js +0 -80
  102. package/function/index.js.map +0 -1
  103. package/index.d.ts +0 -8
  104. package/index.js +0 -17
  105. package/index.js.map +0 -1
  106. package/oauthclient/consts.js +0 -53
  107. package/oauthclient/consts.js.map +0 -1
  108. package/oauthclient/index.d.ts +0 -18
  109. package/oauthclient/index.js +0 -21
  110. package/oauthclient/index.js.map +0 -1
  111. package/oauthclient/interface.d.ts +0 -29
  112. package/oauthclient/interface.js +0 -10
  113. package/oauthclient/interface.js.map +0 -1
  114. package/oauthclient/models.d.ts +0 -40
  115. package/oauthclient/models.js +0 -3
  116. package/oauthclient/models.js.map +0 -1
  117. package/oauthclient/oauthclient.d.ts +0 -146
  118. package/oauthclient/oauthclient.js +0 -414
  119. package/oauthclient/oauthclient.js.map +0 -1
  120. package/utils/promise.d.ts +0 -18
  121. package/utils/promise.js +0 -48
  122. package/utils/promise.js.map +0 -1
  123. package/utils/uuid.d.ts +0 -5
  124. package/utils/uuid.js +0 -16
  125. package/utils/uuid.js.map +0 -1
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ dist/
2
+ node_modules/
package/.eslintrc ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "extends": [
3
+ "eslint-config-alloy/typescript"
4
+ ],
5
+ "rules": {
6
+ "indent": [
7
+ "warn",
8
+ 2
9
+ ],
10
+ "guard-for-in": 0,
11
+ "no-param-reassign": 0,
12
+ "no-undefined": 0,
13
+ "@typescript-eslint/explicit-member-accessibility": 0,
14
+ "@typescript-eslint/no-loss-of-precision": 0,
15
+ "@typescript-eslint/no-duplicate-imports": 0,
16
+ "@typescript-eslint/consistent-type-assertions": 0,
17
+ "@typescript-eslint/member-ordering": 0
18
+ },
19
+ "parserOptions": {
20
+ "ecmaVersion": 6,
21
+ "sourceType": "module",
22
+ "ecmaFeatures": {
23
+ "modules": true
24
+ }
25
+ }
26
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ ## 更新日志
2
+
3
+
4
+ ### 1.3.3beta
5
+
6
+ 1. 三方登录开放 `provider_access_token` 和 `provider_id_token`
7
+
8
+
9
+ ### 1.2.3
10
+
11
+ 1. OAuth2Client 增加 hasCredentials 方法,判断是否存在 credentials(不校验有限性也不触发 refresh token)
12
+ 2. 增加 LocalCredentials 管理 credentials, 简化 OAuth2Client 代码逻辑
13
+
14
+ ### 1.1.3
15
+
16
+ 1. 修复 getAccessToken 方法获取不到 refresh 之后的 token 的 bug
17
+ 2. 增加 SinglePromise 模块
18
+
19
+ ### 1.0.12
20
+
21
+ 1. 添加 ErrorType enum
22
+
23
+ ### 1.0.11
24
+
25
+ 1. OAuth2Client 中 refresh token,如果请求返回的 error 为 invalid_grant 时,删除本地 token
26
+ 2. OAuth2Client 中,对 defaultRequest 的 options 数据进行拷贝,避免修改了传入的参数数据
27
+
28
+ ### 1.0.9
29
+
30
+ 1. 代码格式化
package/Dockerfile ADDED
@@ -0,0 +1,15 @@
1
+ # FROM base_npm:latest
2
+
3
+ # # ARG NPM_TOKEN
4
+ # RUN mkdir -p /usr/src/app
5
+ # WORKDIR /usr/src/app/
6
+
7
+ # COPY package.json .
8
+ # ADD . /usr/src/app
9
+ # RUN npm set registry https://npm.xbase.xyz
10
+ # RUN npm install
11
+ # RUN npm run build
12
+
13
+ # # @todo use semantic-release with gitlab ci to auto release with branch
14
+ # # RUN npm view @xbase/sdk version
15
+ # RUN npm publish
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # Oauth
2
+
3
+ 提供 oauth2 的登录相关功能。
4
+
5
+ ## 主要模块
6
+
7
+ OAuth2Client oauth2 基础模块
8
+
9
+ Auth 登录相关模块
10
+
11
+ ## 使用示例
12
+
13
+ ### 初始化对象
14
+
15
+ ```typescript
16
+ const authOptions = {
17
+ apiOrigin: "服务域名",
18
+ clientId: "客户端ID"
19
+ }
20
+ const auth = new Auth(authOptions)
21
+ ```
22
+
23
+ ```typescript
24
+ import { initializeClient } from "@xbasesdk/xbase"
25
+ const config = {
26
+ env: "xbase-4gh5dh6nf62145a9"
27
+ }
28
+ const client = initializeClient(config)
29
+ // 调用API
30
+ const loginState = await client.auth.hasLoginState()
31
+ // 获取 accessToken
32
+ const accessToken = await client.oAuth2Client.getAccessToken()
33
+
34
+ // 用 OIDC Token 用自己的API
35
+ export interface ExampleData {
36
+ result?: string
37
+ }
38
+ const data = await client.oAuth2Client.request<ExampleData>(
39
+ "https://example.com",
40
+ { withCredentials: true }
41
+ )
42
+ console.log(data.result)
43
+
44
+
45
+
46
+ import {initializeApp} from '@clodbasesdk/oauth/app';
47
+ import {getAuth} from '@clodbasesdk/oauth/auth';
48
+ export const config = {
49
+ env: 'env-4gh5dh6nf62145a9'
50
+ };
51
+ const app = initializeApp(config)
52
+ const auth = getAuth(app)
53
+ // 调用API
54
+ const loginState = await auth.hasLoginState()
55
+ ```
56
+
57
+ ### 账号登录
58
+
59
+ ```typescript
60
+ auth.signIn({
61
+ username: "test",
62
+ password: "test"
63
+ })
64
+ ```
65
+
66
+ ### 手机验证码登录/注册
67
+
68
+ ```typescript
69
+ const phoneNumber = "10012341234"
70
+ // 短信验证码
71
+ const verificationCode = "******"
72
+ const verification = await auth.getVerification({
73
+ phone_number: "+86 " + phoneNumber,
74
+ target: "ANY"
75
+ })
76
+
77
+ let verifyResult = await auth.verify({
78
+ verification_code: verificationCode,
79
+ verification_id: verification ? verification.verification_id : ""
80
+ })
81
+ let isUser = verification ? verification.is_user : false
82
+
83
+ let result
84
+ if (isUser) {
85
+ result = await auth.signIn({
86
+ username: "+86 " + phoneNumber,
87
+ verification_code: verificationCode,
88
+ verification_token: verifyResult.verification_token
89
+ })
90
+ } else {
91
+ result = await auth.signUp({
92
+ phone_number: "+86 " + phoneNumber,
93
+ password: "*******",
94
+ verification_code: verificationCode,
95
+ verification_token: verifyResult.verification_token,
96
+ local: "zh-cn",
97
+ name: "100****1234"
98
+ })
99
+ }
100
+ ```
101
+
102
+ ### 错误处理
103
+
104
+ ```typescript
105
+ try {
106
+ await auth.verify({
107
+ verification_code: verificationCode,
108
+ verification_id: verification ? verification.verification_id : ""
109
+ })
110
+ } catch (error) {
111
+ if (error && error.error_uri === "/v1/auth/verification/verify") {
112
+ switch (error.error) {
113
+ case "not_found": {
114
+ result = i18n.translate("error", "User Not Found")
115
+ break
116
+ }
117
+ case "invalid_password": {
118
+ if (error.details !== undefined && error.details.length > 0) {
119
+ result =
120
+ i18n.translate("error", "Certification failed, you still have") +
121
+ error.details[0].limit_remaining +
122
+ i18n.translate("error", "chances. You can also") +
123
+ "" +
124
+ i18n.translate("error", "Reset Login Password") +
125
+ "</a>"
126
+ } else {
127
+ result = i18n.translate("error", "Password does not match")
128
+ }
129
+ break
130
+ }
131
+ case "user_pending": {
132
+ result = i18n.translate("error", "The account is not activated")
133
+ break
134
+ }
135
+ case "user_blocked": {
136
+ result = i18n.translate("error", "Account has been disabled")
137
+ break
138
+ }
139
+ case "invalid_status": {
140
+ if (error.details !== undefined && error.details.length > 0) {
141
+ let s = error.details[0].retry_delay
142
+ s = s.substring(0, s.length - 1)
143
+ s = parseInt(s)
144
+ result =
145
+ i18n.translate(
146
+ "error",
147
+ "The password input error has reached the upper limit and the account will be locked "
148
+ ) +
149
+ (s < 3600
150
+ ? s / 60 + i18n.translate("error", "minute")
151
+ : s / 3600 + i18n.translate("error", "hour"))
152
+ } else {
153
+ result = i18n.translate(
154
+ "error",
155
+ "Account has been temporarily locked"
156
+ )
157
+ }
158
+ break
159
+ }
160
+ case "invalid_two_factor": {
161
+ result = i18n.translate(
162
+ "error",
163
+ "Secondary authentication code does not match or has expired"
164
+ )
165
+ break
166
+ }
167
+ case "invalid_two_factor_recovery": {
168
+ result = i18n.translate(
169
+ "error",
170
+ "Recovery code does not match or has expired"
171
+ )
172
+ break
173
+ }
174
+ default: {
175
+ result = i18n.translate("error", "unknown")
176
+ break
177
+ }
178
+ }
179
+ }
180
+ }
181
+ ```
182
+
183
+ ## [更新日志](./CHANGELOG.md)
@@ -0,0 +1,99 @@
1
+ import { App } from "@xbasesdk/xbase/app";
2
+ import { getOAuthClient, AuthClient, ErrorType } from "@xbasesdk/xbase/oauthclient";
3
+ import { uuidv4 } from "@xbasesdk/xbase/utils/uuid";
4
+
5
+ export interface FunctionOptions {
6
+ credentialsClient: AuthClient;
7
+ env: string
8
+ }
9
+
10
+ /**
11
+ * Returns the existing `Auth` instance that is associated with the app
12
+ */
13
+ export function getFunction(app: App): Function {
14
+ const credentialsClient = getOAuthClient(app)
15
+ return new Function({
16
+ credentialsClient: credentialsClient,
17
+ env: app.options.clientId
18
+ })
19
+ }
20
+
21
+
22
+ export class Function {
23
+
24
+ private _config: FunctionOptions;
25
+
26
+ /**
27
+ * constructor
28
+ * @param {AuthOptions} opts
29
+ */
30
+ constructor(opts: FunctionOptions) {
31
+ this._config = {
32
+ credentialsClient: opts.credentialsClient,
33
+ env: opts.env,
34
+ }
35
+ }
36
+ /**
37
+ * callFunction call function
38
+ * @param {SignInRequest} params A SignInRequest Object.
39
+ * @return {Promise<Credentials>} A Promise<Credentials> object.
40
+ */
41
+ public async callFunction(params: FunctionRequest): Promise<FunctionResponse> {
42
+ let accessToken: string
43
+ try {
44
+ accessToken = await this._config.credentialsClient.getAccessToken()
45
+ } catch (e) {
46
+ if (e.error === ErrorType.UNAUTHENTICATED) {
47
+ accessToken = ""
48
+ } else {
49
+ return Promise.reject(e)
50
+ }
51
+ }
52
+ const requestId = uuidv4()
53
+ var reqBody = {
54
+ "action": "functions.invokeFunction",
55
+ "dataVersion": "2020-01-10",
56
+ "env": this._config.env,
57
+ "function_name": params.name,
58
+ "request_data": JSON.stringify(params.data),
59
+ "seqId": requestId,
60
+ "access_token": accessToken
61
+ }
62
+ const url = "/web?env=" + this._config.env
63
+ const resp = await this._config.credentialsClient.request<functionHTTPResponse>(url, {
64
+ method: 'POST',
65
+ body: reqBody,
66
+ headers: {
67
+ 'content-type': 'application/json'
68
+ }
69
+ })
70
+ if (resp.code) {
71
+ return Promise.reject({
72
+ error: resp.code,
73
+ error_description: resp.message
74
+ })
75
+ }
76
+ return {
77
+ requestId: resp.requestId,
78
+ result: resp.data.response_data,
79
+ }
80
+ }
81
+ }
82
+
83
+
84
+ export interface FunctionRequest {
85
+ name: string;
86
+ data: any;
87
+ }
88
+
89
+ export interface FunctionResponse {
90
+ requestId: string;
91
+ result: any;
92
+ }
93
+
94
+ export interface functionHTTPResponse {
95
+ requestId: string;
96
+ data: any
97
+ code: string
98
+ message: string
99
+ }
@@ -0,0 +1,101 @@
1
+ import { client, fn } from "./request";
2
+
3
+ async function init() {
4
+ try {
5
+ signInWithUserNameAndPassword()
6
+ } catch (error) {
7
+ // 获取用户信息失败
8
+ console.log('test error: ', error);
9
+ }
10
+
11
+ // try {
12
+ // console.log("rest password")
13
+ // const verificationToken = await client.auth.getVerification({
14
+ // phone_number: "+86 16622002084",
15
+ // })
16
+ // console.log(verificationToken.verification_id, verificationToken.is_user)
17
+ // } catch (error) {
18
+ // // 获取用户信息失败
19
+ // console.log('发送失败 error: ', error);
20
+ // }
21
+ try {
22
+ signInWithProviderToken()
23
+ } catch (error) {
24
+ // 获取用户信息失败
25
+ console.log('grant error error: ', error);
26
+ }
27
+
28
+ const resp = await fn.callFunction({
29
+ name: "test", data: {
30
+ name: "ok"
31
+ }
32
+ })
33
+ console.log(resp.result)
34
+ }
35
+
36
+ init();
37
+
38
+
39
+
40
+ async function signInWithUserNameAndPassword(): Promise<void> {
41
+ // 判断是否已经登录过了
42
+ const loginState = await client.auth.hasLoginState()
43
+ if (!loginState) {
44
+ // 用户名密码登录
45
+ await client.auth.signIn({
46
+ username: "leenanxi",
47
+ password: "qw0000"
48
+ })
49
+ }
50
+ // 获取用户信息,相当于 auth.signInWithUsernameAndPassword
51
+ const userInfoA = await client.auth.getUserProfile()
52
+ console.log(userInfoA)
53
+
54
+ // 修改密码
55
+
56
+ }
57
+
58
+
59
+ async function signInWithProviderToken(): Promise<void> {
60
+ // 判断是否已经登录过了
61
+ const loginState = await client.auth.hasLoginState()
62
+
63
+ console.log("test", loginState)
64
+
65
+ return
66
+
67
+ // if (!loginState) {
68
+ // const crd = await client.auth.signInAnonymously()
69
+ // console.log(crd)
70
+ // }
71
+
72
+ const userInfoA = await client.auth.getUserProfile()
73
+ console.log(userInfoA)
74
+ //
75
+ // // 获取provider token
76
+ // const providerToken = await client.auth.grantProviderToken({
77
+ // provider_id: "weda",
78
+ // provider_access_token: "ya29.a0ARrdaM8nNGa2g3eNWok38QWChn5hcOMZIXIbV48LxcEIt7lSfOt2ERLVRMQh9-iOxMy9anVtVV20ONWWyIHKxeA68D8FlpSZIazLfscLDOY8X1IjzF7kfhLiQwVbPYVeVsj7prOacREaICaaYO632za8bLSp2RU",
79
+ // });
80
+ // // 用provider token 登录
81
+ // client.auth.signInWithProvider({
82
+ // provider_token: providerToken.provider_token,
83
+ // })
84
+
85
+ // 调用云函数
86
+
87
+
88
+
89
+ // 登录成功后,获取用户信息
90
+ const userInfo = await client.auth.getUserProfile()
91
+ console.log(userInfo)
92
+ // 获取 accessToken
93
+ const accessToken = await client.auth.credentialsClient.getAccessToken();
94
+ console.log(accessToken)
95
+ // 用 OIDC 请求信息
96
+ // export interface ExampleData {
97
+ // result?: string;
98
+ // }
99
+ // const data = await client.oAuth2Client.request<ExampleData>("https://example.com", {withCredentials:true})
100
+ // console.log(data.result)
101
+ }
@@ -0,0 +1,11 @@
1
+ import {initializeClient} from '@cloudbase/cloudbase';
2
+ import {getFunction} from './function/function';
3
+
4
+ export const config = {
5
+ apiOrigin: "https://xbase-4gh5dh6nf62145a9.ap-shanghai.tcb-api.tencentcloudapi.com",
6
+ clientId: "xbase-4gh5dh6nf62145a9",
7
+ };
8
+ const client = initializeClient(config);
9
+ const fn = getFunction(client.app);
10
+
11
+ export {client, fn}
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <meta http-equiv="Pragma" content="no-cache">
7
+ <meta http-equiv="Cache-Control" content="no-cache">
8
+ <meta http-equiv="Expires" content="0">
9
+ <title>Demo</title>
10
+ </head>
11
+ <body>
12
+ <div>loading...</div>
13
+ <div>test</div>
14
+ </body>
15
+ </html>
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "device",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "cross-env NODE_ENV=production webpack",
9
+ "start": "cross-env NODE_ENV=development webpack-dev-server"
10
+ },
11
+ "author": "",
12
+ "license": "ISC",
13
+ "dependencies": {
14
+ "@types/qs": "^6.9.1",
15
+ "@xbasesdk/xbase": "file://Users/nx/Documents/go/xbase/src/gitlab.xunlei.cn/xbase/sdk/xbasesdk/dist",
16
+ "cross-env": "^7.0.2",
17
+ "firebase": "^9.0.2",
18
+ "qs": "^6.9.3",
19
+ "webpack": "^4.39.3",
20
+ "webpack-cli": "^4.8.0"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/cli": "^7.14.8",
24
+ "@babel/core": "^7.15.0",
25
+ "clean-webpack-plugin": "^4.0.0-alpha.0",
26
+ "copy-webpack-plugin": "^9.0.1",
27
+ "html-webpack-plugin": "^3.2.0",
28
+ "ts-loader": "^6.1.0",
29
+ "typescript": "^3.8.3",
30
+ "webpack-cli": "^4.8.0",
31
+ "webpack-dev-server": "^4.1.0"
32
+ }
33
+ }
@@ -0,0 +1,71 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Basic Options */
4
+ // "incremental": true, /* Enable incremental compilation */
5
+ "target": "es2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6
+ "module": "es2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
7
+ // "lib": [], /* Specify library files to be included in the compilation. */
8
+ "allowJs": true, /* Allow javascript files to be compiled. */
9
+ // "checkJs": true, /* Report errors in .js files. */
10
+ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11
+ // "declaration": true, /* Generates corresponding '.d.ts' file. */
12
+ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13
+ // "sourceMap": true, /* Generates corresponding '.map' file. */
14
+ // "outFile": "./", /* Concatenate and emit output to single file. */
15
+ "outDir": "./dist/", /* Redirect output structure to the directory. */
16
+ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17
+ // "composite": true, /* Enable project compilation */
18
+ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19
+ // "removeComments": true, /* Do not emit comments to output. */
20
+ // "noEmit": true, /* Do not emit outputs. */
21
+ // "importHelpers": true, /* Import emit helpers from 'tslib'. */
22
+ // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23
+ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24
+
25
+ /* Strict Type-Checking Options */
26
+ "strict": true, /* Enable all strict type-checking options. */
27
+ "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28
+ // "strictNullChecks": true, /* Enable strict null checks. */
29
+ // "strictFunctionTypes": true, /* Enable strict checking of function types. */
30
+ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31
+ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32
+ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33
+ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34
+
35
+ /* Additional Checks */
36
+ // "noUnusedLocals": true, /* Report errors on unused locals. */
37
+ // "noUnusedParameters": true, /* Report errors on unused parameters. */
38
+ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39
+ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40
+
41
+ /* Module Resolution Options */
42
+ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43
+ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44
+ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45
+ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46
+ // "typeRoots": [], /* List of folders to include type definitions from. */
47
+ // "types": [], /* Type declaration files to be included in compilation. */
48
+ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49
+ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50
+ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52
+
53
+ /* Source Map Options */
54
+ // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56
+ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57
+ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58
+
59
+ /* Experimental Options */
60
+ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61
+ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62
+
63
+
64
+
65
+ "strictPropertyInitialization": false,
66
+ "experimentalDecorators": true,
67
+ "strictNullChecks": false,
68
+ "noUnusedLocals": true,
69
+ "declaration": true
70
+ }
71
+ }
File without changes
@@ -0,0 +1,42 @@
1
+ const path = require('path');
2
+ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
3
+ const CopyPlugin = require('copy-webpack-plugin');
4
+ const HtmlWebpackPlugin = require('html-webpack-plugin');
5
+
6
+ const mode = process.env.NODE_ENV;
7
+
8
+ console.log('环境:', mode);
9
+
10
+ module.exports = {
11
+ entry: {
12
+ main: './assets/scripts/index.ts',
13
+ },
14
+ mode,
15
+ devServer: {
16
+ contentBase: path.join(__dirname, 'dist'),
17
+ port: 4000,
18
+ hot: true,
19
+ },
20
+ module: {
21
+ rules: [
22
+ {
23
+ test: /\.tsx?$/,
24
+ use: 'ts-loader',
25
+ exclude: /node_modules/
26
+ }
27
+ ]
28
+ },
29
+ resolve: {
30
+ extensions: [ '.ts', '.tsx', '.js' ]
31
+ },
32
+ output: {
33
+ filename: '[name].[hash:5].js',
34
+ path: path.resolve(__dirname, 'dist')
35
+ },
36
+ plugins: [
37
+ new CleanWebpackPlugin(),
38
+ new HtmlWebpackPlugin({
39
+ template: "./index.html"
40
+ })
41
+ ],
42
+ };