惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
A
About on SuperTechFans
GbyAI
GbyAI
宝玉的分享
宝玉的分享
爱范儿
爱范儿
博客园 - 【当耐特】
博客园 - 司徒正美
博客园 - 聂微东
P
Proofpoint News Feed
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
博客园 - 叶小钗

CloudCannon Blog

Building with AI: Git-based vs headless vs traditional CMS CloudCannon + Astro: performance meets powerful content management Introducing the Astro Component Starter Introducing Jetstream — built on the Astro Component Starter Why we switched to the system font stack Redesigning CloudCannon’s docs with Diátaxis, Lume, and Pagefind Make content editing more visual: upgraded Editable Regions How Configuration Mode makes building editing interfaces easy Your hosting just got an upgrade (and a price cut) Custom testing domains for professional branding Keep your content consistent with input validation Managing multilingual content in CloudCannon Simplify team publishing with conflict resolution and domain tools Open Beta: Publishing Conflict Resolution Getting started with CloudCannon and Astro: Bookshop, components, and live editing Welcome to the CloudCannon Community! Omnichannel delivery is just marketing spin from API-based CMS companies Getting started with CloudCannon and Astro: Snippets and Collections Managing digital assets in CloudCannon: a guide to smart asset storage Understanding CloudCannon's branching workflows and Projects: a complete guide What is a static website? CloudCannon’s 2024 wrapped Getting started with CloudCannon and Astro: WYSIWYG blogging Jamstack vs. WordPress: reasons to make the change The top five static site generators for 2025 (and when to use them!) Free Jekyll themes for 2025: ten great community options Eleventy (11ty) vs. Hugo How to set up WYSIWYG editing with MkDocs Material The rise of static-first websites: why major brands are making the switch Watching your Core Web Vitals on Jamstack
Bundling JavaScript for Jekyll
2020-11-12 · via CloudCannon Blog

Optimising assets can significantly improve load times for visitors to your site. When you build a site on CloudCannon, any assets referenced in your HTML/CSS are automatically minified and served from a CDN. JavaScript files are minified using UglifyJS.

In some cases, you may also want to bundle your JavaScript files together, to reduce the number of HTTP requests made when loading a page. The simplest solution would be to write all your code in a single file, but this compromises maintainability. Instead, you can use various techniques to bundle your files together on build for performance, while keeping them separate during development for sanity.

Jekyll includes Direct link to this section

A simple technique for bundling JavaScript is to use Jekyll includes. With this approach, a single JavaScript file would use {% include %} tags to include other scripts. For example, you could create a bundle.js file with the following:

---
---
{% include /assets/script-1.js %}
{% include /assets/script-2.js %}

Remember to add front matter dashes in bundle.js so that Jekyll processes the Liquid.

This allows you to divide your JavaScript into manageable pieces in development, while only linking to bundle.js on your site:

<script src="{{ site.baseurl }}/js/bundle.js"></script>

Note that if you don't want to keep all your scripts in the _includes folder, you can use the include_relative tag. This will allow you to specify a path relative to the file where the tag is being used (you cannot access higher-level directories however). For better readability, you might also want to keep your includes in the front matter and use Liquid to manage the include tags.

For example, if you are working in /js/bundle.js and your JavaScript files are in /js/scripts/, you could use the following:

---
dir_path: scripts/
scripts:
  - script-1.js
  - script-2.js
---
{% for script in scripts %}
    {% assign script_path = dir_path | append: script %}
    {% include_relative script_path %}
{% endfor %}

Bear in mind that the scripts are simply being concatenated, so you will need to be mindful of conflicting variable names. You can avoid this problem by wrapping each script in a function that is called as soon as it is defined (i.e. an IIFE). Variables within the function cannot be accessed from outside of it, so there won't be any conflicting variables between the included scripts.

---
dir_path: scripts/
scripts:
- script-1.js
- script-2.js
---
{% for script in scripts %}
{% assign script_path = dir_path | append: script %}
(function () {
{% include_relative script_path %}
})();
{% endfor %}

Using webpack Direct link to this section

webpack is a JavaScript bundler. It searches your code for dependencies and bundles scripts together in a single file. First, install webpack:

$ npm install webpack
$ npm install webpack-cli

On the command line, specify each JavaScript file you want to bundle, then specify the destination for the bundle with -o path/to/output.js. For example:

$ node_modules/.bin/webpack js/file-1.js \
js/file-2.js \
js/file-3.js \
-o js/output.js

This will bundle all your scripts into one file. In your <script> tags, you can simply link to the bundled file:

<script src="{{ site.baseurl }}/js/output.js"></script>

Instead of specifying each input file on the command line, you can create an entry.js file. This file should just require/import all the scripts you want to bundle. Then, you can use this as your input and all of the other imported scripts will be bundled.

$ ./node_modules/.bin/webpack path/to/entry.js -o js/output.js

This is very basic use of webpack - see the webpack docs for more configuration options.

Using Gulp Direct link to this section

Gulp is a JavaScript toolkit for automating tasks. You can use this with the gulp-concat package to bundle your JavaScript files together.

First, create a package.json by running npm init. Then install gulp and gulp-concat and add them to your project dev dependencies:

$ npm install --save-dev gulp
$ npm install --save-dev gulp-concat

Create a gulpfile.js at the root of your project with the following:

const gulp = require('gulp');
const concat = require('gulp-concat');

gulp.task('concat-scripts', function() {
return gulp.src('./input-scripts/*.js')
.pipe(concat('output.js'))
.pipe(gulp.dest('./assets/'));
});

gulp.task defines a task called "concat-scripts". This task uses gulp.src to read in all JavaScript files from an /input-scripts/ directory. The scripts are concatenated into an output.js file, which is then saved into the /assets/ directory using gulp.dest.

You can run this task from the command line in your project directory with gulp concat-scripts. Then you can simply link to /assets/output.js on your site:

<script src="{{ site.baseurl }}/assets/output.js"></script>

If you want to minify your script after bundling it, you can do this easily with gulp-uglify. This will reduce the size of your bundled script, to improve performance for visitors to your site.

$ npm install --save-dev gulp-uglify
const gulp = require('gulp');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

gulp.task('concat-and-minify-scripts', function() {
return gulp.src('./input-scripts/*.js')
.pipe(concat('output.js'))
.pipe(uglify())
.pipe(gulp.dest('./assets/'));
});

Prebuild commands in CloudCannon Direct link to this section

CloudCannon supports custom commands, so that you can use tools like Gulp and webpack easily. Simply create a _cloudcannon-prebuild.sh file at the root of your project with any commands you want to run before each site build.

Your prebuild commands run in a Node environment, so you can install any npm packages you need.

For example:

npm install webpack
npm install webpack-cli

./node_modules/.bin/webpack path/to/entry.js -o js/output.js

npm install gulp
npm install gulp-concat

./node_modules/.bin/gulp concat-task

Prebuild commands allow you to incorporate your favourite tools for building and pre-processing into CloudCannon. Bundling JavaScript is just one way to use this functionality - other uses could include fetching API data, running build commands other than Jekyll, or sending messages to your Slack channel.