-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Tiny gitignore cleanup * firefox fix, #36 * ts support * tsx added * transpileoptions improvements and bugfix * tsconfig.json support * add basic typescript test * Default to ES2015 to stay consistent with the js default * remove custom transform, implement check in js transform for ts and put codegen back to commonjs * keep ts transform addition within ts module * ts testing and bugfix * improve jsx parameter and default to preserve and let babel handle it * improve jsx parameter and default to preserve and let babel handle it
- Loading branch information
1 parent
11d109b
commit 757b673
Showing
15 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ coverage | |
dist | ||
lib | ||
!test/**/node_modules | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const JSAsset = require('./JSAsset'); | ||
const config = require('../utils/config'); | ||
const localRequire = require('../utils/localRequire'); | ||
|
||
class TypeScriptAsset extends JSAsset { | ||
async transform() { | ||
super.transform(); | ||
|
||
await this.parseIfNeeded(); | ||
this.isAstDirty = true; | ||
} | ||
|
||
async parse(code) { | ||
// require typescript, installed locally in the app | ||
let typescript = localRequire('typescript', this.name); | ||
|
||
let transpilerOptions = { | ||
compilerOptions: { | ||
module: typescript.ModuleKind.CommonJS, | ||
jsx: typescript.JsxEmit.Preserve | ||
}, | ||
fileName: this.basename | ||
} | ||
|
||
let tsconfig = await config.load(this.name, ['tsconfig.json']); | ||
// Overwrite default if config is found | ||
if (tsconfig) transpilerOptions.compilerOptions = tsconfig.compilerOptions; | ||
transpilerOptions.compilerOptions.noEmit = false; | ||
|
||
// Transpile Module using TypeScript and parse result as ast format through babylon | ||
return await super.parse(typescript.transpileModule(code, transpilerOptions).outputText); | ||
} | ||
} | ||
|
||
module.exports = TypeScriptAsset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function env() { | ||
return process.env.NODE_ENV; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const local = require('./local.json'); | ||
|
||
export function count() { | ||
return local.a + local.b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"a": 1, | ||
"b": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const url = require('./test.txt'); | ||
|
||
export function getRaw() { | ||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hi there |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Local { | ||
a: number; | ||
b: number; | ||
|
||
constructor(a: number, b: number) { | ||
this.a = a; | ||
this.b = b; | ||
} | ||
} | ||
|
||
module.exports = Local; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const Local = require('./Local'); | ||
|
||
export function count() { | ||
let local = new Local(1, 2); | ||
return local.a + local.b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class Local { | ||
a: number; | ||
b: number; | ||
|
||
constructor(a: number, b: number) { | ||
this.a = a; | ||
this.b = b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Local } from './Local'; | ||
|
||
export function count() { | ||
let local = new Local(1, 2); | ||
return local.a + local.b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const {bundle, run, assertBundleTree} = require('./utils'); | ||
|
||
describe('typescript', function () { | ||
it('should produce a ts bundle using ES6 imports', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript/index.ts'); | ||
|
||
assert.equal(b.assets.size, 2); | ||
assert.equal(b.childBundles.size, 0); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output.count, 'function'); | ||
assert.equal(output.count(), 3); | ||
}); | ||
|
||
it('should produce a ts bundle using commonJS require', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript-require/index.ts'); | ||
|
||
assert.equal(b.assets.size, 2); | ||
assert.equal(b.childBundles.size, 0); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output.count, 'function'); | ||
assert.equal(output.count(), 3); | ||
}); | ||
|
||
it('should support json require', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript-json/index.ts'); | ||
|
||
assert.equal(b.assets.size, 2); | ||
assert.equal(b.childBundles.size, 0); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output.count, 'function'); | ||
assert.equal(output.count(), 3); | ||
}); | ||
|
||
it('should support env variables', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript-env/index.ts'); | ||
|
||
assert.equal(b.assets.size, 1); | ||
assert.equal(b.childBundles.size, 0); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output.env, 'function'); | ||
assert.equal(output.env(), 'test'); | ||
}); | ||
|
||
it('should support importing a URL to a raw asset', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript-raw/index.ts'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.js', | ||
assets: ['index.ts', 'test.txt'], | ||
childBundles: [{ | ||
type: 'txt', | ||
assets: ['test.txt'], | ||
childBundles: [] | ||
}] | ||
}); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output.getRaw, 'function'); | ||
assert(/^[0-9a-f]+\.txt$/.test(output.getRaw())); | ||
assert(fs.existsSync(__dirname + '/dist/' + output.getRaw())); | ||
}); | ||
|
||
/*it('should minify in production mode', async function () { | ||
let b = await bundle(__dirname + '/integration/typescript-require/index.ts', { production: true }); | ||
assert.equal(b.assets.size, 2); | ||
assert.equal(b.childBundles.size, 0); | ||
let output = run(b); | ||
assert.equal(typeof output.count, 'function'); | ||
assert.equal(output.count(), 3); | ||
let js = fs.readFileSync(__dirname + '/dist/index.js', 'utf8'); | ||
assert(!js.includes('local.a')); | ||
});*/ | ||
}); |