react-typescript

v3.0.1arrow_drop_down
v4.0.0
v3.1.70
v3.1.65
v3.1.64
v3.1.63
v3.1.62
v3.1.61
v3.1.60
v3.1.59
v3.1.58
v3.1.57
v3.1.56
v3.1.55
v3.1.54
v3.1.53
v3.1.52
v3.1.51
v3.1.50
v3.1.49
v3.1.48
v3.1.47
v3.1.46
v3.1.45
v3.1.44
v3.1.43
v3.1.42
v3.1.41
v3.1.40
v3.1.39
v3.1.38
v3.1.37
v3.1.36
v3.1.35
v3.1.34
v3.1.33
v3.1.32
v3.1.31
v3.1.30
v3.1.29
v3.1.28
v3.1.27
v3.1.26
v3.1.25
v3.1.24
v3.1.23
v3.1.22
v3.1.21
v3.1.20
v3.1.19
v3.1.18
v3.1.17
v3.1.16
v3.1.15
v3.1.14
v3.1.13
v3.1.12
v3.1.11
v3.1.10
v3.1.9
v3.1.8
v3.1.7
v3.1.6
v3.1.5
v3.1.4
v3.1.3
v3.1.2
v3.1.1
v3.1.0
v3.0.30
v3.0.29
v3.0.28
v3.0.27
v3.0.26
v3.0.25
v3.0.24
v3.0.23
v3.0.22
v3.0.21
v3.0.20
v3.0.19
v3.0.18
v3.0.17
v3.0.16
v3.0.15
v3.0.14
v3.0.13
v3.0.12
v3.0.11
v3.0.10
v3.0.9
v3.0.8
v3.0.7
v3.0.6
v3.0.5
v3.0.4
v3.0.3
v3.0.2
v3.0.1
v3.0.0
v0.0.23
v0.0.22
v0.0.21
v0.0.20
v0.0.19
v0.0.18
v0.0.17
v0.0.16
v0.0.15
v0.0.14
v0.0.13
v0.0.12
v0.0.11
v0.0.10
v0.0.9
v0.0.8
v0.0.7
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
165,938
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
4 years ago
SIZE
N/A
Bit react typescript compiler. Compiles a React component for TypeScript.
5 contributors
Install react-typescript as a package?
Copied
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Files
index.js
67 Lines(46 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import ts from 'typescript';
import Vinyl from 'vinyl';
import path from 'path';
import groupBy from '@bit/bit.utils.object.group-by';

import tsconfig from './tsconfig.json';

const compiledFileTypes = ['tsx', 'ts'];

const compileSingleFile = (file, compilerOptions, distPath) => {
  const result = ts.transpileModule(file.contents.toString(), { compilerOptions });

  const mappings = new Vinyl({
    contents: new Buffer(result.sourceMapText),
    base: distPath,
    path: _getDistFilePath(file, distPath, true),
  });
  mappings.basename = _getRevisedFileExtension(file.basename) + '.map';

  const fileContent = result.outputText ? new Buffer(`${result.outputText}\n\n//# sourceMappingURL=${result.sourceMapText}`) : new Buffer(result.outputText);
  const distFile = _getDistFile(file, distPath, true, fileContent);

  return [mappings, distFile];
};

const compile = (files, distPath) => {
  // TODO: change to load from tsconfig.json using ts.parseJsonSourceFileConfigFileContent
  const compilerOptions = tsconfig.compilerOptions;

  // Divide files by whether we should compile them, according to file type.
  const filesByToCompile = groupBy(files, _toCompile);

  const compiled = (!filesByToCompile.true || filesByToCompile.true.length === 0) ? [] : filesByToCompile.true.map(file => compileSingleFile(file, compilerOptions, distPath)).reduce((a, b) => a.concat(b));
  const nonCompiled = !filesByToCompile.false ? [] : filesByToCompile.false.map(file => _getDistFile(file, distPath, false));

  return compiled.concat(nonCompiled);;
}

const _toCompile = (file) => {
  return compiledFileTypes.indexOf(file.extname.replace('.', '')) > -1 && !file.stem.endsWith('.d');
}

const _getDistFile = (file, distPath, reviseExtension, content) => {
  let distFile = file.clone();
  distFile.base = distPath;
  distFile.path = _getDistFilePath(file, distPath, reviseExtension);

  if (content) {
    distFile.contents = content;
  }

  return distFile;
}

const _getDistFilePath = (file, distPath, reviseExtension) => {
  var fileRelative = file.relative;

  if (reviseExtension) fileRelative = _getRevisedFileExtension(file.relative);

  return path.join(distPath, fileRelative);
}

const _getRevisedFileExtension = (fileName) => {
  return fileName.replace('.tsx', '.js').replace('.ts', '.js');
}

export default { compile }; 
Help and resources