国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

webpack打包非模塊化js的方法

2019-11-19 12:38:16
字體:
來源:轉載
供稿:網(wǎng)友

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

模塊化打包實現(xiàn)方式

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

bar.js

export default function bar() { //}

foo.js

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

通過如下,webpack配置很快實現(xiàn)打包。通過插件我們還可以實現(xiàn)文件壓縮,開發(fā)態(tài)我們還可以配置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"  }}

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

/******/ (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

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

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 池州市| 进贤县| 西乡县| 德阳市| 上栗县| 宾川县| 呼玛县| 龙胜| 杭州市| 富蕴县| 剑阁县| 宣化县| 洛浦县| 汉沽区| 邵东县| 静宁县| 安庆市| 金山区| 修文县| 克拉玛依市| 荣昌县| 阜阳市| 泾川县| 江阴市| 恭城| 工布江达县| 广西| 通榆县| 泰兴市| 常熟市| 隆化县| 乌苏市| 贡觉县| 乌拉特中旗| 德阳市| 离岛区| 齐齐哈尔市| 邯郸市| 壤塘县| 新昌县| 英吉沙县|