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

首頁 > 語言 > JavaScript > 正文

webpack打包非模塊化js的方法

2024-05-06 15:28:29
字體:
來源:轉載
供稿:網友

本文主要記錄了非模塊化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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 江北区| 连南| 旺苍县| 九寨沟县| 靖安县| 黑山县| 连南| 图木舒克市| 上饶市| 靖边县| 玉屏| 长乐市| 桐柏县| 镇平县| 新余市| 乐平市| 平原县| 射阳县| 科技| 武义县| 益阳市| 宁德市| 金阳县| 泊头市| 宝丰县| 宝鸡市| 家居| 论坛| 石景山区| 石台县| 萝北县| 新沂市| 达日县| 阿拉尔市| 庄浪县| 鄯善县| 霍邱县| 增城市| 泽州县| 新河县| 新河县|