webpack打包非模塊化js的方法
本文主要記錄了非模塊化js如何使用webpack打包
模塊化打包實(shí)現(xiàn)方式
webpack是模塊打包工具,通過入口文件遞歸遍歷其依賴圖譜,絕對(duì)是打包神器。
bar.js
export default function bar() {
//
}
foo.js
import bar from './bar'; bar();
通過如下,webpack配置很快實(shí)現(xiàn)打包。通過插件我們還可以實(shí)現(xiàn)文件壓縮,開發(fā)態(tài)我們還可以配置sourceMap進(jìn)行代碼調(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 插件將非模塊文件壓縮到一個(gè)文件中。
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)我去不能運(yùn)行??原因是webpack打包會(huì)將每個(gè)文件內(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
怎么辦呢?我們可以對(duì)我們當(dāng)前代碼進(jìn)行修改,讓所有函數(shù)或?qū)傩远寄芡ㄟ^window對(duì)象調(diào)用即可。
(function(Demo) {
Demo.module1 = {
msg:function() {
return 'Hello World';
}
}
})(window.Demo = window.Demo || {})
所以我們對(duì)于上面閉包形式且所有對(duì)象都掛在window對(duì)象這種類型代碼,不會(huì)出現(xiàn)函數(shù)調(diào)用不到現(xiàn)象。通過webpack壓縮后一樣正常運(yùn)行
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js change,propertychange,input事件小議
github上關(guān)于mootools一個(gè)issue的討論很有意思,所以就想測(cè)試記錄下。感興趣的可以點(diǎn)擊原頁面看看2011-12-12
js獲取指定字符前/后的字符串簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨s獲取指定字符前/后的字符串簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)易的熱更新
熱更新是指在應(yīng)用程序運(yùn)行時(shí),對(duì)程序的部分內(nèi)容進(jìn)行更新,而無需重啟整個(gè)應(yīng)用程序,熱更新是在不停止整個(gè)應(yīng)用程序的情況下,將新的代碼、資源或配置應(yīng)用于正在運(yùn)行的應(yīng)用程序,本文講給大家介紹一下使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)易的熱更新,需要的朋友可以參考下2023-08-08
javascript實(shí)現(xiàn)3D切換焦點(diǎn)圖
一款用JavaScript模仿3D立體切換效果的js焦點(diǎn)幻燈片特效,使用方法很簡(jiǎn)單:用鼠標(biāo)拖拽圖片向左右方向就好~2015-10-10
微信小程序開發(fā)之實(shí)現(xiàn)食堂點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何通過微信小程序開發(fā)一個(gè)簡(jiǎn)單的食堂點(diǎn)餐系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以和小編一起學(xué)習(xí)一下2023-01-01

