Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I build sass/less/css in webpack without requiring them in my JS?

I currently have some react components & sass that are being built with webpack successfully. I also have a main sass file that builds to css with a gulp task.

I'd like to only have to use one of these technologies, is it possible to just compile sass to css without an associated require to the sass in an entry file?

Here's an example trying to use WebpackExtractTextPlugin

webpack.config.js

entry_object[build_css + "style.css"] = static_scss + "style.scss"; module.exports = {   entry: entry_object,   output: {     path: './',     filename: '[name]'   }, {   test: /\.scss$/,   include: /.scss$/,   loader: ExtractTextPlugin.extract("style-loader", "css!sass") }   plugins: [     new ExtractTextPlugin("[name]")   ] 

after running webpack, the style.css asset looks like this:

/******/ (function(modules) { // webpackBootstrap /******/    // The module cache /******/    var installedModules = {}; /******/ /******/    // The require function /******/    function __webpack_require__(moduleId) {  ... 
like image 835
ex-zac-tly Avatar asked Apr 21 '16 21:04

ex-zac-tly


People also ask

How do I bundle CSS with webpack?

By default, Webpack will inline your CSS as Javascript tags that injects the CSS into the page. This sounds strange but it lets you do hot reloading in development. In production you extract them to a file using the ExtractTextPlugin, and require it with a normal link tag.

Does Sass-loader require node-Sass?

node. js - Sass-loader requires node-sass >=4 even if that exist - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Is webpack mandatory?

No, Babel and Webpack is not necessary for React stack. You can still find other alternatives to build your favourite stack such as Browserify and Gulp. However, if you want to make things easier, I do recommend you learn and use Babel and Webpack together with React because: You can use modules.


1 Answers

I solved this with the help of @bebraw

As he stated in his comment, webpack will create a dummy javascript file as followed by the pattern in your output.filename when using ExtractTextPlugin. Because I was setting the output file of the ExtractTextPlugin to exactly the same as the name in the output.filename, it was only outputting the javascript file. By ensuring that the name of the output.filename and ExtractTextPlugin output filename are different, I was able to load my sass to css beautifully.

Here's the final example of the webpack.config.js

entry_object[build_css + "style"] = static_scss + "style.scss"; module.exports = {   entry: entry_object,   output: {     path: './',     filename: '[name]'   }, {   test: /\.scss$/,   include: /.scss$/,   loader: ExtractTextPlugin.extract("style-loader", "css!sass") }   plugins: [     new ExtractTextPlugin("[name].css")   ] 
like image 76
ex-zac-tly Avatar answered Sep 21 '22 15:09

ex-zac-tly