-
Notifications
You must be signed in to change notification settings - Fork 47
/
esbuildHelper.mjs
179 lines (173 loc) · 4.87 KB
/
esbuildHelper.mjs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
/* eslint-disable import/no-default-export */
/* eslint-disable promise/no-nesting */
import { clean } from 'esbuild-plugin-clean';
import { copy } from 'esbuild-plugin-copy';
import { sassPlugin } from 'esbuild-sass-plugin';
import autoprefixer from 'autoprefixer';
import babel from 'esbuild-plugin-babel-cached';
import esbuild from 'esbuild';
import livereload from 'livereload';
import postcss from 'postcss';
import postcssPresetEnv from 'postcss-preset-env';
import resolve from 'esbuild-plugin-resolve';
const watch = !!process.env.WATCH;
let server;
const env = {
API_URL: process.env.API_URL,
CHECK_DEPLOY_DATE_INTERVAL: process.env.CHECK_DEPLOY_DATE_INTERVAL,
CI: process.env.CI,
CIRCLE_SHA1: process.env.CIRCLE_SHA1,
COGNITO_CLIENT_ID: process.env.COGNITO_CLIENT_ID,
COGNITO_SUFFIX: process.env.COGNITO_SUFFIX,
DYNAMODB_TABLE_NAME: process.env.DYNAMODB_TABLE_NAME,
EFCMS_DOMAIN: process.env.EFCMS_DOMAIN,
ENV: process.env.ENV,
FILE_UPLOAD_MODAL_TIMEOUT: process.env.FILE_UPLOAD_MODAL_TIMEOUT,
NO_SCANNER: process.env.NO_SCANNER,
NODE_DEBUG: process.env.NODE_DEBUG,
PDF_EXPRESS_LICENSE_KEY: process.env.PDF_EXPRESS_LICENSE_KEY,
PUBLIC_SITE_URL: process.env.PUBLIC_SITE_URL,
SCANNER_RESOURCE_URI: process.env.SCANNER_RESOURCE_URI,
SESSION_MODAL_TIMEOUT: process.env.SESSION_MODAL_TIMEOUT,
SESSION_TIMEOUT: process.env.SESSION_TIMEOUT,
STAGE: process.env.STAGE,
USTC_ENV: process.env.USTC_ENV,
WS_URL: process.env.WS_URL,
};
/**
* used for spinning up or building the UI using es build
*/
export default async function ({
entryPoint,
indexName,
outdir,
reloadServerPort,
replaceHtmlFile,
}) {
if (watch && !process.env.CI) {
server = livereload.createServer({ port: reloadServerPort });
}
const sassMap = new Map();
const buildOptions = {
bundle: true,
define: {
global: 'window',
'process.version': '""',
...Object.entries(env).reduce((acc, [key, value]) => {
acc[`process.env.${key}`] = value ? JSON.stringify(value) : '""';
return acc;
}, {}),
},
entryNames: '[name].[hash]',
entryPoints: [`web-client/src/${entryPoint}`],
format: 'esm',
loader: {
'.html': 'text',
'.pdf': 'file',
'.png': 'dataurl',
'.svg': 'dataurl',
'.ttf': 'file',
'.woff': 'file',
'.woff2': 'file',
},
logLevel: 'info',
metafile: true,
minify: process.env.USTC_ENV === 'prod',
outdir,
plugins: [
clean({
patterns: [`./${outdir}/*`],
}),
resolve({
crypto: 'crypto-browserify',
stream: 'stream-browserify',
}),
sassPlugin({
loadPaths: [
'./node_modules/@uswds',
'./node_modules/@uswds/uswds/packages',
],
async transform(source, resolveDir, filePath) {
let value = sassMap.get(filePath);
if (!value || value.source !== source) {
const { css } = await postcss([
autoprefixer,
postcssPresetEnv({ stage: 0 }),
]).process(source, { from: undefined });
value = { css, source };
sassMap.set(filePath, value);
}
return value.css;
},
}),
babel({
config: {
ignore: ['node_modules'],
plugins: ['babel-plugin-cerebral'],
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true,
},
},
],
[
'@babel/preset-react',
{
runtime: 'automatic',
},
],
],
sourceType: 'unambiguous',
targets: 'defaults',
},
filter: /\.(js|ts|jsx|tsx)$/,
}),
copy({
assets: [
{
from: ['web-client/src/favicons/**/*'],
keepStructure: true,
to: ['.'],
},
{
from: ['web-client/src/site.webmanifest'],
keepStructure: true,
to: ['.'],
},
{
from: ['web-client/src/deployed-date.txt'],
keepStructure: true,
to: ['.'],
},
{
from: [`web-client/src/${indexName}`],
keepStructure: true,
to: ['.'],
},
],
}),
{
name: 'replace-html-and-reload',
setup(build) {
build.onEnd(() => {
console.log('replacing html from setup');
replaceHtmlFile(watch);
server?.refresh('index.html');
});
},
},
],
sourcemap: process.env.USTC_ENV !== 'prod',
splitting: true,
};
if (!watch && !process.env.CI) {
await esbuild.build(buildOptions);
} else {
const ctx = await esbuild.context(buildOptions);
await ctx.watch().catch(() => process.exit(1));
}
}