Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c616ea
commit 0efe478
Showing
10 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import url from 'url'; | ||
import path from 'path'; | ||
import process from 'process'; | ||
import fs from 'fs'; | ||
|
||
// 从package.json中 | ||
// 的dependencies、devDependencies获取项目所需npm模块信息 | ||
const ROOT_PATH = process.cwd(); | ||
const PKG_JSON_PATH = path.join( ROOT_PATH, 'package.json' ); | ||
const PKG_JSON_STR = fs.readFileSync(PKG_JSON_PATH, 'binary'); | ||
const PKG_JSON = JSON.parse(PKG_JSON_STR); | ||
// 项目所需npm模块信息 | ||
const allDependencies = { | ||
...PKG_JSON.dependencies || {}, | ||
...PKG_JSON.devDependencies || {} | ||
} | ||
|
||
//Node原生模信息 | ||
const builtins = new Set( | ||
Object.keys(process.binding('natives')).filter((str) => | ||
/^(?!(?:internal|node|v8)\/)/.test(str)) | ||
); | ||
|
||
// 文件引用兼容后缀名 | ||
const JS_EXTENSIONS = new Set(['.js', '.mjs']); | ||
const JSON_EXTENSIONS = new Set(['.json']); | ||
|
||
export function resolve(specifier, parentModuleURL, defaultResolve) { | ||
// 判断是否为Node原生模块 | ||
if (builtins.has(specifier)) { | ||
return { | ||
url: specifier, | ||
format: 'builtin' | ||
}; | ||
} | ||
|
||
// 判断是否为npm模块 | ||
if ( allDependencies && typeof allDependencies[specifier] === 'string' ) { | ||
return defaultResolve(specifier, parentModuleURL); | ||
} | ||
|
||
// 如果是文件引用,判断是否路径格式正确 | ||
if (/^\.{0,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) { | ||
throw new Error( | ||
`imports must begin with '/', './', or '../'; '${specifier}' does not`); | ||
} | ||
|
||
// 判断是否为*.js、*.mjs、*.json文件 | ||
const resolved = new url.URL(specifier, parentModuleURL); | ||
const ext = path.extname(resolved.pathname); | ||
if (!JS_EXTENSIONS.has(ext) && !JSON_EXTENSIONS.has(ext)) { | ||
throw new Error( | ||
`Cannot load file with non-JavaScript file extension ${ext}.`); | ||
} | ||
|
||
// 如果是*.js、*.mjs文件 | ||
if (JS_EXTENSIONS.has(ext)) { | ||
return { | ||
url: resolved.href, | ||
format: 'esm' | ||
}; | ||
} | ||
|
||
// 如果是*.json文件 | ||
if (JSON_EXTENSIONS.has(ext)) { | ||
return { | ||
url: resolved.href, | ||
format: 'json' | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Koa from 'koa'; | ||
import { render } from './lib/render.js'; | ||
import data from './lib/data.json'; | ||
|
||
let app = new Koa(); | ||
app.use((ctx, next) => { | ||
let view = ctx.url.substr(1); | ||
let content; | ||
if ( view === '' ) { | ||
content = render('index'); | ||
} else if ( view === 'data' ) { | ||
content = data; | ||
} else { | ||
content = render(view); | ||
} | ||
ctx.body = content; | ||
}) | ||
app.listen(3000, ()=>{ | ||
console.log('the modules test server is starting'); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"title": "hello ES6 Modules", | ||
"name": "ES6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import process from 'process'; | ||
|
||
export const PROJECT_PATH = process.cwd(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { PROJECT_PATH } from './path.js'; | ||
import fs from 'fs'; | ||
import path from 'path' | ||
|
||
export const render = ( view ) => { | ||
let viewPath = path.join(PROJECT_PATH, 'view',`${view}.html`); | ||
if ( fs.existsSync(viewPath) ) { | ||
return fs.readFileSync(viewPath, 'binary'); | ||
} else { | ||
return `Error: file ${view}.html is not found!`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "demo1", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node --experimental-modules --loader ./custom-loader.mjs ./index.js" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 9.0.0" | ||
}, | ||
"dependencies": { | ||
"koa": "^2.4.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.11.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>hello world</title> | ||
</head> | ||
<body> | ||
<h1>hello world page</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<title>index</title> | ||
</head> | ||
<body> | ||
<h1>hello world</h1> | ||
<ul> | ||
<li> | ||
<a href="http://127.0.0.1:3000/index">http://127.0.0.1:3000/index</a> | ||
</li> | ||
<li> | ||
<a href="http://127.0.0.1:3000/helloworld">http://127.0.0.1:3000/helloworld</a> | ||
</li> | ||
<li> | ||
<a href="http://127.0.0.1:3000/todo">http://127.0.0.1:3000/todo</a> | ||
</li> | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>todo</title> | ||
</head> | ||
<body> | ||
<h1>todo page</h1> | ||
</body> | ||
</html> |