-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Bug Report
With TypeScript 5.0, issue #46295 was addressed by @andrewbranch and the compiler no longer considers it a build error to have a global script in a project with isolatedModules: true.
However, I was surprised that isolatedModules: true still forces all scripts to emit in module format.
This can be seen in the TypeScript Playground. The following example emits an AMD module, even though the script contents do not contain export/import and moduleDetection was set to auto.
// @module:amd
// @moduleDetection:auto
// @isolatedModules:true
console.log("Hello world!");I expected to be able to control the module emit via moduleDetection. I didn't expect isolatedModules: true to act like moduleDetection: "force".
The use case I was hoping to implement was setting isolatedModules: "true" on a project that is mostly modules, but has a "bootstrap" script that is a non-module, and use transpileModule to emit. I found that the bootstrap script emitted as a module, which failed to work at runtime.
If transpileModule can't safely emit global scripts, and there is not interest in supporting such emit, it might be more intuitive if setting isolatedModules: true also demanded that moduleDetection: "force" be explicitly set.
If transpileModule can safely emit global scripts, ideally isolatedModules: true would stop forcing module emit, leaving the decision to moduleDetection.
🕗 Version & Regression Information
This changed between versions 4.9 and 5.0. (Or rather, the new behavior in 5.0 makes this behavior apparent. Before it didn't matter that isolatedModules forced module format emit, because it also rejected non-modules outright.)
🙁 Actual behavior
From the playground link above,
define(["require", "exports"], function (require, exports) {
"use strict";
// @module:amd
// @moduleDetection:auto
// @isolatedModules:true
console.log("Hello world!");
});🙂 Expected behavior
Expected no AMD module wrapper:
"use strict";
// @module:amd
// @moduleDetection:auto
// @isolatedModules:false
console.log("Hello world!");