前提・実現したいこと
【実現したいこと】
pug、css、sassを同階層でdestフォルダにリアルタイムで自動コンパイルと
cssファイルはコピーさせること
【問題】
js,gulpに関して初心者です。
gulpを使ってsassを同じ階層で自動コンパイルするとエラーが出る。
【概要】
とあるサイトからテンプレートをダウンロードして、自分のしたいことを
肉付けしていきました。
sassを入れる前はpugなど、全て自動コンパイルが出来ていました。
【参考サイト】
https://www.tam-tam.co.jp/tipsnote/html_css/post10973.html
http://hiromode.hatenablog.com/entry/2018/07/29/154522
https://qiita.com/hibikikudo/items/493fbfbbea183c94b38b
発生している問題・エラーメッセージ
gulp.task('default', gulp.series(gulp.parallel(html, sass_style, css, js), gulp.series(browsersync, watchFiles))); ^ TypeError: gulp.parallel is not a function at Object.<anonymous> (C:\Users\Documents\pug-sass-test\gulpfile.js:139:39) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Liftoff.handleArguments (C:\Users\Documents\pug-sass-test\node_modules\gulp\bin\gulp.js:116:3) at Liftoff.execute (C:\Users\Documents\pug-sass-test\node_modules\liftoff\index.js:203:12) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! gulp-pug-test@1.0.0 start: `gulp` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the gulp-pug-test@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
ディレクトリ構造
gulpfile.jsのソースコード
JavaScript
1const gulp = require('gulp'); 2 3//pug 4const pug = require('gulp-pug'); 5const fs = require('fs'); 6const data = require('gulp-data'); 7const path = require('path'); 8const plumber = require('gulp-plumber'); 9const notify = require('gulp-notify'); 10const browserSync = require('browser-sync'); 11 12//css 13const sass = require('gulp-sass'); 14const sassGlob = require('gulp-sass-glob'); 15const postcss = require('gulp-postcss'); 16const flexBugsFixes = require('postcss-flexbugs-fixes'); 17const autoprefixer = require('gulp-autoprefixer'); //Sassにベンダープレフィックスをつける 18const rename = require('gulp-rename'); //ファイルをリネーム 19 20/** 21 * 開発用のディレクトリを指定します。 22 */ 23const src = { 24 // 出力対象は`_`で始まっていない`.pug`ファイル。 25 'html': ['src/**/*.pug', '!' + 'src/**/_*.pug'], 26 // JSONファイルのディレクトリを変数化。 27 'json': 'src/_data/', 28 'css': 'src/**/*.css', 29 'sass_style': ['src/**/*.scss', '!' + 'src/**/_*.scss'], 30 //'styleguideWatch': 'src/**/*.scss', 31 'js': 'src/**/*.js' 32}; 33 34/** 35 * 出力するディレクトリを指定します。 36 */ 37const dest = { 38 'root': 'dest/', 39 'html': 'dest/' 40}; 41 42/** 43 * `.pug`をコンパイルしてから、destディレクトリに出力します。 44 * JSONの読み込み、ルート相対パス、Pugの整形に対応しています。 45 */ 46function html() { 47 // JSONファイルの読み込み。 48 var locals = { 49 'site': JSON.parse(fs.readFileSync(src.json + 'site.json')) 50 } 51 return gulp.src(src.html) 52 // コンパイルエラーを通知します。 53 .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")})) 54 // 各ページごとの`/`を除いたルート相対パスを取得します。 55 .pipe(data(function(file) { 56 locals.relativePath = path.relative(file.base, file.path.replace(/.pug$/, '.html')); 57 return locals; 58 })) 59 .pipe(pug({ 60 // JSONファイルとルート相対パスの情報を渡します。 61 locals: locals, 62 // Pugファイルのルートディレクトリを指定します。 63 // `/_includes/_layout`のようにルート相対パスで指定することができます。 64 basedir: 'src', 65 // Pugファイルの整形。 66 pretty: true 67 })) 68 .pipe(gulp.dest(dest.html)) 69 .pipe(browserSync.reload({stream: true})); 70} 71 72/** 73 * cssファイルをdestディレクトリに出力(コピー)します。 74 */ 75function css() { 76 return gulp.src(src.css, {base: src.root}) 77 .pipe(gulp.dest(dest.root)) 78 .pipe(browserSync.reload({stream: true})); 79} 80 81 82/** 83 * sassファイルをdestディレクトリに同じ階層として出力(コピー)します。 84 */ 85function sass_style() { 86 const plugins = [flexBugsFixes(), autoprefixer()]; 87 return gulp.src(src.scss, {base: src.root}) 88 .pipe(sassGlob()) 89 .pipe(sass({ 90 outputStyle: 'expanded', 91 }).on('error', sass.logError), 92 ) 93 .pipe(plumber({ errorHandler: notify.onError('Error: <%= error.message %>') })) 94 .pipe(postcss(plugins)) 95 .pipe(autoprefixer({ // ベンダープレフィックスの付与 96 browsers: ['last 3 version', 'ie >= 10','iOS >= 9.3', 'Android >= 4.4'], // (ベンダープレフィックスを付与する)対応ブラウザの指定 97 cascade: true // 整形する 98 })) 99 .pipe(rename(function (path) { 100 path.dirname += '/css'; 101 })) 102 .pipe(gulp.dest('../')) 103 .pipe(browserSync.reload({ stream: true })); 104} 105 106/** 107 * jsファイルをdestディレクトリに出力(コピー)します。 108 */ 109function js() { 110 return gulp.src(src.js, {base: src.root}) 111 .pipe(gulp.dest(dest.root)) 112 .pipe(browserSync.reload({stream: true})); 113} 114 115/** 116 * ローカルサーバーを起動します。 117 */ 118function browsersync() { 119 browserSync({ 120 server: { 121 baseDir: dest.root, 122 index: "index.html" 123 } 124 }); 125}; 126 127/** 128 * PugのコンパイルやCSSとjsの出力、browser-syncのリアルタイムプレビューを実行します。 129 */ 130function watchFiles(done) { 131 gulp.watch(src.html).on('change', gulp.series(html, browserReload)); 132 gulp.watch(src.scss).on('change', gulp.series(sass_style, browserReload)); 133 gulp.watch(src.css).on('change', gulp.series(css, browserReload)); 134 gulp.watch(src.js).on('change', gulp.series(js, browserReload)); 135} 136 137gulp.task('default', gulp.series(gulp.parallel(html, sass_style, css, js), gulp.series(browsersync, watchFiles))); 138 139/** 140 * 開発に使うタスクです。 141 * package.jsonに設定をして、`npm run default`で実行できるようにしています。 142 */ 143gulp.task('default', ['watch']); 144
index.pug
pug
1extend /_includes/_layout 2append variables 3 //- Required 4 - var pageTitle= ""; 5 - var pageDescription= site.description; 6 - var pageKeywords= site.keywords; 7 //- Optional 8 - var pageOgpTitle= ""; 9 - var pageOgpImage= site.ogpImage 10 - var pageLang= "ja"; 11 - var pageOgpType= "website"; 12 //- Not modified 13 - var pageUrl= relativePath; 14 15//- 固定ページにのみ追加のCSS 16append css 17 link(rel="stylesheet" href="/about/css/about.css") 18 19//- 固定ページにのみ追加のJS 20append js 21 script(src="/assets/js/common2.js") 22 23block content 24 // contents 25 p Contents変更しますよ 26 // /contents 27
_footer.pug
pug
1// footer 2footer 3 p: small copyright m-field 4// /footer 5
_header.pug
pug
1// header 2header 3 h1.site-name Pugで作ったサイト 4 5nav.global-nav 6 ul 7 li: a(href="#") トップ 8 li: a(href="#") メニュー01 9 li: a(href="#") メニュー02 10 li: a(href="#") メニュー03 11 li: a(href="#") メニュー04 12// /header 13
_layout.pug
pug
1block variables 2doctype html 3html(lang=pageLang) 4 head(prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# " + pageOgpType + ": http://ogp.me/ns/" + pageOgpType + "#") 5 include /_includes/_meta 6 7 body 8 include /_includes/_header 9 10 block content 11 12 include /_includes/_footer 13 include /_includes/_script
base.scss
scss
1$pc: 1024px; // PC 2$tab: 680px; // タブレット 3$sp: 480px; // スマホ 4 5@mixin pc { 6 @media (max-width: ($pc)) { 7 @content; 8 } 9} 10@mixin tab { 11 @media (max-width: ($tab)) { 12 @content; 13 } 14} 15@mixin sp { 16 @media (max-width: ($sp)) { 17 @content; 18 } 19} 20 21.box { 22 @include pc { 23 background-color: red; 24 }; 25 @include tab { 26 background-color: blue; 27 }; 28 @include sp { 29 background-color: yellow; 30 }; 31} 32 33@import "a"; 34@import "b"; 35
_a.scss
_a.scss
1.classA { 2 background-color: red; 3} 4
_b.scss
_b.scss
1.classB { 2 background-color: blue; 3} 4
common.js
js
1(function() { 2 console.log('Hello!!'); 3}()); 4
package.json
json
1{ 2 "name": "gulp-pug-test", 3 "version": "1.0.0", 4 "description": "GulpでPugを実行するためのテスト環境です。", 5 "main": "index.js", 6 "scripts": { 7 "start": "gulp" 8 }, 9 "keywords": [ 10 "gulp", 11 "pug" 12 ], 13 "author": "テンプレ作成者名", 14 "license": "MIT", 15 "devDependencies": { 16 "browser-sync": "^2.26.4", 17 "gulp": "^4.0.1", 18 "gulp-autoprefixer": "^6.1.0", 19 "gulp-clean-css": "^4.0.0", 20 "gulp-data": "^1.3.1", 21 "gulp-notify": "^3.2.0", 22 "gulp-plumber": "^1.2.1", 23 "gulp-postcss": "^8.0.0", 24 "gulp-pug": "^4.0.1", 25 "gulp-rename": "^1.4.0", 26 "gulp-sass": "^4.0.2", 27 "gulp-sass-glob": "^1.0.9", 28 "postcss-flexbugs-fixes": "^4.1.0" 29 } 30}
試したこと
・gulpのバージョン違いによるものかと考えたので
参考サイトに従い、変更すべきところを変更しました。
https://qiita.com/hibikikudo/items/493fbfbbea183c94b38b
・変数名が間違えているのかチェック
・package.jsonのアップデート
・gulpfile.jsのwatch部分の記述を変更
・以下のサイトに従ってパッケージのgulpをアップデートするが
package.jsonではgulp 4.01
コマンドプロンプトでgulp -vをするとなぜか
CLI version 2.1.0
Local version 3.9.1
となってしまう
https://kenyo--c.com/css/1572/
補足情報(FW/ツールのバージョンなど)
gulpのバージョン
CLI version 2.1.0
Local version 3.9.1
node v10.15.3
npm v6.4.1
マルチポスト先
ここにより詳細な情報を記載してください。

回答2件
あなたの回答
tips
プレビュー