esbuildはexport declareを解決してくれな
esbuildがexport declare const scrapbox: Scrapboxを認識してくれない? 対策
minifyすると、IIFEは消える
code:scrapbox-success1.ts
export const scrapbox: Scrapbox = /* @__PURE__ */ (
// deno-lint-ignore no-explicit-any
() => (globalThis as any).scrapbox
)();
code:test-success1.ts
import { scrapbox } from "./scrapbox-success1.ts";
alert(scrapbox.Project.name);
方法2: declare宣言したあと、1度別の変数に代入して、それをexportする code:scrapbox-success2.ts
declare const scrapbox: Scrapbox;
const k = scrapbox;
export { k as scrapbox };
// cannnot export directly
// export { scrapbox };
code:test-success2.ts
import { scrapbox } from "./scrapbox-success2.ts";
alert(scrapbox.Project.name);
Deno v2.0.5とesbuild v0.24.0で動くことを確認した
2021-11-15時点の対策
ファイルごとにdeclare const scrapbox: Scrapboxを定義する
これしかなさそう
10:40:24 これで解決させた
試したもの
No matching export errorが出る
型checkは通る
code:scrapbox.ts
export declare const scrapbox: Scrapbox;
code:test.ts
import { scrapbox } from "./scrapbox.ts";
alert(scrapbox.Project.name);
pass
code:scrapbox2.ts
declare const scrapbox: Scrapbox;
const k = scrapbox;
export { k as scrapbox };
// cannnot export directly
// export { scrapbox };
code:test0.ts
import { scrapbox } from "./scrapbox2.ts";
alert(scrapbox.Project.name);
pass
code:interface.ts
interface Window {
scrapbox: Scrapbox;
}
export const scrapbox = window.scrapbox;
code:test01.ts
import "./interface.ts";
alert(scrapbox.Project.name);
成功する
失敗する
code:namespace.ts
declare namespace ScrapboxUserScript {
const scrapbox: Scrapbox;
}
code:test2.ts
import "./namespace.ts";
alert(scrapbox.Project.name);
The constant "scrapbox" must be initialized
失敗する
code:namespace2.ts
export namespace ScrapboxUserScript {
const scrapbox: Scrapbox;
}
code:test3.ts
import { ScrapboxUserScript } from "./namespace2.ts";
alert(ScrapboxUserScript.scrapbox.Project.name);
The constant "scrapbox" must be initialized
失敗する
成功する
code:global.ts
declare global {
const scrapbox: Scrapbox;
}
code:test4.ts
import "./global.ts";
alert(scrapbox.Project.name);
成功する
code:global2.ts
declare global {
const scrapbox: Scrapbox;
}
const k = scrapbox;
export { k as scrapbox };
// cannnot export directly
// export { scrapbox };
code:test5.ts
import { scrapbox } from "./global2.ts";
alert(scrapbox.Project.name);