generated from kriasoft/graphql-starter-kit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
154 lines (137 loc) · 3.77 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import run from "@rollup/plugin-run";
import spawn from "cross-spawn";
import envars from "envars";
import minimist from "minimist";
import fs from "node:fs/promises";
import copy from "rollup-plugin-copy";
import del from "rollup-plugin-delete";
import pkg from "./package.json";
// AKA, development mode
const isWatch = process.env.ROLLUP_WATCH === "true";
const args = minimist(process.argv.slice(-2));
// Load environment variables
envars.config({ env: args.env });
/**
* Rollup bundler configuration.
*
* @see https://rollupjs.org/
* @type {import("rollup").RollupOptions}
*/
const config = {
input: "./index.ts",
output: {
dir: "./dist",
format: "es",
sourcemap: true,
chunkFileNames: "[name].chunk.js",
},
plugins: [
del({
targets: ["./dist/*", "./dist/.yarn/*"],
runOnce: true,
}),
copy({
targets: [
{
src: "views/**",
dest: "./dist/views",
},
{
src: "package.json",
dest: "./dist",
copyOnce: true,
transform(content) {
const pkg = JSON.parse(content.toString());
delete pkg.scripts;
delete pkg.devDependencies;
delete pkg.envars;
delete pkg.babel;
return JSON.stringify(pkg, null, " ");
},
},
{
src: ["../.yarnrc.yml", "../yarn.lock"],
dest: "./dist",
copyOnce: true,
},
{
src: ["../.yarn/releases"],
dest: "./dist/.yarn",
copyOnce: true,
},
],
copyOnce: true,
}),
nodeResolve({
extensions: [".ts", ".tsx", ".mjs", ".js", ".json", ".node"],
preferBuiltins: true,
}),
commonjs(),
json(),
babel({
extensions: [".ts", ".tsx", ".js", ".mjs"],
babelHelpers: "bundled",
rootMode: "upward",
}),
!isWatch &&
replace({
"process.env.NODE_ENV": `"production"`,
preventAssignment: true,
}),
isWatch &&
run({
execArgv: [
"--require=../.pnp.cjs",
"--require=source-map-support/register",
"--no-warnings",
],
}),
!isWatch && {
name: "yarn",
async writeBundle() {
// Disable global cache to make it work with Google Cloud Buildpacks
spawn.sync("yarn", ["config", "set", "enableGlobalCache", "false"], {
cwd: "./dist",
stdio: "inherit",
});
// Update yarn.lock file to include only the production dependencies
spawn.sync("yarn", ["install", "--mode=update-lockfile"], {
env: {
...process.env,
NODE_OPTIONS: undefined,
YARN_ENABLE_IMMUTABLE_INSTALLS: "false",
},
cwd: "./dist",
stdio: "inherit",
});
await fs.writeFile(
"dist/.gcloudignore",
".pnp.*\n.yarn/*\n!.yarn/patches\n!.yarn/plugins\n!.yarn/releases\n.gcloudignore\nenv.yml\n",
);
},
},
],
external: [...Object.keys(pkg.dependencies), /^node:/].filter(
// Bundle modules that do not properly support ES
(dep) => !["@sendgrid/mail", "http-errors"].includes(dep),
),
// Suppress warnings in 3rd party libraries
onwarn(warning, warn) {
if (
!(
warning.id?.includes("node_modules") ||
warning.message?.startsWith("Unknown CLI flags: env.")
)
) {
warn(warning);
}
},
};
export default config;