Code Guru
Code Guru

Reputation: 15578

How to use webpack with extract-text-webpack-plugin to compile scss files to css

I have created an angular2 project with angular material2 components. Now I want to customise my theme so I have used webpack module as this will create one single file from all the scss files so I have used extract-text-webpack-plugin to create individual files.

Here is my webpack.config.js

var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require("extract-text-webpack-plugin");
// multiple extract instances
let extractCSS = new ExtractTextPlugin('[name].css');

module.exports = {
    entry: [
        '/src/assets/theme/'
    ],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'index.js',
        publicPath: '/dist/'
    },

    module: {
        loaders: [
            { test: /\.scss$/i, loader: extractCSS.extract(['css', 'sass']) }
        ]
    },
    plugins: [
        extractCSS
    ]
}

And here is the script in my package.json

"build:css": "webpack --config webpack.config.js"

and the command to run

npm run build:css

but this is showing

> webpack --config webpack.config.js

Hash: a5e945c400552ce5350f
Version: webpack 3.3.0
Time: 68ms
   Asset    Size  Chunks             Chunk Names
index.js  2.6 kB       0  [emitted]  main
   [0] multi /src/assets/theme/ 28 bytes {0} [built]

ERROR in multi /src/assets/theme/
Module not found: Error: Can't resolve '/src/assets/theme/' in 'D:\Workspace\WebAdmin_Blade_Template_Material2\webapp'
 @ multi /src/assets/theme/

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build:css"
npm ERR! node v6.9.5
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] build:css: `webpack --config webpack.config.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build:css script 'webpack --config webpack.config.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the webapp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     webpack --config webpack.config.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs webapp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls webapp
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:

Problem is with the entry, but not able to solve this? what is the issue? Am I missing something?

Edited

I was missing index.js in src/assets/theme/. Here is the content of this file

require('./theme.scss');
require('./custom.scss');

and installed css-loader and sass-loader.

To make this working changed

loaders: [
                { test: /\.scss$/i, loader: extractCSS.extract(['css', 'sass']) }
            ]

instead of CSS and sass to css-loader and sass-loader.

the problem is, it is creating one single file from all the sccss files in /dist/main.css file. How can I create individiual files for each scss file?

Upvotes: 0

Views: 235

Answers (0)

Related Questions