-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.ts
144 lines (137 loc) · 4.38 KB
/
rollup.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import typescript from "@rollup/plugin-typescript";
import { dts } from "rollup-plugin-dts";
import replace from "@rollup/plugin-replace";
import autoExternal from "rollup-plugin-auto-external";
import terser from "@rollup/plugin-terser";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import {
babel,
// getBabelOutputPlugin,
} from "@rollup/plugin-babel";
const input = "src/index.ts";
function createPlatformsBuildConfig(platform: "dom" | "native", format: "cjs" | "esm") {
return {
input: `src/platforms/${platform}.ts`,
external: [`react-${platform}`],
output: [{
format,
dir: "dist",
entryFileNames: `platform.${format}${platform === "dom" ? "" : ".native"}.js`,
}],
};
}
function createTsDeclareFileBuildConfig() {
const curDate = new Date();
const curDay = curDate.getDate();
// Header declaration of the packaged file
const banner =
"/**\n" +
" * resy\n" +
" * An easy-to-use React data state manager\n" +
" * created by liushanbao <1262300490@qq.com>\n" +
` * (c) 2020-05-05-${curDate.getFullYear()}-${curDate.getMonth() + 1}-${curDay < 10 ? `0${curDay}` : curDay}\n` +
" * Released under the MIT License.\n" +
" */";
return {
input,
output: {
file: "dist/resy.d.ts",
format: "esm",
banner,
},
plugins: [
dts({
tsconfig: "./tsconfig.json",
compilerOptions: {
target: 99,
module: 99,
},
}),
],
};
}
function createModuleBuildConfig(format: "cjs" | "esm", isTerser?: boolean) {
const platforms = `./platform.${format}`;
const terserOpts = isTerser ? [terser()] : [];
return {
input,
output: {
file: `dist/resy.${format}.${isTerser ? "prod." : ""}js`,
format,
},
/**
* @description Because use-sync-external-store, this package only exports CJS modules.
* So here we need to do a separate special identification of an external extension.
* Otherwise, the special export processing in the code will be invalid.
*/
external: [
"react",
platforms,
"use-sync-external-store/shim",
// /@babel\/runtime/,
],
plugins: [
replace({
"react-platform": platforms,
preventAssignment: true,
}),
autoExternal(),
nodeResolve(),
babel({
/**
* @description I forgot which version of "@ rollup/plugin label" requires the display
* of configuration extensions for compiling TS settings,
* and the settings to be displayed are @babel/presets-env
*/
extensions: [".js", ".jsx", ".ts", ".tsx"],
presets: ["@babel/preset-env"],
exclude: "node_modules/**",
// babelHelpers: "runtime",
babelHelpers: "bundled",
}),
/**
* @description "getBabelOutputPlugin" plugin and "runtime" settings are combined
* with "@babel/plugin-transform-runtime", "@babel/runtime-corejs3" and "@babel/runtime",
* "@babel/runtime-corejs3" and "@babel/runtime" these two packs need to be placed in "dependencies"
* And "@babel/plugin-transform-runtime" pack are placed in "devDependencies".
* At the same time, the babel.config.js configuration in the root directory is as follows:
* module.exports = api => {
* api.cache(true);
* return {
* presets: [
* "@babel/preset-env"
* ],
* plugins: [
* [
* "@babel/plugin-transform-runtime",
* {
* corejs: 3
* }
* ]
* ]
* };
* };
* it`s to make the code compatible. However, there is no overall polyfill for the current library.
* Instead, it is left to the developer to handle the overall system involved in the construction to do polyfill.
*/
// getBabelOutputPlugin({
// configFile: "./babel.config.js",
// }),
typescript({
tsconfig: "./tsconfig.json",
}),
...terserOpts,
]
};
}
export default [
createPlatformsBuildConfig("dom", "cjs"),
createPlatformsBuildConfig("native", "cjs"),
createPlatformsBuildConfig("dom", "esm"),
createPlatformsBuildConfig("native", "esm"),
createTsDeclareFileBuildConfig(),
createModuleBuildConfig("cjs"),
createModuleBuildConfig("cjs", true),
createModuleBuildConfig("esm"),
createModuleBuildConfig("esm", true),
];