GithubHelp home page GithubHelp logo

yizhuang / webpack-merge-config Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 58 KB

Merges webpack config in a smarter way as an alternative to webpack-merge that promotes code reuse.

License: MIT License

JavaScript 100.00%

webpack-merge-config's Introduction

Welcome to webpack-merge-config ๐Ÿ‘‹

npm version

Documentation License: MIT

Merges webpack config in a smarter way as an alternative to webpack-merge that promotes code reuse.

Inspiration

Why use this instead of webpack-merge?

See the followings:

const config1 = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: [
          {
            loader: "css-loader",
            options: { importLoaders: 1 },
          },
        ],
      }
    ],
  },
}
const config2 = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: [
          {
            loader: "style-loader"
          },
        ],
      }
    ],
  },
}

const finalConfig = merge(config1, config2);

In webpack-merge the output will be the following which duplicates the same loaders:

const finalConfig = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: [
          {
            loader: "css-loader",
            options: { importLoaders: 1 },
          },
        ],
      },
      {
        test: /\.css$/,
        loader: [
          {
            loader: "style-loader"
          },
        ],
      }
    ],
  },
 }

In webpack-merge-config it will be merged instead as:

const finalConfig = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: [
          {
            loader: "style-loader"
          },
          {
            loader: "css-loader",
            options: { importLoaders: 1 },
          },
        ],
      },
    ],
  },
 }

Install

$ npm install webpack-config-merge --save-dev

const merge = require('webpack-config-merge');
module.exports = merge(config1, config2, {
  priority: 2
});
// If priority is 2, the lib will use unshift instead of push, this is useful for CSS related config.
// For example, appends MiniCssExtractPlugin to the front.
// Else, the third argument can be left empty.

More examples

const config1 = {
  plugins: [
    new HtmlWebPackPlugin({
      filename: 'index.html'
    })
  ]
}

const config2 = {
  plugins: [
    new HtmlWebPackPlugin({
      hash: true
    })
  ]
}
const finalConfig = merge(config1, config2);

Output:
const finalConfig = {
    plugins: [
    new HtmlWebPackPlugin({
      filename: 'index.html',
      hash: true
    })
  ]
}


const config1 = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: [
          {
            loader: "style-loader"
          },
        ],
      }
    ],
  },
  plugins: [
    new CopyWebpackPlugin([
      { from: '/1', to: '/2' },
    ])
  ]
}

const config2 = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: [
          AnyLoader
        ],
      }
    ],
  },
  plugins: [
    new CopyWebpackPlugin([
      { from: '/3', to: '/4' },
    ])
  ]
}

const finalConfig = merge(config1, config2);

Output:
const finalConfig = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loader: [
         {
            loader: "style-loader"
          },
          AnyLoader
        ],
      }
    ],
  },
  plugins: [
    new CopyWebpackPlugin([
      { from: '/1', to: '/2' },
      { from: '/3', to: '/4' },
    ])
  ]
}

Author

๐Ÿ‘ค YIZHUANG

Show your support

Give a โญ๏ธ if this project helped you!

๐Ÿ“ License

Copyright ยฉ 2019 YIZHUANG.
This project is MIT licensed.


This project structure was generated with โค๏ธ by git-repo-npm-bootster

webpack-merge-config's People

Contributors

yizhuang avatar

Stargazers

Faremax avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.