lib.shims

  • Type:
type Shims = {
  cjs?: {
    'import.meta.url'?: boolean;
  };
  esm?: {
    __filename?: boolean;
    __dirname?: boolean;
    require?: boolean;
  };
};
  • Default:
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']

  • Default: true

Options:

  • true: when format is cjs, the import.meta.url in 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: the import.meta.url will be leave 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 shim the global __filename of CommonJS in ESM output.

  • Default: false

Options:

  • true: when format is esm, the __filename in source code will be replaced with the filename of the current module.

    For example, given the following source code:

    console.log(__filename);

    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 __filename will be leave as is, which will cause a runtime error when running the output.

shims.esm.__dirname

Whether to shim the global __dirname of CommonJS in ESM output.

  • Default: false

Options:

  • true: when format is esm, the __dirname in source code will be replaced with the directory name of the current module.

    For example, given the following source code:

    console.log(__dirname);

    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 __dirname will be leave as is, which will cause a runtime error when running the output.

shims.esm.require

Whether to shim the global require of CommonJS in ESM output.

  • Default: false

Options:

  • true: when format is esm, there will be a require that created by createRequire at the beginning of the output which can be accessed in source code like the global require like 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: the require will be leave as is, which will cause a runtime error when running the output.