質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Sass

Sassは、プログラミング風のコードでCSSを生成できるスタイルシート言語です。 scss ファイルを、変換(コンパイル)してCSSファイルを作成します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

gulp

gulpは、Node.jsをベースとしたタスク自動化ツールの一つ。ストリームでファイルを処理することが特徴です。CSSプリプロセッサの使用時のコンパイルや、CSS・JavaScriptファイルの圧縮・結合などを自動的に行うことができます。

Q&A

解決済

1回答

773閲覧

Gulpでプレフィックス自動付与とCSSの順番整理が機能しない

kadot

総合スコア27

Sass

Sassは、プログラミング風のコードでCSSを生成できるスタイルシート言語です。 scss ファイルを、変換(コンパイル)してCSSファイルを作成します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

gulp

gulpは、Node.jsをベースとしたタスク自動化ツールの一つ。ストリームでファイルを処理することが特徴です。CSSプリプロセッサの使用時のコンパイルや、CSS・JavaScriptファイルの圧縮・結合などを自動的に行うことができます。

0グッド

0クリップ

投稿2019/05/20 01:13

編集2019/05/20 03:08

gulpで「autoprefixer」と「css-declaration-sorter」がうまく機能してないようです。
(プレフィックスが全く付与されていない、CSSの順番も自分で記述したものから変更なくバラバラ)

それ以外のパッケージは機能していて、コンパイルまでされています。
エラーも出ていません。

ファイル構成とgulpfile.jsは以下です。
イメージ説明

gulpfile

