-
Notifications
You must be signed in to change notification settings - Fork 722
feat(build): integrate esbuild for improved build performance and add CSS processing #1777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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, | ||
7HR4IZ3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a hardcoded path to the built worker file makes the code more brittle. If the build output structure changes, this path will need to be updated. It's better to use the
new URL('./worker.js', import.meta.url)syntax.esbuildsupports this and will automatically bundle the worker and generate the correct path. This also allows you to remove the worker as a separate entry point in youresbuild.jsconfiguration, simplifying it.