Skip to content

Commit

Permalink
fix(tsbb): fix compilation type errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jan 23, 2021
1 parent ec848ec commit b4e9e51
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 27 deletions.
2 changes: 2 additions & 0 deletions example/basic/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="jest" />

import index from './';

describe('sum', () => {
Expand Down
13 changes: 9 additions & 4 deletions example/basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { sum } from './utils/sum';

class Test {
constructor() { }
constructor() {}
count() {
return 10
return 10;
}
}

export default () => {
const test = new Test()
async function testHandle() {
console.log('>>>');
}

export default async () => {
const test = new Test();
await testHandle();
return sum(1, test.count());
};
2 changes: 1 addition & 1 deletion example/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"target": "es2017",
"target": "ES2017",
"noImplicitAny": true,
"resolveJsonModule": true,
"moduleResolution": "node",
Expand Down
2 changes: 1 addition & 1 deletion example/express/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"target": "es2017",
"target": "ES2017",
"noImplicitAny": true,
"resolveJsonModule": true,
"moduleResolution": "node",
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"private": true,
"license": "MIT",
"scripts": {
"build:basic": "lerna exec \"npm run build\" --scope @template/basic --stream",
"test:basic": "lerna exec \"npm run coverage\" --scope @template/basic --stream",
"build:express": "lerna exec \"npm run build\" --scope @template/express --stream",
"build:hapi": "lerna exec \"npm run build\" --scope @template/hapi --stream",
"build:basic": "lerna exec \"npm run build\" --scope @template/basic",
"test:basic": "lerna exec \"npm run coverage\" --scope @template/basic",
"build:express": "lerna exec \"npm run build\" --scope @template/express",
"build:hapi": "lerna exec \"npm run build\" --scope @template/hapi",
"build:react-component": "lerna exec \"npm run build && npm run doc\" --scope @template/react-component --stream",
"build:react-component-tsx": "lerna exec \"npm run build\" --scope @template/react-component-tsx",
"watch": "lerna exec \"tsc -p ./ --types --outDir lib --watch\" --scope create-tsbb --scope tsbb --stream",
"watch": "lerna exec \"tsc -p ./ --types --outDir lib --watch\" --scope tsbb",
"build": "lerna exec \"tsc -p ./ --types --outDir lib\" --scope create-tsbb --scope tsbb --stream",
"type-check": "lerna exec \"tsc --noEmit\" --scope create-tsbb --scope tsbb --stream",
"hoist": "lerna bootstrap --hoist",
Expand Down
27 changes: 15 additions & 12 deletions packages/tsbb/src/babel/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { transformFile, BabelFileResult, TransformOptions, loadOptions } from '@babel/core';
import { IOptions } from '@tsbb/babel-preset-tsbb'
import { IOptions } from '@tsbb/babel-preset-tsbb';

export interface ITransformResult extends BabelFileResult {
options: TransformOptions;
Expand All @@ -20,38 +20,41 @@ export default (filePath: string, options: ITransformOptions, targets: ITargets)
if (targets === 'react') {
let presetOptions: IOptions = {
targets: { browsers: ['last 2 versions'] },
}
};
if (options.envName === 'cjs') {
presetOptions.modules = 'cjs';
presetOptions.transformRuntime = {
// https://github.com/babel/babel/issues/10261#issuecomment-549940457
version: require('@babel/helpers/package.json').version,
} as any
} as any;
}
if (options.envName === 'esm') {
presetOptions.modules = false;
presetOptions.transformRuntime = {
useESModules: true,
version: require('@babel/helpers/package.json').version,
} as any
} as any;
}
babelOptions = {
presets: [
[ require.resolve('@tsbb/babel-preset-tsbb'), { ...presetOptions } ],
[require.resolve('@tsbb/babel-preset-tsbb'), { ...presetOptions }],
require.resolve('@babel/preset-react'),
],
}
};
} else if (targets === 'node') {
babelOptions = {
presets: [
[require.resolve('@babel/preset-env'), {
loose: false,
targets: { node: '8' },
}],
[
require.resolve('@babel/preset-env'),
{
loose: false,
targets: { node: true },
},
],
require.resolve('@babel/preset-typescript'),
],
plugins: []
}
plugins: [],
};
}
return new Promise<ITransformResult>((resolve: (value?: ITransformResult) => ITransformResult | any, reject) => {
if (!/^(cjs|esm)$/.test(options.envName) && targets !== 'node') {
Expand Down
14 changes: 10 additions & 4 deletions packages/tsbb/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ITypesArgs extends IMyYargsArgs {
project?: string;
'out-dir'?: string;
outDir?: string;
emitDeclarationOnly?: boolean;
target?: string;
tsconf?: string;
}
Expand All @@ -32,6 +33,11 @@ export function builder(yarg: Argv) {
type: 'string',
default: 'lib',
},
emitDeclarationOnly: {
describe: 'Only emit .d.ts declaration files.',
type: 'boolean',
default: true,
},
target: {
describe: 'Specify ECMAScript target version.',
type: 'string',
Expand Down Expand Up @@ -65,10 +71,9 @@ export async function handler(args: ITypesArgs) {
tscArgs.push(args.target);
}

// tscArgs.push('--types');
// if (args.emitDeclarationOnly) {
// tscArgs.push('--emitDeclarationOnly');
// }
if (args.emitDeclarationOnly) {
tscArgs.push('--emitDeclarationOnly');
}

if (args.project) {
tscArgs.push('--project');
Expand All @@ -93,6 +98,7 @@ export async function handler(args: ITypesArgs) {
tscArgs = tscArgs.concat(args.tsconf.split(' '));
}
const projectPath = path.resolve(process.cwd(), args.sourceRoot || '');

try {
await executeCommand('tsc', tscArgs, projectPath);
if (!args.watch) {
Expand Down

0 comments on commit b4e9e51

Please sign in to comment.