webpack学习第二十步—— 打包TypeScript

2020-02-14  本文已影响0人  Love小六六

使用typescript

npm install ts-loader --save
npm install typescript --save
class Greeter {
    greeting: string
    constructor(message: string) {
        this.greeting = message
    }
    greet() {
        return "Hello, " + this.greeting
    }
}

let greeter = new Greeter("world")

alert(greeter.greet())
const path = require('path')
module.exports = {
    mode: 'development',
    entry: './src/index.tsx',
    module: {
        rules: [{
            test: /\.tsx?$/,
            use: 'ts-loader',
            exclude: /node_modules/
        }]
    },
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname,'build')
    }
}
{
  "compilerOptions": {
    "outDir": "./build",
    "module": "es6",
    "target": "es5",
    "allowJs": true
  },
  "exclude": [
    "node_modules"
  ]
}

使用三方库

import * as _ from 'lodash'
class Greeter {
    greeting: string
    constructor(message: string) {
        this.greeting = message
    }
    greet() {
        return _.join(["Hello,",this.greeting],' ')
    }
}

let greeter = new Greeter("world")

alert(greeter.greet())
上一篇下一篇

猜你喜欢

热点阅读