-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
121 lines (113 loc) · 4.33 KB
/
rollup.config.js
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
// gets the rollup config for build
// merges the user provided config with the default config
// and returns the merged config
import babelPlugin from "@rollup/plugin-babel";
import path from "path";
import fs from "fs";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import { createRequire } from "node:module";
import torusConfig from "./torus.config.js";
import paths, { appModuleFileExtensions } from "./paths.js";
import { readFile, readJSONFile } from "../helpers/utils.js";
import babelConfig from "./babel.config.js";
import tsconfigBuild from "./tsconfig.build.js";
const require = createRequire(import.meta.url);
const pkg = readJSONFile(paths.appPackageJson);
const babelPluginOptions = {
extensions: appModuleFileExtensions.map((x) => `.${x}`),
babelHelpers: "runtime",
babelrc: false,
...babelConfig,
configFile: false,
};
if (fs.existsSync(paths.appBrowserslistConfig)) {
babelPluginOptions.browserslistConfigFile = paths.appBrowserslistConfig;
} else {
babelPluginOptions.targets = torusConfig.browserslistrc;
}
// We just return the user's array value
const userConfig = fs.existsSync(paths.appRollupConfig) ? await readFile(paths.appRollupConfig) : {};
// we want to create only one build set with rollup
const getDefaultConfig = (name) => {
const allDeps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})];
const baseConfig = {
input: paths.appIndexFile,
external: [...allDeps, ...allDeps.map((x) => new RegExp(`^${x}/`)), /@babel\/runtime/],
...(userConfig.baseConfig || {}),
};
// const esmCombinedExport = {
// ...baseConfig,
// output: { file: path.resolve(paths.appBuild, `${name}.esm.js`), format: "es", sourcemap: process.env.NODE_ENV === "development" },
// plugins: [
// // Allows node_modules resolution
// resolve({
// extensions: appModuleFileExtensions.map((x) => `.${x}`),
// modulesOnly: true,
// preferBuiltins: false,
// }),
// ...(baseConfig.plugins || []),
// babelPlugin(babelPluginOptions),
// ],
// };
const esmOriginalExport = {
...baseConfig,
output: { preserveModules: true, dir: path.resolve(paths.appBuild, "lib.esm"), format: "es", sourcemap: process.env.NODE_ENV === "development" },
plugins: [
// Allows node_modules resolution
resolve({
extensions: appModuleFileExtensions.map((x) => `.${x}`),
modulesOnly: true,
preferBuiltins: false,
}),
...(baseConfig.plugins || []),
babelPlugin(babelPluginOptions),
],
};
// const cjsCombinedExport = {
// ...baseConfig,
// output: { file: path.resolve(paths.appBuild, `${name}.cjs.js`), format: "cjs", sourcemap: process.env.NODE_ENV === "development" },
// plugins: [
// // Allows node_modules resolution
// resolve({
// extensions: appModuleFileExtensions.map((x) => `.${x}`),
// modulesOnly: true,
// preferBuiltins: false,
// }),
// commonjs(),
// babelPlugin(babelPluginOptions),
// ...(baseConfig.plugins || []),
// ],
// };
const cjsOriginalExport = {
...baseConfig,
output: { preserveModules: true, dir: path.resolve(paths.appBuild, "lib.cjs"), format: "cjs", sourcemap: process.env.NODE_ENV === "development" },
plugins: [
typescript({
...tsconfigBuild.compilerOptions,
tsconfig: fs.existsSync(paths.appTsBuildConfig) ? paths.appTsBuildConfig : paths.appTsConfig,
noEmitOnError: process.env.NODE_ENV === "production",
outDir: path.resolve(paths.appBuild, "lib.cjs"),
declarationDir: path.resolve(paths.appBuild, "lib.cjs/types"),
typescript: require("ts-patch/compiler"),
sourceMap: process.env.NODE_ENV === "development",
}),
// Allows node_modules resolution
resolve({
extensions: appModuleFileExtensions.map((x) => `.${x}`),
modulesOnly: true,
preferBuiltins: false,
}),
...(baseConfig.plugins || []),
babelPlugin(babelPluginOptions),
],
};
const finalTasks = [];
// if (torusConfig.esm) finalTasks.push(esmCombinedExport);
if (torusConfig.libEsm) finalTasks.push(esmOriginalExport);
if (torusConfig.libCjs) finalTasks.push(cjsOriginalExport);
return finalTasks;
};
export default (name) => {
return getDefaultConfig(name);
};