feat: 補 ESM export { createCjkAutospace }
下游 Vite / Rollup / TypeScript 靜態 ESM 解析需要真正之 `export`
關鍵字;原 UMD-ish(module.exports + globalThis)對 ESM 消費端
仍須走副作用導入 + globalThis 取值之 workaround。
於檔尾既有 CJS / globalThis 兩塊後再加 `export { createCjkAutospace };
順序刻意如此,讓串接端(IIFE 函式體內不能有 module-level export)
只需以 `sed '/^export {/d'` 剝最後一行即安全。
four-form export 同時支援:ESM named import / CommonJS require /
globalThis / IIFE 串接(後者需剝 export 行)。README 同步補上。
This commit is contained in:
@@ -64,12 +64,18 @@ autospace.apply(document.body);
|
||||
|
||||
## 模組形式
|
||||
|
||||
UMD-ish:
|
||||
四種消費場景並存:
|
||||
|
||||
- **ES module / TypeScript**:直接 `import { createCjkAutospace } from "./cjk-autospace.js"`(搭配 `cjk-autospace.d.ts`)
|
||||
- **CommonJS / Node**:`module.exports.createCjkAutospace`
|
||||
- **Browser global**:`globalThis.createCjkAutospace`
|
||||
- **Inline `<script>`**:函式宣告即註冊於 enclosing scope
|
||||
- **ES module / TypeScript**:`import { createCjkAutospace } from "./cjk-autospace.js"`(檔尾 `export { createCjkAutospace }`,配 `cjk-autospace.d.ts`)
|
||||
- **CommonJS / Node**:`require("./cjk-autospace.js").createCjkAutospace`(檔尾 `module.exports`)
|
||||
- **Browser global**:載入後可 `globalThis.createCjkAutospace`
|
||||
- **IIFE 串接**:把本檔內容 `cat` 進單一閉包(如 Claude 模板 `build.sh` / Nvim peek build hook)— `export {…}` 為 ESM module-level 語法,串入函式體內會語法錯誤,串接端須剝掉那行:
|
||||
|
||||
```sh
|
||||
sed '/^export[[:space:]]*{/d' cjk-autospace.js
|
||||
```
|
||||
|
||||
其餘 `module.exports` / `globalThis` 兩塊在閉包內安全(`typeof` 檢查不會 throw、globalThis 賦值無副作用)。
|
||||
|
||||
## 授權
|
||||
|
||||
|
||||
Reference in New Issue
Block a user