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
Code language: PHP (php)

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" } }
Code language: JSON / JSON with Comments (json)

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" ] }
Code language: JSON / JSON with Comments (json)

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", }
Code language: PHP (php)

build