import { assert } from"https://deno.land/std@0.73.0/testing/asserts.ts";const [diagnostics,emitMap] =awaitDeno.compile("/foo.ts", {"/foo.ts":`import * as bar from "./bar.ts";\nconsole.log(bar);\n`,"/bar.ts":`export const bar = "bar";\n`, },{ outDir:"dist" } );assert(diagnostics ==null); // ensuring no diagnostics are returnedconsole.log(emitMap);
輸出:
{
"dist/bar.js.map": '{"version":3,"file":"bar.js","sourceRoot":"","sources":["file:///bar.ts"],"names":[],"mappings":"AAA...',
"dist/bar.js": 'export const bar = "bar";\n//# sourceMappingURL=bar.js.map',
"dist/foo.js.map": '{"version":3,"file":"foo.js","sourceRoot":"","sources":["file:///foo.ts"],"names":[],"mappings":"AAA...',
"dist/foo.js": 'import * as bar from "./bar.ts";\nconsole.log(bar);\n//# sourceMappingURL=foo.js.map'
}
注意!本章介紹的三種 API 都是不穩定的功能,所以在執行時都需要加入 --unstable 唷!
像是: deno run --unstable compile.ts
此外,關於什麼是 map ,筆者也有將相關文章放到本日的延伸閱讀唷!
Deno.bundle()
該 API 類似於 Deno.compiler() 。不同的是,它會直接回傳打包後的結果。
至於可選參數都與 Deno.compiler() 相同。
範例:
import { assert } from"https://deno.land/std@0.73.0/testing/asserts.ts";const [diagnostics,emitMap] =awaitDeno.bundle("/foo.ts", {"/foo.ts":`import * as bar from "./bar.ts";\nconsole.log(bar);\n`,"/bar.ts":`export const bar = "bar";\n`, },{ outDir:"dist" } );assert(diagnostics ==null); // ensuring no diagnostics are returnedconsole.log(emitMap);
輸出:
/ Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.// This is a specialised implementation of a System module loader."use strict";// @ts-nocheck/* eslint-disable */let System, __instantiate;(() => {constr=newMap(); System = {register(id, d, f) {r.set(id, { d, f, exp: {} }); }, };asyncfunctiondI(mid, src) {let id =mid.replace(/\.\w+$/i,"");if (id.includes("./")) {const [o,...ia] =id.split("/").reverse(), [,...sa] =src.split("/").reverse(),oa= [o];let s =0, i;while ((i =ia.shift())) {if (i ==="..") s++;elseif (i ===".") break;elseoa.push(i); }if (s <sa.length) oa.push(...sa.slice(s)); id =oa.reverse().join("/"); }returnr.has(id) ?gExpA(id) :import(mid); }functiongC(id, main) {return { id,import: (m) =>dI(m, id), meta: { url: id, main }, }; }functiongE(exp) {return (id, v) => {conste=typeof id ==="string"? { [id]: v } : id;for (const [id,value] ofObject.entries(e)) {Object.defineProperty(exp, id, { value, writable:true, enumerable:true, }); }return v; }; }functionrF(main) {for (const [id,m] ofr.entries()) {const { f,exp } = m;const { execute: e, setters: s } =f(gE(exp),gC(id, id === main));deletem.f;m.e = e;m.s = s; } }asyncfunctiongExpA(id) {if (!r.has(id)) return;constm=r.get(id);if (m.s) {const { d,e,s } = m;deletem.s;deletem.e;for (let i =0; i <s.length; i++) s[i](awaitgExpA(d[i]));constr=e();if (r) await r; }returnm.exp; }functiongExp(id) {if (!r.has(id)) return;constm=r.get(id);if (m.s) {const { d,e,s } = m;deletem.s;deletem.e;for (let i =0; i <s.length; i++) s[i](gExp(d[i]));e(); }returnm.exp; }__instantiate= (m, a) => { System = __instantiate =undefined;rF(m);return a ?gExpA(m) :gExp(m); };})();System.register("bar", [],function (exports_1, context_1) {"use strict";var bar;var __moduleName = context_1 &&context_1.id;return { setters: [],execute:function () {exports_1("bar", bar ="bar"); } };});System.register("foo", ["bar"],function (exports_2, context_2) {"use strict";var bar;var __moduleName = context_2 &&context_2.id;return { setters: [function (bar_1) { bar = bar_1; } ],execute:function () {console.log(bar); } };});__instantiate("foo",false);
Deno.transpileOnly()
該 API 是基於 TypeScript 的 transpileModule() ,該功能會將任何型別去除並輸出成 JavaScript 程式。同時,它也不具有型別檢查和依賴模組解析的功能,相較於前兩個 API ,它僅接受兩個參數: options 和 sources 。
接著,讓我們看看範例:
constresult=awaitDeno.transpileOnly({"/foo.ts":`enum Foo { Foo, Bar, Baz };\n`, });console.log(result["/foo.ts"].source);console.log(result["/foo.ts"].map);