Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Steven/issue 30/generator #32

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ node_modules
!.yarn/sdks
!.yarn/versions
## for some test file start
test.*
test.*
## for build file
**/dist/*
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"private": true,
"workspaces": [
"web-infras/parser",
"web-infras/common",
"web-infras/parser",
"web-infras/generator",
"web-infras/bundler",
"web-infras/analyzer",
"website/frontend"
],
Expand Down
2 changes: 2 additions & 0 deletions web-infras/bundler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Dev dir
dev/**
6 changes: 6 additions & 0 deletions web-infras/bundler/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## tsconfig
**/tsconfig.json
## rollup config
**/rollup.config.js
## README
**/README.md
21 changes: 21 additions & 0 deletions web-infras/bundler/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"arrowParens": "always",
"bracketSameLine": false,
"bracketSpacing": true,
"semi": true,
"experimentalTernaries": false,
"singleQuote": false,
"jsxSingleQuote": false,
"quoteProps": "as-needed",
"trailingComma": "all",
"singleAttributePerLine": false,
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"proseWrap": "preserve",
"insertPragma": false,
"printWidth": 110,
"requirePragma": false,
"tabWidth": 2,
"useTabs": false,
"embeddedLanguageFormatting": "auto"
}
2 changes: 2 additions & 0 deletions web-infras/bundler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Web Infra Bundler
WIP JS bundler.
27 changes: 27 additions & 0 deletions web-infras/bundler/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @ts-check
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";

export default tseslint.config(
{
ignores: ["**/rollup.config.js", "**/dist/"],
},
eslint.configs.recommended,
...tseslint.configs.recommended,
{
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{
args: "all",
argsIgnorePattern: "^_",
caughtErrors: "all",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
varsIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
},
},
);
44 changes: 44 additions & 0 deletions web-infras/bundler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "web-infra-bundler",
"packageManager": "[email protected]",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "rollup --config --bundleConfigAsCjs rollup.config.js",
"dev": "ts-node --swc ./src/dev.ts",
"format": "yarn prettier . --check",
"format:write": "yarn prettier . --write",
"lint": "yarn eslint",
"lint:fix": "yarn eslint --fix"
},
"devDependencies": {
"@eslint/js": "^9.14.0",
"@rollup/plugin-typescript": "^11.1.5",
"@swc/core": "^1.7.10",
"@types/eslint": "^9",
"@types/eslint__js": "^8.42.3",
"chalk": "4.1.2",
"eslint": "^9.14.0",
"nodemon": "^3.0.1",
"prettier": "^3.3.2",
"rollup": "^4.1.4",
"rollup-plugin-dts": "^6.1.0",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"tslib": "^2.6.2",
"typescript": "^5.6.3",
"typescript-eslint": "^8.13.0",
"web-infra-common": "workspace:*",
"web-infra-generator": "workspace:*",
"web-infra-parser": "workspace:*"
},
"lint-staged": {
"*.{ts}": [
"yarn format:write",
"yarn lint:fix"
],
"*.md": [
"yarn format:write"
]
}
}
32 changes: 32 additions & 0 deletions web-infras/bundler/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import typescriptPlugin from "@rollup/plugin-typescript";
import { dts as typescriptTypePlugin } from "rollup-plugin-dts";
import path from "path";

/** @type {import('rollup').RollupOptions} */
export default [
{
input: 'src/index.ts',
output: {
file: './dist/index.js',
format: 'cjs'
},
plugins: [
typescriptPlugin({
tsconfig: path.join(__dirname, "tsconfig.build.json"),
}),
]
},
{
input: 'src/index.ts',
output: {
file: './dist/index.d.ts',
format: 'es'
},
plugins: [
typescriptPlugin({
tsconfig: path.join(__dirname, "tsconfig.build.json"),
}),
typescriptTypePlugin()
]
}
];
52 changes: 52 additions & 0 deletions web-infras/bundler/src/chunkGraph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { BundlerConfig } from "@/src/config";
import { DepGraph, ModuleId } from "@/src/depGraph";
import { resolvePath } from "@/src/utils/resolvePath";
import type { Chunk } from "@/src/chunkGraph/type";
import { renderRuntimeModuleTemplate, runtimeModulize } from "@/src/chunkGraph/runtimeModuleResolver";
/**
*
* @param entry
* @param graph
* @returns
*/
function getReverseDFSOrdering(entry: ModuleId, graph: DepGraph): ModuleId[] {
const orders: ModuleId[] = [];
const mark: Record<ModuleId, boolean> = {};
function dfsVisit(current: ModuleId) {
if (mark[current] === true) return;
const deps = graph.moduleMap[current].importer;
mark[current] = true;
for (const dep of deps) {
dfsVisit(dep);
}
orders.push(current);
}
dfsVisit(entry);
return orders;
}

export function buildChunks(graph: DepGraph, config: BundlerConfig): Chunk[] {
const configs = [config];
const chunks: Array<Chunk> = [];
for (const config of configs) {
const entryAbsolutPath = resolvePath(config.entry, process.cwd());
const reverseTopoOrders = getReverseDFSOrdering(entryAbsolutPath, graph);
chunks.push({
moduleIds: reverseTopoOrders,
});
}
return chunks;
}

export async function renderChunks(config: BundlerConfig, chunks: Chunk[], graph: DepGraph) {
let index = 0;
for (const chunk of chunks) {
runtimeModulize(chunk, graph);
const code = renderRuntimeModuleTemplate(config.entry, chunk, graph);
const outputAbsolutePath = path.resolve(resolvePath(config.output, process.cwd()), `output_${index}.js`);
await fs.writeFile(outputAbsolutePath, code);
index++;
}
}
19 changes: 19 additions & 0 deletions web-infras/bundler/src/chunkGraph/runtimeModuleResolver/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createSourcePosition } from "web-infra-common";

export const DUMMY_SOURCE_POSITION = createSourcePosition();
export const RUNTIME_MODULE_IMPORT_CALLEE = "_runtime_module_import";
export const RUNTIME_MODULE_EXPORT_REF = "_runtime_module_export";
export const RUNTIME_MODULE_MAP = "_runtime_module_map";
export const RUNTIME_MODULE_CACHE = "_runtime_module_cache";
export const RUNTIME_MODULE_BOOTSTRAP_TEMPLATE = `
var ${RUNTIME_MODULE_CACHE} = {};
var ${RUNTIME_MODULE_IMPORT_CALLEE} = (source) => {
const runtimeCache = ${RUNTIME_MODULE_CACHE}[source];
if(runtimeCache) {
return runtimeCache;
}
const ${RUNTIME_MODULE_EXPORT_REF} = {}
${RUNTIME_MODULE_MAP}[source](${RUNTIME_MODULE_IMPORT_CALLEE}, ${RUNTIME_MODULE_EXPORT_REF});
${RUNTIME_MODULE_CACHE}[source] = ${RUNTIME_MODULE_EXPORT_REF};
return ${RUNTIME_MODULE_EXPORT_REF};
}`;
Loading
Loading