Skip to content

Commit

Permalink
feat(typescript): warn when compilerOptions.module is not esnext (#788)
Browse files Browse the repository at this point in the history
* feat(typescript): warn when compilerOptions.module is not esnext

* fix: remove extra space

* test: update snapshots

* fix: add 2015 to list of valid ModuleKinds
  • Loading branch information
shellscape authored Feb 4, 2021
1 parent cdd90c7 commit 8b1691e
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"codecov-lite": "^1.0.3",
"del-cli": "^3.0.1",
"eslint-config-rollup": "^1.0.0",
"esm": "^3.2.25",
"execa": "^4.0.3",
"globby": "^11.0.1",
"husky": "^4.2.5",
Expand Down
1 change: 1 addition & 0 deletions packages/typescript/ava.config.js
20 changes: 1 addition & 19 deletions packages/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,5 @@
"rollup": "^2.14.0",
"typescript": "^4.1.2"
},
"types": "types/index.d.ts",
"ava": {
"babel": {
"compileEnhancements": false
},
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"files": [
"!**/fixtures/**",
"!**/output/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
}
"types": "types/index.d.ts"
}
3 changes: 3 additions & 0 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import getPluginOptions from './options/plugin';
import { emitParsedOptionsErrors, parseTypescriptConfig } from './options/tsconfig';
import { validatePaths, validateSourceMap } from './options/validate';
import findTypescriptOutput, { getEmittedFile } from './outputFile';
import { preflight } from './preflight';
import createWatchProgram, { WatchProgramHelper } from './watchProgram';
import TSCache from './tscache';

Expand Down Expand Up @@ -46,6 +47,8 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
buildStart() {
emitParsedOptionsErrors(ts, this, parsedOptions);

preflight(parsedOptions, this);

// Fixes a memory leak https:/rollup/plugins/issues/322
if (!program) {
program = createWatchProgram(ts, this, {
Expand Down
26 changes: 24 additions & 2 deletions packages/typescript/src/options/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';

import { PluginContext } from 'rollup';
import type { ExtendedConfigCacheEntry, ParsedCommandLine } from 'typescript';
import type {
Diagnostic,
ExtendedConfigCacheEntry,
MapLike,
ParsedCommandLine,
ProjectReference,
TypeAcquisition,
WatchDirectoryFlags,
WatchOptions
} from 'typescript';

import { RollupTypescriptOptions } from '../../types';
import diagnosticToWarning from '../diagnostics/toWarning';
Expand All @@ -16,6 +25,19 @@ import {
} from './interfaces';
import { normalizeCompilerOptions, makePathsAbsolute } from './normalize';

export interface TypeScriptConfig {
autoSetSourceMap: boolean;
options: CompilerOptions;
typeAcquisition?: TypeAcquisition | undefined;
fileNames: string[];
projectReferences?: readonly ProjectReference[] | undefined;
watchOptions?: WatchOptions | undefined;
raw?: any;
errors: Diagnostic[];
wildcardDirectories?: MapLike<WatchDirectoryFlags> | undefined;
compileOnSave?: boolean | undefined;
}

/**
* Finds the path to the tsconfig file relative to the current working directory.
* @param relativePath Relative tsconfig path given by the user.
Expand Down Expand Up @@ -89,7 +111,7 @@ export function parseTypescriptConfig(
ts: typeof import('typescript'),
tsconfig: RollupTypescriptOptions['tsconfig'],
compilerOptions: PartialCompilerOptions
) {
): TypeScriptConfig {
/* eslint-disable no-undefined */
const cwd = process.cwd();
makePathsAbsolute(compilerOptions, cwd);
Expand Down
19 changes: 19 additions & 0 deletions packages/typescript/src/preflight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PluginContext } from 'rollup';
import { ModuleKind } from 'typescript';

import { TypeScriptConfig } from './options/tsconfig';

const moduleError = `
Rollup requires that TypeScript produces ES Modules. Unfortunately your configuration specifies a
"module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext"
in the target tsconfig.json file or plugin options.`.replace(/\n/g, '');

let undef;
const validModules = [ModuleKind.ES2015, ModuleKind.ES2020, ModuleKind.ESNext, undef];

// eslint-disable-next-line import/prefer-default-export
export const preflight = (config: TypeScriptConfig, context: PluginContext) => {
if (!validModules.includes(config.options.module)) {
context.warn(`@rollup/plugin-typescript: ${moduleError}`);
}
};
3 changes: 3 additions & 0 deletions packages/typescript/test/fixtures/bad-module/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const answer = 42;
// eslint-disable-next-line no-console
console.log(`the answer is ${answer}`);
5 changes: 5 additions & 0 deletions packages/typescript/test/fixtures/bad-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "commonjs"
}
}
18 changes: 18 additions & 0 deletions packages/typescript/test/snapshots/warnings.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Snapshot report for `test/warnings.ts`

The actual snapshot is saved in `warnings.ts.snap`.

Generated by [AVA](https://avajs.dev).

## bad module in tsconfig

> Snapshot 1
[
{
code: 'PLUGIN_WARNING',
message: '@rollup/plugin-typescript: Rollup requires that TypeScript produces ES Modules. Unfortunately your configuration specifies a "module" other than "esnext". Unless you know what you\'re doing, please change "module" to "esnext" in the target tsconfig.json file or plugin options.',
plugin: 'typescript',
toString: Function {},
},
]
Binary file added packages/typescript/test/snapshots/warnings.ts.snap
Binary file not shown.
17 changes: 17 additions & 0 deletions packages/typescript/test/warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'ava';
import { rollup } from 'rollup';

import typescript from '..';

test.beforeEach(() => process.chdir(__dirname));

test.serial('bad module in tsconfig', async (t) => {
const warnings: any[] = [];
await rollup({
input: 'fixtures/bad-module/main.ts',
plugins: [typescript({ tsconfig: 'fixtures/bad-module/tsconfig.json' })],
onwarn: (warning) => warnings.push(warning)
});

t.snapshot(warnings);
});
12 changes: 10 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions shared/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
extensions: ['js', 'ts'],
files: ['!**/fixtures/**', '!**/output/**', '!**/helpers/**', '!**/recipes/**', '!**/types.ts'],
require: ['ts-node/register', 'esm']
};
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["es6"],
"module": "esnext",
"moduleResolution": "node",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["packages", "scripts", "util"]
"include": ["packages", "scripts", "shared", "util"]
}

0 comments on commit 8b1691e

Please sign in to comment.