Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(babel-typescript) transpile typescript code using babel #162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

<script lang="ts">
import TypeScriptChild from './TypeScriptChild.vue'

export default {
computed: {
exclamationMarks(): string {
Expand Down
7 changes: 7 additions & 0 deletions e2e/__projects__/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-typescript": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/runtime-corejs2": "^7.3.1",
"@vue/test-utils": "^1.0.0-beta.28",
"jest": "^24.0.0",
"vue-jest": "file:../../../"
Expand Down
7 changes: 7 additions & 0 deletions e2e/__projects__/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-typescript": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/runtime-corejs2": "^7.3.1",
"@vue/test-utils": "^1.0.0-beta.28",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-plugin-syntax-jsx": "^6.18.0",
Expand Down
7 changes: 7 additions & 0 deletions e2e/__projects__/custom-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-typescript": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/runtime-corejs2": "^7.3.1",
"@vue/test-utils": "^1.0.0-beta.28",
"jest": "^24.0.0",
"node-sass": "^4.11.0",
Expand Down
7 changes: 7 additions & 0 deletions e2e/__projects__/style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@babel/preset-typescript": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/runtime-corejs2": "^7.3.1",
"@vue/test-utils": "^1.0.0-beta.28",
"jest": "^24.0.0",
"node-sass": "^4.11.0",
Expand Down
8 changes: 0 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const crypto = require('crypto')
const babelJest = require('babel-jest')
const tsJest = require('ts-jest')

module.exports = {
process: require('./process'),
Expand All @@ -20,13 +19,6 @@ module.exports = {
}),
'hex'
)
.update(
tsJest.getCacheKey(fileData, filename, configString, {
instrument,
rootDir
}),
'hex'
)
.digest('hex')
}
}
38 changes: 4 additions & 34 deletions lib/typescript-transformer.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
const ensureRequire = require('./ensure-require')
const babelJest = require('babel-jest')
const getBabelOptions = require('./utils').getBabelOptions
const getTsJestConfig = require('./utils').getTsJestConfig
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
const getTSBabelOptions = require('./utils').getTSBabelOptions
const getCustomTransformer = require('./utils').getCustomTransformer
const getVueJestConfig = require('./utils').getVueJestConfig

module.exports = {
process(scriptContent, filePath, config) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, tsconfig)

res.outputText = stripInlineSourceMap(res.outputText)

const inputSourceMap =
res.sourceMapText !== undefined ? JSON.parse(res.sourceMapText) : ''

// handle ES modules in TS source code in case user uses non commonjs module
// output and there is no .babelrc.
let inlineBabelOptions = {}
if (
tsconfig.compilerOptions.module !== typescript.ModuleKind.CommonJS &&
!babelOptions
) {
inlineBabelOptions = {
plugins: [require('@babel/plugin-transform-modules-commonjs')]
}
}
const babelOptions = getTSBabelOptions()
const customTransformer =
getCustomTransformer(vueJestConfig['transform'], 'js') || {}
const transformer = customTransformer.process
? customTransformer
: babelJest.createTransformer(
Object.assign(inlineBabelOptions, {
inputSourceMap
})
)

return transformer.process(res.outputText, filePath, config)
: babelJest.createTransformer(babelOptions)
return transformer.process(scriptContent, filePath, config)
}
}
59 changes: 51 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const loadPartialConfig = require('@babel/core').loadPartialConfig
const createTransformer = require('ts-jest').createTransformer
const ensureRequire = require('./ensure-require')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')

const fetchTransformer = function fetchTransformer(key, obj) {
for (const exp in obj) {
Expand Down Expand Up @@ -62,12 +63,6 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
return loadPartialConfig(opts).options
}

const getTsJestConfig = function getTsJestConfig(config) {
const tr = createTransformer()
const { typescript } = tr.configsFor(config)
return { compilerOptions: typescript.options }
}

function isValidTransformer(transformer) {
return (
isFunction(transformer.process) ||
Expand Down Expand Up @@ -114,12 +109,60 @@ const logResultErrors = result => {
}
}

const getTSBabelOptions = () => {
const ts = getTsInstance()
const { compilerOptions } = getTsConfig()
const TSBabelOptions = {
presets: [
'@babel/env',
['@babel/preset-typescript', { allExtensions: true }]
],
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', { corejs: 2 }]
]
}
if (compilerOptions.module === ts.ModuleKind.CommonJS) {
TSBabelOptions.plugins.push('@babel/plugin-transform-modules-commonjs')
}
return TSBabelOptions
}

const getTsConfig = function getTsConfig(
pathToConfig = path.resolve(process.cwd(), './tsconfig.json')
) {
const ts = getTsInstance()
if (!fs.existsSync(pathToConfig)) {
return { compilerOptions: {} }
}
const configJson = ts.parseConfigFileTextToJson(
pathToConfig,
ts.sys.readFile(pathToConfig)
)
const { options: compilerOptions } = ts.parseJsonConfigFileContent(
configJson.config,
ts.sys,
ts.getDirectoryPath(pathToConfig),
{},
pathToConfig
)
return { compilerOptions }
}

const getTsInstance = function getTSInstance() {
ensureRequire('typescript', ['typescript'])
return require('typescript')
}

module.exports = {
stripInlineSourceMap,
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getTSBabelOptions,
getBabelOptions,
getVueJestConfig,
transformContent,
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@vue/test-utils": "^1.0.0-beta.25",
"babel-core": "^7.0.0-bridge.0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yohodopo / @eddyerburgh Also updating to babel-jest@24 should enable us to remove the dependency on the babel-core bridge, as babel-jest supports babel@7 now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's is because of our dependency on ts-jest. Now that ts-jest v24.0.0 is released, we no longer need this.

"babel-jest": "^23.6.0",
"coffeescript": "^2.3.2",
"conventional-changelog": "^1.1.5",
"eslint": "^5.12.0",
Expand Down Expand Up @@ -67,12 +65,20 @@
"vue-template-compiler": "^2.x"
},
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-decorators": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"@babel/preset-typescript": "^7.1.0",
"@babel/runtime-corejs2": "^7.3.1",
"@vue/component-compiler-utils": "^2.4.0",
"babel-jest": "24.1.0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yohodopo / @eddyerburgh Is there a reason why we pin this to 24.1.0? Should this maybe be a peer dependency and/or also point to a version range with ^?

"chalk": "^2.1.0",
"extract-from-css": "^0.4.4",
"source-map": "^0.5.6",
"ts-jest": "^23.10.5"
"source-map": "^0.5.6"
},
"repository": {
"type": "git",
Expand Down
Loading