docs/in-depth/non-esm-output-formats.md
Rolldown supports non-ESM output formats. Some features in ESM are not supported in non-ESM formats and Rolldown will emit messages or provide polyfills for them.
Top level await is not supported in non-ESM formats. Rolldown outputs an error if it encounters top level await when the output format is not ESM.
import.metaimport.meta is a syntax error in non-ESM formats. To avoid that from happening, Rolldown replaces import.meta with other values.
import.meta propertiesRolldown supports the following well-known import.meta properties:
import.meta.urlimport.meta.dirnameimport.meta.filenameThese properties are polyfilled when the output format is CJS. In other formats, it will be handled as same as the other properties.
:::: tip Polyfilling import.meta.url in IIFE and UMD
Rollup supports polyfilling import.meta.url in IIFE and UMD formats. However, Rolldown does not support this feature. If you need to polyfill it, you can use the following config:
::: code-group
import { defineConfig } from 'rolldown';
const importMetaUrlPolyfillVariableName = '__import_meta_url__';
export default defineConfig({
transform: {
define: {
'import.meta.url': importMetaUrlPolyfillVariableName,
},
},
output: {
format: 'iife',
intro:
"var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;" +
`var ${importMetaUrlPolyfillVariableName} = (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('main.js', document.baseURI).href)`,
},
});
import { defineConfig } from 'rolldown';
const importMetaUrlPolyfillVariableName = '__import_meta_url__';
export default defineConfig({
transform: {
define: {
'import.meta.url': importMetaUrlPolyfillVariableName,
},
},
output: {
format: 'umd',
intro:
"var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;" +
`var ${importMetaUrlPolyfillVariableName} = (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('main.js', document.baseURI).href))`,
},
});
:::
::::
import.meta object itselfOther properties and import.meta object itself are replaced with {}. Since this does not keep the original value, Rolldown emits a warning in this case.