你知道那些長長的js怎麼來的嗎?
- 2019 年 10 月 5 日
- 筆記
今天不知道寫啥東西,隨便寫了點,好水啊
大家知不知道每次用js逆向時,發現那些長長的js程式碼,那可不是人寫的。那到底是怎麼來的,前端的人應該都知道用框架生成的,沒錯就是webpack
webpack
Webpack 是當下最熱門的前端資源模組化管理和打包工具,它可以將許多鬆散耦合的模組按照依賴和規則打包成符合生產環境部署的前端資源。
安裝 nvm
https://github.com/nvm-sh/nvm
通過 curl 安裝:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
通過 wget 安裝:wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
安裝 Node.js 和 NPM
·nvm install v10.15.3 ·檢查是否安裝成功:node -v, npm -v
安裝 webpack 和 webpack-cli
創建空⽬目錄和 package.json ·
- mkdir my-project
- cd my-project ·
- npm init -y
創建webpack.config.js
- webpack.config.js
// 載入path const path = require('path'); module.exports = { entry: './src/index.js', output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js' }, mode: 'production' };
- entry打包的js
- output輸出的js
創建src/index.js
// 導入helloworld.js import { helloworld } from './helloworld'; document.write(helloworld());
創建src/helloworld.js
// export 導入 export function helloworld() { return 'Hello webpack'; }
這樣通過打包index.js輸出到dist文件夾中的bundle.js,執行npm run build
將其打包
這是bundle.js就是你看不懂的js程式碼
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t),document.write("Hello webpack")}]);
這段程式碼就是列印Hello webpack一樣的意思,只不過通過js生成的

新建./dist/index.html 導入
<body> <script src="./bundle.js"></script> </body>

這該就是你zaijs慢慢扣的程式碼來源

一直原創,從未轉載