iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🥤

Building a Simple Markup Environment with Gulp.js and Browsersync

に公開

Article Content

An example of setting up a simple markup environment using HTML and CSS (SCSS).

Goals

  • Setting up a markup environment without using React or Vue
  • Sharing settings like indentation among development members
  • Synchronizing HTML and CSS modifications in real-time

Things to Configure for Environment Setup

  • Installing Node.js
  • Configuring .gitignore
  • Configuring gulpfile
  • Configuring .editorconfig

Steps

Installing Node.js

There are many articles about installing Node.js, so I will skip that part.
Personally, I recommend installing Volta as a version management tool.  
https://zenn.dev/taichifukumoto/articles/how-to-use-volta

If you have already installed Volta, install the latest LTS version as follows:

volta install node

Configuring .editorconfig

By setting up .editorconfig in the root directory, you can define and maintain a consistent coding style across different editors and IDEs.
By configuring it according to the project's coding rules, as long as it's managed by Git, you can unify the coding style among members.

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = tab
max_line_length = 120
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

Configuring .gitignore

Since I don't want to upload node_modules or .vscode to Git, I'll set up .gitignore in the root directory before I forget.

node_modules
.vscode

Installing gulp.js and browser-sync

Install gulp.js as a task runner.

yarn add -D browser-sync
or
npm install -D browser-sync

browser-sync will also be used in coordination with gulp.js, so install it beforehand.

yarn add -D gulp
or
npm install -D gulp

Configuring gulpfile.js

Configure gulp.js in gulpfile.js.  
Describe the BrowserSync integration and task settings.

gulpfile.js
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var connectSSI  = require('connect-ssi');

gulp.task('default', function() {
  browserSync.init({
    watchOptions: {
      ignoreInitial: true,
      ignored: [ '**/*.+(jpg|jpeg|png|gif|svg)', 'scss', 'node_modules']
    },
    middleware: [
      connectSSI({
        baseDir: './',
        ext: '.html'
      })
    ],
    server: "./"
  });
});

Adding script to package.json

Add a script to execute gulp in package.json.

package.json
"scripts": {
    "start": "gulp"
  },

Starting the Environment

Execute the following command to run BrowserSync.

yarn start
or
npm run start

When BrowserSync is executed, a log like the following will appear in the console.

[Browsersync] Access URLs:
 --------------------------------------
       Local: http://localhost:3000
    External: http://192.168.2.114:3000
 --------------------------------------
          UI: http://localhost:3001
 UI External: http://localhost:3001
 --------------------------------------

The External URL will be the URL of the launched local server.

Thoughts

As an example of setting up a simple markup environment using HTML and CSS (SCSS), I used Gulp.js, but there are various other methods, such as using Grunt.

When it comes to using module bundlers (such as webpack), the learning cost for configuration can be high, so it is necessary to select an appropriate task runner or module bundler based on the scale and purpose of the project.
For that reason, I wanted to try setting up several environments myself to get a feel for the implementation.

Reference Articles

https://qiita.com/naru0504/items/82f09881abaf3f4dc171
https://ics.media/entry/3405/
https://ics.media/entry/3405/
https://zenn.dev/yuki0410/articles/8ee3433cfa5b807968d0-2
https://note.com/kei_01011/n/n34220e142583

Discussion