Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
David Sheldrick committed May 7, 2017
0 parents commit 10fe272
Show file tree
Hide file tree
Showing 10 changed files with 4,676 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .babelrc
@@ -0,0 +1,5 @@
{
"presets": [
"react-native"
]
}
7 changes: 7 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,7 @@
module.exports = {
extends: "standard",
plugins: ["standard", "promise", "jest"],
env: {
"jest/globals": true
}
};
58 changes: 58 additions & 0 deletions .gitignore
@@ -0,0 +1,58 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
5 changes: 5 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,5 @@
// Place your settings in this file to overwrite default and user settings.
{
"editor.formatOnSave": true,
"editor.formatOnPaste": true
}
39 changes: 39 additions & 0 deletions __specs__/__snapshots__/transformer.test.js.snap
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`the transformer works 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
function Cheese() {
return { cheese: 'stilton' };
}
exports.default = Cheese;"
`;

exports[`the transformer works 2`] = `
Object {
"mappings": "AAAA;;AACAA,OAAOC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C,EAAEC,OAAO,IAAT,EAA7C;AACA,SAASC,MAAT,GAAkB;AACd,WAAO,EAAEC,QAAQ,SAAV,EAAP;AACH;AACDH,QAAQI,OAAR,GAAkBF,MAAlB",
"names": Array [
"Object",
"defineProperty",
"exports",
"value",
"Cheese",
"cheese",
"default",
],
"sources": Array [
"blah.tsx",
],
"sourcesContent": Array [
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", { value: true });
function Cheese() {
return { cheese: 'stilton' };
}
exports.default = Cheese;
//# sourceMappingURL=module.js.map",
],
"version": 3,
}
`;
19 changes: 19 additions & 0 deletions __specs__/transformer.test.js
@@ -0,0 +1,19 @@
const file = `
type Cheese = {
readonly cheese: string
}
export default function Cheese(): Cheese {
return {cheese: 'stilton'};
}
`

const transformer = require('../')

describe('the transformer', () => {
it('works', () => {
const result = transformer.transform(file, 'blah.tsx', {})
expect(result.code).toMatchSnapshot()
expect(result.map).toMatchSnapshot()
})
})
59 changes: 59 additions & 0 deletions index.js
@@ -0,0 +1,59 @@
'use strict'
const ts = require('typescript')
const upstreamTransformer = require('react-native/packager/transformer')
const fs = require('fs')
const appRootPath = require('app-root-path')
const path = require('path')
const process = require('process')
const TSCONFIG_PATH = process.env.TSCONFIG_PATH

const { SourceMapConsumer, SourceMapGenerator } = require('source-map')

function composeSourceMaps (tsMap, babelMap) {
const map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(babelMap))
map.applySourceMap(new SourceMapConsumer(tsMap))
return map.toJSON()
}

const tsConfig = (() => {
if (TSCONFIG_PATH) {
const resolvedTsconfigPath = path.resolve(process.cwd(), TSCONFIG_PATH)
if (fs.existsSync(resolvedTsconfigPath)) {
return require(resolvedTsconfigPath)
}
console.warn(
'tsconfig file specified by TSCONFIG_PATH environment variable was not found'
)
console.warn(`TSCONFIG_PATH = ${TSCONFIG_PATH}`)
console.warn(`resolved = ${resolvedTsconfigPath}`)
console.warn('looking in app root directory')
}
const tsConfigPath = appRootPath.resolve('tsconfig.json')
if (fs.existsSync(tsConfigPath)) {
return require(tsConfigPath)
}
throw new Error(`Unable to find tsconfig.json at ${tsConfigPath}`)
})()

const compilerOptions = Object.assign(tsConfig.compilerOptions, {
sourceMap: true
})

module.exports.transform = function (sourceCode, filename, options) {
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
const tsCompileResult = ts.transpileModule(sourceCode, { compilerOptions })
const babelCompileResult = upstreamTransformer.transform(
tsCompileResult.outputText,
filename,
Object.assign({}, options, { generateSourceMaps: true })
)
return Object.assign({}, babelCompileResult, {
map: composeSourceMaps(
tsCompileResult.sourceMapText,
babelCompileResult.map
)
})
} else {
return upstreamTransformer.transform(sourceCode, filename, options)
}
}
36 changes: 36 additions & 0 deletions package.json
@@ -0,0 +1,36 @@
{
"name": "react-native-typescript-transformer",
"version": "1.0.0",
"description": "TypeScript transformer for react-native",
"main": "index.js",
"repository": "https://github.com/ds300/react-native-typescript-transformer",
"author": "David Sheldrick",
"license": "MIT",
"dependencies": {
"app-root-path": "^2.0.1",
"source-map": "^0.5.6",
"typescript": "^2.3.2"
},
"peerDependencies": {
"react-native": "^0.44.0"
},
"devDependencies": {
"babel-jest": "^20.0.0",
"babel-preset-react-native": "^1.9.1",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jest": "^20.0.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"jest": "^20.0.0",
"prettier": "^1.3.1"
},
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
}
7 changes: 7 additions & 0 deletions tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true
}
}

0 comments on commit 10fe272

Please sign in to comment.