-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvite.lib.js
More file actions
127 lines (117 loc) · 3.64 KB
/
Copy pathvite.lib.js
File metadata and controls
127 lines (117 loc) · 3.64 KB
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
const fs = require("fs");
const path = require("path");
const browserTargets = async () => {
const { default: browserslistToEsbuild } =
await import("browserslist-to-esbuild");
return browserslistToEsbuild(undefined, { env: "production" });
};
const browserProcessEnvValues = (mode, env) => ({
NODE_ENV: mode,
PUBLIC_URL: env.PUBLIC_URL ?? "",
ASSETS_URL: env.ASSETS_URL || env.PUBLIC_URL || "",
HTML_RENDERER_URL: env.HTML_RENDERER_URL ?? "",
REACT_APP_API_ENDPOINT: env.REACT_APP_API_ENDPOINT ?? "",
REACT_APP_AUTHENTICATION_CLIENT_ID:
env.REACT_APP_AUTHENTICATION_CLIENT_ID ?? "",
REACT_APP_ALLOWED_IFRAME_ORIGINS: env.REACT_APP_ALLOWED_IFRAME_ORIGINS ?? "",
REACT_APP_SCRATCH_FRAME_URL: env.REACT_APP_SCRATCH_FRAME_URL ?? "",
REACT_APP_SENTRY_DSN: env.REACT_APP_SENTRY_DSN ?? "",
REACT_APP_SENTRY_ENV: env.REACT_APP_SENTRY_ENV ?? "",
});
const processEnvBuildDefine = (mode, env) => {
const values = browserProcessEnvValues(mode, env);
return Object.fromEntries(
Object.entries(values).map(([key, value]) => [
`process.env.${key}`,
JSON.stringify(value),
]),
);
};
// Vite dev serving does not transform these process.env references.
const injectProcessEnvIntoDevHtml = (mode, env) => ({
name: "inject-process-env-into-dev-html",
apply: "serve",
transformIndexHtml: {
order: "pre",
handler() {
return [
{
tag: "script",
injectTo: "head-prepend",
children: `window.process = { env: ${JSON.stringify(browserProcessEnvValues(mode, env))} };`,
},
];
},
},
});
const resolveViteBase = (mode, env) => {
if (mode === "development") return "/";
const raw = env.PUBLIC_URL || "/";
return raw.endsWith("/") ? raw : `${raw}/`;
};
const editorVitePlugins = (react, svgr, nodePolyfills) => [
react(),
svgr({
include: "**/src/assets/icons/**/*.svg",
svgrOptions: { exportType: "default" },
}),
nodePolyfills({
include: ["stream", "path", "url", "assert"],
globals: { process: false, Buffer: true, global: true },
}),
];
const emitDeferredClassicBundleHtml = ({ template, fileName, bundle }) => ({
name: `emit-deferred-classic-bundle-html:${fileName}`,
apply: "build",
generateBundle() {
const html = fs.readFileSync(template, "utf8").replace(
/<script\s+type="module"\s+src="\/src\/[^"]+"><\/script>\s*/,
// Defer keeps bundle evaluation after document.body exists.
`<script defer src="${bundle}"></script>`,
);
this.emitFile({ type: "asset", fileName, source: html });
},
});
const classicIifeBuildOptions = ({
root,
entry,
name,
cleansOutput = false,
target,
}) => ({
target,
outDir: path.resolve(root, "build"),
emptyOutDir: cleansOutput,
copyPublicDir: cleansOutput,
chunkSizeWarningLimit: 10 * 1024,
rolldownOptions: {
transform: { define: { "import.meta": "{}" } },
input: path.resolve(root, entry),
output: {
format: "iife",
entryFileNames: `${name}.js`,
assetFileNames: "assets/[name]-[hash][extname]",
},
},
});
const copyDirectoryContentsTarget = ({ root, sourceDirectory, dest }) => {
const absoluteSourceDirectory = path.resolve(root, sourceDirectory);
const sourceDirectorySegmentCount = path
.relative(root, absoluteSourceDirectory)
.split(path.sep).length;
return {
src: `${absoluteSourceDirectory.replace(/\\/g, "/")}/**/*`,
dest,
rename: { stripBase: sourceDirectorySegmentCount },
};
};
module.exports = {
browserTargets,
processEnvBuildDefine,
injectProcessEnvIntoDevHtml,
resolveViteBase,
editorVitePlugins,
emitDeferredClassicBundleHtml,
classicIifeBuildOptions,
copyDirectoryContentsTarget,
};