forked from microsoft/accessibility-insights-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
162 lines (141 loc) · 4.39 KB
/
esbuild.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
155
156
157
158
159
160
161
162
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const path = require('path');
const { argv } = require('process');
const NodeResolve = require('@esbuild-plugins/node-resolve');
const esbuild = require('esbuild');
const yargs = require('yargs');
const { CreateReplaceJsonValidatorPlugin } = require('./replace-plugin');
const { CreateStylePlugin } = require('./style-plugin');
const src = './src';
const argsObj = yargs(argv).argv;
const webExtensionEntryFiles = {
injected: `${src}/injected/client-init.ts`,
popup: `${src}/popup/popup-init.ts`,
insights: `${src}/views/insights/initializer.ts`,
detailsView: `${src}/DetailsView/details-view-initializer.ts`,
devtools: `${src}/Devtools/dev-tool-init.ts`,
serviceWorker: `${src}/background/service-worker-init.ts`,
debugTools: `${src}/debug-tools/initializer/debug-tools-init.tsx`,
};
const devWebExtensionOutdir = path.join(__dirname, 'extension/devBundle');
const prodWebExtensionOutDir = path.join(__dirname, 'extension/prodBundle');
function isReactDevtoolsInstalled() {
try {
require.resolve('react-devtools');
return true;
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return false;
} else {
throw error;
}
}
}
function checkToAddReactDevTools(givenEntryFiles) {
if (isReactDevtoolsInstalled()) {
givenEntryFiles.detailsView = `${src}/DetailsView/details-view-init-with-react-devtools.ts`;
givenEntryFiles.popup = `${src}/popup/popup-init-with-react-devtools.ts`;
}
}
function ignoreNodeModules(givenPlugins) {
// esbuild doesn't have an easy way to ignore node_modules for monorepos,
// so this plugin is necessary (marks all node_modules as external).
// Thread: https://github.com/evanw/esbuild/issues/619
givenPlugins.push(
NodeResolve.NodeResolvePlugin({
extensions: ['.ts', '.tsx', '.js'],
onResolved: resolved => {
if (resolved.includes('node_modules')) {
return {
external: true,
};
}
return resolved;
},
}),
);
}
// Default behavior; builds dev extension.
let entryFiles = webExtensionEntryFiles;
let outdir = devWebExtensionOutdir;
let isProd = false;
let platform = 'browser';
let external = [];
let format = 'iife';
let define;
let target = ['chrome90', 'firefox90'];
let plugins = [CreateStylePlugin()];
let minify = false;
let sourcemap = true;
switch (argsObj.env) {
case 'prod':
minify = true;
sourcemap = false;
outdir = prodWebExtensionOutDir;
define = {
global: 'globalThis',
};
plugins = plugins.concat(CreateReplaceJsonValidatorPlugin());
break;
case 'report':
entryFiles = { report: `${src}/reports/package/reporter-factory.ts` };
outdir = path.join(__dirname, 'packages/report/bundle');
format = 'cjs';
ignoreNodeModules(plugins);
break;
case 'validator':
entryFiles = {
validator: `${src}/packages/assessment-validator/index.ts`,
};
outdir = path.join(__dirname, 'packages/validator/bundle');
platform = 'node';
ignoreNodeModules(plugins);
break;
case 'axe-config':
entryFiles = {
'axe-config-generator': `${src}/packages/axe-config/generate-config-json.ts`,
};
outdir = path.join(__dirname, 'packages/axe-config/bundle');
platform = 'node';
ignoreNodeModules(plugins);
break;
// dev web extension
default:
define = {
global: 'globalThis',
};
plugins = plugins.concat(CreateReplaceJsonValidatorPlugin());
checkToAddReactDevTools(entryFiles);
break;
}
const config = {
plugins,
entryPoints: entryFiles,
format,
platform,
external,
outbase: src,
bundle: true,
minify: minify,
sourcemap: sourcemap,
target,
outdir: outdir,
outExtension: {
'.js': '.bundle.js',
},
logLevel: 'info',
define,
};
async function run() {
if (argv.includes('--watch')) {
var ctx = await esbuild.context(config);
await ctx.watch();
} else {
await esbuild.build(config);
}
}
run().catch(e => {
console.error(e);
process.exit(1);
});