普段は Sass や CSS なんて全然書かない & あまりにもしょうもない内容なんですが、あまりググっても引っかからなかったのでメモ。
ちょっと Sass を試してみたいと思い、公式サイトのサンプルコードを
Webpack で CSS にコンパイルしてみようかと思ったら大いにハマりました。こんな感じのコード↓
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
最初に試した事
まずは ここの解説記事を参考に、必要なプラグイン群をインストールしました。
$ npm install --save-dev webpack extract-text-webpack-plugin node-sass style-loader css-loader sass-loader
ちなみに、インストール時の各プラグインのバージョン情報は以下の通り。
{
...
"devDependencies": {
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.2",
"node-sass": "^4.6.0",
"sass-loader": "^6.0.6",
"style-loader": "^0.19.0",
"webpack": "^3.8.1"
}
}
次に、Webpack のコンフィグを準備しました。
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: {
'style': path.join(__dirname, 'src', 'sass', 'style.scss'),
},
output: {
path: path.join(__dirname, 'build', 'app'),
filename: 'static/css/[name].css'
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
)
}
]
},
plugins: [
new ExtractTextPlugin('[name].css')
]
};
これで ./src/sass/style.scss
がコンパイルされて、./build/app/static/css/style.css
が生成されるはず! (パスが長い)
webpack --progress
でコンパイルを実行してみると特にエラー無くコンパイルできた模様。
生成された style.css
を確認してみると...?
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
// removed by extract-text-webpack-plugin
/***/ })
/******/ ]);
/*# sourceMappingURL=style.css.map*/
...。(白目)
なお、source map を使ってブラウザ上で元の style.scss
を確認してみたところ、
// removed by extract-text-webpack-plugin
//////////////////
// WEBPACK FOOTER
// ./src/sass/style.scss
// module id = 0
// module chunks = 0
...。(失神)
で、何が悪かったのか
検索してみるとほとんどそれらしい情報は出てこないのですが、一件だけまさにこの症状についての Stack Overflow の書き込みを発見。(https://stackoverflow.com/questions/44693588/compiling-sass-with-webpack-3)
曰く、output に設定した filename は ExtractTextPlugin の出力ファイル名と一致させなければならない とのこと。
つまり、webpack.config.js
を
...
module.exports = {
...
output: {
path: path.join(__dirname, 'build', 'app'),
filename: 'static/css/[name].css'
},
...
plugins: [
new ExtractTextPlugin('[name].css')
]
};
ではなく、
...
module.exports = {
...
output: {
path: path.join(__dirname, 'build', 'app'),
filename: 'static/css/[name].css'
},
...
plugins: [
new ExtractTextPlugin('static/css/[name].css')
]
};
のようにして出力される CSS ファイル名を一致させる必要があるとのことでした。
確かにこの修正だけであっさり Sass から CSS にきちんとコンパイルされました...
Webpack でのコンパイル時にエラーかなにか吐いてくれよ...