在杰诺有可能版本import语句的依赖关系,而且也没有package.json像npm。
但是如何在一个地方管理它的 URL 和版本?
我将在我的系统中声明多个文件和依赖项。
例如:
dateUtils.ts
import { parseDate } from 'https://deno.land/std@0.50.0/datetime/mod.ts';
const DEFAULT_MASK = "dd-mm-yyyy";
export function parse(date: string): Date {
return parseDate(date, DEFAULT_MASK);
};
Run Code Online (Sandbox Code Playgroud)
服务.ts
import { v4 } from "https://deno.land/std/uuid/mod.ts";
export function process(request: any) {
const { token } = request;
const isValid = v4.validate(token);
console.log(`Token validity: ${isValid}`)
};
Run Code Online (Sandbox Code Playgroud)
应用程序
import { parse } from "./dateUtil.ts";
import * as service from "./service.ts";
const date = parse("20-05-2020");
console.log(date);
const request = {
token: "408d30e5-1e90-45e3-b299-6520feee6d76"
}
service.process(request)
Run Code Online (Sandbox Code Playgroud)
其中deno有一个将进口合并到deps.ts.
export { parseDate } from 'https://deno.land/std@0.50.0/datetime/mod.ts';
export { v4 } from "https://deno.land/std/uuid/mod.ts";
Run Code Online (Sandbox Code Playgroud)
然后可以将导出导入到应用程序的其他模块/脚本中:
import { v4 } from "./deps.ts";
import { parseDate } from './deps.ts';
const myUUID = v4.generate();
parseDate("20-05-2020", "dd-mm-yyyy")
Run Code Online (Sandbox Code Playgroud)
使用这种方法时,应该考虑完整性检查和锁定文件:
// Add a new dependency to "src/deps.ts", used somewhere else.
export { xyz } from "https://unpkg.com/xyz-lib@v0.9.0/lib.ts";
Run Code Online (Sandbox Code Playgroud)
# Create/update the lock file "lock.json".
deno cache --lock=lock.json --lock-write src/deps.ts
# Include it when committing to source control.
git add -u lock.json
git commit -m "feat: Add support for xyz using xyz-lib"
git push
Run Code Online (Sandbox Code Playgroud)
为了避免https://deno.land/std/uuid/mod.ts在任何地方打字,你可以使用导入地图
// import_map.json
{
"imports": {
"http/": "https://deno.land/std/http/"
}
}
Run Code Online (Sandbox Code Playgroud)
// import_map.json
{
"imports": {
"http/": "https://deno.land/std/http/"
}
}
Run Code Online (Sandbox Code Playgroud)
deno run --importmap=import_map.json --allow-net server.ts
Run Code Online (Sandbox Code Playgroud)
导入地图目前不稳定,但您可以在--unstableflag后面使用它们。
除此之外,通常有一个deps.ts文件,您将在其中重新导出所有依赖项。
// server.ts
import { serve } from "http/server.ts";
const body = new TextEncoder().encode("Hello World\n");
for await (const req of serve(":8000")) {
req.respond({ body });
}
Run Code Online (Sandbox Code Playgroud)
deno run --importmap=import_map.json --allow-net server.ts
Run Code Online (Sandbox Code Playgroud)