Skip to content

Commit

Permalink
perf: cache tsconf
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Oct 1, 2017
1 parent 2e1f555 commit e119cd3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
36 changes: 34 additions & 2 deletions lib/compilers/typescript-compiler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
const ensureRequire = require('../ensure-require')
const compileBabel = require('./babel-compiler')
const tsconfig = require('tsconfig')
const cache = require('../cache')
const logger = require('../logger')

const defaultTypescriptConfig = {
'compilerOptions': {
'module': 'es2015',
'moduleResolution': 'node',
'allowSyntheticDefaultImports': true,
'strict': true,
'noEmit': true
},
'include': [
'**/*.ts'
]
}

function getTypescriptConfig () {
// const cachedConfig = cache.get('typescript-config')
// if (cachedConfig) {
// return cachedConfig
// } else {
const { config } = tsconfig.loadSync(process.cwd())

if (!config) {
logger.info('no tsconfig.json found, defaulting to default typescript options')
}

const typescriptConfig = config || defaultTypescriptConfig
cache.set('typescript-config', typescriptConfig)
return typescriptConfig
// }
}

module.exports = function compileTypescript (scriptContent) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const file = tsconfig.loadSync(process.cwd())
const tsConfig = getTypescriptConfig()

const res = typescript.transpileModule(scriptContent, file.config)
const res = typescript.transpileModule(scriptContent, tsConfig)

return compileBabel(res.outputText, res.sourceMapText)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"release": "build/release.sh",
"release:note": "node build/gen-release-note.js",
"test": "npm run lint && ./test.sh",
"unit": "jest test --no-cache --runInBand --coverage --coverageDirectory test/coverage"
"unit": "./test.sh",
"unit:run": "jest test --no-cache --runInBand --coverage --coverageDirectory test/coverage"
},
"author": "Edd Yerburgh",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ cp package.json temp-dir/package.json
cp .babelrc temp-dir/.babelrc
cp tsconfig.json temp-dir/tsconfig.json
cd temp-dir
yarn unit
yarn unit:run
10 changes: 8 additions & 2 deletions test/TypeScript.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { shallow } from 'vue-test-utils'
import { resolve } from 'path'
import TypeScript from './resources/TypeScript.vue'
import jestVue from '../jest-vue'
import { resolve } from 'path'
import { readFileSync } from 'fs'
import { shallow } from 'vue-test-utils'
import cache from '../lib/cache'

beforeEach(() => {
cache.flushAll()
})

test('processes .vue files', () => {
shallow(TypeScript)
})

test.skip('generates inline sourcemap', () => {
const expectedMap = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljLnZ1ZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7OztBQUdBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBSEE7QUFLQTtBQVBBO0FBU0E7QUFDQTtBQUNBO0FBQ0E7QUFGQTtBQUlBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFIQTtBQWpCQSIsInNvdXJjZXNDb250ZW50IjpbIjx0ZW1wbGF0ZT5cbiAgICA8ZGl2IGNsYXNzPVwiaGVsbG9cIj5cbiAgICAgICAgPGgxIDpjbGFzcz1cImhlYWRpbmdDbGFzc2VzXCI+e3sgbXNnIH19PC9oMT5cbiAgICA8L2Rpdj5cbjwvdGVtcGxhdGU+XG5cbjxzY3JpcHQ+XG4gICAgZXhwb3J0IGRlZmF1bHQge1xuICAgICAgICBuYW1lOiAnYmFzaWMnLFxuICAgICAgICBjb21wdXRlZDoge1xuICAgICAgICAgICAgaGVhZGluZ0NsYXNzZXM6IGZ1bmN0aW9uIGhlYWRpbmdDbGFzc2VzKCkge1xuICAgICAgICAgICAgICAgIHJldHVybiB7XG4gICAgICAgICAgICAgICAgICAgIHJlZDogdGhpcy5pc0NyYXp5LFxuICAgICAgICAgICAgICAgICAgICBibHVlOiAhdGhpcy5pc0NyYXp5LFxuICAgICAgICAgICAgICAgICAgICBzaGFkb3c6IHRoaXMuaXNDcmF6eSxcbiAgICAgICAgICAgICAgICB9O1xuICAgICAgICAgICAgfSxcbiAgICAgICAgfSxcbiAgICAgICAgZGF0YTogZnVuY3Rpb24gZGF0YSgpIHtcbiAgICAgICAgICAgIHJldHVybiB7XG4gICAgICAgICAgICAgICAgbXNnOiAnV2VsY29tZSB0byBZb3VyIFZ1ZS5qcyBBcHAnLFxuICAgICAgICAgICAgICAgIGlzQ3Jhenk6IGZhbHNlLFxuICAgICAgICAgICAgfTtcbiAgICAgICAgfSxcbiAgICAgICAgbWV0aG9kczoge1xuICAgICAgICAgICAgdG9nZ2xlQ2xhc3M6IGZ1bmN0aW9uIHRvZ2dsZUNsYXNzKCkge1xuICAgICAgICAgICAgICAgIHRoaXMuaXNDcmF6eSA9ICF0aGlzLmlzQ3Jhenk7XG4gICAgICAgICAgICB9LFxuICAgICAgICB9LFxuICAgIH07XG48L3NjcmlwdD5cbiJdfQ=='
const filePath = resolve(__dirname, './resources/TypeScript.vue')
Expand Down

0 comments on commit e119cd3

Please sign in to comment.