stylusで生成したソースに対して、autoprefixerを適用する方法を教えてください。
情報を集めていると、postcssでautoprefixerを使用するという情報を得られました。
そのため、後述するwebpack.config.js
のように、stylusに対して、次のローダーを使用するように調整しました。
- vue-style-loader
- css-loader
- postcss-loader(autoprefixerを指定しています。)
- stylus-loader
ビルド自体はできるものの、autoprefixerで追加されるはずのスタイルが追加できませんでした。
webpackとstylusを組み合わせた状態で、autoprefixを使うにはどのように設定すればよいでしょうか?
仕様
先日設問した時のソースコードをベースに作っていますので、重複する箇所もあります。
ディレクトリ構成
下記の様な構成になっています。
ビルドしたいファイルは、src/stylus
配下にあるファイルです。
. ├── dest(ビルドしたファイルを置くディレクトリです) │ ├── bundle.js │ └── index.html ├── node_modules ├── package-lock.json ├── package.json ├── src(ビルドする元のファイルです) │ ├── App.vue │ ├── components │ │ └── Counter.vue │ ├── index.js │ ├── pug │ │ ├── index.pug │ │ └── parts │ └── stylus │ ├── app.stylus │ └── common.styl ├── static └── webpack.config.js
インストールしているnpmのライブラリ
package.jsonのライブラリの中身をそのまま転記して恐縮ですが、次のライブラリをインストールしています。
試行錯誤でインストールしたものもあり、不要なものもあるかもしれません。
"devDependencies": { "@babel/core": "^7.2.2", "@babel/preset-env": "^7.3.1", "autoprefixer": "^9.4.7", "babel-loader": "^8.0.5", "clean-webpack-plugin": "^1.0.1", "css-loader": "^2.1.0", "html-webpack-plugin": "^3.2.0", "postcss-loader": "^3.0.0", "pug": "^2.0.3", "pug-plain-loader": "^1.0.0", "raw-loader": "^1.0.0", "style-loader": "^0.23.1", "stylus": "^0.54.5", "stylus-loader": "^3.0.2", "vue-loader": "^15.6.2", "vue-template-compiler": "^2.5.22", "webpack": "^4.29.0", "webpack-cli": "^3.2.1", "webpack-dev-server": "^3.1.14" }, "dependencies": { "vue": "^2.5.22" }, "babel": { "presets": [ "@babel/preset-env" ] },
ファイルについて
webpack.config.js
js
1'use strict' 2 3const path = require('path') 4 5const vueLoaderPlugin = require('vue-loader/lib/plugin') 6const htmlWebpackPlugin = require('html-webpack-plugin') 7const cleanWebpackPlugin = require('clean-webpack-plugin') 8module.exports = { 9 10 // エントリーポイント設定 11 entry: { 12 // エントリーポイントのファイル 13 app: './src/index.js' 14 }, 15 16 // 出力設定 17 output: { 18 // 出力ディレクトリ 19 path: path.resolve(__dirname, './dest'), 20 // 出力ファイル名 21 filename: 'bundle.js' 22 }, 23 24 // テストサーバーの設定 25 devServer: { 26 // 起動時のrootサーバー 27 contentBase: path.resolve(__dirname, 'public') 28 }, 29 30 // モジュール設定 31 module: { 32 rules: [ 33 { 34 test: /.vue$/, 35 loader: 'vue-loader', 36 options: { 37 postcss: [require('autoprefixer')] 38 } 39 }, 40 { 41 test: /.js$/, 42 loader: 'babel-loader' 43 }, 44 { 45 test: /.pug$/, 46 oneOf: [ 47 { 48 resourceQuery: /^?vue/, 49 use: [ 50 'pug-plain-loader' 51 ] 52 }, 53 { 54 use: [ 55 'raw-loader', 56 'pug-plain-loader' 57 ] 58 } 59 ] 60 }, 61 { 62 test: /.css$/, 63 use: [ 64 'vue-style-loader', 65 'css-loader', 66 { 67 loader: 'postcss-loader', 68 options: { 69 plugins: [ 70 require('autoprefixer')({ 71 grid: true 72 }) 73 ] 74 } 75 }, 76 ] 77 }, 78 { 79 test: /.styl(us)?$/, 80 exclude: /node_modules/, 81 use: [ 82 { 83 loader: 'vue-style-loader' 84 }, 85 { 86 loader: 'css-loader' 87 }, 88 { 89 loader: 'postcss-loader', 90 options: { 91 plugins: [ 92 require('autoprefixer')({ 93 grid: true 94 }) 95 ] 96 } 97 }, 98 { 99 loader: 'stylus-loader' 100 } 101 ] 102 } 103 ] 104 }, 105 106 // リザーブ 107 resolve: { 108 extensions: ['.js', '.vue'], 109 alias: { 110 vue$: 'vue/dist/vue.esm.js' 111 } 112 }, 113 114 // プラグイン設定 115 plugins: [ 116 new cleanWebpackPlugin(['dest']), 117 new vueLoaderPlugin(), 118 new htmlWebpackPlugin({ 119 template: './src/pug/index.pug', 120 inject: true 121 }) 122 ] 123}
stylusのファイル
ファイル一覧のcommon.styl
の中身です。
ブラウザによって記述が変わるdisplay flex
を追加しましたので、この部分が各ブラウザ毎の記述が追加される想定です。
.lists display flex justify-content space-between width: 100% .lists__list display block width 100px height 100px background-color green
index.js(エントリーポイントに使用しているjs)
js
1import Vue from 'vue' 2import App from './App' 3 4import './stylus/app.styl' 5 6new Vue({ 7 el: '#app', 8 components: { App }, 9 template: '<app/>' 10})

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。