Skip to content

Commit

Permalink
fix(typescript): fix ESM build (#1311)
Browse files Browse the repository at this point in the history
Do not use named imports from TypeScript
  • Loading branch information
lukastaegert authored Oct 11, 2022
1 parent fa80acc commit dfd4de3
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 13 deletions.
3 changes: 1 addition & 2 deletions packages/typescript/src/customTransformers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BuilderProgram, CustomTransformers, Program, TypeChecker } from 'typescript';
import type { BuilderProgram, CustomTransformers, Program, TypeChecker } from 'typescript';

import type { CustomTransformerFactories, TransformerStage, TransformerFactory } from '../types';

/**
* Merges all received custom transformer definitions into a single CustomTransformers object
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/moduleResolution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
ModuleResolutionHost,
ResolvedModuleFull,
ResolvedProjectReference,
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/options/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as defaultTs from 'typescript';
import defaultTs from 'typescript';

import type { RollupTypescriptOptions, PartialCompilerOptions } from '../../types';
import { getTsLibPath } from '../tslib';
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript/src/options/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';

import { PluginContext } from 'rollup';
import {
import typescript from 'typescript';
import type {
Diagnostic,
ExtendedConfigCacheEntry,
MapLike,
ModuleKind,
ModuleResolutionKind,
ParsedCommandLine,
ProjectReference,
TypeAcquisition,
Expand All @@ -28,6 +27,7 @@ import {
} from './interfaces';
import { normalizeCompilerOptions, makePathsAbsolute } from './normalize';

const { ModuleKind, ModuleResolutionKind } = typescript;
export interface TypeScriptConfig {
autoSetSourceMap: boolean;
options: CompilerOptions;
Expand Down
3 changes: 2 additions & 1 deletion packages/typescript/src/preflight.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PluginContext, RollupOptions } from 'rollup';
import { ModuleKind } from 'typescript';
import typescript from 'typescript';

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

const { ModuleKind } = typescript;
interface PreflightOptions {
config: TypeScriptConfig;
context: PluginContext;
Expand Down
5 changes: 4 additions & 1 deletion packages/typescript/src/tslib.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileURLToPath } from 'url';

import resolve, { SyncOpts } from 'resolve';

// const resolveIdAsync = (file: string, opts: AsyncOpts) =>
Expand All @@ -19,7 +21,8 @@ export const getTsLibPath = () => {
try {
// eslint-disable-next-line no-underscore-dangle
return resolveId(process.env.__TSLIB_TEST_PATH__ || 'tslib/tslib.es6.js', {
basedir: __dirname
// @ts-ignore import.meta.url is allowed because the Rollup plugin injects the correct module format
basedir: fileURLToPath(new URL('.', import.meta.url))
});
} catch (_) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/src/watchProgram.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PluginContext } from 'rollup';
import { DiagnosticCategory } from 'typescript';

import typescript from 'typescript';
import type {
Diagnostic,
EmitAndSemanticDiagnosticsBuilderProgram,
Expand All @@ -17,6 +16,7 @@ import { DiagnosticsHost } from './diagnostics/host';
import { Resolver } from './moduleResolution';
import { mergeTransformers } from './customTransformers';

const { DiagnosticCategory } = typescript;
type BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram;

// @see https://github.com/microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/typescript/test/node_modules/current-package

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

23 changes: 23 additions & 0 deletions packages/typescript/test/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { fileURLToPath } from 'url';

import test from 'ava';

import { rollup } from 'rollup';

import typescript from 'current-package';

import { getCode, onwarn } from '../../../util/test.js';

test.beforeEach(() => process.chdir(fileURLToPath(new URL('.', import.meta.url))));

test.serial('works as ESM build', async (t) => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [typescript({ tsconfig: 'fixtures/basic/tsconfig.json', target: 'es5' })],
onwarn
});
const code = await getCode(bundle, { format: 'es' });

t.false(code.includes('number'), code);
t.false(code.includes('const'), code);
});
2 changes: 1 addition & 1 deletion shared/ava.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
workerThreads: false,
extensions: ['js', 'ts'],
extensions: ['js', 'mjs', 'ts'],
files: ['!**/fixtures/**', '!**/output/**', '!**/helpers/**', '!**/recipes/**', '!**/types.ts'],
require: ['ts-node/register', 'esm']
};

0 comments on commit dfd4de3

Please sign in to comment.