Skip to content

Commit 21da5e6

Browse files
committed
build: generate separate d.ts file for esm/cjs
1 parent f516515 commit 21da5e6

File tree

11 files changed

+289
-435
lines changed

11 files changed

+289
-435
lines changed

package.json

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,157 @@
1818
"release:major": "standard-version --release-as major",
1919
"commit": "git-cz"
2020
},
21-
"main": "lib/cjs/index.js",
22-
"module": "lib/es/index.js",
23-
"browser": "lib/umd/index.js",
24-
"unpkg": "lib/umd/index.js",
25-
"types": "lib/index.d.ts",
26-
"typings": "lib/index.d.ts",
21+
"main": "dist/cjs/index.js",
22+
"module": "dist/esm/index.js",
23+
"browser": "dist/umd/index.min.js",
24+
"unpkg": "dist/umd/index.min.js",
25+
"types": "dist/types/index.d.ts",
26+
"typings": "dist/types/index.d.ts",
2727
"exports": {
2828
".": {
29-
"import": "./lib/es/index.js",
30-
"require": "./lib/cjs/index.js",
31-
"types": "./lib/index.d.ts"
29+
"import": "./dist/esm/index.js",
30+
"require": "./dist/cjs/index.js",
31+
"types": "./dist/types/index.d.ts"
32+
},
33+
"./array": {
34+
"import": "./dist/esm/array.js",
35+
"require": "./dist/cjs/array.js",
36+
"types": "./dist/types/array.d.ts"
37+
},
38+
"./async": {
39+
"import": "./dist/esm/async.js",
40+
"require": "./dist/cjs/async.js",
41+
"types": "./dist/types/async.d.ts"
42+
},
43+
"./base64": {
44+
"import": "./dist/esm/base64.js",
45+
"require": "./dist/cjs/base64.js",
46+
"types": "./dist/types/base64.d.ts"
47+
},
48+
"./clipboard": {
49+
"import": "./dist/esm/clipboard.js",
50+
"require": "./dist/cjs/clipboard.js",
51+
"types": "./dist/types/clipboard.d.ts"
52+
},
53+
"./cloneDeep": {
54+
"import": "./dist/esm/cloneDeep.js",
55+
"require": "./dist/cjs/cloneDeep.js",
56+
"types": "./dist/types/cloneDeep.d.ts"
57+
},
58+
"./cookie": {
59+
"import": "./dist/esm/cookie.js",
60+
"require": "./dist/cjs/cookie.js",
61+
"types": "./dist/types/cookie.d.ts"
62+
},
63+
"./date": {
64+
"import": "./dist/esm/date.js",
65+
"require": "./dist/cjs/date.js",
66+
"types": "./dist/types/date.d.ts"
67+
},
68+
"./dom": {
69+
"import": "./dist/esm/dom.js",
70+
"require": "./dist/cjs/dom.js",
71+
"types": "./dist/types/dom.d.ts"
72+
},
73+
"./download": {
74+
"import": "./dist/esm/download.js",
75+
"require": "./dist/cjs/download.js",
76+
"types": "./dist/types/download.d.ts"
77+
},
78+
"./easing": {
79+
"import": "./dist/esm/easing.js",
80+
"require": "./dist/cjs/easing.js",
81+
"types": "./dist/types/easing.d.ts"
82+
},
83+
"./file": {
84+
"import": "./dist/esm/file.js",
85+
"require": "./dist/cjs/file.js",
86+
"types": "./dist/types/file.d.ts"
87+
},
88+
"./func": {
89+
"import": "./dist/esm/func.js",
90+
"require": "./dist/cjs/func.js",
91+
"types": "./dist/types/func.d.ts"
92+
},
93+
"./math": {
94+
"import": "./dist/esm/math.js",
95+
"require": "./dist/cjs/math.js",
96+
"types": "./dist/types/math.d.ts"
97+
},
98+
"./number": {
99+
"import": "./dist/esm/number.js",
100+
"require": "./dist/cjs/number.js",
101+
"types": "./dist/types/number.d.ts"
102+
},
103+
"./object": {
104+
"import": "./dist/esm/object.js",
105+
"require": "./dist/cjs/object.js",
106+
"types": "./dist/types/object.d.ts"
107+
},
108+
"./path": {
109+
"import": "./dist/esm/path.js",
110+
"require": "./dist/cjs/path.js",
111+
"types": "./dist/types/path.d.ts"
112+
},
113+
"./qs": {
114+
"import": "./dist/esm/qs.js",
115+
"require": "./dist/cjs/qs.js",
116+
"types": "./dist/types/qs.d.ts"
117+
},
118+
"./random": {
119+
"import": "./dist/esm/random.js",
120+
"require": "./dist/cjs/random.js",
121+
"types": "./dist/types/random.d.ts"
122+
},
123+
"./string": {
124+
"import": "./dist/esm/string.js",
125+
"require": "./dist/cjs/string.js",
126+
"types": "./dist/types/string.d.ts"
127+
},
128+
"./tooltip": {
129+
"import": "./dist/esm/tooltip.js",
130+
"require": "./dist/cjs/tooltip.js",
131+
"types": "./dist/types/tooltip.d.ts"
132+
},
133+
"./tree": {
134+
"import": "./dist/esm/tree.js",
135+
"require": "./dist/cjs/tree.js",
136+
"types": "./dist/types/tree.d.ts"
137+
},
138+
"./type": {
139+
"import": "./dist/esm/type.js",
140+
"require": "./dist/cjs/type.js",
141+
"types": "./dist/types/type.d.ts"
142+
},
143+
"./unique": {
144+
"import": "./dist/esm/unique.js",
145+
"require": "./dist/cjs/unique.js",
146+
"types": "./dist/types/unique.d.ts"
147+
},
148+
"./url": {
149+
"import": "./dist/esm/url.js",
150+
"require": "./dist/cjs/url.js",
151+
"types": "./dist/types/url.d.ts"
152+
},
153+
"./validator": {
154+
"import": "./dist/esm/validator.js",
155+
"require": "./dist/cjs/validator.js",
156+
"types": "./dist/types/validator.d.ts"
157+
},
158+
"./variable": {
159+
"import": "./dist/esm/variable.js",
160+
"require": "./dist/cjs/variable.js",
161+
"types": "./dist/types/variable.d.ts"
162+
},
163+
"./watermark": {
164+
"import": "./dist/esm/watermark.js",
165+
"require": "./dist/cjs/watermark.js",
166+
"types": "./dist/types/watermark.d.ts"
32167
}
33168
},
34169
"sideEffects": false,
35170
"files": [
36-
"lib"
171+
"dist"
37172
],
38173
"keywords": [
39174
"sculp-js",

rollup.config.js

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,69 +29,100 @@ function transformCamel(str) {
2929
const moduleName = transformCamel(pkgName);
3030

3131
export default [
32+
// ESM build (JavaScript only)
3233
{
3334
input: `src/${isCore ? 'core-index.ts' : 'index.ts'}`,
34-
output: [
35-
{
36-
dir: 'lib/cjs',
37-
format: 'cjs',
38-
entryFileNames: '[name].js',
39-
preserveModules: true,
40-
preserveModulesRoot: 'src',
41-
exports: 'named',
42-
banner
43-
},
44-
{
45-
dir: 'lib/es',
46-
format: 'esm',
47-
entryFileNames: '[name].js',
48-
preserveModules: true,
49-
preserveModulesRoot: 'src',
50-
exports: 'named',
51-
banner
52-
}
53-
],
35+
output: {
36+
dir: 'dist/esm',
37+
format: 'esm',
38+
entryFileNames: '[name].js',
39+
preserveModules: true,
40+
preserveModulesRoot: 'src',
41+
exports: 'named',
42+
banner
43+
},
5444
plugins: [
5545
clear({
56-
targets: ['lib']
46+
targets: ['dist']
5747
}),
5848
subpathExternals(pkg),
5949
resolve(),
6050
commonjs(),
6151
typescript({
6252
tsconfig: 'tsconfig.json',
63-
include: ['src/**/*.ts']
53+
include: ['src/**/*.ts'],
54+
// declaration: true,
55+
declarationMap: true,
56+
outDir: 'dist/esm'
6457
}),
6558
json()
6659
]
6760
},
61+
// CJS build (JavaScript only)
6862
{
6963
input: `src/${isCore ? 'core-index.ts' : 'index.ts'}`,
7064
output: {
71-
dir: 'lib/umd',
72-
format: 'umd',
73-
entryFileNames: 'index.js',
74-
name: moduleName,
65+
dir: 'dist/cjs',
66+
format: 'cjs',
67+
entryFileNames: '[name].js',
68+
preserveModules: true,
69+
preserveModulesRoot: 'src',
70+
exports: 'named',
7571
banner
7672
},
7773
plugins: [
7874
resolve(),
7975
commonjs(),
8076
typescript({
8177
tsconfig: 'tsconfig.json',
82-
include: ['src/**/*.ts']
78+
include: ['src/**/*.ts'],
79+
// declaration: true,
80+
declarationMap: true,
81+
outDir: 'dist/cjs'
8382
}),
84-
json(),
85-
terser()
83+
json()
8684
]
8785
},
86+
// Type declarations build (separate build for .d.ts files)
8887
{
89-
// 生成 .d.ts 类型声明文件
9088
input: `src/${isCore ? 'core-index.ts' : 'index.ts'}`,
9189
output: {
92-
file: pkg.types,
90+
dir: 'dist/types',
9391
format: 'esm'
9492
},
95-
plugins: [dts()]
93+
plugins: [
94+
resolve(),
95+
commonjs(),
96+
typescript({
97+
tsconfig: 'tsconfig.json',
98+
include: ['src/**/*.ts'],
99+
declaration: true,
100+
declarationMap: true,
101+
outDir: 'dist/types',
102+
emitDeclarationOnly: true
103+
}),
104+
json()
105+
]
106+
},
107+
// UMD build (JavaScript only)
108+
{
109+
input: `src/${isCore ? 'core-index.ts' : 'index.ts'}`,
110+
output: {
111+
dir: 'dist/umd',
112+
format: 'umd',
113+
entryFileNames: 'index.min.js',
114+
name: moduleName,
115+
banner
116+
},
117+
plugins: [
118+
resolve(),
119+
commonjs(),
120+
typescript({
121+
tsconfig: 'tsconfig.json',
122+
include: ['src/**/*.ts']
123+
}),
124+
json(),
125+
terser()
126+
]
96127
}
97128
];

src/base64.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,70 @@
11
import { getGlobal } from './func';
22
import { isNullOrUnDef } from './type';
3-
import { weAtob, weBtoa } from './we-decode';
3+
4+
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
5+
// eslint-disable-next-line
6+
const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
7+
/**
8+
* 字符串编码成Base64, 平替浏览器的btoa, 不包含中文的处理 (适用于任何环境,包括小程序)
9+
* @param {string} string
10+
* @returns {string}
11+
*/
12+
export function weBtoa(string: string): string {
13+
// 同window.btoa: 字符串编码成Base64
14+
string = String(string);
15+
let bitmap,
16+
a,
17+
b,
18+
c,
19+
result = '',
20+
i = 0;
21+
const strLen = string.length;
22+
const rest = strLen % 3;
23+
for (; i < strLen; ) {
24+
if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
25+
throw new TypeError(
26+
"Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
27+
);
28+
bitmap = (a << 16) | (b << 8) | c;
29+
result +=
30+
b64.charAt((bitmap >> 18) & 63) +
31+
b64.charAt((bitmap >> 12) & 63) +
32+
b64.charAt((bitmap >> 6) & 63) +
33+
b64.charAt(bitmap & 63);
34+
}
35+
return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
36+
}
37+
/**
38+
* Base64解码为原始字符串,平替浏览器的atob, 不包含中文的处理(适用于任何环境,包括小程序)
39+
* @param {string} string
40+
* @returns {string}
41+
*/
42+
export function weAtob(string: string): string {
43+
// 同window.atob: Base64解码为原始字符串
44+
string = String(string).replace(/[\t\n\f\r ]+/g, '');
45+
if (!b64re.test(string))
46+
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
47+
string += '=='.slice(2 - (string.length & 3));
48+
let bitmap,
49+
result = '',
50+
r1,
51+
r2,
52+
i = 0;
53+
for (const strLen = string.length; i < strLen; ) {
54+
bitmap =
55+
(b64.indexOf(string.charAt(i++)) << 18) |
56+
(b64.indexOf(string.charAt(i++)) << 12) |
57+
((r1 = b64.indexOf(string.charAt(i++))) << 6) |
58+
(r2 = b64.indexOf(string.charAt(i++)));
59+
result +=
60+
r1 === 64
61+
? String.fromCharCode((bitmap >> 16) & 255)
62+
: r2 === 64
63+
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
64+
: String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
65+
}
66+
return result;
67+
}
468

569
function stringToUint8Array(str) {
670
const utf8 = encodeURIComponent(str); // 将字符串转换为 UTF-8 编码
@@ -19,7 +83,7 @@ function uint8ArrayToString(uint8Array) {
1983
* @param base64 base64编码的字符串
2084
* @returns 原始字符串,包括中文内容
2185
*/
22-
export function decodeFromBase64(base64: string): string {
86+
export function b64decode(base64: string): string {
2387
const binaryString = !isNullOrUnDef(getGlobal('atob')) ? (getGlobal('atob') as any)(base64) : weAtob(base64);
2488
const len = binaryString.length;
2589
const bytes = new Uint8Array(len);
@@ -37,7 +101,7 @@ export function decodeFromBase64(base64: string): string {
37101
* @param rawStr 原始字符串,包括中文内容
38102
* @returns base64编码的字符串
39103
*/
40-
export function encodeToBase64(rawStr: string): string {
104+
export function b64encode(rawStr: string): string {
41105
const utf8Array = !isNullOrUnDef(getGlobal('TextEncoder'))
42106
? new (getGlobal('TextEncoder') as any)().encode(rawStr)
43107
: stringToUint8Array(rawStr);

src/core-index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export * from './number';
1111
export * from './unique';
1212
export * from './tree';
1313
export * from './math';
14-
export * from './we-decode';
1514
export * from './base64';
1615
export * from './validator';
1716
export * from './variable';

0 commit comments

Comments
 (0)