From 695a0d7c2969e026771db3aa44790d41af99fc0a Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 4 Aug 2016 00:39:58 +0300 Subject: [PATCH 1/3] Transpile with buble --- package.json | 3 +- rollup.config.js | 6 +-- src/{fixExportClass.ts => fixExportClass.js} | 8 ++-- src/{index.ts => index.js} | 32 +++++++------- src/string.js | 3 ++ src/string.ts | 3 -- tsconfig.json | 44 -------------------- 7 files changed, 28 insertions(+), 71 deletions(-) rename src/{fixExportClass.ts => fixExportClass.js} (88%) rename src/{index.ts => index.js} (86%) create mode 100644 src/string.js delete mode 100644 src/string.ts delete mode 100644 tsconfig.json diff --git a/package.json b/package.json index 04ed076..9c0909a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.7.7", "description": "Seamless integration between Rollup and TypeScript.", "main": "dist/rollup-plugin-typescript.cjs.js", + "module": "dist/rollup-plugin-typescript.es6.js", "jsnext:main": "dist/rollup-plugin-typescript.es6.js", "files": [ "dist", @@ -37,7 +38,7 @@ "mocha": "^2.3.3", "rimraf": "^2.5.4", "rollup": "^0.25.7", - "rollup-plugin-typescript": "^0.5.0" + "rollup-plugin-buble": "^0.12.1" }, "repository": { "type": "git", diff --git a/rollup.config.js b/rollup.config.js index deb0218..1c99694 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,7 +1,7 @@ -import typescript from 'rollup-plugin-typescript'; +import buble from 'rollup-plugin-buble'; export default { - entry: 'src/index.ts', + entry: 'src/index.js', external: [ 'compare-versions', @@ -14,6 +14,6 @@ export default { ], plugins: [ - typescript() + buble() ] }; diff --git a/src/fixExportClass.ts b/src/fixExportClass.js similarity index 88% rename from src/fixExportClass.ts rename to src/fixExportClass.js index 96d5913..a45ff3d 100644 --- a/src/fixExportClass.ts +++ b/src/fixExportClass.js @@ -31,13 +31,13 @@ import * as tippex from 'tippex'; // export { A }; // // The solution is to replace the previous export syntax with the latter. -export default function fix ( code: string, id: string ): string { +export default function fix ( code, id ) { // Erase comments, strings etc. to avoid erroneous matches for the Regex. const cleanCode = getErasedCode( code, id ); const re = /export\s+(default\s+)?((?:abstract\s+)?class)(?:\s+(\w+))?/g; - let match: RegExpExecArray; + let match; while ( match = re.exec( cleanCode ) ) { // To keep source maps intact, replace non-whitespace characters with spaces. @@ -61,7 +61,7 @@ export default function fix ( code: string, id: string ): string { return code; } -function getErasedCode ( code: string, id: string ): string { +function getErasedCode ( code, id ) { try { return tippex.erase( code ); } catch (e) { @@ -69,7 +69,7 @@ function getErasedCode ( code: string, id: string ): string { } } -function erase ( code: string, start: number, length: number ): string { +function erase ( code, start, length ) { const end = start + length; return code.slice( 0, start ) + diff --git a/src/index.ts b/src/index.js similarity index 86% rename from src/index.ts rename to src/index.js index a516022..1ed0799 100644 --- a/src/index.ts +++ b/src/index.js @@ -11,7 +11,7 @@ import compareVersions from 'compare-versions'; import { endsWith } from './string'; import fixExportClass from './fixExportClass'; - +/* interface Options { tsconfig?: boolean; include?: string | string[]; @@ -19,16 +19,16 @@ interface Options { typescript?: typeof ts; module?: string; } - +*/ const resolveHost = { - directoryExists ( dirPath: string ): boolean { + directoryExists ( dirPath ) { try { return statSync( dirPath ).isDirectory(); } catch ( err ) { return false; } }, - fileExists ( filePath: string ): boolean { + fileExists ( filePath ) { try { return statSync( filePath ).isFile(); } catch ( err ) { @@ -37,12 +37,12 @@ const resolveHost = { } }; -function goodErrors ( diagnostic: ts.Diagnostic ): boolean { +function goodErrors ( diagnostic ) { // All errors except `Cannot compile modules into 'es6' when targeting 'ES5' or lower.` return diagnostic.code !== 1204; } -function getDefaultOptions(): any { +function getDefaultOptions() { return { noEmitHelpers: true, module: 'es2015', @@ -53,7 +53,7 @@ function getDefaultOptions(): any { // Gratefully lifted from 'look-up', due to problems using it directly: // https://github.com/jonschlinkert/look-up/blob/master/index.js // MIT Licenced -function findFile( cwd: string, filename: string ): string { +function findFile( cwd, filename ) { let fp = cwd ? ( cwd + '/' + filename ) : filename; if ( existsSync( fp ) ) { @@ -74,7 +74,7 @@ function findFile( cwd: string, filename: string ): string { return null; } -function compilerOptionsFromTsConfig( typescript: typeof ts ): ts.CompilerOptions { +function compilerOptionsFromTsConfig( typescript ) { const cwd = process.cwd(); const tsconfig = typescript.readConfigFile( findFile( cwd, 'tsconfig.json' ), path => readFileSync( path, 'utf8' ) ); @@ -84,7 +84,7 @@ function compilerOptionsFromTsConfig( typescript: typeof ts ): ts.CompilerOption return tsconfig.config.compilerOptions; } -function adjustCompilerOptions( typescript: typeof ts, options: any ) { +function adjustCompilerOptions( typescript, options ) { // Set `sourceMap` to `inlineSourceMap` if it's a boolean // under the assumption that both are never specified simultaneously. if ( typeof options.inlineSourceMap === 'boolean' ) { @@ -104,7 +104,7 @@ function adjustCompilerOptions( typescript: typeof ts, options: any ) { } } -export default function typescript ( options: Options ) { +export default function typescript ( options ) { options = assign( {}, options || {} ); const filter = createFilter( @@ -115,7 +115,7 @@ export default function typescript ( options: Options ) { delete options.exclude; // Allow users to override the TypeScript version used for transpilation. - const typescript: typeof ts = options.typescript || ts; + const typescript = options.typescript || ts; delete options.typescript; @@ -149,7 +149,7 @@ export default function typescript ( options: Options ) { const compilerOptions = parsed.options; return { - resolveId ( importee: string, importer: string ): string { + resolveId ( importee, importer ) { // Handle the special `typescript-helpers` import itself. if ( importee === 'typescript-helpers' ) { return path.resolve( __dirname, '../src/typescript-helpers.js' ); @@ -157,11 +157,11 @@ export default function typescript ( options: Options ) { if ( !importer ) return null; - var result: ts.ResolvedModuleWithFailedLookupLocations; + var result; if ( compareVersions( typescript.version, '1.8.0' ) < 0 ) { // Suppress TypeScript warnings for function call. - result = (typescript as any).nodeModuleNameResolver( importee, importer, resolveHost ); + result = typescript.nodeModuleNameResolver( importee, importer, resolveHost ); } else { result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost ); } @@ -177,7 +177,7 @@ export default function typescript ( options: Options ) { return null; }, - transform ( code: string, id: string ): { code: string, map: any } { + transform ( code, id ) { if ( !filter( id ) ) return null; const transformed = typescript.transpileModule( fixExportClass( code, id ), { @@ -217,7 +217,7 @@ export default function typescript ( options: Options ) { `\nimport { __assign, __awaiter, __extends, __decorate, __metadata, __param } from 'typescript-helpers';`, // Rollup expects `map` to be an object so we must parse the string - map: JSON.parse(transformed.sourceMapText as string) + map: JSON.parse(transformed.sourceMapText) }; } }; diff --git a/src/string.js b/src/string.js new file mode 100644 index 0000000..4e38cf1 --- /dev/null +++ b/src/string.js @@ -0,0 +1,3 @@ +export function endsWith ( str, tail ) { + return !tail.length || str.slice( -tail.length ) === tail; +} diff --git a/src/string.ts b/src/string.ts deleted file mode 100644 index d863554..0000000 --- a/src/string.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function endsWith ( str: string, tail: string ): boolean { - return !tail.length || str.slice( -tail.length ) === tail; -} diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 17b76d8..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "compileOnSave": false, - "compilerOptions": { - "noImplicitAny": true - }, - "filesGlob": [ - "!node_modules", - "**/*.ts", - "**/*.tsx" - ], - "exclude": [ - "node_modules" - ], - "files": [ - "src/fixExportClass.ts", - "src/index.ts", - "src/string.ts", - "test.ts", - "test/sample/async/main.ts", - "test/sample/basic/main.ts", - "test/sample/const-enums/enums.ts", - "test/sample/const-enums/main.ts", - "test/sample/dedup-helpers/A.ts", - "test/sample/dedup-helpers/B.ts", - "test/sample/dedup-helpers/Base.ts", - "test/sample/dedup-helpers/main.ts", - "test/sample/dts/main.ts", - "test/sample/dts/node_modules/an-import.d.ts", - "test/sample/export-abstract-class/main.ts", - "test/sample/export-class-fix/default.ts", - "test/sample/export-class-fix/main.ts", - "test/sample/export-class-fix/named.ts", - "test/sample/export-class/Foo.ts", - "test/sample/export-class/main.ts", - "test/sample/import-class/A.ts", - "test/sample/import-class/main.ts", - "test/sample/overriding-typescript/main.ts", - "test/sample/syntax-error/missing-type.ts", - "test/sample/jsx/main.tsx" - ], - "atom": { - "rewriteTsconfig": true - } -} From 78cfbfeb84bf8d4ac33596186f45abc0bddee065 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 4 Aug 2016 00:53:02 +0300 Subject: [PATCH 2/3] Upgrade rollup and fix windows paths issue --- package.json | 4 ++-- src/index.js | 4 +++- test/test.js | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9c0909a..242f44d 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,9 @@ "typescript": "^1.8.9" }, "devDependencies": { - "mocha": "^2.3.3", + "mocha": "^3.0.0", "rimraf": "^2.5.4", - "rollup": "^0.25.7", + "rollup": "^0.34.3", "rollup-plugin-buble": "^0.12.1" }, "repository": { diff --git a/src/index.js b/src/index.js index 1ed0799..2fe930c 100644 --- a/src/index.js +++ b/src/index.js @@ -159,6 +159,8 @@ export default function typescript ( options ) { var result; + importer = importer.split('\\').join('/'); + if ( compareVersions( typescript.version, '1.8.0' ) < 0 ) { // Suppress TypeScript warnings for function call. result = typescript.nodeModuleNameResolver( importee, importer, resolveHost ); @@ -208,7 +210,7 @@ export default function typescript ( options ) { }); if ( fatalError ) { - throw new Error( `There were TypeScript errors transpiling "${id}"` ); + throw new Error( `There were TypeScript errors transpiling` ); } return { diff --git a/test/test.js b/test/test.js index aff2c87..585d10d 100644 --- a/test/test.js +++ b/test/test.js @@ -82,7 +82,7 @@ describe( 'rollup-plugin-typescript', function () { it( 'reports diagnostics and throws if errors occur during transpilation', function () { return bundle( 'sample/syntax-error/missing-type.ts' ).catch( function ( error ) { - assert.ok( error.message.indexOf( 'There were TypeScript errors' ) === 0, 'Should reject erroneous code.' ); + assert.ok( error.message.indexOf( 'There were TypeScript errors transpiling' ) !== -1, 'Should reject erroneous code.' ); }); }); From 224dd5d38fc4431bf00b78a1d75eed50b63bb02b Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Thu, 4 Aug 2016 00:58:19 +0300 Subject: [PATCH 3/3] Rename es6 to es --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 242f44d..c5fb309 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "version": "0.7.7", "description": "Seamless integration between Rollup and TypeScript.", "main": "dist/rollup-plugin-typescript.cjs.js", - "module": "dist/rollup-plugin-typescript.es6.js", - "jsnext:main": "dist/rollup-plugin-typescript.es6.js", + "module": "dist/rollup-plugin-typescript.es.js", + "jsnext:main": "dist/rollup-plugin-typescript.es.js", "files": [ "dist", "src", @@ -23,8 +23,8 @@ "prepublish": "npm run test", "pretest": "npm run build", "build:cjs": "rollup -c -f cjs -o dist/rollup-plugin-typescript.cjs.js", - "build:es6": "rollup -c -o dist/rollup-plugin-typescript.es6.js", - "build": "npm run build:cjs && npm run build:es6", + "build:es": "rollup -c -o dist/rollup-plugin-typescript.es.js", + "build": "npm run build:cjs && npm run build:es", "prebuild": "rimraf dist/*" }, "dependencies": {