Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Last active November 6, 2017 17:23
Show Gist options
  • Save allenhwkim/ff408fd96a44fa4ace3369f82cf75c94 to your computer and use it in GitHub Desktop.
Save allenhwkim/ff408fd96a44fa4ace3369f82cf75c94 to your computer and use it in GitHub Desktop.
Starting Webpack

Starting Webpack

Simple Example

index.js

import bar from './bar';
bar();

bar.js

export default function bar() {
  //
}

webpack.config.js

module.exports = {
  entry: './app.js',
  output: {
    filename: 'bundle.js'
  }
};

index.html

<html>
  <head> ... </head>
  <body>
    ...
    <script src="bundle.js"></script>
  </body>
</html>

Concepts

. entry - what files should I start to make a bundle?
. output  - what’s the bundle name and path?
. loaders - It's not .js, but .txt, .css, .html, .ts, .jsx? Then, what should I do?
. plugins - What should I do more, e.g, minification, copy rights?

Getting Started

. $ npm install --save-dev webpack . ## create a webpack.config.js . $ webpack

webpack.config.js

entry

module.exports = {
  entry: './path/to/my/entry/file.js'
};

output

module.exports = {
  ...
  output: {
    filename: 'my-first-webpack.bundle.js'
  }
};

loaders

module.exports = {
  ...
  module: {
    rules: [
      { test: /\.ts$/, loader: 'ts-loader'}
    ]
  }
}

plugins

module.exports = {
  ...
  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
}

webpack for development

$ npm install webpack-dev-server --save-dev
$ node_modules/.bin/webpack-dev-server

Demos

https://github.com/ruanyf/webpack-demos

$ git clone https://github.com/ruanyf/webpack-demos.git
$ cd webpack-demos
$ npm i
$ cd demo01
$ webpack-dev-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment