@@ -18,7 +18,15 @@ import {
1818} from "./lib/compiler.ts" ;
1919import { type ShimOptions , shimOptionsToTransformShims } from "./lib/shims.ts" ;
2020import { getNpmIgnoreText } from "./lib/npm_ignore.ts" ;
21- import type { PackageJson , ScriptTarget } from "./lib/types.ts" ;
21+ import {
22+ resolvePolyfillOptions ,
23+ resolveUseImportMetaPolyfill ,
24+ } from "./lib/polyfills.ts" ;
25+ import type {
26+ PackageJson ,
27+ PolyfillOptions ,
28+ ScriptTarget ,
29+ } from "./lib/types.ts" ;
2230import { glob , runNpmCommand , standardizePath } from "./lib/utils.ts" ;
2331import {
2432 type SpecifierMappings ,
@@ -30,7 +38,11 @@ import { getPackageJson } from "./lib/package_json.ts";
3038import { getTestRunnerCode } from "./lib/test_runner/get_test_runner_code.ts" ;
3139
3240export { emptyDir } from "@std/fs/empty-dir" ;
33- export type { PackageJson } from "./lib/types.ts" ;
41+ export type {
42+ PackageJson ,
43+ PolyfillName ,
44+ PolyfillOptions ,
45+ } from "./lib/types.ts" ;
3446export type { JsxEmit , LibName , SourceMapOptions } from "./lib/compiler.ts" ;
3547export type { ShimOptions } from "./lib/shims.ts" ;
3648
@@ -88,6 +100,20 @@ export interface BuildOptions {
88100 * @default true
89101 */
90102 esModule ?: boolean ;
103+ /** Explicitly enables or disables polyfills, overriding what
104+ * `compilerOptions.target` implies.
105+ *
106+ * Provide `true` or `false` to enable or disable all polyfills, or an object
107+ * to control them individually:
108+ *
109+ * ```ts
110+ * polyfills: { importMeta: false }
111+ * ```
112+ *
113+ * @remarks The `importMeta` polyfill cannot be disabled unless `scriptModule`
114+ * is `false`, because `import.meta` is not valid CommonJS.
115+ */
116+ polyfills ?: PolyfillOptions ;
91117 /** Skip running `npm install`.
92118 * @default false
93119 */
@@ -250,6 +276,14 @@ export async function build(options: BuildOptions): Promise<void> {
250276 ( ! ! options . declaration && ! options . skipSourceOutput ) ;
251277 const packageManager = options . packageManager ?? "npm" ;
252278 const scriptTarget = options . compilerOptions ?. target ?? "ES2021" ;
279+ const polyfills = resolvePolyfillOptions ( options . polyfills ) ;
280+ // `import.meta` call sites are rewritten by the TypeScript compiler rather
281+ // than the transform, so resolve this up front and keep both sides in sync
282+ polyfills [ "importMeta" ] = resolveUseImportMetaPolyfill ( {
283+ polyfills,
284+ target : scriptTarget ,
285+ emitScriptModule : options . scriptModule !== false ,
286+ } ) ;
253287 const entryPoints : EntryPoint [ ] = options . entryPoints . map ( ( e , i ) => {
254288 if ( typeof e === "string" ) {
255289 return {
@@ -433,7 +467,9 @@ export async function build(options: BuildOptions): Promise<void> {
433467 program = project . createProgram ( ) ;
434468 emit ( {
435469 transformers : {
436- before : [ compilerTransforms . transformImportMeta ] ,
470+ before : polyfills [ "importMeta" ]
471+ ? [ compilerTransforms . transformImportMeta ]
472+ : [ ] ,
437473 } ,
438474 } ) ;
439475 writeFile (
@@ -458,7 +494,9 @@ export async function build(options: BuildOptions): Promise<void> {
458494 program = getProgramAndMaybeTypeCheck ( "script" ) ;
459495 emit ( {
460496 transformers : {
461- before : [ compilerTransforms . transformImportMeta ] ,
497+ before : polyfills [ "importMeta" ]
498+ ? [ compilerTransforms . transformImportMeta ]
499+ : [ ] ,
462500 } ,
463501 } ) ;
464502 writeFile (
@@ -634,6 +672,7 @@ export async function build(options: BuildOptions): Promise<void> {
634672 testShims,
635673 mappings : options . mappings ,
636674 target : scriptTarget ,
675+ polyfills,
637676 importMap : options . importMap ,
638677 configFile : options . configFile ,
639678 cwd : path . toFileUrl ( cwd ) . toString ( ) ,
0 commit comments