LoginSignup
2
2

More than 5 years have passed since last update.

Webpackチュートリアル

Last updated at Posted at 2016-11-06

tutorial) http://webpack.github.io/docs/tutorials/getting-started/

Step1: src.js -> dst.js

# install webpack
$ mkidr /path/to/dir
$ npm init
$ npm install webpack --save-dev
// js/entry.js
document.write('it works');
# entry.js -> bundle.js
$ node_modules/webpack/bin/webpack.js js/entry.js js/bundle.js

Step2: content.js -> entry.js -> bundle.js

// content.js
module.exports = 'it works';

// entry.js
document.write(require('./content.js'));
# content.js -> entry.js -> bundle.js
$ node_modules/webpack/bin/webpack.js js/entry.js js/bundle.js

Step3: CSS loading

$ npm install css-loader style-loader
/* css/style.css */
body {
    background-color: yellow;
}
// entry.js
require('../css/style.css');
...
$ node_modules/webpack/bin/webpack.js ./js/entry.js ./js/bundle.js --module-bind 'css=style!css'

Step4 Config file

// webpack.config.js
module.exports = {
    entry: "./entry.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: "style!css" }
        ]
    }
};
$ node_modules/.bin/webpack

Tips

# Display progress
$ webpack --progress --colors

# Watch
$ webpack --watch
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2