Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolatedModules acts like moduleDetection: "force" #54094

Closed
wnayes opened this issue May 2, 2023 · 0 comments · Fixed by #54218
Closed

isolatedModules acts like moduleDetection: "force" #54094

wnayes opened this issue May 2, 2023 · 0 comments · Fixed by #54218
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@wnayes
Copy link
Contributor

wnayes commented May 2, 2023

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!");

https://www.typescriptlang.org/play?module=2&moduleDetection=2&isolatedModules=true&ssl=4&ssc=29&pln=1&pc=1#code/PTAEAEFsHsBMFcA2BTAXAQ0rAUCCMEUARZAF2QGNSBLaAOw3lOlzHGoGdpF1zYBZOEmQdUpAE7xk2CvS4oAdImgBzABQAiABLJEy0AHdo4xLACEGgJQBuIA

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!");
@andrewbranch andrewbranch added the Bug A bug in TypeScript label May 2, 2023
@andrewbranch andrewbranch self-assigned this May 2, 2023
@andrewbranch andrewbranch added this to the TypeScript 5.1.0 milestone May 2, 2023
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants