Skip to content

Commit c24736c

Browse files
authored
Remove glob-all dependency and added gzip plugin (#82)
Added esbuild gzip plugin, removed glob-all dependency and ability to pass npm scripts arguments to solid jsx.
1 parent 680c917 commit c24736c

6 files changed

Lines changed: 1151 additions & 4963 deletions

File tree

.github/workflows/11tybuild-update.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Checkout repo
21-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2222

2323
- name: Set up Node.js 18.x
2424
uses: actions/setup-node@v3
@@ -47,7 +47,11 @@ jobs:
4747
add: '["*.json"]'
4848
env:
4949
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50-
50+
51+
- name: Npm Rebuild # for buildmeta and manifest changes to take effect
52+
if: env.UPDATE_NPM != 'true'
53+
run: npm run minify
54+
5155
- name: Deploy
5256
if: env.UPDATE_NPM != 'true'
5357
uses: peaceiris/actions-gh-pages@v3
@@ -58,7 +62,7 @@ jobs:
5862

5963
- name: pull request on npm update
6064
if: env.UPDATE_NPM == 'true'
61-
uses: peter-evans/create-pull-request@v5
65+
uses: peter-evans/create-pull-request@v4
6266
with:
6367
token: ${{ secrets.GITHUB_TOKEN }}
6468
commit-message: Update dependencies

config/build/esbuild.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,56 @@
11
// https://www.seancdavis.com/posts/javascript-for-11ty-with-esbuild/
22
const esbuild = require('esbuild');
3-
const glob = require('glob-all'); // to enable * glob pattern in esbuild
43
const isProd = process.env.ELEVENTY_ENV === 'prod' ? true : false;
54
const isDev = process.env.ELEVENTY_ENV === 'dev' ? true : false;
65
const { solidPlugin } = require('esbuild-plugin-solid');
76
const manifestPlugin = require('esbuild-plugin-manifest');
7+
const gzipPlugin = require('@luncheon/esbuild-plugin-gzip');
88
const { http, default_schemes } = require('@hyrious/esbuild-plugin-http');
99
// cacheMap stores { url => contents }, you can easily persist it in file system - see https://github.com/hyrious/esbuild-plugin-http
1010
let cacheMap = new Map();
1111
const fs = require('fs');
1212
const path = require("path");
1313

14+
// Get arguments from npm script (such as --pathprefix) - https://reflect.run/articles/sending-command-line-arguments-to-an-npm-script/
15+
const parseArgs = (args) => {
16+
const parsedArgs = {};
17+
18+
args.forEach((arg) => {
19+
const parts = arg.split("=");
20+
21+
parsedArgs[parts[0].slice(2)] = parts[1];
22+
});
23+
24+
return parsedArgs;
25+
};
26+
27+
const npmScriptArgs = parseArgs(process.argv);
28+
29+
// pathPrefix and defineEnv const's access the environment variable PATHPREFIX set by the npm scripts (in the package.json) which is passed to solid-js by esbuild.js. Esbuild defines the environmental variables to pass through to solid-js app using the define config.
30+
const pathPrefix = npmScriptArgs.pathprefix || '';
31+
32+
const defineEnv = {
33+
'process.env.PATHPREFIX': JSON.stringify(pathPrefix),
34+
// Add other environment variables as needed
35+
};
36+
1437
const esbuildOpts = {
15-
entryPoints: glob.sync(['src/scripts/jsx/*.jsx', 'src/scripts/js/*.js', 'dist/app/*.css']), // include css so that its in the manifest.json
38+
entryPoints: ['src/scripts/jsx/*.jsx', 'src/scripts/js/*.js', 'dist/app/*.css']), // include css so that its in the manifest.json
1639
entryNames: isProd ? '[name]-[hash]' : '[name]',
1740
outExtension: isProd ? {'.js': '.min.js', '.css': '.min.css'} : {'.js': '.js', '.css': '.css'},
1841
allowOverwrite: !isProd, // overwrite dist/app/style.css when in dev mode
1942
bundle: true,
2043
minify: isProd,
44+
write: !isProd, // this is required for the gzipPlugin to work
2145
treeShaking: isProd,
2246
outdir: './dist/app',
2347
sourcemap: !isProd,
2448
target: isProd ? 'es6' : 'esnext',
2549
metafile: true,
50+
define: defineEnv,
2651
plugins: [
27-
// Runs develeopment build (skips purgingcss) if isProd = false when ELEVENTY_ENV != 'prod'
52+
// To run development/staging build (skips purgingcss) if isProd = false when ELEVENTY_ENV != 'prod'.
53+
// This is implimented in the package.json scripts
2854
http({
2955
filter: (url) => true,
3056
schemes: { default_schemes },
@@ -48,6 +74,15 @@ const esbuildOpts = {
4874
]
4975
}
5076

77+
// If isProd include gzipPlugin. This is pushed into esBuildOpts.plugins because in dev/staging mode the esBuild's write api must be true. But the gzipPlugin requires it to be false.
78+
if (isProd) {
79+
esbuildOpts.plugins.push(gzipPlugin({
80+
uncompressed: isProd,
81+
gzip: isProd,
82+
brotli: isProd,
83+
}));
84+
}
85+
5186
module.exports = async () => {
5287
let ctx = await esbuild.context({
5388
...esbuildOpts,

config/build/purgecss.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// A modified version of https://github.com/arslanakram/esbuild-plugin-purgecss-2.0
22
const esbuild = require('esbuild');
3-
const glob = require('glob-all');
43
const fs = require('fs');
54
const { PurgeCSS } = require('purgecss');
65
const path = require('path');
@@ -47,14 +46,14 @@ let purgecssPlugin = function purgecssPlugin(options) {
4746

4847
module.exports = async () => {
4948
let result = await esbuild.build({
50-
entryPoints: glob.sync(['dist/app/*.css']),
49+
entryPoints: ['dist/app/*.css'],
5150
allowOverwrite: true,
5251
minify: true,
5352
outdir: './dist/app',
5453
plugins: [
5554
purgecssPlugin({
5655
// For your production build. Add other content by using a glob-all pattern glob.sync(["dist/*.html", "dist/**/index.html"])
57-
content: ["dist/index.html"]
56+
content: ["src/scripts/jsx/*.jsx", "src/scripts/jsx/**/*.jsx", "dist/index.html"]
5857
})
5958
]
6059
})

config/shortcodes/solidify.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const esbuild = require("esbuild");
2-
const glob = require('glob-all'); // to enable * glob pattern in esbuild
32
const isProd = process.env.ELEVENTY_ENV === 'prod' ? true : false
43
const { solidPlugin } = require('esbuild-plugin-solid');
54
const fsPromises = require('fs').promises;
@@ -9,9 +8,8 @@ module.exports = async (code, filename, bundled) => {
98
let bundleJsx = bundled !== 'bundleOff' ? true : false;
109
await fsPromises.writeFile('./_tmp/solid-' + filename + '.jsx', code),
1110

12-
// esm version
1311
await esbuild.build({
14-
entryPoints: glob.sync(['_tmp/solid-*.jsx']),
12+
entryPoints: ['_tmp/solid-*.jsx'],
1513
entryNames: '[name]',
1614
// write: false,
1715
outdir: './_tmp/app',
@@ -34,4 +32,4 @@ module.exports = async (code, filename, bundled) => {
3432
})
3533
const solidifyJsx = await fsPromises.readFile('./_tmp/app/solid-' + filename + '.js', 'utf8');
3634
return `<script type="module">${solidifyJsx}</script>`;
37-
};
35+
};

0 commit comments

Comments
 (0)