53
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue CLI 3でグローバルなSCSSを読み込ませる

Last updated at Posted at 2019-09-11

ググったら情報がいろいろあって、ちょっと悩んだので整理。

Sassが使えるようにする

$ yarn add -D node-sass sass-loader

.scssファイルを準備

↓こんな感じでパーシャル(名前が_で始まるファイル)があるとして、

フォルダ構成
src/
|- assets/
|  |- sass/
|  |  |- abstracts/
|  |  |  |- _functions.scss
|  |  |  |- _mixins.scss
|  |  |  |- _variables.scss
|  |  |  
|  |  |- base/
|  |  |  |- _reset.scss
|  |  |  
|  |  |- layout/
|  |  |  |- _grid.scss
|  |  |
|  |  |- themes/
|  |  |  |- _default.scss
|  |  |
|  |  |- main.scss
|  |  |- prepends.scss
|  ...
|
|- App.vue

全.vueファイルにインポートしたい(具体的なCSSを吐かない)ものはprepends.scssに入れる

prepends.scss
@import "./abstracts/variables";
@import "./abstracts/functions";
@import "./abstracts/mixins";

具体的なCSSを出力するものはmain.scssに入れる

main.scss
@import "./base/reset";
@import "./layout/grid";
@import "./themes/default";

プロジェクトに読み込ませる

main.jsに↓を追加する。

main.js

require('@/assets/sass/main.scss');

vue.config.js(無ければ作成して)のmodule.exportsに↓を追加する。

vue.config.js
module.exports = {
  css: {
    loaderOptions: {
      scss: {
        prependData: '@import "./src/assets/sass/prepends.scss";'
      }
    }
  }
};

ちなみに↑のprependData:data:と書くと

ValidationError: Invalid options object. Sass Loader has been initialised using an options object that does not match the API schema.
 - options has an unknown property 'data'. These properties are valid:
   object { implementation?, sassOptions?, prependData?, sourceMap?, webpackImporter? }

こんなエラーが出てビルドが通らなくなっているので注意。

53
46
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
53
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?