G
G
gallantalex2017-03-08 15:03:40
JavaScript
gallantalex, 2017-03-08 15:03:40

How to compile to multiple files in webpack?

I need webpack scripts to output separately, and styles in a separate css file. How to do it? While the webpack config is like this and everything compiles to index.js:

var path = require('path');

module.exports = {
  entry: './entry.js',
  output: {
    filename: 'testApp/www/index.js'
  },  
  resolve: {
    extensions: ['.js', '.jsx', '.json']
  },
  module: {
    loaders: [
      { test: /\.jsx?$/,
        loader: 'babel-loader',
        include: path.resolve(__dirname, "app"),
        query:
        {
          presets:['react', 'es2015', 'stage-0']
        }
      },
      {
        test: /\.scss$/,
        loader: 'style-loader!css-loader!sass-loader'
      }
    ]
  }
};

What to add to create a css-file style/index.css&

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2017-03-08
@maxfarseer

You need to use extractTextPlugin Config snippet
:
for webpack 2:

module.exports = {
  devtool: 'cheap-module-source-map',
  entry: [
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'production/public/'),
    filename: 'bundle.js',
    publicPath: '/',
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new ExtractTextPlugin({
      filename: 'style.css',
      disable: false,
      allChunks: false, // true
    })
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        loaders: ['babel-loader'],
        include: path.join(__dirname, 'src'),
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: ['css-loader', 'postcss-loader'],
          publicPath: '/public',
        }),
      }, {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: ['css-loader', 'postcss-loader', 'sass-loader'],
          publicPath: '/public',
        }),
      },
....

For webpack 1:
module.exports = {
  devtool: 'cheap-module-source-map',
  entry: [
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'production/public/'),
    filename: 'bundle.js',
    publicPath: '/',
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new ExtractTextPlugin('style.css', {
      allChunks: false
    })
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel'],
        include: path.join(__dirname, 'src')
      },
      {
        test: /\.scss$/,
        include: path.join(__dirname, 'src'),
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader!sass-loader')
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
      },
...

Of course, you need to remember to add the ExtractTextPlugin
const ExtractTextPlugin = require('extract-text-webpack-plugin');

G
gallantalex, 2017-03-08
@gallantalex

That is, I now have it written like this, but still a bunch of errors fall out:

var path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ExtendPlugin = require('extended-define-webpack-plugin');

module.exports = {
  devtool: 'cheap-module-source-map',
  entry: [
    './entry.js'
  ],
  output: {
    path: path.join(__dirname, 'production/public/'),
    filename: 'bundle.js',
    publicPath: '/',
  },
  plugins: [
    new ExtendPlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new ExtractTextPlugin({
      filename: '/testApp/www/style.css',
      disable: false,
      allChunks: false, // true
    })
  ],
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loaders: ['babel-loader'],
        include: path.join(__dirname, 'src'),
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: ['style-loader','css-loader','sass-loader'],
          publicPath: '/public',
        }),
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: ['style-loader','css-loader','sass-loader'],
          publicPath: '/public',
        }),
      }
    ]
  }
};

A
alvvi, 2017-03-08
@alvvi

I’ve only been familiar with webpack for a couple of days, but I saw in the documentation a compilation of this kind: module.exports = [{ }, { }, { }];
, which, in fact, will take several configs, which, most likely, can be configured in the way you need, but I haven’t used it in practice yet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question