-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathbuild.js
More file actions
137 lines (130 loc) · 3.93 KB
/
Copy pathbuild.js
File metadata and controls
137 lines (130 loc) · 3.93 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
128
129
130
131
132
133
134
135
136
137
import * as esbuild from 'esbuild'
import { glsl } from "esbuild-plugin-glsl";
import { createRequire } from "module";
import commandLineArgs from 'command-line-args'
import path from 'path';
import { writeFileSync } from 'fs';
import fs from 'node:fs';
import vuePlugin from 'esbuild-vue/src/index.js';
const require = createRequire(import.meta.url);
const pkg = require("./package.json");
const options = commandLineArgs([
{name: 'target', type: String},
{name: 'watch', type: Boolean},
{name: 'serve', type: Boolean},
{name: 'host', type: String},
{name: 'port', type: Number},
{name: 'analyze', type: Boolean},
])
function conditionalImportPlugin(name, config) {
return {
name: 'conditional-import-plugin-'+name,
/**
* @param {esbuild.PluginBuild} build
*/
setup(build) {
build.onResolve({ filter: config.filter }, args => {
if (config.library) {
return { path: path.join( import.meta.dirname, 'node_modules', path.dirname(args.path), config.file) };
} else {
return { path: path.join(args.resolveDir, path.dirname(args.path), config.file) };
}
});
}
};
};
function createJsonPlugin(ext_suffix, namespace) {
return {
name: `${namespace}-plugin`,
setup(build) {
// Intercept import paths with the specified extension
build.onResolve({ filter: new RegExp(`${ext_suffix}$`) }, args => ({
path: path.join(
args.resolveDir,
args.path
),
namespace: `${namespace}-ns`,
}))
// Load paths tagged with the specified namespace and treat them as JSON
build.onLoad({ filter: /.*/, namespace: `${namespace}-ns` }, async (args) => {
// Read the content of the file
const content = await fs.promises.readFile(args.path, 'utf8');
// Return the content as JSON
return {
contents: content,
loader: 'json',
}
})
}
};
};
const isApp = options.target == 'electron';
const dev_mode = options.watch || options.serve;
const minify = !dev_mode;
/**
* @type {esbuild.BuildOptions} BuildOptions
*/
const config = {
entryPoints: ['./js/main.js'],
define: {
isApp: isApp.toString(),
appVersion: `"${pkg.version}"`,
},
platform: 'node',
target: 'es2020',
format: 'esm',
bundle: true,
minify,
outfile: './dist/bundle.js',
mainFields: ['module', 'main'],
logLevel: 'info',
logOverride: {
'commonjs-variable-in-esm': 'silent'
},
external: [
'electron',
],
loader: {
'.bbtheme': 'text',
'.png': 'dataurl'
},
plugins: [
conditionalImportPlugin(2, {
filter: /native_apis/,
file: isApp ? 'native_apis.ts' : 'native_apis_web.ts'
}),
conditionalImportPlugin(3, {
filter: /vue.js/,
file: dev_mode ? 'vue.js' : 'vue.min.js',
library: true,
}),
conditionalImportPlugin(1, {
filter: /desktop/,
file: isApp ? 'desktop.js' : 'web.js'
}),
createJsonPlugin('.bbkeymap', 'bbkeymap'),
vuePlugin(),
glsl({
minify
})
],
sourcemap: true,
}
if (options.watch || options.serve) {
let ctx = await esbuild.context(config);
if (isApp) {
await ctx.watch({});
} else {
await ctx.serve({
servedir: import.meta.dirname,
host: options.host,
port: options.port
});
}
} else {
if (options.analyze) config.metafile = true;
let result = await esbuild.build(config);
if (options.analyze) {
writeFileSync('./dist/esbuild-metafile.json', JSON.stringify(result.metafile))
}
}