首页 > webpack 如何按顺序合并文件

webpack 如何按顺序合并文件

var a = ['a.js','b.js','c.js'];
module.exports = {
    entry: a,
    output: {
        path: path.join(__dirname, './out'),
        filename: 'out.js'
    }
};

webpack 为什么不是按数组中文件的顺序去合并js文件的?


不明白你的意思,这个合并js应该是按照数据中的顺序开始合并的。

// a.js
export default a = () => console.log("a.js")

// b.js
export default a = () => console.log("b.js")

// c.js
export default a = () => console.log("c.js")

按照你上面的步骤,bundle.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] = {
/******/             exports: {},
/******/             id: moduleId,
/******/             loaded: false
/******/         };

/******/         // Execute the module function
/******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/         // Flag the module as loaded
/******/         module.loaded = 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;

/******/     // __webpack_public_path__
/******/     __webpack_require__.p = "";

/******/     // Load entry module and return exports
/******/     return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

    __webpack_require__(1);
    __webpack_require__(2);
    module.exports = __webpack_require__(3);


/***/ },
/* 1 */
/***/ function(module, exports) {

    "use strict";

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = a = function a() {
      return console.log("a.js");
    };

/***/ },
/* 2 */
/***/ function(module, exports) {

    "use strict";

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = a = function a() {
      return console.log("b.js");
    };

/***/ },
/* 3 */
/***/ function(module, exports) {

    "use strict";

    Object.defineProperty(exports, "__esModule", {
      value: true
    });

    exports.default = a = function a() {
      return console.log("c.js");
    };

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

入参中模块的顺序和你定义的数组的顺序是一样的。

最后导出模块的是最后一个文件的 export default, 在上面的例子中就是 c().

If you pass an array: All modules are loaded upon startup. The last one is exported.

【热门文章】
【热门文章】