LoginSignup
2
1

More than 3 years have passed since last update.

開発準備 gulp+オートリロード

Posted at

サーバー起動とオートリロード環境を準備する

開発環境を整備する

  • centOS
  • vagrant
  • node.js
  • gulp
  1. npm 設定ファイルの作成
  2. gulp のインストール
  3. gulp 詳細設定

1. npm  設定ファイルの作成

npm init

必要項目を入力する

package name: (study) study
version: (1.0.0)
description: js lesson
entry point: (index.js)
test command:
git repository: https://github.com/YKHSJP/study
keywords:
author:
license: (ISC)

package.json が作成される

{
  "name": "study",
  "version": "1.0.0",
  "description": "js lesson",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YKHSJP/study.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/YKHSJP/study/issues"
  },
  "homepage": "https://github.com/YKHSJP/study#readme"
}

2. gulp をインストール

$ npm install gulp --save-dev

--save パラメータは、 package.json の dependencies という項目に追加される
--save-dev パラメータは、 package.json の devDependencies という項目に追加される

package.json

{
  "name": "study",
  "version": "1.0.0",
  "description": "js lesson",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YKHSJP/study.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/YKHSJP/study/issues"
  },
  "homepage": "https://github.com/YKHSJP/study#readme",
  "dependencies": {},
  "devDependencies": {
    "gulp": "^4.0.2"
  }
}

3. gulp 詳細設定

サーバーを起動、ファイルの更新でリロードする

$ npm i --save-dev browser-sync

package.json

{
  "name": "study",
  "version": "1.0.0",
  "description": "js lesson",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/YKHSJP/study.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/YKHSJP/study/issues"
  },
  "homepage": "https://github.com/YKHSJP/study#readme",
  "dependencies": {},
  "devDependencies": {
    "browser-sync": "^2.26.7",
    "gulp": "^4.0.2"
  }
}

gulpfile.js

  1. gulpbrowser-sync を install して
  2. 変数 serverbrowser-sync を起動する関数を代入
  3. 変数 reloadbrowser-sync をリロードする関数を代入
  4. 変数 watchgulp.watch'public/*.*' が変更されたら reload を実行するよう代入
  5. gulp.taskserverwatch を実行するよう設定
// -- install package
const gulp = require("gulp");
const browserSync = require("browser-sync");

// -- build server
const server = done => {
  return browserSync.init({
    open: 'external',
    server: {
      baseDir: 'public/',
      index: 'index.html'
    }
  })
  done()
}

// -- reload
const reload = done => {
  browserSync.reload()
  done()
}

// -- watch
const watch = done => {
  gulp.watch(
    'public/*.*',
    gulp.series(reload)
  )
  done()
}

gulp.task("default", gulp.parallel(server,watch))

public/index.html を用意しておく

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello world</title>
  </head>
  <body>
    <p>Nice to meet you.</p>
  </body>
</html>

gulp を実行

$ npx gulp

public/index.html を修正、上書き保存

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello world</title>
  </head>
  <body>
    <p>Nice to meet you.</p>
    <p>me too.</p>
  </body>
</html>

sample1.gif

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1