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

fix(typescript): fix ESM build #1311

Merged
merged 1 commit into from Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/typescript/src/customTransformers.ts
@@ -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
@@ -1,4 +1,4 @@
import {
import type {
ModuleResolutionHost,
ResolvedModuleFull,
ResolvedProjectReference,
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/options/plugin.ts
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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']
};