How to link and copy CSS files from source folder to dist folder?

Here is my folder structure:

enter image description here

I want to minimize and link CSS files inside my folder src/cssand output it as a separate CSS file inside dist. All the examples that I have seen so far recommend require-in the CSS file inside the JS file. I do not need this. Is there a way to tune in webpack.config.jsto just minimize and copy these files?

+6
source share
2 answers

It worked.

Install dev-dependecies

npm i extract-text-webpack-plugin --save-dev
npm i css-loader --save-dev

webpack.config.js

const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const extractCSS = new ExtractTextPlugin('bundle.min.css')

module.exports = {
  entry: {
    'bundle.min.css': [
      __dirname + '/src/styles/abc.css',
      __dirname + '/src/styles/xyz.css',
      __dirname + '/src/styles/mno.css'
    ]
  },
  devtool: '',
  output: {
    path: __dirname + '/dist/styles/',
    filename: '[name]'

  },
  module: {
    rules: [{
        test: /\.css$/i,
        use: extractCSS.extract({
          use: {
            loader: 'css-loader',
            options: {
              minimize: true
            }
          }
        })
    }]
  },
  resolve: {
    alias: {},
    modules: [],
    extensions: ['.css']
  },
  plugins: [
    extractCSS
  ]
};

bundle.min.csswill be generated. Based on minimize: true/false, a decision will be made on minimization. Enjoy it!

+2
source

It will be completed in three stages:

; css-loader style-loader extract-text-webpack-plugin .

:

const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const path = require('path');
module.exports = {
  entry: {
    app: './src/index.js'
  },
  output: {
    path: 'dist',
    filename: 'js/[name]-bundle.js'
  },
  devtool: "cheap-source-map",
  resolveLoader: {
    modules: [
      'node_modules',
      path.join(__dirname, '../node_modules'),
    ]
  },
  module: {
    loaders: [
      {
        test: /.css?$/,
        loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }),
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("css/[name].css"),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
        screw_ie8: true,
        conditionals: true,
        unused: true,
        comparisons: true,
        sequences: true,
        dead_code: true,
        evaluate: true,
        join_vars: true,
        if_return: true
      },
      output: {
        comments: false
      }
    }),
  ]
}

, require('./style.css');

, .

css, file-loader, .

:

{
    test: /.png?$/,
    loader: 'file-loader?name=img/[name].[ext]',
    exclude: /node_modules/
}

UgligyJsPlugin CSS.

0

Source: https://habr.com/ru/post/1015795/


All Articles