-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
276 lines (245 loc) · 7.67 KB
/
mod.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* dsbuild - Deno + esbuild */
import { dirname, esbuild, initCss, transformCss } from "./deps.ts";
import { denoLoaderPlugin, denoResolverPlugin } from "./deps.ts";
import { parseArgs } from "./deps.ts";
import { isAbsolute, join, resolve, normalize } from "./deps.ts";
import { serve, setServeDir } from "./serve.ts";
import { buildMdx, mdxPlugin } from "./plugin-mdx.ts";
import { buildReactStatic } from "./plugin-react-static.tsx";
import { build } from "./plugin-main.ts";
import { buildCss } from "./plugin-css.ts";
import { extname, parse } from "./deps.ts";
import {
DSBUILD_VERSION,
IS_DEV,
DEFAULT_IN_FILES,
DEFAULT_IN_FOLDER,
DEFAULT_OUT_FILE,
DEFAULT_STATIC_FILE,
DEFAULT_SERVE_DIR,
DEFAULT_CSS_FILE,
REACT_TS_CONFIG,
REACT_STATIC_TS_CONFIG_DEV,
} from "./stuff.ts";
if (import.meta.main) {
const helpText = `dsbuild (Deno + esbuild) v${DSBUILD_VERSION}
This is a simple build tool for Deno + esbuild. It compiles Deno TypeScript
to a single JavaScript file that can be run in the browser.
Example usage:
\`dsbuild --in src/app.ts --out public/app.js\`
- Build \`src/app.ts\` to \`public/app.js\`
\`dsbuild --live\`
- Live reloads \`src/app.ts\` as you make changes.
\`dsbuild --live --serve\`
- Live build and serve \`public/\` on \`localhost:8080\`
\`dsbuild --serve-only\`
- Serve \`public/\` on \`localhost:8080\` without building
\`dsbuild --import-map import-map.json\`
- Build with import map
\`dsbuild --tsconfig\`
- Generate a tsconfig.json you can use for Deno + browser development
\`dsbuild --denoconfig\`
- Generate a deno.json you can use for Deno + browser development
\`dsbuild --init=react\`
- Initialize a React project (can also be \`mdx\`, \`node\`, \'basic\', \`react-static\`)
`;
const args = parseArgs(Deno.args);
const isHelp = args["help"] || args["h"];
const isVersion = args["version"] || args["v"];
if (isHelp) {
console.log(helpText);
Deno.exit(0);
}
if (isVersion) {
console.log(DSBUILD_VERSION);
Deno.exit(0);
}
const generateTsConfig = args["tsconfig"] || false;
const generateDenoConfig = args["denoconfig"] || false;
const init = args["init"] || false;
let inFile = args["in"] || args["_"].join("");
const inFiles = inFile.includes(",") ? inFile.split(",") : undefined;
let outFile = args["out"];
let outDir: string | undefined;
let outbase = args["outbase"]
try {
const statResult = await Deno.stat(outFile);
if (statResult && statResult.isDirectory) {
outDir = outFile;
outFile = undefined;
}
} catch (_) { /**/ }
let serveDir = args["serve-dir"] || DEFAULT_SERVE_DIR;
let importMap = args["import-map"];
let useHash = args["hash"];
let logLevel = args["log-level"] || "info";
const target = args["target"] ? args["target"].split(",") : null;
const isWatch = args["live"] || args["watch"] || args["w"] || args["l"];
const isServe = args["serve"] || args["serve-only"] || args["s"];
const isServeOnly = args["serve-only"];
const isMdx = args["mdx"];
const isReactStatic = args["react-static"];
const isCss = args["css"];
const external = args["external"] ? args["external"].split(",") : [];
// Replace relative with absolute paths
const cwd = Deno.cwd();
if (inFile && !isAbsolute(inFile)) {
inFile = join(cwd, inFile);
}
if (outFile && !isAbsolute(outFile)) {
outFile = join(cwd, outFile);
}
if (!outFile && isCss) {
outFile = DEFAULT_CSS_FILE;
} else if (!outFile && isReactStatic) {
outFile = DEFAULT_STATIC_FILE;
} else if (!outFile && !outDir) {
outFile = DEFAULT_OUT_FILE;
}
// Set up default import map
if (importMap === "null" || importMap === "false") {
importMap = null;
} else {
if (importMap && !isAbsolute(importMap)) {
importMap = join("file://", cwd, importMap);
}
if (!importMap) {
const importMaps = ["import-map.json", "deno.json"]; // NOTE: .jsonc doesn't work yet
for (const map of importMaps) {
const potentialImportMap = join(cwd, map).replace("file:", "");
try {
const stat = await Deno.stat(potentialImportMap);
if (stat.isFile) {
importMap = join("file://", potentialImportMap);
break;
}
} catch (_e) {
// ignore
}
}
}
}
if (!isAbsolute(serveDir)) {
serveDir = join(cwd, serveDir);
}
if (init) {
const initStr = typeof init === "string" ? init : "basic";
const rootDir = join(import.meta.url, "..").replace("file:", "");
const exampleDir = join(rootDir, "examples", initStr).replace("file:", "");
// recursively copy files from ./examples/[example] to cwd
const files = Deno.readDirSync(exampleDir);
const copyFileOrFolder = async (src: string, dest: string) => {
if (src.endsWith(".gitignore") || src.endsWith(".git")) {
return;
}
const stat = await Deno.stat(src);
if (stat.isFile) {
const contents = await Deno.readTextFile(src);
console.log(`Copying ${src} to ${dest} (file)`);
try {
const isForce = args["force"] || false;
await Deno.writeTextFile(dest, contents, {
createNew: isForce ? undefined : true,
});
} catch (e) {
console.error(`Error copying ${src} to ${dest}: ${e}`);
console.error(
'You already have one of these files already at your destination. Please only use --init from an empty directory. (or use "--force" to overwrite existing files)'
);
Deno.exit(1);
}
} else if (stat.isDirectory) {
await Deno.mkdir(dest, { recursive: true });
const files = Deno.readDirSync(src);
console.log(`Copying ${src} to ${dest} (directory)`);
for (const file of files) {
const srcPath = join(src, file.name);
const destPath = join(dest, file.name);
await copyFileOrFolder(srcPath, destPath);
}
}
};
for (const file of files) {
await copyFileOrFolder(join(exampleDir, file.name), join(cwd, file.name));
}
Deno.exit(0);
}
if (generateTsConfig) {
// tsconfig.json for deno+browser dev
const str = JSON.stringify(REACT_TS_CONFIG, null, 2);
console.log(str);
if (args["out"]) {
await Deno.writeTextFile(outFile, str);
}
Deno.exit(0);
}
if (generateDenoConfig) {
const str = JSON.stringify(REACT_STATIC_TS_CONFIG_DEV, null, 2);
console.log(str);
if (args["out"]) {
await Deno.writeTextFile(outFile, str);
}
Deno.exit(0);
}
if (isMdx) {
await buildMdx({
inFile: inFile || DEFAULT_IN_FOLDER,
outFile,
outDir,
watch: isWatch,
});
Deno.exit(0);
}
if (isReactStatic) {
await buildReactStatic({
inFile: inFile || DEFAULT_IN_FOLDER,
outFile,
watch: isWatch,
});
Deno.exit(0);
}
if (isCss) {
await buildCss({
inFile: inFile || DEFAULT_IN_FOLDER,
outFile,
watch: isWatch,
});
Deno.exit(0);
}
if (!inFile) {
// Look for default input file
for (const file of DEFAULT_IN_FILES) {
const path = join(...file.split("/"));
try {
const stat = await Deno.stat(path);
if (stat.isFile) {
inFile = file;
break;
}
} catch (_e) {
// ignore
}
}
}
if (!inFile && !isServeOnly) {
console.error("No input file found. Use --in to specify an input file.");
Deno.exit(1);
}
await build({
watch: isWatch,
serve: isServeOnly ? "only" : isServe,
importMap,
target,
inFile,
inFiles,
outFile,
outDir,
outbase,
hash: useHash,
serveDir,
plugins: [mdxPlugin],
logLevel,
external
});
Deno.exit(0);
}