> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt.

# lib.shims

- **类型：**

```ts
type Shims = {
  cjs?: {
    'import.meta.url'?: boolean;
    'import.meta.dirname'?: boolean;
    'import.meta.filename'?: boolean;
  };
  esm?: {
    __filename?: boolean;
    __dirname?: boolean;
    require?: boolean;
  };
};
```

- **默认值：**

```js
const defaultShims = {
  cjs: {
    'import.meta.url': true,
    'import.meta.dirname': true,
    'import.meta.filename': true,
  },
  esm: {
    __filename: false,
    __dirname: false,
    require: false,
  },
};
```

- **顶层配置：** 支持

配置 CommonJS 和 ESM 产物的 [shims（垫片）](https://developer.mozilla.org/zh-CN/docs/Glossary/Shim)。

## shims.cjs

将字段设置为 `true` 以启用对应的 CommonJS 产物 shims。

### shims.cjs\['import.meta.url']

是否在 CommonJS 产物为 `import.meta.url` 注入 shims。

- **默认值：** `true`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `cjs` 时，源代码中的 `import.meta.url` 将被替换为当前模块的 URL。

  例如，给定以下源代码：

  ```js
  import { readFileSync } from 'fs';
  const buffer = readFileSync(new URL('./data.proto', import.meta.url));
  ```

  将被转换为以下 CJS 产物代码：

  ```js
  const { readFileSync } = require('fs');
  const buffer = readFileSync(
    new URL(
      './data.proto',
      /*#__PURE__*/ (function () {
        return typeof document === 'undefined'
          ? new (module.require('url'.replace('', '')).URL)(
              'file:' + __filename,
            ).href
          : (document.currentScript && document.currentScript.src) ||
              new URL('main.js', document.baseURI).href;
      })(),
    ),
  );
  ```

- `false`：`import.meta.url` 将保持原样，这将会导致运行时错误。

### shims.cjs\['import.meta.dirname']

是否在 CommonJS 产物为 `import.meta.dirname` 注入 shims。

- **默认值：** `true`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `cjs` 时，源代码中的 `import.meta.dirname` 将被替换为 `__dirname`。

  例如，给定以下源代码：

  ```js
  console.log(import.meta.dirname);
  ```

  将被转换为以下 CJS 产物代码：

  ```js
  console.log(__dirname);
  ```

- `false`：`import.meta.dirname` 将保持原样，这将会导致运行时错误。

### shims.cjs\['import.meta.filename']

是否在 CommonJS 产物为 `import.meta.filename` 注入 shims。

- **默认值：** `true`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `cjs` 时，源代码中的 `import.meta.filename` 将被替换为 `__filename`。

  例如，给定以下源代码：

  ```js
  console.log(import.meta.filename);
  ```

  将被转换为以下 CJS 产物代码：

  ```js
  console.log(__filename);
  ```

- `false`：`import.meta.filename` 将保持原样，这将会导致运行时错误。

## shims.esm

将字段设置为 `true` 以启用对应的 ESM 产物 shims。

### shims.esm.\_\_filename

是否在 ESM 产物中为 CommonJS 的全局 `__filename` 注入 shims。

- **默认值：** `false`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `esm` 时，源代码中的 `__filename` 将被替换为当前模块的文件名。

  例如，给定以下源代码：

  ```js
  console.log(__filename);
  ```

  ESM 产物将被转换为：

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  var src_filename = __webpack_fileURLToPath__(import.meta.url);
  console.log(src_filename);
  ```

- `false`：`__filename` 将保持原样，这将会导致运行时错误。

### shims.esm.\_\_dirname

是否在 ESM 产物中为 CommonJS 的全局 `__dirname` 注入 shims。

- **默认值：** `false`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `esm` 时，源代码中的 `__dirname` 将被替换为当前模块的目录名。

  例如，给定以下源代码：

  ```js
  console.log(__dirname);
  ```

  ESM 产物将被转换为：

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  console.log(src_dirname);
  ```

- `false`：`__dirname` 将保持原样，这将会导致运行时错误。

### shims.esm.require

是否在 ESM 产物中为 CommonJS 的全局 `require` 注入 shims。

- **默认值：** `false`

选项：

- `true`：当 [format](/zh/config/lib/format.md) 为 `esm` 时，在产物的开头将会创建一个由 `createRequire` 生成的 `require`，可以在源代码中像 CommonJS 的全局 `require` 一样使用。

  例如，给定以下源代码：

  ```js
  const someModule = require('./someModule');

  // dynamic require
  const dynamicRequiredModule = require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  const someModulePath = require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

  ESM 产物将被转换为：

  ```js
  import __rslib_shim_module__ from 'node:module';
  const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(
    import.meta.url,
  );
  // dynamic require
  require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

- `false`: `require` 将保持原样，这将会导致运行时错误。
