forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.js
More file actions
171 lines (152 loc) · 3.93 KB
/
esbuild.js
File metadata and controls
171 lines (152 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
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
#!/usr/bin/env node
const path = require("node:path");
const fs = require("node:fs/promises");
const babel = require("@babel/core");
const esbuild = require("esbuild");
const sass = require("sass");
const postcss = require("postcss");
const tagLoader = require("html-tag-js/jsx/tag-loader");
const postcssConfig = require("../postcss.config.js");
const args = process.argv.slice(2);
const modeArgIndex = args.indexOf("--mode");
const mode =
modeArgIndex > -1 && args[modeArgIndex + 1]
? args[modeArgIndex + 1]
: "development";
const isProd = mode === "production";
const watch = args.includes("--watch");
const root = path.resolve(__dirname, "..");
const outdir = path.join(root, "www", "build");
const target = ["es5"];
async function ensureCleanOutdir() {
await fs.rm(outdir, { recursive: true, force: true });
await fs.mkdir(outdir, { recursive: true });
}
async function processCssFile(filePath) {
const isSass = /\.(sa|sc)ss$/.test(filePath);
let css;
if (isSass) {
const result = sass.compile(filePath, {
style: isProd ? "compressed" : "expanded",
loadPaths: [
path.dirname(filePath),
path.join(root, "src"),
path.join(root, "node_modules"),
],
});
css = result.css;
} else {
css = await fs.readFile(filePath, "utf8");
}
const postcssPlugins = postcssConfig.plugins || [];
const processed = await postcss(postcssPlugins).process(css, {
from: filePath,
map: !isProd ? { inline: true } : false,
});
return processed.css;
}
const babelPlugin = {
name: "babel-transform",
setup(build) {
build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
const source = await fs.readFile(args.path, "utf8");
const result = await babel.transformAsync(source, {
filename: args.path,
configFile: path.join(root, ".babelrc"),
sourceType: "unambiguous",
caller: {
name: "esbuild",
supportsStaticESM: true,
supportsDynamicImport: true,
},
});
const transformed = result && result.code ? result.code : source;
const contents = tagLoader(transformed);
return { contents, loader: "js" };
});
},
};
const cssPlugin = {
name: "css-modules-as-text",
setup(build) {
build.onLoad({ filter: /\.(sa|sc|c)ss$/ }, async (args) => {
const isModule = /\.m\.(sa|sc|c)ss$/.test(args.path);
const contents = await processCssFile(args.path);
return {
contents,
loader: isModule ? "text" : "css",
};
});
},
};
const nodeFallbackPlugin = {
name: "node-fallbacks",
setup(build) {
const emptyNamespace = "empty-module";
build.onResolve({ filter: /^path$/ }, () => ({
path: require.resolve("path-browserify"),
}));
build.onResolve({ filter: /^crypto$/ }, () => ({
path: "crypto",
namespace: emptyNamespace,
}));
build.onLoad({ filter: /.*/, namespace: emptyNamespace }, () => ({
contents: "export default {};",
loader: "js",
}));
},
};
async function run() {
await ensureCleanOutdir();
const buildOptions = {
absWorkingDir: root,
entryPoints: {
main: "./src/main.js",
console: "./src/lib/console.js",
},
outdir,
entryNames: "[name]",
chunkNames: "[name].chunk",
assetNames: "[name][ext]",
publicPath: "/build/",
bundle: true,
format: "iife",
platform: "browser",
target,
minify: isProd,
sourcemap: !isProd,
define: {
"process.env.NODE_ENV": JSON.stringify(mode),
},
nodePaths: [path.join(root, "src")],
loader: {
".hbs": "text",
".md": "text",
".png": "file",
".svg": "file",
".jpg": "file",
".jpeg": "file",
".ico": "file",
".ttf": "file",
".woff2": "file",
".webp": "file",
".eot": "file",
".woff": "file",
".webm": "file",
".mp4": "file",
".wav": "file",
},
plugins: [babelPlugin, cssPlugin, nodeFallbackPlugin],
};
if (watch) {
const ctx = await esbuild.context(buildOptions);
await ctx.watch();
console.log("esbuild is watching for changes...");
return;
}
await esbuild.build(buildOptions);
}
run().catch((error) => {
console.error(error);
process.exit(1);
});