Yii2 项目中使用 Laravel-mix

https://laravel-mix.com/docs/6.0/installation

.gitignore

# exclude node_modules and dist files from git
node_modules
frontend/web/dist

package.json

{
  "private": true,
  "scripts": {
    "dev": "npm run development",
    "development": "mix",
    "watch": "mix watch",
    "watch-poll": "mix watch -- --watch-options-poll=1000",
    "hot": "mix watch --hot",
    "prod": "npm run production",
    "production": "mix --production"
  },
  "devDependencies": {
    "laravel-mix": "^6.0.43",
    "ts-loader": "^9.2.7",
    "typescript": "^4.6.2"
  },
  "dependencies": {
    "jquery": "^3.6.0"
  }
}

webpack.mix.js

let mix = require('laravel-mix');

// for frontend
mix.ts('frontend/views/js/app.ts', 'frontend/web/dist')
  .setPublicPath('frontend/web/dist');

tsconfig.json

{
  "exclude": [
    "node_modules"
  ]
}

create ts source file

## frontend/views/js/app.ts
interface User {
  id: number
  firstName: string
  lastName: string
  role: string
}

const user: User = {
  id:1,
  firstName: "Angela",
  lastName: "Davis",
  role: "Professor",
}

build