jdpack-hsh 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/index.js ADDED
@@ -0,0 +1,14 @@
1
+ #! /usr/bin/env node
2
+
3
+ // console.log('jdpack 打包功能')
4
+ // console.log(process.cwd()) // 打印当前命令所处的目录
5
+
6
+ const Compiler = require("../lib/Compiler")
7
+ const path = require('path')
8
+ const configPath = path.resolve(process.cwd(), 'webpack.config.js')
9
+ const configObj = require(configPath)
10
+ // console.log(configObj)
11
+
12
+ const compile = new Compiler(configObj)
13
+ compile.run()
14
+ console.log(compile.modules)
@@ -0,0 +1,125 @@
1
+ /**
2
+ * 编译代码 生成打包后的内容
3
+ * 根据配置文件 entry 入口文件
4
+ * 生成依赖对象
5
+ * 传递给 webpack 自执行函数
6
+ *
7
+ */
8
+ const fs = require('fs');
9
+ const ejs = require('ejs')
10
+ const path = require('path');
11
+
12
+ /**
13
+ * 导入ast相关模块
14
+ */
15
+ const { parse } = require('@babel/parser')
16
+ const generator = require('@babel/generator').default
17
+ const traverse = require('@babel/traverse').default
18
+ const t = require('@babel/types')
19
+
20
+
21
+ class Compiler {
22
+ /**
23
+ * 配置文件
24
+ * @param config
25
+ */
26
+ constructor(config) {
27
+ this.config = config
28
+ this.modules = {} // 模块依赖对象 保存代码里面所有的模块依赖关系 key 模块依赖路径 value 模块
29
+ }
30
+ run() {
31
+ this.buildModules(this.config.entry)
32
+
33
+ // 生成bundle.js
34
+ this.generatorBundlejs()
35
+ }
36
+
37
+ /**
38
+ *
39
+ * @param moduleId 依赖模块的路径
40
+ */
41
+ buildModules(moduleId) {
42
+ let code = this.getCode(moduleId)
43
+ let { deps, newCode } = this.parseModule(code)
44
+ this.modules[moduleId] = newCode
45
+ // console.log(deps, newCode);
46
+ // 针对 deps 里面再次做处理 引入的依赖文件里面可能还有require
47
+ deps.forEach(item => {
48
+ this.buildModules(item)
49
+ })
50
+ // 根据依赖对象打包生成代码
51
+
52
+ }
53
+ /**
54
+ * 将代码里面的依赖做替换
55
+ */
56
+ parseModule(code) {
57
+ let mainRootPath = path.dirname(this.config.entry)
58
+ let deps = [] // 存储了代码里面所有的依赖路径
59
+ const ast = parse(code)
60
+ // console.log(ast);
61
+ // 对require节点做替换 替换成 __webpack_require__
62
+ traverse(ast, {
63
+ CallExpression(NodePath) {
64
+ let node = NodePath.node
65
+ // console.log(node.callee.name)
66
+ if (node.callee.name === 'require') {
67
+ node.callee.name = '__webpack_require__'
68
+ // 对依赖路径做替换
69
+ let depPath = node.arguments[0].value
70
+ if (!path.extname(depPath)) {
71
+ depPath += '.js'
72
+ }
73
+ depPath = '.\\' + path.join(mainRootPath, depPath)
74
+ depPath = depPath.replace(/\\/g, '/')
75
+ node.arguments[0] = t.stringLiteral(depPath)
76
+ // console.log(depPath)
77
+ deps.push(depPath)
78
+ }
79
+
80
+ }
81
+ })
82
+ let newCode = generator(ast).code
83
+ // console.log(newCode)
84
+ return { deps, newCode }
85
+ }
86
+
87
+ /**
88
+ * // 入口文件里面的内容跟 如果有其他的require 进行再次生成依赖对象
89
+ // 现根据生成的入口文件的依赖对象 生成打包文件
90
+ */
91
+ generatorBundlejs() {
92
+ /**
93
+ * 使用ejs根据依赖对象 生成打包后的bundle.js文件
94
+ * 1 读取模板
95
+ * 2 使用ejs 做模板
96
+ * 3 将打包后的内容根据webpack.config.js 里面的output保存
97
+ */
98
+ let bundlePath = path.resolve(__dirname, 'bundlejsTemplate.ejs')
99
+ let bundleTemplate = fs.readFileSync(bundlePath, 'utf-8')
100
+ // console.log(bundleTemplate)
101
+ let renderCode = ejs.render(bundleTemplate, {
102
+ moduleId: this.config.entry,
103
+ modules: this.modules
104
+ })
105
+ // console.log(renderCode)
106
+ let outputPath = this.config.output.path
107
+ // 判断输出路径是否存在 否则创建
108
+ if (!fs.existsSync(outputPath)) {
109
+ fs.mkdirSync(outputPath)
110
+ }
111
+ let outputFilePath = path.resolve(outputPath, this.config.output.filename)
112
+ fs.writeFileSync(outputFilePath, renderCode)
113
+ }
114
+
115
+
116
+ /**
117
+ *
118
+ * @param modulePath 模块依赖的路径
119
+ */
120
+ getCode(modulePath) {
121
+ return fs.readFileSync(modulePath, 'utf8')
122
+ }
123
+ }
124
+
125
+ module.exports = Compiler
@@ -0,0 +1,98 @@
1
+ /******/ (function(modules) { // webpackBootstrap
2
+ /******/ // The module cache
3
+ /******/ var installedModules = {};
4
+ /******/
5
+ /******/ // The require function
6
+ /******/ function __webpack_require__(moduleId) {
7
+ /******/
8
+ /******/ // Check if module is in cache
9
+ /******/ if(installedModules[moduleId]) {
10
+ /******/ return installedModules[moduleId].exports;
11
+ /******/ }
12
+ /******/ // Create a new module (and put it into the cache)
13
+ /******/ var module = installedModules[moduleId] = {
14
+ /******/ i: moduleId,
15
+ /******/ l: false,
16
+ /******/ exports: {}
17
+ /******/ };
18
+ /******/
19
+ /******/ // Execute the module function
20
+ /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21
+ /******/
22
+ /******/ // Flag the module as loaded
23
+ /******/ module.l = true;
24
+ /******/
25
+ /******/ // Return the exports of the module
26
+ /******/ return module.exports;
27
+ /******/ }
28
+ /******/
29
+ /******/
30
+ /******/ // expose the modules object (__webpack_modules__)
31
+ /******/ __webpack_require__.m = modules;
32
+ /******/
33
+ /******/ // expose the module cache
34
+ /******/ __webpack_require__.c = installedModules;
35
+ /******/
36
+ /******/ // define getter function for harmony exports
37
+ /******/ __webpack_require__.d = function(exports, name, getter) {
38
+ /******/ if(!__webpack_require__.o(exports, name)) {
39
+ /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
40
+ /******/ }
41
+ /******/ };
42
+ /******/
43
+ /******/ // define __esModule on exports
44
+ /******/ __webpack_require__.r = function(exports) {
45
+ /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
46
+ /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
47
+ /******/ }
48
+ /******/ Object.defineProperty(exports, '__esModule', { value: true });
49
+ /******/ };
50
+ /******/
51
+ /******/ // create a fake namespace object
52
+ /******/ // mode & 1: value is a module id, require it
53
+ /******/ // mode & 2: merge all properties of value into the ns
54
+ /******/ // mode & 4: return value when already ns object
55
+ /******/ // mode & 8|1: behave like require
56
+ /******/ __webpack_require__.t = function(value, mode) {
57
+ /******/ if(mode & 1) value = __webpack_require__(value);
58
+ /******/ if(mode & 8) return value;
59
+ /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
60
+ /******/ var ns = Object.create(null);
61
+ /******/ __webpack_require__.r(ns);
62
+ /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
63
+ /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
64
+ /******/ return ns;
65
+ /******/ };
66
+ /******/
67
+ /******/ // getDefaultExport function for compatibility with non-harmony modules
68
+ /******/ __webpack_require__.n = function(module) {
69
+ /******/ var getter = module && module.__esModule ?
70
+ /******/ function getDefault() { return module['default']; } :
71
+ /******/ function getModuleExports() { return module; };
72
+ /******/ __webpack_require__.d(getter, 'a', getter);
73
+ /******/ return getter;
74
+ /******/ };
75
+ /******/
76
+ /******/ // Object.prototype.hasOwnProperty.call
77
+ /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
78
+ /******/
79
+ /******/ // __webpack_public_path__
80
+ /******/ __webpack_require__.p = "";
81
+ /******/
82
+ /******/
83
+ /******/ // Load entry module and return exports
84
+ /******/ return __webpack_require__(__webpack_require__.s = "<%-moduleId%>");
85
+ /******/ })
86
+ /************************************************************************/
87
+ /******/ ({
88
+
89
+ /***/
90
+ <% for (let key in modules) { %>
91
+ "<%-key%>": (function(module, exports, __webpack_require__) {
92
+
93
+ <%-modules[key]%>
94
+
95
+
96
+ /***/ }),
97
+ <% } %>
98
+ /******/ });
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "jdpack-hsh",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "jdpack": "./bin/index.js"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "type": "commonjs",
16
+ "dependencies": {
17
+ "@babel/generator": "^7.29.1",
18
+ "@babel/parser": "^7.29.0",
19
+ "@babel/traverse": "^7.29.0",
20
+ "@babel/types": "^7.29.0",
21
+ "ejs": "^4.0.1"
22
+ }
23
+ }