ๅ ็บๅทฅไฝ็้ไฟๆฅ่งธไบ angular,ๆไปฅๅญธ็ฟไบ typescript,่ฆบๅพ้ๅทฅๅ ทๅพๅฅฝ็จ,ๆผๆฏๅธๆๅจ้ angular ็็ฐๅขไธๅฏซ js ๆไน็จ typescript,้ๆ่ฆๅ็จ webpack ๅนซๅฟๆๅ ๆ browser ๅฏไปฅไฝฟ็จ็ๅ งๅฎน。
็ฎ้็ตๆง
├─app
└─src
│ index.ts
│
└─class
chan.ts
│ index.html
│ tsconfig.json
│ webpack.config.js
config
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"allowJs": true
}
}
typescript ็ๅบ็ค้ ็ฝฎ
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
entry: {
index: {import: './src/index.ts'}
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.resolve(__dirname, 'app'),
},
};
webpack ็้
็ฝฎ,ๆๆๆ entry ๆๆๅฎ็ๆชๆก่ผธๅบๅฐ app ๅๆจฃ็ๆชๅ,็จ้ spa ็ๆนๅผ้็ผๅพๅพ้ๆฏไธๅ้ ้ขไฝฟ็จไธๅ็ js,ๆไปฅ้ๅๆถๆงๅฐ user ไธฆไธ่ฝ็ฏ็่ณๆบ,ๅ ็บๆฏๅ js ้ฝๆๅ
ไธไปฝ jquery,ๅชๆฏๅฐ้็ผ่
ๆนไพฟ็ๅทฅๅ
ท่ๅทฒ,้ค้ไฝ ๆๆๆกๆ็จๅผๅฏซๅฐๆฏๅ้ ้ข้้ขไธไบ็ธๅนฒๆพ,้ฃๅฐฑๅฏไปฅไธๆฏๆชๆกๆๅคฉไธ,ไนๆฏ spa ็้ไฝๆนๅผ。
npm i --save webpack webpack-cli typescript ts-loader
ๅฎ่ฃ compiler ้่ฆ็ๅทฅๅ ท
ไฝฟ็จๆนๅผ
npm i -s jquery dayjs
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Typescript + Webpack</title>
<script src="app/index.js"></script>
</head>
<body>
<div id="content"></div>
</body>
</html>
index.ts
import $ from 'jquery';
import { Chan } from './class/chan';
$(function () {
const chan = new Chan();
$('div#content').html(chan.getNow());
});
chan.ts
import dayjs from 'dayjs';
export class Chan {
public getNow(): string {
return dayjs().toString();
}
}
ไฝฟ็จ webpack compiler ไนๅพๆๅ้ ้ขไธ็ div#content ๆๅบ็พ Fri, 29 Jan 2021 09:09:52 GMT,ๅคงๅๅๆ。