lib.shims
type Shims = {
  cjs?: {
    'import.meta.url'?: boolean;
  };
  esm?: {
    __filename?: boolean;
    __dirname?: boolean;
    require?: boolean;
  };
};
const defaultShims = {
  cjs: {
    'import.meta.url': true,
  },
  esm: {
    __filename: false,
    __dirname: false,
    require: false,
  },
};
Configure the shims for CommonJS and ESM output.
shims.cjs
Set the fields to true to enable the corresponding shims for CommonJS output.
shims.cjs['import.meta.url']
Whether to inject shims for the import.meta.url in CommonJS output.
Options:
- 
true: when format iscjs, theimport.meta.urlin source code will be replaced with the URL of the current module.
 For example, given the following source code: import { readFileSync } from 'fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
 
the CJS output will be transformed to: 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: theimport.meta.urlwill be left as is, which will cause a runtime error when running the output.
 
shims.esm
Set the fields to true to enable the corresponding shims for ESM output.
shims.esm.__filename
Whether to inject shims for the global __filename of CommonJS in ESM output.
Options:
- 
true: when format isesm, the__filenamein source code will be replaced with the filename of the current module.
 For example, given the following source code: the ESM output will be transformed to: 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: the__filenamewill be left as is, which will cause a runtime error when running the output.
 
shims.esm.__dirname
Whether to inject shims for the global __dirname of CommonJS in ESM output.
Options:
- 
true: when format isesm, the__dirnamein source code will be replaced with the directory name of the current module.
 For example, given the following source code: the ESM output will be transformed to: 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: the__dirnamewill be left as is, which will cause a runtime error when running the output.
 
shims.esm.require
Whether to inject shims for the global require of CommonJS in ESM output.
Options:
- 
true: when format isesm, there will be arequirethat is created bycreateRequireat the beginning of the output which can be accessed in source code like the globalrequirelike CommonJS.
 For example, given the following source code: 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);
 
the ESM output will be transformed to: import __rslib_shim_module__ from '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: therequirewill be left as is, which will cause a runtime error when running the output.