webpack打包非模塊化js的方法
本文主要記錄了非模塊化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進行代碼調(diào)試(chrome瀏覽器支持sourcemap調(diào)試)。
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打包會將每個文件內(nèi)容放入閉包函數(shù)中,我們?nèi)フ{(diào)用閉包中的函數(shù),當(dāng)然不行啦。
/******/ (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
怎么辦呢?我們可以對我們當(dāng)前代碼進行修改,讓所有函數(shù)或?qū)傩远寄芡ㄟ^window對象調(diào)用即可。
(function(Demo) {
Demo.module1 = {
msg:function() {
return 'Hello World';
}
}
})(window.Demo = window.Demo || {})
所以我們對于上面閉包形式且所有對象都掛在window對象這種類型代碼,不會出現(xiàn)函數(shù)調(diào)用不到現(xiàn)象。通過webpack壓縮后一樣正常運行
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS防抖節(jié)流函數(shù)的實現(xiàn)與使用場景
在行走江湖的過程中,會出現(xiàn)很多性能優(yōu)化的問題來讓你手足無措,那么這篇文章主要給大家介紹了關(guān)于JS防抖節(jié)流函數(shù)的實現(xiàn)與使用場景,針對這兩個問題來為你答疑解惑,需要的朋友可以參考下2021-07-07
javaScript 實現(xiàn)重復(fù)輸出給定的字符串的常用方法小結(jié)
這篇文章主要介紹了javaScript 實現(xiàn)重復(fù)輸出給定的字符串的常用方法,總結(jié)分析了JavaScript重復(fù)輸出給定字符串的4種常見操作技巧,需要的朋友可以參考下2020-02-02
TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級import或export的文件都被認(rèn)為是一個模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項的相關(guān)資料,需要的朋友可以參考下2023-03-03
boostrap模態(tài)框二次彈出清空原有內(nèi)容的方法
今天小編就為大家分享一篇boostrap模態(tài)框二次彈出清空原有內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

