本文主要记录了非模块化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压缩后一样正常运行

最新文章

  1. vbscript input select 添加个option根据value值到指定位置--相当于排序
  2. [LintCode] Surrounded Regions 包围区域
  3. 互联网中级Javascript面试题
  4. 关于app.config不能即时保存读取的解决方案
  5. Objective-C:Foundation框架-常用类-NSNumber
  6. windows phone 8 开发系列(二)Hello Wp8!
  7. 删除旧Ambari集群
  8. storm的设计思想
  9. NEURAL NETWORKS, PART 2: THE NEURON
  10. 四句话表明JSON格式定义
  11. windows服务器性能监控工具、方法及关键指标
  12. mac开启22port
  13. lintcode 1: Data Stream Median
  14. 代码契约CodeContract(八)
  15. 解决修改mysql的data_dir所引发的错误
  16. Gist - ES6 Iterator
  17. Linux整合Apache和SVN
  18. 【Java】MyBatis与Spring框架整合(一)
  19. 定义一个servlet用于处理所有外部接口类 架构思路
  20. 小程序的tab标签实现效果

热门文章

  1. java.util.HashSet
  2. Nginx完整配置配置样例【官方版】
  3. 【深度学习系列】一起来参加百度 PaddlePaddle AI 大赛吧!
  4. SQL基础学习_04_视图
  5. phpstorm+wamp+xdebug配置php调试环境
  6. javascript 之作用域链-10
  7. 第十一章:Python の 网络编程基础(三)
  8. Maven代理设置
  9. PE解析器的编写(三)——区块表的解析
  10. Core Animation文档翻译 (第一篇)