Skip to content

Commit

Permalink
feat: add esm demo
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshenhai committed Nov 22, 2017
1 parent 2c616ea commit 0efe478
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,5 +3,6 @@ npm-debug.log
_book/
demo/*/node_modules
demo/*/npm-debug.log
demo/*/package-lock.json
demo/*/upload-files/
demo/upload-async/static/image/
72 changes: 72 additions & 0 deletions demo/esm/custom-loader.mjs
@@ -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'
};
}

}
20 changes: 20 additions & 0 deletions demo/esm/index.js
@@ -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');
})
4 changes: 4 additions & 0 deletions demo/esm/lib/data.json
@@ -0,0 +1,4 @@
{
"title": "hello ES6 Modules",
"name": "ES6"
}
3 changes: 3 additions & 0 deletions demo/esm/lib/path.js
@@ -0,0 +1,3 @@
import process from 'process';

export const PROJECT_PATH = process.cwd();
12 changes: 12 additions & 0 deletions demo/esm/lib/render.js
@@ -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!`;
}
}
20 changes: 20 additions & 0 deletions demo/esm/package.json
@@ -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"
}
}
8 changes: 8 additions & 0 deletions demo/esm/view/helloworld.html
@@ -0,0 +1,8 @@
<html>
<head>
<title>hello world</title>
</head>
<body>
<h1>hello world page</h1>
</body>
</html>
19 changes: 19 additions & 0 deletions demo/esm/view/index.html
@@ -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>
8 changes: 8 additions & 0 deletions demo/esm/view/todo.html
@@ -0,0 +1,8 @@
<html>
<head>
<title>todo</title>
</head>
<body>
<h1>todo page</h1>
</body>
</html>

0 comments on commit 0efe478

Please sign in to comment.