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 賦值無副作用)。
|
||||
|
||||
## 授權
|
||||
|
||||
|
||||
+17
-1
@@ -555,7 +555,22 @@ function createCjkAutospace(options)
|
||||
return { apply: apply };
|
||||
}
|
||||
|
||||
/* ----- UMD-ish export ----- */
|
||||
/* ----- 多形 export -----
|
||||
*
|
||||
* 四種消費場景並存:
|
||||
* ESM(Vite / Rollup / TypeScript):使用 `export { createCjkAutospace }`,
|
||||
* 可 `import { createCjkAutospace } from "./cjk-autospace.js"`;
|
||||
* CommonJS(Node `require`):使用 `module.exports`;
|
||||
* Browser global / `<script>` 標籤:使用 `globalThis` 賦值;
|
||||
* IIFE 串接(build.sh / Nvim peek hook 把本檔內容 cat 進單個閉包):
|
||||
* `export` 語句屬 ESM 之 module-level 語法,串入函式體內會語法錯誤,
|
||||
* 故串接端須剝掉 `export {` 起始之那行(單行 `sed '/^export {/d'`
|
||||
* 即可)。其餘 module.exports / globalThis 兩塊在閉包內安全(typeof
|
||||
* 檢查不會 throw、globalThis 賦值無副作用)。
|
||||
*
|
||||
* 順序:先 CJS / globalThis(不影響語法解析),最後 ESM `export`,
|
||||
* 讓串接端只需剝最後一塊。
|
||||
*/
|
||||
if (typeof module !== "undefined" && module.exports)
|
||||
{
|
||||
module.exports = { createCjkAutospace: createCjkAutospace };
|
||||
@@ -564,3 +579,4 @@ if (typeof globalThis !== "undefined" && !globalThis.createCjkAutospace)
|
||||
{
|
||||
globalThis.createCjkAutospace = createCjkAutospace;
|
||||
}
|
||||
export { createCjkAutospace };
|
||||
|
||||
Reference in New Issue
Block a user