webpack打包非模塊化js


本文主要記錄了非模塊化js如何使用webpack打包

模塊化打包實現方式

webpack是模塊打包工具,通過入口文件遞歸遍歷其依賴圖譜,絕對是打包神器。

bar.js

export default function bar() {
  //
}

foo.js

import bar from './bar';

bar();

通過如下,webpack配置很快實現打包。通過插件我們還可以實現文件壓縮,開發態我們還可以配置sourceMap進行代碼調試(chrome瀏覽器支持sourcemap調試)。

module.exports = {
  entry: './foo.js',
  output: {
    filename: 'bundle.js'
  },
  devtool: "source-map", 
    plugins: [
        // compress js
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true
        })
    ]
}

非模塊化文件打包壓縮

這里我們可以使用webpack可以配置多入口文件及ExtractTextPlugin 插件將非模塊文件壓縮到一個文件中。

m1.js

functon a() {
    console.log('m1 file')
}

m2.js

functon b() {
    console.log('m2 file')
}

webpack配置文件

var webpack = require('webpack')
var path = require('path')

module.exports = {
    entry: {
        'app': [
            './src/a.js',
            './src/b.js'
        ]
    },
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "[name].js"
    }
}

打包后,發現我去不能運行??原因是webpack打包會將每個文件內容放入閉包函數中,我們去調用閉包中的函數,當然不行啦。

/******/ (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, exports, __webpack_require__) {

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


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



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

function b() {
    console.log('b file')
}

/***/ })
/******/ ]);
//# sourceMappingURL=app.js.map

怎么辦呢?我們可以對我們當前代碼進行修改,讓所有函數或屬性都能通過window對象調用即可。

(function(Demo) {
    
    Demo.module1 = {
        msg:function() {
            return 'Hello World';
        }

    }
})(window.Demo = window.Demo || {})

所以我們對於上面閉包形式且所有對象都掛在window對象這種類型代碼,不會出現函數調用不到現象。通過webpack壓縮后一樣正常運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM