mez-easyjs 0.1.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/LICENSE +21 -0
- package/README.md +40 -0
- package/bin/create-easy-app.js +60 -0
- package/bin/easy.js +7 -0
- package/compiler.d.ts +37 -0
- package/package.json +74 -0
- package/runtime.d.ts +103 -0
- package/src/commands/build.js +284 -0
- package/src/commands/create.js +476 -0
- package/src/commands/dev.js +434 -0
- package/src/commands/generate.js +104 -0
- package/src/commands/ws.js +157 -0
- package/src/compiler/codegen.js +1334 -0
- package/src/compiler/css.js +117 -0
- package/src/compiler/errors.js +65 -0
- package/src/compiler/expr.js +142 -0
- package/src/compiler/index.js +105 -0
- package/src/compiler/parse.js +115 -0
- package/src/compiler/strip-ts.js +39 -0
- package/src/compiler/template.js +419 -0
- package/src/index.js +128 -0
- package/src/runtime/app.js +115 -0
- package/src/runtime/component.js +222 -0
- package/src/runtime/css.js +35 -0
- package/src/runtime/errors.js +121 -0
- package/src/runtime/escape.js +62 -0
- package/src/runtime/events.js +151 -0
- package/src/runtime/hmr.js +157 -0
- package/src/runtime/index.js +40 -0
- package/src/runtime/overlay.js +95 -0
- package/src/runtime/reactive.js +172 -0
- package/src/runtime/router.js +423 -0
- package/src/runtime/scheduler.js +39 -0
- package/src/runtime/store.js +42 -0
- package/src/transform.js +106 -0
- package/templates/minimal/README.md +21 -0
- package/templates/minimal/easy.config.js +7 -0
- package/templates/minimal/index.html +13 -0
- package/templates/minimal/package.json +14 -0
- package/templates/minimal/src/App.easy +24 -0
- package/templates/minimal/src/main.js +4 -0
- package/templates/minimal/src/styles.css +14 -0
- package/templates/starter/README.md +18 -0
- package/templates/starter/easy.config.js +7 -0
- package/templates/starter/index.html +13 -0
- package/templates/starter/package.json +14 -0
- package/templates/starter/src/App.easy +69 -0
- package/templates/starter/src/components/Counter.easy +76 -0
- package/templates/starter/src/components/Greeting.easy +24 -0
- package/templates/starter/src/main.js +11 -0
- package/templates/starter/src/pages/About.easy +20 -0
- package/templates/starter/src/pages/Home.easy +69 -0
- package/templates/starter/src/styles.css +14 -0
- package/templates/tailwind/README.md +23 -0
- package/templates/tailwind/easy.config.js +7 -0
- package/templates/tailwind/index.html +19 -0
- package/templates/tailwind/package.json +14 -0
- package/templates/tailwind/src/App.easy +53 -0
- package/templates/tailwind/src/main.js +4 -0
- package/templates/tailwind/src/styles.css +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Easy.js contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# mez-easyjs
|
|
2
|
+
|
|
3
|
+
Ultra-lightweight frontend framework: **HTML / CSS / JS** SFCs, **no Virtual DOM**, surgical DOM updates. One package includes the **runtime**, **compiler**, and **CLI**.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i mez-easyjs
|
|
9
|
+
# or globally for the CLI
|
|
10
|
+
npm i -g mez-easyjs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx easy create my-app --template starter --yes
|
|
17
|
+
cd my-app
|
|
18
|
+
npm run dev
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { createApp, reactive, createRouter, createStore } from 'mez-easyjs';
|
|
23
|
+
import { compile } from 'mez-easyjs/compiler';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Also available: `mez-easyjs/runtime` (same as the main export).
|
|
27
|
+
|
|
28
|
+
## CLI
|
|
29
|
+
|
|
30
|
+
| Command | Description |
|
|
31
|
+
|---------|-------------|
|
|
32
|
+
| `easy create [name]` | Interactive scaffold (`minimal` · `starter` · `tailwind`) |
|
|
33
|
+
| `create-easy-app [name]` | Same wizard (npm bin alias) |
|
|
34
|
+
| `easy dev` | Dev server + HMR + error overlay |
|
|
35
|
+
| `easy build` | Production `dist/` (hashed assets, minify) |
|
|
36
|
+
| `easy generate <component\|page> <Name>` | Scaffold files |
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* npx create-easy-app [name] [--template minimal|starter|tailwind] …
|
|
4
|
+
* Thin entry that runs the same wizard as `easy create`.
|
|
5
|
+
*/
|
|
6
|
+
import { createProject } from '../src/commands/create.js';
|
|
7
|
+
|
|
8
|
+
const argv = process.argv.slice(2);
|
|
9
|
+
const flags = {};
|
|
10
|
+
const args = [];
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < argv.length; i++) {
|
|
13
|
+
const a = argv[i];
|
|
14
|
+
if (a === '--help' || a === '-h') {
|
|
15
|
+
console.log(`
|
|
16
|
+
create-easy-app — scaffold an Easy.js project
|
|
17
|
+
|
|
18
|
+
Usage:
|
|
19
|
+
create-easy-app [name] [options]
|
|
20
|
+
npx create-easy-app my-app --template starter
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--name <n> Project name
|
|
24
|
+
--template <id> minimal | starter | tailwind
|
|
25
|
+
--lang js JavaScript only (TypeScript coming soon)
|
|
26
|
+
--cwd <dir> Parent directory
|
|
27
|
+
--yes Skip confirmation prompts
|
|
28
|
+
--skip-install Skip npm install
|
|
29
|
+
--skip-git Skip git init / commit
|
|
30
|
+
`);
|
|
31
|
+
process.exit(0);
|
|
32
|
+
}
|
|
33
|
+
if (a.startsWith('--')) {
|
|
34
|
+
const key = a.slice(2);
|
|
35
|
+
const next = argv[i + 1];
|
|
36
|
+
if (next && !next.startsWith('-')) {
|
|
37
|
+
flags[key] = next;
|
|
38
|
+
i++;
|
|
39
|
+
} else {
|
|
40
|
+
flags[key] = true;
|
|
41
|
+
}
|
|
42
|
+
} else if (a.startsWith('-') && a.length === 2) {
|
|
43
|
+
const map = { y: 'yes', t: 'template', n: 'name' };
|
|
44
|
+
const key = map[a[1]] || a[1];
|
|
45
|
+
const next = argv[i + 1];
|
|
46
|
+
if (next && !next.startsWith('-') && key !== 'yes') {
|
|
47
|
+
flags[key] = next;
|
|
48
|
+
i++;
|
|
49
|
+
} else {
|
|
50
|
+
flags[key] = true;
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
args.push(a);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
createProject(args[0], flags).catch((err) => {
|
|
58
|
+
console.error(err.message || err);
|
|
59
|
+
process.exit(1);
|
|
60
|
+
});
|
package/bin/easy.js
ADDED
package/compiler.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal TypeScript stubs for the mez-easyjs compiler.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface CompileOptions {
|
|
6
|
+
filename?: string;
|
|
7
|
+
id?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface CompileResult {
|
|
11
|
+
code: string;
|
|
12
|
+
map: null;
|
|
13
|
+
scopeId: string;
|
|
14
|
+
styles: string;
|
|
15
|
+
dependencies: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function compile(source: string, options?: CompileOptions): CompileResult;
|
|
19
|
+
|
|
20
|
+
export function parseSFC(source: string, filename?: string): unknown;
|
|
21
|
+
export function parseTemplate(html: string, options?: { filename?: string }): unknown;
|
|
22
|
+
export function scopeCSS(css: string, scopeId: string): string;
|
|
23
|
+
export function compileExpression(
|
|
24
|
+
expression: string,
|
|
25
|
+
options?: { locals?: string[] }
|
|
26
|
+
): { code: string; keys: string[] };
|
|
27
|
+
export function stripTypeScript(source: string): string;
|
|
28
|
+
|
|
29
|
+
export class CompileError extends Error {
|
|
30
|
+
filename?: string;
|
|
31
|
+
line?: number;
|
|
32
|
+
column?: number;
|
|
33
|
+
hint?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function formatCompileError(err: CompileError): string;
|
|
37
|
+
export function buildFrame(source: string, line: number, column: number): string;
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mez-easyjs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Ultra-lightweight frontend framework — runtime, compiler, and CLI in one package",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"easy": "bin/easy.js",
|
|
8
|
+
"easyjs": "bin/easy.js",
|
|
9
|
+
"create-easy-app": "bin/create-easy-app.js"
|
|
10
|
+
},
|
|
11
|
+
"main": "./src/runtime/index.js",
|
|
12
|
+
"module": "./src/runtime/index.js",
|
|
13
|
+
"types": "./runtime.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./runtime.d.ts",
|
|
17
|
+
"import": "./src/runtime/index.js",
|
|
18
|
+
"default": "./src/runtime/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./runtime": {
|
|
21
|
+
"types": "./runtime.d.ts",
|
|
22
|
+
"import": "./src/runtime/index.js",
|
|
23
|
+
"default": "./src/runtime/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./compiler": {
|
|
26
|
+
"types": "./compiler.d.ts",
|
|
27
|
+
"import": "./src/compiler/index.js",
|
|
28
|
+
"default": "./src/compiler/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"files": [
|
|
34
|
+
"bin",
|
|
35
|
+
"src",
|
|
36
|
+
"templates",
|
|
37
|
+
"runtime.d.ts",
|
|
38
|
+
"compiler.d.ts",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"esbuild": "^0.25.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"easyjs",
|
|
50
|
+
"easy.js",
|
|
51
|
+
"frontend",
|
|
52
|
+
"reactivity",
|
|
53
|
+
"compiler",
|
|
54
|
+
"sfc",
|
|
55
|
+
"cli",
|
|
56
|
+
"create-easy-app",
|
|
57
|
+
"no-vdom",
|
|
58
|
+
"csp",
|
|
59
|
+
"xss"
|
|
60
|
+
],
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "git+https://github.com/easyjs/easy-js.git",
|
|
64
|
+
"directory": "packages/easyjs"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/easyjs/easy-js#readme",
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/easyjs/easy-js/issues"
|
|
69
|
+
},
|
|
70
|
+
"engines": {
|
|
71
|
+
"node": ">=18"
|
|
72
|
+
},
|
|
73
|
+
"license": "MIT"
|
|
74
|
+
}
|
package/runtime.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal TypeScript stubs for the mez-easyjs runtime public API.
|
|
3
|
+
* Enough for TS consumers to import; not a full type rewrite.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ReactiveOptions {
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function reactive<T extends object>(target: T): T;
|
|
11
|
+
export function isReactive(value: unknown): boolean;
|
|
12
|
+
export function raw<T>(value: T): T;
|
|
13
|
+
|
|
14
|
+
export interface EasyApp {
|
|
15
|
+
config: { dev: boolean };
|
|
16
|
+
use(plugin: unknown, options?: unknown): EasyApp;
|
|
17
|
+
provide(key: string | symbol, value: unknown): EasyApp;
|
|
18
|
+
mount(selectorOrEl: string | Element): unknown;
|
|
19
|
+
unmount(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createApp(rootComponent: unknown): EasyApp;
|
|
23
|
+
|
|
24
|
+
export function defineComponent(options: Record<string, unknown>): unknown;
|
|
25
|
+
export function mountComponent(
|
|
26
|
+
component: unknown,
|
|
27
|
+
host: Element,
|
|
28
|
+
props?: Record<string, unknown>,
|
|
29
|
+
appContext?: Record<string, unknown>
|
|
30
|
+
): unknown;
|
|
31
|
+
export function unmountComponent(instance: unknown): void;
|
|
32
|
+
|
|
33
|
+
export function createRouter(options: Record<string, unknown>): unknown;
|
|
34
|
+
export function useRouter(): unknown;
|
|
35
|
+
export const RouterView: unknown;
|
|
36
|
+
|
|
37
|
+
export function createStore<T extends object>(initial: T): T;
|
|
38
|
+
|
|
39
|
+
export function injectStyles(id: string, css: string): void;
|
|
40
|
+
export function removeStyles(id: string): void;
|
|
41
|
+
export function replaceStyles(id: string, css: string): void;
|
|
42
|
+
|
|
43
|
+
export function setupDelegation(
|
|
44
|
+
root: Element,
|
|
45
|
+
getContext: (node: Element) => unknown
|
|
46
|
+
): void;
|
|
47
|
+
export function createEventContext(
|
|
48
|
+
methods: Record<string, unknown>,
|
|
49
|
+
state: unknown,
|
|
50
|
+
extras?: Record<string, unknown>
|
|
51
|
+
): unknown;
|
|
52
|
+
export function on(
|
|
53
|
+
el: Element,
|
|
54
|
+
type: string,
|
|
55
|
+
fn: EventListener,
|
|
56
|
+
options?: boolean | AddEventListenerOptions
|
|
57
|
+
): () => void;
|
|
58
|
+
export function off(
|
|
59
|
+
el: Element,
|
|
60
|
+
type: string,
|
|
61
|
+
fn: EventListener,
|
|
62
|
+
options?: boolean | EventListenerOptions
|
|
63
|
+
): void;
|
|
64
|
+
|
|
65
|
+
export function nextTick(fn?: () => void): Promise<void>;
|
|
66
|
+
export function queueJob(job: () => void): void;
|
|
67
|
+
|
|
68
|
+
export function escapeHtml(value: unknown): string;
|
|
69
|
+
export function escapeAttr(value: unknown): string;
|
|
70
|
+
export function sanitizeUrl(value: unknown): string;
|
|
71
|
+
export function isEventAttr(name: string): boolean;
|
|
72
|
+
export function isUrlAttr(name: string): boolean;
|
|
73
|
+
export const URL_ATTRS: Set<string>;
|
|
74
|
+
|
|
75
|
+
export class EasyError extends Error {
|
|
76
|
+
hint?: string;
|
|
77
|
+
filename?: string;
|
|
78
|
+
component?: string;
|
|
79
|
+
cause?: unknown;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function reportError(err: unknown, meta?: Record<string, unknown>): void;
|
|
83
|
+
export function setOverlayHandler(fn: ((err: unknown) => void) | null): void;
|
|
84
|
+
export function pushBoundary(boundary: unknown): () => void;
|
|
85
|
+
export function peekBoundary(): unknown;
|
|
86
|
+
export function withErrorBoundary<T>(boundary: unknown, fn: () => T): T;
|
|
87
|
+
export function createBoundary(host: Element, options?: Record<string, unknown>): unknown;
|
|
88
|
+
|
|
89
|
+
export function installOverlay(): void;
|
|
90
|
+
export function showOverlay(err: unknown, meta?: Record<string, unknown>): void;
|
|
91
|
+
export function hideOverlay(): void;
|
|
92
|
+
|
|
93
|
+
export function enableHmr(): unknown;
|
|
94
|
+
export function isHmrEnabled(): boolean;
|
|
95
|
+
export function registerHmrInstance(
|
|
96
|
+
moduleId: string,
|
|
97
|
+
instance: unknown,
|
|
98
|
+
meta?: Record<string, unknown>
|
|
99
|
+
): () => void;
|
|
100
|
+
export function applyHmrUpdate(moduleId: string, mod: unknown): void;
|
|
101
|
+
export function replaceScopedStyles(scopeId: string, css: string): void;
|
|
102
|
+
export function snapshotState(instance: unknown): unknown;
|
|
103
|
+
export function restoreState(instance: unknown, snap: unknown): void;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import * as esbuild from 'esbuild';
|
|
6
|
+
import { compile } from '../compiler/index.js';
|
|
7
|
+
import { loadConfig } from '../transform.js';
|
|
8
|
+
|
|
9
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const RUNTIME_ENTRY = path.resolve(__dirname, '../runtime/index.js');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Production build — Vite/React-like output:
|
|
14
|
+
* dist/index.html
|
|
15
|
+
* dist/assets/index-[hash].js
|
|
16
|
+
* dist/assets/index-[hash].css
|
|
17
|
+
* dist/assets/[chunk]-[hash].js (code-split routes)
|
|
18
|
+
*/
|
|
19
|
+
export async function runBuild(flags = {}) {
|
|
20
|
+
const root = path.resolve(flags.cwd || process.cwd());
|
|
21
|
+
const config = await loadConfig(root);
|
|
22
|
+
const outDir = path.resolve(root, flags.outDir || config.outDir || 'dist');
|
|
23
|
+
const minify = flags['no-minify'] ? false : flags.minify !== false && flags['minify'] !== 'false';
|
|
24
|
+
const entryRel = config.entry || 'src/main.js';
|
|
25
|
+
const entry = path.resolve(root, entryRel);
|
|
26
|
+
|
|
27
|
+
if (!fs.existsSync(entry)) {
|
|
28
|
+
throw new Error(`Entry not found: ${entryRel}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log(`Building ${root} → ${outDir}`);
|
|
32
|
+
|
|
33
|
+
fs.rmSync(outDir, { recursive: true, force: true });
|
|
34
|
+
const assetsDir = path.join(outDir, 'assets');
|
|
35
|
+
fs.mkdirSync(assetsDir, { recursive: true });
|
|
36
|
+
|
|
37
|
+
// Copy public assets to dist root (favicon, etc.)
|
|
38
|
+
const publicDir = path.join(root, config.publicDir || 'public');
|
|
39
|
+
if (fs.existsSync(publicDir)) {
|
|
40
|
+
copyDir(publicDir, outDir);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const collectedCss = [];
|
|
44
|
+
const cssSeen = new Set();
|
|
45
|
+
|
|
46
|
+
// Global stylesheet linked from index.html
|
|
47
|
+
const indexHtmlPath = path.join(root, 'index.html');
|
|
48
|
+
let html = fs.existsSync(indexHtmlPath)
|
|
49
|
+
? fs.readFileSync(indexHtmlPath, 'utf8')
|
|
50
|
+
: defaultHtml(entryRel);
|
|
51
|
+
|
|
52
|
+
const linkCss = [...html.matchAll(/<link[^>]+href=["']([^"']+\.css)["'][^>]*>/gi)];
|
|
53
|
+
for (const m of linkCss) {
|
|
54
|
+
const href = m[1];
|
|
55
|
+
if (/^https?:/i.test(href)) continue;
|
|
56
|
+
const cssPath = path.resolve(root, href.replace(/^\//, ''));
|
|
57
|
+
if (fs.existsSync(cssPath) && !cssSeen.has(cssPath)) {
|
|
58
|
+
cssSeen.add(cssPath);
|
|
59
|
+
collectedCss.push(`/* ${path.relative(root, cssPath)} */\n` + fs.readFileSync(cssPath, 'utf8'));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const result = await esbuild.build({
|
|
64
|
+
absWorkingDir: root,
|
|
65
|
+
entryPoints: [entry],
|
|
66
|
+
bundle: true,
|
|
67
|
+
splitting: true,
|
|
68
|
+
format: 'esm',
|
|
69
|
+
platform: 'browser',
|
|
70
|
+
target: ['es2020'],
|
|
71
|
+
outdir: assetsDir,
|
|
72
|
+
entryNames: minify ? 'index-[hash]' : 'index',
|
|
73
|
+
chunkNames: minify ? '[name]-[hash]' : '[name]',
|
|
74
|
+
assetNames: minify ? 'asset-[name]-[hash]' : 'asset-[name]',
|
|
75
|
+
minify,
|
|
76
|
+
sourcemap: false,
|
|
77
|
+
metafile: true,
|
|
78
|
+
write: true,
|
|
79
|
+
logLevel: 'warning',
|
|
80
|
+
define: {
|
|
81
|
+
'process.env.NODE_ENV': '"production"',
|
|
82
|
+
__EASY_DEV__: 'false'
|
|
83
|
+
},
|
|
84
|
+
plugins: [
|
|
85
|
+
easyPlugin({
|
|
86
|
+
root,
|
|
87
|
+
collectedCss,
|
|
88
|
+
cssSeen,
|
|
89
|
+
runtimeEntry: RUNTIME_ENTRY
|
|
90
|
+
})
|
|
91
|
+
]
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Write CSS bundle
|
|
95
|
+
const cssSource = collectedCss.filter(Boolean).join('\n\n');
|
|
96
|
+
const cssHash = shortHash(cssSource || 'empty');
|
|
97
|
+
const cssFileName = minify ? `index-${cssHash}.css` : 'index.css';
|
|
98
|
+
const cssOut = path.join(assetsDir, cssFileName);
|
|
99
|
+
const minCss = minify ? minifyCss(cssSource) : cssSource;
|
|
100
|
+
fs.writeFileSync(cssOut, minCss || '/* no styles */\n');
|
|
101
|
+
|
|
102
|
+
// Find main JS entry from metafile
|
|
103
|
+
const outputs = Object.keys(result.metafile.outputs);
|
|
104
|
+
const entryOut = outputs.find((o) => {
|
|
105
|
+
const meta = result.metafile.outputs[o];
|
|
106
|
+
return meta.entryPoint && path.resolve(root, meta.entryPoint) === entry;
|
|
107
|
+
}) || outputs.find((o) => /index[^/\\]*\.js$/i.test(o) && !o.includes('chunk-'));
|
|
108
|
+
|
|
109
|
+
if (!entryOut) {
|
|
110
|
+
throw new Error('Build produced no JS entry — check entry point and .easy compilation.');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const jsFileName = path.basename(entryOut);
|
|
114
|
+
|
|
115
|
+
// Rewrite index.html → single entry + assets
|
|
116
|
+
html = html.replace(/<script[^>]*\/@easy\/hmr\.js[^>]*><\/script>\s*/gi, '');
|
|
117
|
+
html = html.replace(/<link[^>]+href=["'][^"']+\.css["'][^>]*>\s*/gi, (tag) =>
|
|
118
|
+
/https?:/i.test(tag) ? tag : ''
|
|
119
|
+
);
|
|
120
|
+
html = html.replace(/<script[^>]+src=["'][^"']+["'][^>]*><\/script>\s*/gi, (tag) =>
|
|
121
|
+
/type=["']module["']/i.test(tag) || /src=["'][^"']*main/i.test(tag) ? '' : tag
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
if (!/<link[^>]+rel=["']stylesheet["']/i.test(html)) {
|
|
125
|
+
html = html.replace(
|
|
126
|
+
/<\/head>/i,
|
|
127
|
+
` <link rel="stylesheet" href="./assets/${cssFileName}" />\n</head>`
|
|
128
|
+
);
|
|
129
|
+
} else {
|
|
130
|
+
html = html.replace(
|
|
131
|
+
/<\/head>/i,
|
|
132
|
+
` <link rel="stylesheet" href="./assets/${cssFileName}" />\n</head>`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!/<script[^>]+type=["']module["']/i.test(html)) {
|
|
137
|
+
html = html.replace(
|
|
138
|
+
/<\/body>/i,
|
|
139
|
+
` <script type="module" src="./assets/${jsFileName}"></script>\n</body>`
|
|
140
|
+
);
|
|
141
|
+
} else {
|
|
142
|
+
html = html.replace(
|
|
143
|
+
/<\/body>/i,
|
|
144
|
+
` <script type="module" src="./assets/${jsFileName}"></script>\n</body>`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Ensure we didn't leave duplicate empty gaps; collapse multiple stylesheet/script we may have doubled
|
|
149
|
+
html = dedupeAssetTags(html, cssFileName, jsFileName);
|
|
150
|
+
|
|
151
|
+
fs.writeFileSync(path.join(outDir, 'index.html'), html);
|
|
152
|
+
|
|
153
|
+
const jsFiles = fs.readdirSync(assetsDir).filter((f) => f.endsWith('.js'));
|
|
154
|
+
const cssFiles = fs.readdirSync(assetsDir).filter((f) => f.endsWith('.css'));
|
|
155
|
+
|
|
156
|
+
console.log(`✔ Build complete → ${outDir}`);
|
|
157
|
+
console.log(` index.html`);
|
|
158
|
+
for (const f of cssFiles) console.log(` assets/${f}`);
|
|
159
|
+
for (const f of jsFiles) console.log(` assets/${f}`);
|
|
160
|
+
console.log(` Serve with any static host, e.g. npx serve dist`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function easyPlugin({ root, collectedCss, cssSeen, runtimeEntry }) {
|
|
164
|
+
return {
|
|
165
|
+
name: 'easy-js',
|
|
166
|
+
setup(build) {
|
|
167
|
+
// Resolve mez-easyjs / mez-easyjs/runtime → source package
|
|
168
|
+
build.onResolve({ filter: /^mez-easyjs(\/runtime(\/.*)?)?$/ }, (args) => {
|
|
169
|
+
if (
|
|
170
|
+
args.path === 'mez-easyjs' ||
|
|
171
|
+
args.path === 'mez-easyjs/runtime' ||
|
|
172
|
+
args.path === 'mez-easyjs/runtime/'
|
|
173
|
+
) {
|
|
174
|
+
return { path: runtimeEntry };
|
|
175
|
+
}
|
|
176
|
+
const sub = args.path.replace(/^mez-easyjs\/runtime\//, '');
|
|
177
|
+
const file = path.resolve(
|
|
178
|
+
path.dirname(runtimeEntry),
|
|
179
|
+
sub.endsWith('.js') ? sub : `${sub}.js`
|
|
180
|
+
);
|
|
181
|
+
return { path: file };
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Compile .easy / .ejs SFCs
|
|
185
|
+
build.onLoad({ filter: /\.(easy|ejs)$/ }, async (args) => {
|
|
186
|
+
const source = await fs.promises.readFile(args.path, 'utf8');
|
|
187
|
+
const result = compile(source, {
|
|
188
|
+
filename: path.basename(args.path)
|
|
189
|
+
});
|
|
190
|
+
if (result.styles && result.styles.trim() && !cssSeen.has(args.path + ':styles')) {
|
|
191
|
+
cssSeen.add(args.path + ':styles');
|
|
192
|
+
collectedCss.push(
|
|
193
|
+
`/* ${path.relative(root, args.path)} */\n` + result.styles
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
// Production: styles are in the CSS asset — skip duplicate runtime injection
|
|
197
|
+
let code = result.code;
|
|
198
|
+
if (result.styles && result.styles.trim()) {
|
|
199
|
+
code = code
|
|
200
|
+
.replace(
|
|
201
|
+
/replaceStyles\(__SCOPE,\s*__STYLES\);/g,
|
|
202
|
+
'/* styles extracted to assets/*.css */'
|
|
203
|
+
)
|
|
204
|
+
.replace(
|
|
205
|
+
/injectStyles\([^)]+\);/g,
|
|
206
|
+
'/* styles extracted to assets/*.css */'
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
// Rewrite .easy imports stay as .easy — esbuild will load them via this plugin
|
|
210
|
+
return {
|
|
211
|
+
contents: code,
|
|
212
|
+
loader: 'js',
|
|
213
|
+
resolveDir: path.dirname(args.path)
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Plain CSS imported from JS
|
|
218
|
+
build.onLoad({ filter: /\.css$/ }, async (args) => {
|
|
219
|
+
const css = await fs.promises.readFile(args.path, 'utf8');
|
|
220
|
+
if (!cssSeen.has(args.path)) {
|
|
221
|
+
cssSeen.add(args.path);
|
|
222
|
+
collectedCss.push(`/* ${path.relative(root, args.path)} */\n` + css);
|
|
223
|
+
}
|
|
224
|
+
return { contents: 'export default {};', loader: 'js' };
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function dedupeAssetTags(html, cssFileName, jsFileName) {
|
|
231
|
+
// Keep a single stylesheet + single module script pointing at our hashed assets
|
|
232
|
+
let out = html;
|
|
233
|
+
const cssTag = ` <link rel="stylesheet" href="./assets/${cssFileName}" />`;
|
|
234
|
+
const jsTag = ` <script type="module" src="./assets/${jsFileName}"></script>`;
|
|
235
|
+
|
|
236
|
+
out = out.replace(/<link[^>]+href=["']\.\/assets\/[^"']+\.css["'][^>]*>\s*/gi, '');
|
|
237
|
+
out = out.replace(/<script[^>]+src=["']\.\/assets\/[^"']+\.js["'][^>]*><\/script>\s*/gi, '');
|
|
238
|
+
|
|
239
|
+
if (!/<\/head>/i.test(out)) {
|
|
240
|
+
out = `<!DOCTYPE html><html><head>${cssTag}</head><body><div id="app"></div>${jsTag}</body></html>`;
|
|
241
|
+
return out;
|
|
242
|
+
}
|
|
243
|
+
out = out.replace(/<\/head>/i, `${cssTag}\n</head>`);
|
|
244
|
+
out = out.replace(/<\/body>/i, `${jsTag}\n</body>`);
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function defaultHtml(entryRel) {
|
|
249
|
+
return `<!DOCTYPE html>
|
|
250
|
+
<html lang="en">
|
|
251
|
+
<head>
|
|
252
|
+
<meta charset="UTF-8" />
|
|
253
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
254
|
+
<title>Easy.js App</title>
|
|
255
|
+
</head>
|
|
256
|
+
<body>
|
|
257
|
+
<div id="app"></div>
|
|
258
|
+
<script type="module" src="/${entryRel.replace(/\\\\/g, '/')}"></script>
|
|
259
|
+
</body>
|
|
260
|
+
</html>
|
|
261
|
+
`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function minifyCss(css) {
|
|
265
|
+
return String(css || '')
|
|
266
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
267
|
+
.replace(/\s+/g, ' ')
|
|
268
|
+
.replace(/\s*([{}:;,])\s*/g, '$1')
|
|
269
|
+
.trim();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function shortHash(s) {
|
|
273
|
+
return createHash('sha256').update(s).digest('hex').slice(0, 8);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function copyDir(src, dest) {
|
|
277
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
278
|
+
for (const name of fs.readdirSync(src)) {
|
|
279
|
+
const from = path.join(src, name);
|
|
280
|
+
const to = path.join(dest, name);
|
|
281
|
+
if (fs.statSync(from).isDirectory()) copyDir(from, to);
|
|
282
|
+
else fs.copyFileSync(from, to);
|
|
283
|
+
}
|
|
284
|
+
}
|