Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit e89f284

Browse files
authored
Merge pull request #176 from alephjs/rewrite-bundler
Rewrite bundler
2 parents 1d7baf8 + a0d9973 commit e89f284

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2812
-2103
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
2-
*.env
2+
.env
3+
.env.*
34
Thumbs.db
45
compiler/target/
56
compiler/pkg/

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"typescript.format.semicolons": "remove",
55
"typescript.preferences.quoteStyle": "single",
66
"[javascript]": {
7-
"editor.defaultFormatter": "vscode.typescript-language-features"
7+
"editor.defaultFormatter": "vscode.typescript-language-features",
8+
"editor.formatOnSave": false,
89
},
910
"[typescript]": {
1011
"editor.defaultFormatter": "vscode.typescript-language-features",
@@ -25,6 +26,6 @@
2526
"editor.formatOnSave": true,
2627
},
2728
"deno.enable": true,
28-
"deno.unstable": false,
29+
"deno.unstable": true,
2930
"deno.importMap": "./import_map.json"
3031
}

CONTRIBUTING.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ You will need [Deno](https://deno.land/) 1.7+.
2121
7. Marge to master branch by our maintainers.
2222

2323
```bash
24-
# set dev env
25-
echo 'ALEPH_DEV_PORT=2021' > .local.env
26-
2724
# ssr/development with HMR
28-
deno run -A cli.ts dev ./examples/hello-world -L debug
25+
ALEPH_DEV_PORT=2020 deno run -A --unstable --location=http://localhost dev ./examples/hello-world -L debug
2926

3027
# ssr/production
31-
deno run -A cli.ts start ./examples/hello-world -L debug
28+
ALEPH_DEV_PORT=2020 deno run -A --unstable --location=http://localhost start ./examples/hello-world -L debug
3229

3330
# ssg
34-
deno run -A cli.ts build ./examples/hello-world -L debug
31+
ALEPH_DEV_PORT=2020 deno run -A --unstable --location=http://localhost build ./examples/hello-world -L debug
3532

3633
# run all tests
37-
deno test -A --location=http://localhost
34+
deno test -A --unstable --location=https://deno.land/x/aleph
3835
```
3936

4037
## Project Structure

cli.ts

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ Options:
3333

3434
async function main() {
3535
const { _: args, ...options } = flags.parse(Deno.args)
36-
const hasCommand = args.length > 0 && args[0] in commands
37-
const command = (hasCommand ? String(args.shift()) : 'dev') as keyof typeof commands
3836

3937
// prints aleph.js version
40-
if (options.v && command != 'upgrade') {
38+
if (options.v) {
4139
console.log(`aleph.js v${VERSION}`)
4240
Deno.exit(0)
4341
}
4442

4543
// prints aleph.js and deno version
46-
if (options.version && command != 'upgrade') {
44+
if (options.version) {
4745
const { deno, v8, typescript } = Deno.version
4846
console.log([
4947
`aleph.js ${VERSION}`,
@@ -54,18 +52,43 @@ async function main() {
5452
Deno.exit(0)
5553
}
5654

57-
// prints help message
55+
// prints help message when the command not found
56+
if (!(args.length > 0 && args[0] in commands)) {
57+
console.log(helpMessage)
58+
Deno.exit(0)
59+
}
60+
61+
const command = String(args.shift()) as keyof typeof commands
62+
63+
// prints command help message
5864
if (options.h || options.help) {
59-
if (hasCommand) {
60-
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
61-
console.log(commands[command] + '\n' + helpMessage)
62-
Deno.exit(0)
63-
})
64-
return
65-
} else {
65+
import(`./cli/${command}.ts`).then(({ helpMessage }) => {
66+
console.log(commands[command])
6667
console.log(helpMessage)
6768
Deno.exit(0)
68-
}
69+
})
70+
return
71+
}
72+
73+
// import command module
74+
const { default: cmd } = await import(`./cli/${command}.ts`)
75+
76+
// execute `init` command
77+
if (command === 'init') {
78+
await cmd(args[0])
79+
return
80+
}
81+
82+
// execute `upgrade` command
83+
if (command === 'upgrade') {
84+
await cmd(options.v || options.version || args[0] || 'latest')
85+
return
86+
}
87+
88+
// proxy https://deno.land/x/aleph on localhost
89+
const v = Deno.env.get('ALEPH_DEV_PORT')
90+
if (v !== undefined && /^\d+$/.test(v)) {
91+
localProxy(parseInt(v))
6992
}
7093

7194
// sets log level
@@ -74,25 +97,15 @@ async function main() {
7497
log.setLevel(l.toLowerCase() as LevelNames)
7598
}
7699

77-
if (!hasCommand && !args[0]) {
78-
const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i], dep: 1 }
79-
const pagesDir = path.join(path.resolve('.'), 'pages')
80-
let hasIndexPage = false
81-
if (existsDirSync(pagesDir)) {
82-
for await (const { path: p } of walk(pagesDir, walkOptions)) {
83-
if (path.basename(p).split('.')[0] === 'index') {
84-
hasIndexPage = true
85-
}
86-
}
87-
}
88-
if (!hasIndexPage) {
89-
console.log(helpMessage)
90-
Deno.exit(0)
91-
}
100+
// check working Dir
101+
const workingDir = path.resolve(String(args[0] || '.'))
102+
if (!existsDirSync(workingDir)) {
103+
log.fatal('No such directory:', workingDir)
92104
}
105+
Deno.chdir(workingDir)
93106

94107
// load .env
95-
for await (const { path: p, } of walk(Deno.cwd(), { exts: ['env'], maxDepth: 1 })) {
108+
for await (const { path: p, } of walk(workingDir, { match: [/(^|\/|\\)\.env(\.|$)/i], maxDepth: 1 })) {
96109
const text = await Deno.readTextFile(p)
97110
text.split('\n').forEach(line => {
98111
let [key, value] = util.splitBy(line, '=')
@@ -104,25 +117,7 @@ async function main() {
104117
log.info('load env from', path.basename(p))
105118
}
106119

107-
// proxy https://deno.land/x/aleph on localhost
108-
localProxy()
109-
110-
const { default: cmd } = await import(`./cli/${command}.ts`)
111-
switch (command) {
112-
case 'init':
113-
await cmd(args[0])
114-
break
115-
case 'upgrade':
116-
await cmd(options.v || options.version || args[0] || 'latest')
117-
break
118-
default:
119-
const appDir = path.resolve(String(args[0] || '.'))
120-
if (!existsDirSync(appDir)) {
121-
log.fatal('No such directory:', appDir)
122-
}
123-
await cmd(appDir, options)
124-
break
125-
}
120+
await cmd(workingDir, options)
126121
}
127122

128123
if (import.meta.main) {

cli/upgrade.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { colors } from '../deps.ts'
2-
import { VERSION } from '../version.ts'
1+
import { colors, path } from '../deps.ts'
2+
import { existsFileSync } from '../shared/fs.ts'
3+
4+
const versionMetaUrl = 'https://cdn.deno.land/aleph/meta/versions.json'
35

46
export const helpMessage = `
57
Usage:
68
aleph upgrade
79
810
Options:
9-
-v, --version <version> The upgrading version
11+
--version <version> The version to upgrade to
1012
-h, --help Prints help message
1113
`
1214

13-
export default async function (version: string) {
15+
export default async function (version = 'latest') {
1416
console.log('Looking up latest version...')
15-
const metaUrl = 'https://cdn.deno.land/aleph/meta/versions.json'
16-
const { latest, versions } = await (await fetch(metaUrl)).json()
17+
const { latest, versions } = await (await fetch(versionMetaUrl)).json()
1718
if (version === 'latest') {
1819
version = latest
1920
} else if (!versions.includes(version)) {
@@ -23,17 +24,31 @@ export default async function (version: string) {
2324
Deno.exit(1)
2425
}
2526
}
26-
if (version === 'v' + VERSION) {
27-
console.log('Already up-to-date!')
28-
Deno.exit(0)
29-
}
27+
28+
const denoExecPath = Deno.execPath()
29+
const cmdExists = existsFileSync(path.join(path.dirname(denoExecPath), 'aleph'))
3030
const p = Deno.run({
31-
cmd: [Deno.execPath(), 'install', '-A', '-f', '--location', 'http://localhost', '-n', 'aleph', `https://deno.land/x/aleph@${version}/cli.ts`],
31+
cmd: [
32+
denoExecPath,
33+
'install',
34+
'-A',
35+
'-f',
36+
'--unstable',
37+
'-n', 'aleph',
38+
'--location', 'https://deno.land/x/aleph',
39+
`https://deno.land/x/aleph@${version}/cli.ts`
40+
],
3241
stdout: 'null',
33-
stderr: 'piped'
42+
stderr: 'inherit'
3443
})
35-
Deno.stderr.write(await p.stderrOutput())
36-
console.log(`Aleph.js is up to ${version}!`)
37-
p.close()
38-
Deno.exit(0)
44+
const status = await p.status()
45+
if (status.success) {
46+
if (cmdExists) {
47+
console.log(`Aleph.js is up to ${version}!`)
48+
} else {
49+
console.log('Aleph.js was installed successfully!')
50+
console.log(`Run 'aleph --help' to get started`)
51+
}
52+
}
53+
Deno.exit(status.code)
3954
}

0 commit comments

Comments
 (0)