R
R
RemakeRr2017-05-16 11:39:22
webpack
RemakeRr, 2017-05-16 11:39:22

NODE_ENV. Webpack 2. How to export styles to a separate css file?

Hey! Who understands node and webpack2 ?
Need help. The problem is this.
In webpack.config.js, it is configured so that when developing css styles, files are not unloaded into separate css files. but were inside js. And during production they expressed themselves. But the problem is with node_env. I have windows, I think because of this it does not work. When you exit, it turns out that the styles are inside the js file.
The console says ""NODE_ENV" is not an internal or external
command, operable program or batch file."
How to solve this problem? tried different webpack and npm plugins. I don't know much about node.

var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");
var path = require("path");
var bootstrapEntryPoints = require('./webpack.bootstrap.config');

var isProd = process.env.NODE_ENV === 'production' // true or false
var cssDev = ['style-loader', 'css-loader', 'sass-loader']
var cssProd = ExtractTextPlugin.extract({
    fallback: 'style-loader',
    loader: ['css-loader', 'sass-loader'],
    publicPath: '/dist'
})

var cssConfig = isProd ? cssProd : cssDev;
var bootstrapConfig = isProd ? bootstrapEntryPoints.prod : bootstrapEntryPoints.dev;

module.exports = {
    entry: {
        app: './src/app.js',
        bootstrap: bootstrapConfig
    },
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: '[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.sass$/,
                use: cssConfig,
                /*use: ExtractTextPlugin.extract({
                 fallback: "style-loader",
                 use: ['css-loader','sass-loader'],
                 publicPath: '/dist'
                 })*/
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.pug/,
                loaders: ['html-loader', 'pug-html-loader'],
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                use: [
                    'file-loader?name=images/[name].[ext]',
                    //'file-loader?name=[name].[ext]&outputPath=images/&publicPath=images/',
                    'image-webpack-loader'
                ]
            },
            /*{
             test: /\.(woff|woff2|eot|ttf|otf)$/,
             use: ['file-loader?name=fonts/[name].[ext]']
             },
             {
             test: /\.(woff|woff2|eot|ttf|otf)$/,
             use: ['url-loader?limit=10000&name=fonts/[name].[ext]'
             ]
             },*/
            { test: /\.(woff|woff2|eot|ttf|otf)$/, use: 'url-loader?limit=10000&name=fonts/[name].[ext]' },
            { test: /\.(ttf|eot)$/, use: 'file-loader?name=fonts/[name].[ext]' },
            // Bootstrap 3
            {
                test:/bootstrap-sass[\/\\]assets[\/\\]javascripts[\/\\]/,
                use: 'imports-loader?jQuery=jquery'
            }

        ]
    },
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        compress: true,
        //hot: true,
        stats: "errors-only",
        port: 9000,
        open: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'Index Html Webpack plugin',
            /*minify: {
             collapseWhitespace: true,
             removeComments: true,
             },*/
            hash: true,
            /*chunks: 'index',*/
            //excludeChunks: ['contact'],
            template: './src/index.pug',
        }),
        /*new HtmlWebpackPlugin({
         title: 'Contact Page',
         minify: {
         collapseWhitespace: true,
         removeComments: true,
         },
         hash: true,
         chunks: ['contact'],
         /!*excludeChunks: ['index'],*!/
         filename: 'contact.html',
         template: './src/contact.html',
         }),*/
        new ExtractTextPlugin({
            filename: '/css/[name].css',
            disable: !isProd,
            //disable: false,
            allChunks: true
        }),
        /*new ExtractTextPlugin({
         filename: 'contact.css',
         disable: false,
         allChunks: true
         }),*/
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NamedModulesPlugin(),
        /*new PurifyCSSPlugin({
         // Give paths to parse for rules. These should be absolute!
         paths: glob.sync(path.join(__dirname, 'src/!*.html'))
         })*/
    ]
}

"scripts": {
    "dev": "webpack-dev-server",
    "prod": "npm run clean && webpack -p",
    "clean": "rimraf ./dist/*"
  },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Masterov, 2017-05-16
@AlexMasterov

cross-env

"scripts": {
  "prod": "cross-env NODE_ENV=production webpack",
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question