Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Pure Typescript component #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6,375 changes: 6,375 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"main": "index.js",
"scripts": {
"build": "webpack",
"dev": "webpack-dev-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand All @@ -22,10 +23,14 @@
},
"devDependencies": {
"css-loader": "^0.28.1",
"ts-loader": "^2.0.3",
"fork-ts-checker-webpack-plugin": "^0.2.9",
"hash-sum": "^1.0.2",
"ts-loader": "^3.2.0",
"typescript": "^2.3.2",
"vue-loader": "^12.0.3",
"vue-hot-reload-api": "^2.2.4",
"vue-loader": "^13.5.0",
"vue-template-compiler": "^2.5.2",
"webpack": "^2.5.0"
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.5"
}
}
29 changes: 29 additions & 0 deletions src/components/HelloTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Vue, Component, Prop } from "vue-property-decorator";

@Component({
template: `
<div>
<div class="greeting">Hello {{name}}{{exclamationMarks}}</div>
<button @click="decrement">-</button>
<button @click="increment">+</button>
</div>`
})
export default class HelloTs extends Vue {
@Prop() name: string;
@Prop() initialEnthusiasm: number;

enthusiasm = this.initialEnthusiasm;

increment() {
this.enthusiasm++;
}
decrement() {
if (this.enthusiasm > 1) {
this.enthusiasm--;
}
}

get exclamationMarks(): string {
return Array(this.enthusiasm + 1).join('!');
}
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from "vue";
import HelloComponent from "./components/Hello.vue";
import HelloDecoratorComponent from "./components/HelloDecorator.vue";
import HelloTsComponent from "./components/HelloTs";

let v = new Vue({
el: "#app",
Expand All @@ -11,11 +12,14 @@ let v = new Vue({
<hello-component :name="name" :initialEnthusiasm="5" />
<h1>Hello Decorator Component</h1>
<hello-decorator-component :name="name" :initialEnthusiasm="5" />
</div>
<h1>Hello Typescript only</h1>
<hello-ts-component :name="name" :initialEnthusiasm="5" />
</div>
`,
data: { name: "World" },
components: {
HelloComponent,
HelloDecoratorComponent
HelloDecoratorComponent,
HelloTsComponent
}
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"target": "es5",
"experimentalDecorators": true
},
"include": [
"./src/**/*"
"files": [
"./src/index.ts"
]
}
37 changes: 37 additions & 0 deletions vue-ts-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = function (content) {
this.cacheable()
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
var hash = require('hash-sum')
var cache = Object.create(null)

function genId (file) {
return cache[file] || (cache[file] = hash(file))
}

if (!isProduction) {
var filePath = this.resourcePath
var moduleId = genId(filePath)
var regex = new RegExp(/(\S+) = __decorate\(\[\s+Component/gi)
var matches = regex.exec(content)
if (matches !== null) {
content += `
/* hot reload */
${matches[1]}.options.__file = '${filePath}'
if (module.hot) {(function () {
var hotApi = require('vue-hot-reload-api')
hotApi.install(require('vue'), false)
if (!hotApi.compatible) return
module.hot.accept()
if (!module.hot.data) {
console.log('data defined')
hotApi.createRecord('${moduleId}', ${matches[1]})
} else {
console.log('data already')
hotApi.reload('${moduleId}', ${matches[1]})
}
})()}
`
}
}
return content
}
35 changes: 25 additions & 10 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var path = require('path')
var webpack = require('webpack')
var ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')

module.exports = {
entry: './src/index.ts',
Expand All @@ -20,18 +20,25 @@ module.exports = {
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
use: [
{
loader: path.resolve('./vue-ts-loader.js')
},
{
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsSuffixTo: [/\.vue$/]
}
}
],
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
Expand All @@ -50,7 +57,8 @@ module.exports = {
},
devServer: {
historyApiFallback: true,
noInfo: true
noInfo: true,
hot: true
},
performance: {
hints: false
Expand All @@ -75,6 +83,13 @@ if (process.env.NODE_ENV === 'production') {
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
}),
new ForkTsCheckerWebpackPlugin()
])
} else {
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
])
// module.exports.entry = ['webpack-dev-server/client', module.exports.entry]
}
Loading