Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. {
  2. "name": "pug-sass-test-kaihen",
  3. "version": "1.0.0",
  4. "description": "Gulpを使用したPugの導入テストリポジトリです。",
  5. "main": "browsersync.js",
  6. "scripts": {
  7. "test": "echo "Error: no test specified" && exit 1",
  8. "gulp": "gulp"
  9. },
  10. "keywords": [
  11. "gulp",
  12. "pug",
  13. "Sass"
  14. ],
  15. "author": "",
  16. "license": "ISC",
  17. "devDependencies": {
  18. "browser-sync": "^2.26.5",
  19. "gulp": "^4.0.1",
  20. "autoprefixer": "^9.5.1",
  21. "gulp-data": "^1.3.1",
  22. "gulp-notify": "^3.2.0",
  23. "gulp-plumber": "^1.2.1",
  24. "gulp-postcss": "^8.0.0",
  25. "gulp-pug": "^4.0.1",
  26. "gulp-rename": "^1.4.0",
  27. "gulp-sass": "^4.0.2",
  28. "gulp-sass-glob": "^1.0.9",
  29. "postcss-flexbugs-fixes": "^4.1.0"
  30. }
  31. }
  32.  
  33. 'use strict';
  34.  
  35. const gulp = require('gulp');
  36.  
  37. //pug
  38. const pug = require('gulp-pug');
  39. const fs = require('fs');
  40. const data = require('gulp-data');
  41. const path = require('path');
  42. const plumber = require('gulp-plumber');
  43. const notify = require('gulp-notify');
  44. const browserSync = require('browser-sync');
  45.  
  46. //css
  47. const sass = require('gulp-sass');
  48. const sassGlob = require('gulp-sass-glob');
  49. const postcss = require('gulp-postcss');
  50. const flexBugsFixes = require('postcss-flexbugs-fixes');
  51. const autoprefixer = require('autoprefixer'); //Sassにベンダープレフィックスをつける
  52. const rename = require('gulp-rename'); //ファイルをリネーム
  53.  
  54. /**
  55. * 開発用のディレクトリを指定します。
  56. */
  57. const src = {
  58. // 出力対象は`_`で始まっていない`.pug`ファイル。
  59. 'html': ['src/**/*.pug', '!' + 'src/**/_*.pug'],
  60. // JSONファイルのディレクトリを変数化。
  61. 'json': 'src/_data/',
  62. 'css': 'src/**/*.css',
  63. 'sass_style': ['src/**/*.scss', '!' + 'src/**/_*.scss'],
  64. //'styleguideWatch': 'src/**/*.scss',
  65. 'js': 'src/**/*.js',
  66. 'root': 'src/'
  67. };
  68.  
  69. /**
  70. * 出力するディレクトリを指定します。
  71. */
  72. const dest = {
  73. 'root': 'dest/',
  74. 'html': 'dest/'
  75. };
  76.  
  77. /**
  78. * `.pug`をコンパイルしてから、destディレクトリに出力します。
  79. * JSONの読み込み、ルート相対パス、Pugの整形に対応しています。
  80. */
  81. function html() {
  82. // JSONファイルの読み込み。
  83. var locals = {
  84. 'site': JSON.parse(fs.readFileSync(src.json + 'site.json'))
  85. };
  86. return gulp.src(src.html)
  87. // コンパイルエラーを通知します。
  88. .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
  89. // 各ページごとの`/`を除いたルート相対パスを取得します。
  90. .pipe(data(function (file) {
  91. locals.relativePath = path.relative(file.base, file.path.replace(/.pug$/, '.html'));
  92. return locals;
  93. }))
  94. .pipe(pug({
  95. // JSONファイルとルート相対パスの情報を渡します。
  96. locals: locals,
  97. // Pugファイルのルートディレクトリを指定します。
  98. // `/_includes/_layout`のようにルート相対パスで指定することができます。
  99. basedir: 'src',
  100. // Pugファイルの整形。
  101. pretty: true
  102. }))
  103. .pipe(gulp.dest(dest.html))
  104. .pipe(browserSync.reload({stream: true}));
  105. }
  106.  
  107. /**
  108. * cssファイルをdestディレクトリに出力(コピー)します。
  109. */
  110. function css() {
  111. return gulp.src(src.css, {base: src.root})
  112. .pipe(gulp.dest(dest.root))
  113. .pipe(browserSync.reload({stream: true}));
  114. }
  115.  
  116.  
  117. /**
  118. * sassファイルをdestディレクトリに同じ階層として出力(コピー)します。
  119. */
  120. function sass_style() {
  121. const plugins = [flexBugsFixes(), autoprefixer({ // ベンダープレフィックスの付与
  122. browsers: ['last 3 version', 'ie >= 10', 'iOS >= 9.3', 'Android >= 4.4'], // (ベンダープレフィックスを付与する)対応ブラウザの指定
  123. cascade: true // 整形する
  124. })];
  125. return gulp.src(src.sass_style, {base: src.root})
  126. .pipe(sassGlob())
  127. .pipe(sass({
  128. outputStyle: 'expanded',
  129. }).on('error', sass.logError),
  130. )
  131. .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
  132. .pipe(postcss(plugins))
  133. .pipe(rename(function (path) {
  134. path.dirname += '/css';
  135. }))
  136. .pipe(gulp.dest(dest.root))
  137. .pipe(browserSync.reload({stream: true}));
  138. }
  139.  
  140.  
  141. /**
  142. * jsファイルをdestディレクトリに出力(コピー)します。
  143. */
  144. function js() {
  145. return gulp.src(src.js, {base: src.root})
  146. .pipe(gulp.dest(dest.root))
  147. .pipe(browserSync.reload({stream: true}));
  148. }
  149.  
  150. /**
  151. * ローカルサーバーを起動します。
  152. */
  153. function browser_sync(done) {
  154. browserSync({
  155. server: {
  156. baseDir: dest.root,
  157. index: "index.html"
  158. }
  159. });
  160. done();
  161. }
  162.  
  163. /**
  164. * PugのコンパイルやCSSとjsの出力、browser-syncのリアルタイムプレビューを実行します。
  165. */
  166. function watchFiles(done) {
  167. const browserReload = () => {
  168. browserSync.reload();
  169. done();
  170. };
  171. gulp.watch(src.html).on('change', gulp.series(html, browserReload));
  172. gulp.watch(src.sass_style).on('change', gulp.series(sass_style, browserReload));
  173. gulp.watch(src.css).on('change', gulp.series(css, browserReload));
  174. gulp.watch(src.js).on('change', gulp.series(js, browserReload));
  175. }
  176.  
  177. gulp.task('watch', gulp.series(gulp.parallel(html, sass_style, css, js), gulp.series(browser_sync, watchFiles)));
  178.  
  179.  
  180. /**
  181. * 開発に使うタスクです。
  182. * package.jsonに設定をして、`npm run default`で実行できるようにしています。
  183. */
  184. gulp.task('default', gulp.task('watch'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement