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

feat(typescript): warn when compilerOptions.module is not esnext #788

Merged
merged 4 commits into from
Feb 4, 2021

Conversation

shellscape
Copy link
Collaborator

@shellscape shellscape commented Feb 2, 2021

Rollup Plugin Name: typescript

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: Fixes #519

Description

This PR adds a warning when the typescript plugin is passed configuration with "module" set to a value other than "esnext" or "es2020". Please see the snapshots for the phrasing of the warning message. This was not made an error due to the existence of edge cases for advanced compiling in which someone may actually want to output commonjs.

@shellscape
Copy link
Collaborator Author

@benmccann you may also be interested in this change

// eslint-disable-next-line import/prefer-default-export
export const preflight = (config: TypeScriptConfig, context: PluginContext) => {
let undef;
if (![ModuleKind.ESNext, ModuleKind.ES2020, undef].includes(config.options.module)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should ES2016 be included?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not in the list of supported module types. I went with the TS definition for ModuleKind

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too restrictive. Besides those, ES6 and ES2015 are also perfectly fine for use with Rollup as they also imply the use of import/export but just do not support dynamic import statements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that light, maybe the wording of the message could be changed: ...Unfortunately your configuration specifies a
"module" that does not support that. ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for reference for onlookers https://github.com/microsoft/TypeScript/blob/79c72751df2aaed2b2ba8caa4170895c343a3d5b/lib/protocol.d.ts#L2598-L2607)

as long as we're sure that ES6 and ES2015 are valid, I'm good to add them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in the TypeScript documentation, they usually write ESNext, but I think this is more like a style point.

Copy link
Member

@tjenkinson tjenkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. just a few comments

packages/typescript/src/preflight.ts Outdated Show resolved Hide resolved
// eslint-disable-next-line import/prefer-default-export
export const preflight = (config: TypeScriptConfig, context: PluginContext) => {
let undef;
if (![ModuleKind.ESNext, ModuleKind.ES2020, undef].includes(config.options.module)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit using a variable over undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elegantly gets around linter issue

packages/typescript/src/options/tsconfig.ts Show resolved Hide resolved
packages/typescript/src/options/tsconfig.ts Show resolved Hide resolved
packages/typescript/src/options/tsconfig.ts Show resolved Hide resolved
packages/typescript/src/options/tsconfig.ts Show resolved Hide resolved
packages/typescript/src/options/tsconfig.ts Show resolved Hide resolved
in the target tsconfig.json file or plugin options.`.replace(/\n/g, '');

let undef;
const validModules = [ModuleKind.ES2015, ModuleKind.ES2020, ModuleKind.ESNext, undef];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukastaegert @NotWoods Note here that ES6 isn't available in this context. There seems to be some weird fragmentation in the TypeScript source. Here's the code from typescript.js that define the available values:

    (function (ModuleKind) {
        ModuleKind[ModuleKind["None"] = 0] = "None";
        ModuleKind[ModuleKind["CommonJS"] = 1] = "CommonJS";
        ModuleKind[ModuleKind["AMD"] = 2] = "AMD";
        ModuleKind[ModuleKind["UMD"] = 3] = "UMD";
        ModuleKind[ModuleKind["System"] = 4] = "System";
        // NOTE: ES module kinds should be contiguous to more easily check whether a module kind is *any* ES module kind.
        //       Non-ES module kinds should not come between ES2015 (the earliest ES module kind) and ESNext (the last ES
        //       module kind).
        ModuleKind[ModuleKind["ES2015"] = 5] = "ES2015";
        ModuleKind[ModuleKind["ES2020"] = 6] = "ES2020";
        ModuleKind[ModuleKind["ESNext"] = 99] = "ESNext";
    })(ModuleKind = ts.ModuleKind || (ts.ModuleKind = {}));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES6 likely gets transformed into ES2015 when the strings are converted into the equivalent enum values.

@shellscape shellscape merged commit 8b1691e into master Feb 4, 2021
@shellscape shellscape deleted the feat/typescript/module-error branch February 4, 2021 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript: adding a warning when using module: commonjs
4 participants