From fd0d78739d2b5c6a96d33b092188aa8f3e45e5d2 Mon Sep 17 00:00:00 2001 From: rxliuli Date: Mon, 17 Oct 2022 20:15:16 +0800 Subject: [PATCH] docs: Instructions for adding shims configuration (#740) --- docs/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/README.md b/docs/README.md index 9f0a5e24..0ee99f4a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -488,6 +488,21 @@ For more details: tsup --help ``` +### Inject cjs and esm shims + +Enabling this option will fill in some code when building esm/cjs to make it work, such as `__dirname` which is only available in the cjs module and `import.meta.url` which is only available in the esm module + +```ts +import { defineConfig } from 'tsup' + +export default defineConfig({ + shims: true, +}) +``` + +- When building the cjs bundle, it will compile `import.meta.url` as `typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href` +- When building the esm bundle, it will compile `__dirname` as `path.dirname(fileURLToPath(import.meta.url))` + ## Troubleshooting ### error: No matching export in "xxx.ts" for import "xxx"