Skip to content

Commit

Permalink
init: repo
Browse files Browse the repository at this point in the history
  • Loading branch information
hua1995116 committed Jun 27, 2020
0 parents commit c25fe50
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
node_modules
happy-file
docs/.vuepress/dist
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# tiny-webpack

## Install

```bash
npm install
npm install -D typescript
npm install -D ts-node
```

## Get Start

```bash
npm run build
```
48 changes: 48 additions & 0 deletions bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(function(modules) {
// webpackBootstrap
// The module cache
var installedModules = {};

// The require function
function 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, require);

// Flag the module as loaded
module.l = true;

// Return the exports of the module
return module.exports;
}
require("./example/index.js");
})({
"./example/index.js": function(module, exports, require) {
const a = require("./a.js");
const b = require("./b.js");

console.log(a());
b();
},
"./a.js": function(module, exports, require) {
module.exports = function() {
console.log("a");
return "aaaaa";
};
},
"./b.js": function(module, exports, require) {
module.exports = function() {
console.log("b");
};
},
});
85 changes: 85 additions & 0 deletions bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as fs from 'fs';
import * as path from 'path';
const REQUIRE_REG_GLOBAL = /require\(("|')(.+)("|')\)/g;
const REQUIRE_REG_SINGLE = /require\(("|')(.+)("|')\)/;

interface GraphStruct {
context: string;
moduleId: string;
}

class TinyWebpack {
private entryPath: string
private graph: GraphStruct[]
constructor(entryPath: string) {
this.entryPath = entryPath;
this.graph = [];
this.init();
}
init() {
this.createGraph(this.entryPath, this.entryPath);
fs.writeFileSync('bundle.js', this.bundle(this.graph))
}
createGraph(rootPath: string, relativePath: string) {
const context = fs.readFileSync(rootPath, 'utf-8');
const childrens = context.match(REQUIRE_REG_GLOBAL);
this.graph.push({
context,
moduleId: relativePath,
})
const dirname = path.dirname(rootPath);
if (childrens) {
childrens.forEach(child => {
const childPath = child.match(REQUIRE_REG_SINGLE)[2];
this.createGraph(path.join(dirname, childPath), childPath);
});
}
}
bundle(graph: GraphStruct[]) {
let modules = '';
graph.forEach(module => {
modules += `"${module.moduleId}":function (module, exports, require){
${module.context}
},`;
});
const bundleOutput = `
(function(modules) {
// webpackBootstrap
// The module cache
var installedModules = {};
// The require function
function 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,
require
);
// Flag the module as loaded
module.l = true;
// Return the exports of the module
return module.exports;
}
require("${graph[0].moduleId}");
})({${modules}})
`;
return bundleOutput;
}
}

new TinyWebpack('./example/index.js');
4 changes: 4 additions & 0 deletions example/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function () {
console.log('a');
return 'aaaaa';
}
3 changes: 3 additions & 0 deletions example/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
console.log('b');
}
5 changes: 5 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const a = require('./a.js');
const b = require('./b.js');

console.log(a());
b();
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>webpack</title>
</head>
<body>
<script src="./bundle.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "tiny-webpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "ts-node bundler.ts"
},
"keywords": [],
"author": "hua1995116 <[email protected]> (https://github.com/hua1995116)",
"license": "MIT",
"devDependencies": {
"@types/node": "^14.0.14"
}
}

0 comments on commit c25fe50

Please sign in to comment.