0

I have a Wordpress theme that I'm migrating from gulp to webpack. I'm having a heck of a time with it not compiling SASS files. It's watching and compiling the JS file just fine but SASS files won't compile into a CSS. Here's my webpack-config.js

const path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: ['./js/app.js', './sass/style.scss'],
  plugins: [
    new UglifyJSPlugin(),
    new MiniCssExtractPlugin({
      filename: 'style.min.css',
      chunkFilename: "[name].css"
    })
  ],
  output: {
    filename: 'app-compiled.min.js',
    path: path.resolve(__dirname, 'js/assets')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  },
  module: {
    rules: [
      {
        test: /\.(scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'url-loader',
          'css-loader',
          'sass-loader'
        ]
      },
    ]
  }
};

My end goal is to have the css and js in separate files. I thought the optimization was supposed to take care of that, but it's not. From other forms, the MiniCssExtractPlugin plugin needs to be right before the css-loader but if I do that, I get a bunch of unresolved font and img errors from within the SCSS files. With the Mini in the use, it doesn't compile and I get a...

const resource = this._identifier.split('!').pop();
                                      ^

TypeError: Cannot read property 'split' of undefined

Without it, the files get watched but doesn't compile anywhere. I just need the SASS to compile. Help!!!

cbloss793
  • 1,555
  • 4
  • 19
  • 30

1 Answers1

1

I've been at this for awhile and I knew the moment I'd post this question, I'd find a right combo. I needed a couple additional options to the css-loader. Found the solution here.

This webpack-config.js worked for me (note: I had to change the output folder so the sass didn't get compiled in the js folder)

const path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: ['./js/app.js', './sass/style.scss'],
  plugins: [
    new UglifyJSPlugin(),
    new MiniCssExtractPlugin({
      filename: 'style.min.css',
      chunkFilename: "[name].css"
    })
  ],
  output: {
    filename: 'app-compiled.min.js',
    path: path.resolve(__dirname, 'assets')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  },
  module: {
    rules: [
      {
        test: /\.(scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader:'css-loader',
            options: { url: false, sourceMap: true }
          },
          'sass-loader'
        ]
      },
    ]
  }
};
cbloss793
  • 1,555
  • 4
  • 19
  • 30