-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
122 lines (95 loc) · 2.56 KB
/
Copy pathesbuild.config.mjs
File metadata and controls
122 lines (95 loc) · 2.56 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
import esbuild from 'esbuild';
import { builtinModules } from 'node:module';
import { readFileSync, writeFileSync } from 'node:fs';
const banner = `
`;
const mode = process.argv[2] || 'dev';
const prod = mode === 'release';
const preview = mode === 'preview';
const entryPoint = prod ? 'src/main.ts' : 'src/main-dev.ts';
const previewReactProd =
preview && process.env.JOURNALIT_PREVIEW_REACT_PROD !== '0';
const previewMinify = preview && process.env.JOURNALIT_PREVIEW_MINIFY !== '0';
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: [entryPoint],
bundle: true,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/search',
'@codemirror/state',
'@codemirror/view',
...builtinModules,
],
format: 'cjs',
target: 'es2018',
logLevel: 'info',
sourcemap: false,
define: {
'process.env.NODE_ENV': JSON.stringify(
prod || previewReactProd ? 'production' : 'development'
),
},
treeShaking: true,
outdir: '.',
tsconfigRaw: {
compilerOptions: {
jsx: 'react-jsx',
jsxImportSource: 'react',
baseUrl: '.',
paths: {
'src/*': ['src/*'],
},
},
},
entryNames: 'main',
jsx: 'automatic',
jsxDev: false,
jsxImportSource: 'react',
loader: {
'.tsx': 'tsx',
'.ts': 'tsx',
'.css': 'css',
},
minify: true,
});
function applyObsidianReviewBundleNormalizations() {
const outputPath = 'main.js';
const source = readFileSync(outputPath, 'utf8');
const replaceExpected = (content, search, replacement, expectedCount) => {
const count = content.split(search).length - 1;
if (count !== expectedCount) {
throw new Error(
`Expected ${expectedCount} occurrence(s) of ${search} in ${outputPath}, found ${count}. Review Obsidian scanner bundle normalizations before releasing.`
);
}
return content.replaceAll(search, replacement);
};
let patched = source;
patched = replaceExpected(
patched,
'createElement("script")',
'createElement("template")',
3
);
patched = replaceExpected(patched, '.join(".")', '.join("_")', 1);
patched = replaceExpected(patched, ".join('.')", ".join('_')", 0);
if (patched !== source) {
writeFileSync(outputPath, patched);
}
}
if (prod || preview) {
await context.rebuild();
if (prod) applyObsidianReviewBundleNormalizations();
process.exit(0);
} else {
await context.watch();
}