-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtsup.config.ts
238 lines (222 loc) · 7.26 KB
/
tsup.config.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { polyfillNode } from "esbuild-plugin-polyfill-node";
import { globSync } from 'glob';
// import { globbySync } from 'globby';
// import { print } from 'q-i';
import { defineConfig, type Options } from 'tsup';
interface MyOptions extends Options {
d?: string
}
const DIR_SRC = 'src/main/resources';
const AND_BELOW = '**';
const TEST_EXT = `{spec,test}.{ts,tsx}`;
const TEST_FILES = `*.${TEST_EXT}`;
const ASSETS_PATH = `${DIR_SRC}/assets`;
// const CLIENT_GLOB_EXTENSIONS = '{tsx,ts,jsx,js}';
const DECLARATION_FILES = globSync(`${DIR_SRC}/${AND_BELOW}/*.d.ts`);
// const DECLARATION_FILES = globbySync(`${RESOURCES_PATH}/${AND_BELOW}/*.d.ts`);
// print({DECLARATION_FILES}, { maxItems: Infinity }); process.exit(0);
const ASSET_FILES = globSync(`${ASSETS_PATH}/${AND_BELOW}/*.*`);
// const ASSET_FILES = globbySync(`${ASSETS_PATH}/${AND_BELOW}/*.*`);
// print({ASSET_FILES}, { maxItems: Infinity }); process.exit(0);
const SERVER_FILES = globSync(
`${DIR_SRC}/${AND_BELOW}/*.ts`,
{
absolute: false,
ignore: DECLARATION_FILES.concat(
ASSET_FILES,
globSync(`${DIR_SRC}/${AND_BELOW}/${TEST_FILES}`)
),
}
);
// print({SERVER_FILES}, { maxItems: Infinity }); // process.exit(0);
export default defineConfig((options: MyOptions) => {
// print(options, { maxItems: Infinity });
if (options.d === 'build/resources/main') {
return {
entry: SERVER_FILES,
esbuildPlugins: [
polyfillNode({
globals: {
buffer: false,
process: false
},
polyfills: {
_stream_duplex: false,
_stream_passthrough: false,
_stream_readable: false,
_stream_transform: false,
_stream_writable: false,
assert: false,
'assert/strict': false,
async_hooks: false,
buffer: false,
child_process: false,
cluster: false,
console: false,
constants: false,
crypto: false,
dgram: false,
diagnostics_channel: false,
dns: false,
domain: false,
events: false,
fs: false,
'fs/promises': false,
http: false,
http2: false,
https: false,
module: false,
net: false,
os: false,
path: false,
perf_hooks: false,
process: "empty",
punycode: false,
querystring: false,
readline: false,
repl: false,
stream: false,
string_decoder: false,
sys: false,
timers: false,
'timers/promises': false,
tls: false,
tty: false,
url: false,
util: true,
v8: false,
vm: false,
wasi: false,
worker_threads: false,
zlib: false,
}
}) // ReferenceError: "navigator" is not defined
],
esbuildOptions(options, context) {
// options.alias = {
// 'util': './src/main/resources/my_node_modules/util/util.js'
// };
options.banner = {
js: `const globalThis = (1, eval)('this');`
};
options.chunkNames = '_chunks/[name]-[hash]';
// options.target = 'es2020'; // tsup does this for us :)
// options.tsconfig = 'tsconfig.tsup.json'
},
external: [ // All these are available runtime in the jar file:
'/lib/cache',
'/lib/enonic/asset',
'/lib/enonic/static',
/^\/lib\/explorer/,
'/lib/galimatias',
'/lib/graphql',
'/lib/graphql-connection',
/^\/lib\/guillotine/,
'/lib/http-client',
'/lib/license',
'/lib/router',
'/lib/util',
'/lib/vanilla',
'/lib/thymeleaf',
'/lib/xp/admin',
'/lib/xp/app',
'/lib/xp/auditlog',
'/lib/xp/auth',
'/lib/xp/cluster',
'/lib/xp/common',
'/lib/xp/content',
'/lib/xp/context',
'/lib/xp/event',
'/lib/xp/export',
'/lib/xp/grid',
'/lib/xp/i18n',
'/lib/xp/io',
'/lib/xp/mail',
'/lib/xp/node',
'/lib/xp/portal',
'/lib/xp/project',
'/lib/xp/repo',
'/lib/xp/scheduler',
'/lib/xp/schema',
'/lib/xp/task',
'/lib/xp/value',
'/lib/xp/vhost',
'/lib/xp/websocket',
],
format: 'cjs',
inject: [
// 'node_modules/buffer/index.js',
// 'node_modules/core-js/stable/global-this.js', // doesn't help for esbuild-plugin-polyfill-node
// 'src/main/resources/lib/nashorn/global.ts', // does help for esbuild-plugin-polyfill-node :)
// 'node_modules/core-js/stable/array/flat.js',
// This works, but might not be needed, because I'm using arrayIncludes from @enonic/js-utils
'node_modules/core-js/stable/array/includes.js',
// 'node_modules/core-js/stable/math/trunc.js', // Needed by pretty-ms
// 'node_modules/core-js/stable/number/is-finite.js', // Needed by pretty-ms
// 'node_modules/core-js/stable/number/is-integer.js',
// 'node_modules/core-js/stable/parse-float.js',
// This is needed when treeshake: true
// So I'm assuming that the injects are not treeshaken
// 'node_modules/core-js/stable/reflect/index.js'
],
'main-fields': 'main,module',
minify: false, // Minifying server files makes debugging harder
noExternal: [
// To check if you have forgotten any try this command
// clear && npm run clean && npm run build:tsup && grep -r 'require(' build/resources/main|grep -v '("\.'|grep -v '("\/'
// TODO: These might need Polyfills for global to run in Nashorn:
// '@enonic/explorer-utils', //
// '@enonic/fnv-plus', //
/^@enonic\/js-utils/, // many places
// 'array.prototype.find', // tasks/webcrawl/webcrawl
'cheerio', //
'gql-query-builder', //
'core-js', //
// 'd3-dsv', //
'deep-object-diff', //
// 'diff', //
'fast-deep-equal', //
'human-object-diff', //
// 'is-generator-function', // my_node_modules/util
'object.getownpropertydescriptors', // tasks/webcrawl/webcrawl
'polyfill-crypto.getrandomvalues', //
// 'reflect-metadata', //
'robots-txt-guard', //
'serialize-javascript', //
// 'traverse', //
'uri-js', //
// Without it TypeError: null is not a function
// 'util', // node_modules/object-inspect/util.inspect.js
],
platform: 'neutral',
// https://tsup.egoist.dev/#code-splitting
// Code splitting currently only works with the esm output format,
// and it's enabled by default.
// If you want code splitting for cjs output format as well, try
// using --splitting flag which is an experimental feature to get
// rid of the limitation in esbuild.
splitting: true, // Might cause weird error messages at runtime
shims: false, // https://tsup.egoist.dev/#inject-cjs-and-esm-shims
sourcemap: false,
// target: 'es5', // Set in tsconfig.tsup.json
// https://tsup.egoist.dev/#tree-shaking
// Tree shaking
// esbuild has tree shaking enabled by default, but sometimes it's
// not working very well, see #1794 #1435, so tsup offers an
// additional option to let you use Rollup for tree shaking instead
// This option has the same type as the treeshake option in Rollup:
// https://rollupjs.org/configuration-options/#treeshake
//
// Fails after 1m 10s when splitting: false
// treeshake: true // causes ReferenceError: "Reflect" is not defined
//
// In conclusion: The default tree-shaking that esbuild provides is
// enough, we don't need extra tree-shaking rollup.
tsconfig: 'tsconfig.tsup.json'
};
}
if (options.d === 'build/resources/main/static') {
return import('./tsup/static').then(m => m.default());
}
throw new Error(`Unconfigured directory:${options.d}!`)
});