Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Typescript support #109

Merged
merged 6 commits into from
Jul 18, 2017
Merged
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
1 change: 1 addition & 0 deletions config/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ rollup.rollup({
'stylus',
'vue-template-es2015-compiler',
'vue-template-validator',
'typescript'
].indexOf(id) > -1
}
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"posthtml": "^0.9.2",
"posthtml-attrs-parser": "^0.1.1",
"rollup-pluginutils": "^2.0.1",
"typescript": "^2.4.1",
"vue-template-compiler": "*",
"vue-template-es2015-compiler": "^1.5.0",
"vue-template-validator": "^1.1.5"
Expand Down
72 changes: 45 additions & 27 deletions src/injections.js

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { templateJs, moduleJs, scopeJs, renderJs } from './injections'
import { coffee } from './script/index'
import { coffee, typescript } from './script/index'

export default {
// Style compilation options.
Expand Down Expand Up @@ -73,28 +73,32 @@ export default {
inject: {
template: {
js: templateJs,
babel: templateJs
ts: templateJs,
coffee: templateJs
},

render: {
js: renderJs,
babel: renderJs
ts: renderJs,
coffee: renderJs
},

module: {
js: moduleJs,
babel: moduleJs
ts: moduleJs,
coffee: moduleJs
},

scoped: {
js: scopeJs,
babel: scopeJs
ts: scopeJs,
coffee: scopeJs
}
},

// script languages.
script: {
coffee,
coffeescript: coffee
ts: typescript
}
}
3 changes: 2 additions & 1 deletion src/script/coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export default function (script) {
return new Promise((resolve, reject) => {
coffee.compile(script.code, { bare: true }, (status, output) => {
if (status === 0) {
script.code = output
script.code = output.replace(/^\/\/ Generated by CoffeeScript [\d]+.[\d]+.[\d]+/i, '')
console.log(script.code)

resolve(script)
} else {
Expand Down
1 change: 1 addition & 0 deletions src/script/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as coffee } from './coffee'
export { default as typescript } from './typescript'
14 changes: 14 additions & 0 deletions src/script/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Typescript from 'typescript'
import debug from '../debug'
export default async function (script, id, content, options, nodes) {
debug(`Typescript: Compiling ${id}`)
options.typescript = options.typescript || {}
const config = Object.assign({}, options.typescript, { fileName: id })
config.compilerOptions = Object.assign({}, options.typescript.compilerOptions, {
experimentalDecorators: true,
module: Typescript.ModuleKind.ES2015,
moduleResolution: Typescript.ModuleResolutionKind.NodeJs
})
script.code = (await Typescript.transpileModule(script.code, config)).outputText
return script
}
34 changes: 25 additions & 9 deletions src/vueTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function getNodeAttrs (node) {
*/
function padContent (content) {
return content
.split(/\r?\n/g)
.map(() => '')
.join('\n')
.split(/\r?\n/g)
.map(() => '')
.join('\n')
}

function validateTemplate (code, content, id) {
Expand All @@ -54,9 +54,9 @@ async function processTemplate (source, id, content, options, nodes, modules) {
const extras = { modules, id, lang: source.attrs.lang }
const code = deIndent(source.code)
const template = await (
options.disableCssModuleStaticReplacement !== true
? templateProcessor(code, extras, options)
: code
options.disableCssModuleStaticReplacement !== true
? templateProcessor(code, extras, options)
: code
)

if (!options.compileTemplate) {
Expand All @@ -66,11 +66,28 @@ async function processTemplate (source, id, content, options, nodes, modules) {
return htmlMinifier.minify(template, options.htmlMinifier)
}

/* eslint-disable complexity */
function normalizeLang (any) {
switch (any) {
case 'coffee':
case 'coffeescript':
case 'coffee-script':
return 'coffee'
case 'ts':
case 'typescript':
case 'type-script':
return 'ts'
default:
return 'js'
}
}
/* eslint-enable complexity */

async function processScript (source, id, content, options, nodes, modules, scoped) {
const template = await processTemplate(nodes.template[0], id, content, options, nodes, modules)

debug(`Process script: ${id}`)
const lang = 'js'
const lang = normalizeLang(source.attrs.lang)

if (source.attrs.lang && ['js', 'babel'].indexOf(source.attrs.lang) < 0) {
if (!(source.attrs.lang in options.script)) {
Expand Down Expand Up @@ -123,7 +140,7 @@ async function processStyle (styles, id, content, options) {
const style = styles[i]

const code = deIndent(
padContent(content.slice(0, content.indexOf(style.code))) + style.code
padContent(content.slice(0, content.indexOf(style.code))) + style.code
)

const map = (new MagicString(code)).generateMap({ hires: true })
Expand Down Expand Up @@ -199,7 +216,6 @@ const hasScoped = function (styles) {
return scoped || style.scoped
}, false)
}

export default async function vueTransform (code, id, options) {
const nodes = parseTemplate(code)
const css = await processStyle(nodes.style, id, code, options, nodes)
Expand Down
3 changes: 1 addition & 2 deletions test/expects/coffee.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Generated by CoffeeScript 1.12.4
var component;

component = { template: "<h1 :id=\"id\" @click=\"hi\">hello</h1><input type=\"text\">",
component = { template: "<h1 :id=\"id\" @click=\"hi\">hello</h1>",
data: function() {
return [2, 4, 6, 8];
}
Expand Down
7 changes: 7 additions & 0 deletions test/expects/typescript-export-obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var app = {
};

var __$app = Object.assign(app, { template: "<h1 :id=\"id\" @click=\"hi\">hello</h1><input type=\"text\">",});
__$app.prototype = app.prototype;

export default __$app;
12 changes: 12 additions & 0 deletions test/expects/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var component = { template: "<h1 :id=\"id\" @click=\"hi\">hello</h1><input type=\"text\">",
data: function () { return ({
hello: 'world!'
}); },
methods: {
hello: function () {
return this.hello;
}
}
};

export default component;
2 changes: 2 additions & 0 deletions test/fixtures/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {
}
1 change: 0 additions & 1 deletion test/fixtures/coffee.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<h1 :id="id" @click="hi">hello</h1>
<input type="text">
</template>

<script lang="coffee">
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/typescript-export-obj.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<h1 :id="id" @click="hi">
hello</h1>
<input type="text">
</template>

<script lang="ts">
import app from './app.ts'
export default app
</script>
24 changes: 24 additions & 0 deletions test/fixtures/typescript.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<h1 :id="id" @click="hi">
hello</h1>
<input type="text">
</template>

<script lang="ts">
import Vue from 'vue'
interface Data {
hello: string
}
const component = {
data: () => ({
hello: 'world!'
}),
methods: {
hello() {
return this.hello
}
}
} as Vue.ComponentOptions<Data & Vue>

export default component
</script>
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function test(name) {
describe('rollup-plugin-vue', function () {
fs.readdirSync(path.resolve(__dirname, 'fixtures'))
.forEach(function (file) {
test(file.substr(0, file.length - 4))
file.endsWith('.vue') && test(file.substr(0, file.length - 4))
})
})

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3243,6 +3243,10 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.1.tgz#c3ccb16ddaa0b2314de031e7e6fee89e5ba346bc"

uglify-js@2.7.x, uglify-js@^2.6, uglify-js@^2.6.1, uglify-js@^2.7.5:
version "2.7.5"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.7.5.tgz#4612c0c7baaee2ba7c487de4904ae122079f2ca8"
Expand Down