解决方案:
1。如何使用外部配置文件配置 ESBuild?
- 在根目录下新建一个文件:esbuild.js,内容如下:
import esbuild from "esbuild";
esbuild
.build({
entryPoints: ["src/styles/style.css", "src/scripts/script.js"],
outdir: "dist",
bundle: true,
plugins: [],
})
.then(() => console.log("⚡ Build complete! ⚡"))
.catch(() => process.exit(1));
- 在您的 package.json 中添加以下代码:
{
...
"scripts": {
...
"build": "node esbuild.js"
},
...
}
- 使用 npm run build 命令运行构建,这将捆绑您的样式表和脚本并将它们输出到 dist 目录中。
- 有关更多详细信息和/或添加自定义构建选项,请参阅ESBuild's Build API documentation。
2。 ESBuild 不支持开箱即用的 SCSS。如何配置 esbuild-sass-plugin 等外部插件,更进一步,如何设置 PostCSS 和 Autoprefixer 等插件?
- 安装 npm 依赖:
npm i -D esbuild-sass-plugin postcss autoprefixer
- 将您的 esbuild.js 编辑为以下代码:
import esbuild from "esbuild";
import { sassPlugin } from "esbuild-sass-plugin";
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
// Generate CSS/JS Builds
esbuild
.build({
entryPoints: ["src/styles/style.scss", "src/scripts/script.js"],
outdir: "dist",
bundle: true,
metafile: true,
plugins: [
sassPlugin({
async transform(source) {
const { css } = await postcss([autoprefixer]).process(source);
return css;
},
}),
],
})
.then(() => console.log("⚡ Build complete! ⚡"));
3。如何使用自动重建设置开发服务器?
- ESBuild对此有限制,可以传入
watch: true或run its server。 It doesn't allow both。
- ESBuild 还有另一个限制,it doesn't have HMR support,就像 Webpack 一样。
- 所以要忍受这两个限制并仍然允许服务器,我们可以使用Live Server。使用
npm i -D @compodoc/live-server 安装它。
- 在根目录下新建一个文件:esbuild_watch.js,内容如下:
import liveServer from '@compodoc/live-server';
import esbuild from 'esbuild';
import { sassPlugin } from 'esbuild-sass-plugin';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
// Turn on LiveServer on http://localhost:7000
liveServer.start({
port: 7000,
host: 'localhost',
root: '',
open: true,
ignore: 'node_modules',
wait: 0,
});
// Generate CSS/JS Builds
esbuild
.build({
logLevel: 'debug',
metafile: true,
entryPoints: ['src/styles/style.scss', 'src/scripts/script.js'],
outdir: 'dist',
bundle: true,
watch: true,
plugins: [
sassPlugin({
async transform(source) {
const { css } = await postcss([autoprefixer]).process(
source
);
return css;
},
}),
],
})
.then(() => console.log('⚡ Styles & Scripts Compiled! ⚡ '))
.catch(() => process.exit(1));
- 编辑 package.json 中的 scripts:
{
...
"scripts": {
...
"build": "node esbuild.js",
"watch": "node esbuild_watch.js"
},
...
}
- 要运行构建,请使用此命令
npm run build。
- 使用自动重建运行开发服务器运行
npm run watch。这是一种“老套”的做事方式,但做得还算不错。
4。如何设置 PurgeCSS?
我为此找到了一个很棒的插件:esbuild-plugin-purgecss by peteryuan 但它不允许为需要解析的 html/views 路径传递选项,所以我
创建了完成这项工作的esbuild-plugin-purgecss-2。要进行设置,请阅读以下内容:
- 安装依赖
npm i -D esbuild-plugin-purgecss-2 glob-all。
- 将以下代码添加到您的 esbuild.js 和 esbuild_watch.js 文件中:
// Import Dependencies
import glob from 'glob-all';
import purgecssPlugin2 from 'esbuild-plugin-purgecss-2';
esbuild
.build({
plugins: [
...
purgecssPlugin2({
content: glob.sync([
// Customize the following URLs to match your setup
'./*.html',
'./views/**/*.html'
]),
}),
],
})
...
- 现在运行
npm run build 或npm run watch 将从上面代码中glob.sync([...] 中提到的文件路径中清除CSS。
TL;DR:
- 在根目录 esbuild.js 中创建一个外部配置文件,并在
scripts: {..} 内的 package.json 中添加运行它的命令,例如"build": "node esbuild.js" 使用npm run build 引用和运行配置文件。
- ESBuild 不支持 HMR。此外,您可以使用 ESBuild 使用
watch 或 serve,但不能同时使用两者。要克服这些问题,请使用单独的开发服务器库,例如 Live Server。
- 有关完整设置,请参阅我在 github 上的 custom-esbuild-with-scss-purgecss-and-liveserver 存储库。
最后说明:
我知道这是一个很长的话题,但我花了很多时间才弄清楚这些。我的目的是让其他人研究同样的问题并试图找出从哪里开始。
谢谢。