Skip to content

Instantly share code, notes, and snippets.

@kkotaro0111
Last active December 25, 2018 11:31
Show Gist options
  • Save kkotaro0111/235e5fadb0423ad764286a28c8fda025 to your computer and use it in GitHub Desktop.
Save kkotaro0111/235e5fadb0423ad764286a28c8fda025 to your computer and use it in GitHub Desktop.
webpack.config.jsの基本

基本形

  • 最低限これで動く
  • yarn add --dev webpack webpack-cli
const webpack = require('webpack');
const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist')
  }
}

babel対応

  • const が var になる等
  • yarn add --dev @babel/core @babel/preset-env babel-loader
const webpack = require('webpack');
const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env'
              ]
            }
          }
        ]
      }
    ]
  }
};

sass対応

  • yarn add --dev sass-loader node-sass style-loader css-loader
const webpack = require('webpack');
const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env'
              ]
            }
          }
        ]
      },
      {
        test: /\.sass$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              url: false,
              sourceMap: true, // sourceMapに対応させるなら
              importLoaders: 1
            },
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true // sourceMapに対応させるなら
            }
          }
        ]
      }
    ]
  }
};

SASSのCSSファイル化

  • 生成されるcssをファイルとして別途書き出す
  • yarn add --dev mini-css-extract-plugin
const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env'
              ]
            }
          }
        ]
      },
      {
        test: /\.sass$/,
        use: [
          MiniCssExtractPlugin.loader, // style-loaderの代わりにこれを使う。style-loaderはcssをjsに組み込むもの
          {
            loader: 'css-loader',
            options: {
              url: false,
              sourceMap: true,
              importLoaders: 1
            },
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true
            }
          }
        ]
      }
    ]
  },
  plugins: [
      new MiniCssExtractPlugin({
        filename: '../css/style.css'
      })
  ]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment