-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsup.config.js
91 lines (87 loc) · 2.43 KB
/
tsup.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
import { defineConfig } from "tsup";
import pkg from "./package.json";
const dependencies = Object.keys(pkg.dependencies);
const peerDependencies = Object.keys(pkg.peerDependencies);
export default defineConfig({
clean: true,
entry: ["src/index.ts"],
format: ["cjs", "esm"],
treeshake: true,
minify: true,
bundle: true,
dts: true,
tsconfig: "tsconfig.json",
splitting: !true,
sourcemap: !true,
noExternal: dependencies,
external: peerDependencies,
injectStyle: true,
esbuildOptions(options) {
options.define = {
"process.env.NODE_ENV": JSON.stringify("production"),
};
options.banner = {
js: '"use client"',
};
},
async onSuccess() {
console.log("Build success");
},
esbuildPlugins: [
{
name: "css-module",
setup(build) {
build.onResolve(
{ filter: /\.module\.css$/, namespace: "file" },
(args) => ({
path: `${args.path}#css-module`,
namespace: "css-module",
pluginData: {
pathDir: path.join(args.resolveDir, args.path),
},
})
);
build.onLoad(
{ filter: /#css-module$/, namespace: "css-module" },
async (args) => {
const { pluginData } = args;
const source = await fsPromises.readFile(
pluginData.pathDir,
"utf8"
);
let cssModule = {};
const result = await postcss([
postcssModules({
getJSON(_, json) {
cssModule = json;
},
}),
]).process(source, { from: pluginData.pathDir });
return {
pluginData: { css: result.css },
contents: `import "${
pluginData.pathDir
}"; export default ${JSON.stringify(cssModule)}`,
};
}
);
build.onResolve(
{ filter: /\.module\.css$/, namespace: "css-module" },
(args) => ({
path: path.join(args.resolveDir, args.path, "#css-module-data"),
namespace: "css-module",
pluginData: args.pluginData,
})
);
build.onLoad(
{ filter: /#css-module-data$/, namespace: "css-module" },
(args) => ({
contents: args.pluginData.css,
loader: "css",
})
);
},
},
],
target: ["es6", "esnext", "node14"],
});