1const gulp = require("gulp"); 2const plumber = require('gulp-plumber'); 3const sass = require("gulp-sass"); 4const sassGlob = require("gulp-sass-glob"); 5const notify = require("gulp-notify"); 6const postcss = require("gulp-postcss"); 7const autoprefixer = require("autoprefixer"); 8const cssdeclsort = require("css-declaration-sorter"); 9const mqpacker = require("css-mqpacker"); 10 11gulp.task("default", function() { 12 return gulp.watch("scss/**/*.scss", function() { 13 14 return ( 15 gulp 16 .src("scss/**/*.scss") 17 18 .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")})) 19 .pipe(sassGlob()) 20 21 .pipe(sass({outputStyle: "expanded"}).on("error", sass.logError)) 22 23 .pipe(postcss([autoprefixer()])) 24 .pipe(postcss([cssdeclsort({order: 'smacss'})])) 25 .pipe(postcss([mqpacker()])) 26 27 .pipe(gulp.dest("www/css")) 28 ); 29 }); 30});

書籍通り記述したはずなのですがうまくいかず、パッケージ名でググっても良い記事がヒットしません。。
もしわかる方いらっしゃれば教えていただきたいです。

よろしくお願いします。

追加情報(scssと実行結果)

例えばこの箇所です。

scss

1.l-header { 2 height: 68px; 3 4 &__wrapper { 5 position: relative; 6 padding: 0 4.26%; 7 display: flex; 8 justify-content: space-between; 9 align-items: center; 10 } 11 12 &__sp-menu { 13 position: absolute; 14 top: 24px; 15 right: 0; 16 17 span { 18 width: 26px; 19 height: 3px; 20 color: #3E3E3E; 21 border-radius: 50%; 22 23 &:nth-child(1) { 24 top: 0; 25 } 26 27 &:nth-child(2) { 28 top: 5.5px; 29 } 30 31 &:nth-child(1) { 32 top: 8.5px; 33 } 34 } 35 } 36}

CSS

1.l-header { 2 height: 68px; 3} 4 5.l-header__wrapper { 6 display: flex; 7 position: relative; 8 align-items: center; 9 justify-content: space-between; 10 padding: 0 4.26%; 11} 12 13.l-header__sp-menu { 14 position: absolute; 15 top: 24px; 16 right: 0; 17} 18 19.l-header__sp-menu span { 20 width: 26px; 21 height: 3px; 22 border-radius: 50%; 23 color: #3E3E3E; 24} 25 26.l-header__sp-menu span:nth-child(1) { 27 top: 0; 28} 29 30.l-header__sp-menu span:nth-child(2) { 31 top: 5.5px; 32} 33 34.l-header__sp-menu span:nth-child(1) { 35 top: 8.5px; 36}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

s8_chu

2019/05/20 02:06

質問者さんの SCSS と その実行結果を教えていただけませんか?
kadot

2019/05/20 03:08

いまSCSSと実行結果を追加しました。(質問を更新しました) ただ、ひとつ発覚したことがありまして、s8_chuさんのくださった回答を見て、改めてCSS見直したら順番変わっていました。。いろいろ調べてくださっているのに本当にすいません…!僕の不注意でした。頭ごちゃごちゃで一部だけ見て質問してしまいました。気をつけます。 プレフィックスの方はついてないようです。例えばflexのところです。
s8_chu

2019/05/20 03:16

Autoprefixer が機能しているかを確認する方法も、回答文に書いてありますので、一度質問者さんの環境でも試してみてください。
kadot

2019/05/20 03:32

すいません…! いま試してみました。回答と同じコード書いたら、実行結果でプレフィックス付与されました! ということは僕の環境でも正しく動作しているということですね。 僕の理解が間違ってるのかもしれないのですが、 これが display: flex; こうなる display: -webkit-box; display: -ms-flexbox; display: flex; のかなと思っていたのですが、もしかしてこれはまた別の話…だったりするのでしょうか?
s8_chu

2019/05/20 03:59

モダンブラウザでは、 flexbox 関連のプロパティにベンダープレフィックスが不要なため、デフォルトではベンタープレフィックスが付与されることはありません。 オプションにて設定を変更することでベンダープレフィックスが付与されます。
kadot

2019/05/20 04:16

なるほど!そういうことなんですね! 解決しました。 丁寧に教えてくださり感謝しています。 今度このサービスを使う時は、しっかり見直して自分の質問にミスがないように気をつけます。 正常に動作しているかの確認も教えていただいた考え方を活かしていきたいと思います。 本当にありがとうございました。
guest

回答1

0

ベストアンサー

こちらの環境では、 Autoprefixer と CSS Declaration Sorter は正常に機能しました。正常に機能することを確認した手順を示します。

最初に、質問文のようなディレクトリ構造を作成したうえで、 package.json を以下のように記述し、 npm install コマンドを実行しました。

JSON

1{ 2 "name": "q190298", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": { 7 "test": "echo \"Error: no test specified\" && exit 1" 8 }, 9 "keywords": [], 10 "author": "", 11 "license": "ISC", 12 "dependencies": { 13 "autoprefixer": "^9.5.1", 14 "css-declaration-sorter": "^4.0.1", 15 "css-mqpacker": "^7.0.0", 16 "gulp": "^4.0.2", 17 "gulp-notify": "^3.2.0", 18 "gulp-plumber": "^1.2.1", 19 "gulp-postcss": "^8.0.0", 20 "gulp-sass": "^4.0.2", 21 "gulp-sass-glob": "^1.0.9" 22 } 23}

次に、 scss ディレクトリに style.scss を作成し、以下の内容を記述しました。

SCSS

1$dir: "/img/"; 2 3.container { 4 flex-direction: column; 5 $margin: 5px; 6 display: flex; 7 width: 100%; 8 border: 4em solid #fff; 9 10 .foo { 11 overflow: scroll; 12 width: calc(100vh - (100vw - 100%)); 13 background-color: #faa; 14 background-image: url($dir + "/foo.img"); 15 margin-top: $margin + 3px; 16 padding: 4em 6em 3em; 17 font-size: 15vw; 18 height: 150vh; 19 } 20 21 .bar { 22 height: 60vh; 23 background-color: #faf; 24 overflow: hidden; 25 margin-top: #{$margin} + 3px; 26 font-size: 30vw; 27 background-image: url("#{$dir}/foo.jpg"); 28 width: calc(100vw - 20%); 29 } 30} 31 32::placeholder { 33 color: #abf; 34} 35 36@media (min-resolution: 2dppx) { 37 .image { 38 background-image: url($dir + "bar.png"); 39 } 40}

ここで、 Autoprefixer, CSS Declaration Sorter が動作しているかを確かめるためには、以下の項目を確認すれば良いです。

  • placeholder 擬似要素は実験的な機能であるため、ブラウザとそのバージョンによっては、ベンダープレフィックスを付与する必要があります。そのため、この擬似要素にプレフィックスが付与されれば、 Autoprefixer は正常に動作していることになります。

  • 上記の SCSS は適当に書いたものですが、質問者さんが選択したプロパティ順序 (SMACSS) では、 box-sizing プロパティが一番上に、 display プロパティが上から二番目に配置されるように整形されるはずです(参考)。そのため、 box-sizing, display プロパティの位置が変化すれば、 CSS Declaration Sorter が正常に動作していることになります。


また、 gulpfile.js を以下のように変更し、 css ディレクトリ下に normal ディレクトリ、 formatted ディレクトリを作成することで、 Autoprefixer と CSS Declaration Sorter を実行する前後での CSS の変化を見ることにしました。

JavaScript

1const gulp = require("gulp"); 2const plumber = require('gulp-plumber'); 3const sass = require("gulp-sass"); 4const sassGlob = require("gulp-sass-glob"); 5const notify = require("gulp-notify"); 6const postcss = require("gulp-postcss"); 7const autoprefixer = require("autoprefixer"); 8const cssdeclsort = require("css-declaration-sorter"); 9const mqpacker = require("css-mqpacker"); 10 11gulp.task("default", function () { 12 return gulp.watch("scss/**/*.scss", function () { 13 14 return ( 15 gulp 16 .src("scss/**/*.scss") 17 .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")})) 18 .pipe(sassGlob()) 19 .pipe(sass({outputStyle: "expanded"}).on("error", sass.logError)) 20 .pipe(gulp.dest("www/css/normal")) 21 .pipe(postcss([autoprefixer()])) 22 .pipe(postcss([cssdeclsort({order: 'smacss'})])) 23 .pipe(postcss([mqpacker()])) 24 .pipe(gulp.dest("www/css/formatted")) 25 ); 26 }); 27});

そして、npx gulp コマンドを実行し、 style.scss を変更、保存すると、 normal, formatted ディレクトリには、それぞれ以下のような style.css が出力されました。

normal ディレクトリ

CSS

1.container { 2 flex-direction: column; 3 display: flex; 4 width: 50%; 5 border: 4em solid #fff; 6} 7 8.container .foo { 9 overflow: scroll; 10 width: calc(100vh - (100vw - 100%)); 11 background-color: #faa; 12 background-image: url("/img//foo.img"); 13 margin-top: 8px; 14 padding: 4em 6em 3em; 15 display: block; 16 font-size: 15vw; 17 height: 150vh; 18} 19 20.container .bar { 21 height: 60vh; 22 background-color: #faf; 23 overflow: hidden; 24 margin-top: 5px + 3px; 25 box-sizing: border-box; 26 font-size: 30vw; 27 background-image: url("/img//foo.jpg"); 28 width: calc(100vw - 20%); 29} 30 31::placeholder { 32 color: #abf; 33} 34 35@media (min-resolution: 2dppx) { 36 .image { 37 background-image: url("/img/bar.png"); 38 } 39}

formatted ディレクトリ

CSS

1.container { 2 display: flex; 3 flex-direction: column; 4 width: 50%; 5 border: 4em solid #fff; 6} 7 8.container .foo { 9 display: block; 10 width: calc(100vh - (100vw - 100%)); 11 height: 150vh; 12 margin-top: 8px; 13 padding: 4em 6em 3em; 14 overflow: scroll; 15 background-color: #faa; 16 background-image: url("/img//foo.img"); 17 font-size: 15vw; 18} 19 20.container .bar { 21 box-sizing: border-box; 22 width: calc(100vw - 20%); 23 height: 60vh; 24 margin-top: 5px + 3px; 25 overflow: hidden; 26 background-color: #faf; 27 background-image: url("/img//foo.jpg"); 28 font-size: 30vw; 29} 30 31::-webkit-input-placeholder { 32 color: #abf; 33} 34 35::-moz-placeholder { 36 color: #abf; 37} 38 39:-ms-input-placeholder { 40 color: #abf; 41} 42 43::-ms-input-placeholder { 44 color: #abf; 45} 46 47::placeholder { 48 color: #abf; 49} 50 51@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx) { 52 .image { 53 background-image: url("/img/bar.png"); 54 } 55}

整形後のコードは、最初に挙げた 2 つの動作条件を満たしているため、これらのパッケージと、 gulpfile.js は正常に機能しているとわかります。

投稿2019/05/20 02:07

編集2019/05/20 03:14
s8_chu

総合スコア14731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問