dayjournal memo

Total 975 articles!!

webpack #001 – インストール&ビルド

Yasunori Kirimoto's avatar


webpackは、複数のJSファイルを1つにまとめるビルドツールです。CSS等もJSに含めてビルドすることもできるようです。利用するときは、webpack単体で利用するかgulp等と組み合わせるかの判断が必要そうですね。

今回は、webpackをインストールしてビルドするところまでを試したいと思います。


まず、Node.jsでwebpackをインストールします。今回は最新版のwebpack3をインストールしてみます。


npm init -y



npm install -D webpack


インストールが完了したら、package.jsonのscriptsの項目を書き換えます。


  "scripts": {
    "build": "webpack"
  },
  


次に、ビルドをしてみたいと思います。今回は複数のJSファイルを統合します。

まず下記のような構造で実行ファイルを作成します。


sample01.js


export function sample01() {
    alert('sample01を実行!!');
}

sample02.js


export function sample02() {
    alert('sample02を実行!!');
}

main.js


import {sample01} from './js/sample01.js';
import {sample02} from './js/sample02.js';

sample01();
sample02();

webpack.config.js


module.exports = {
    entry: './_src/main.js',
    output: {
        path: __dirname + '/build',
        filename: 'app.js'
    }
};

ファイルの準備ができたらビルドします。


npm run build


ビルドが成功すると、buildディレクトリに全てのJSが統合されたapp.jsが作成されます。

app.js


/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__js_sample01_js__ = __webpack_require__(1);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__js_sample02_js__ = __webpack_require__(2);

Object(__WEBPACK_IMPORTED_MODULE_0__js_sample01_js__["a" /* sample01 */])();
Object(__WEBPACK_IMPORTED_MODULE_1__js_sample02_js__["a" /* sample02 */])();

/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = sample01;
function sample01() {
    alert('sample01を実行!!');
}

/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = sample02;
function sample02() {
    alert('sample02を実行!!');
}

/***/ })
/******/ ])

app.jsを読み込むindex.htmlを作成して実行してみます。sample01.jsとsample02.jsの内容が実行されているのを確認できます。

index.html


<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <title>webpack Sample</title>

    <script src="./app.js"></script>

</head>
<body>

    <h1>webpack !!</h1>

</body>
</html>


現在はgulpを利用しているのですが、環境によってはそろそろ(いまさら?)webpackも必要になってきているので導入していこうと思います。今回はwebpackをインストールしてビルドするところまで確認しました。



book

Q&A