Laravel + Vue.js + TypeScriptでローカル開発環境を作っていきます。
Laravel + Vue.jsの環境構築は構築は色んなことに情報があると思うので、
より安全な開発を行うことができるTypeScriptを導入したバージョンの環境構築について記載していきます。
コピペでできるのでお試しください。
インストール&プロジェクト作成
composer create-project laravel/laravel --prefer-dist sample_app cd sample_app yarn install npm i -D vue npm install vue-template-compiler --save-dev npm install --save vue-property-decorator
環境設定
tsconfig.jsonを追加
{ "compilerOptions": { "outDir": "./built/", "sourceMap": true, "strict": true, "noImplicitReturns": true, "noImplicitAny": true, "module": "es2015", "experimentalDecorators": true, "emitDecoratorMetadata": true, "moduleResolution": "node", "target": "es5", "lib": [ "es2016", "dom" ] }, "include": [ "resources/assets/ts/**/*" ] }
vue.shims.d.ts
declare module "*.vue" { import Vue from "vue"; export default Vue; }
webpack.mix.js
let mix = require('laravel-mix'); mix.ts('resources/assets/ts/app.ts', 'public/js') .sass('resources/assets/sass/app.scss', 'public/css');
package.json(scripts内のパスのみ修正)
{ "private": true, "scripts": { "dev": "node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", "watch": "node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", "watch-poll": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", "hot": "node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", "production": "node node_modules/cross-env/src/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" }, "devDependencies": { "axios": "^0.19", "cross-env": "^7.0", "laravel-mix": "^5.0.1", "lodash": "^4.17.13", "resolve-url-loader": "^3.1.0", "sass": "^1.15.2", "sass-loader": "^8.0.0", "ts-loader": "^6.2.1", "typescript": "^3.8.3", "vue": "^2.6.11", "vue-property-decorator": "^8.4.1", "vue-template-compiler": "^2.6.11" }, "dependencies": { "webpack": "^4.42.0" } }
resources/assets/sass/app.scss
とりあえず空ファイル作成
resources/views/welcome.blade.php
<!doctype html> <html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Laravel</title> <link rel="stylesheet" href="/css/app.css"> </head> <body> <main> <div id="app"></div> </main> <script src="/js/app.js"></script> </body> </html>
resources/assets/ts/app.ts
import Vue from 'vue'; import bootstrap from './bootstrap'; import AppComponent from './components/App.vue'; bootstrap(); const app = new Vue({ el: '#app', render: h => h(AppComponent) });
resources/assets/ts/bootstrap.ts
import Axios, { AxiosStatic } from 'axios'; declare global { interface Window { axios: AxiosStatic; } interface Element { content: string; } } export default function bootstrap() { window.axios = Axios; window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; let token = document.head.querySelector('meta[name="csrf-token"]'); if (token) { window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; } else { console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); } }
resources/assets/ts/components/App.vue
<template> <div class="App"> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <h1>{{ message }}</h1> </div> </div> </div> </div> </template> <script lang="ts"> import { Vue, Component } from 'vue-property-decorator'; @Component export default class App extends Vue { message = 'Laravel + Vue.js + TypeScript'; } </script> <style scoped> h1 { text-align: center; margin: 4rem 0; } </style>
実行
ターミナルを2つ起動し、それぞれのターミナルで下記のコマンドを打ち込む。
こうする事で、TypeScriptは自動的にコンパイルされる。
npm run watch
php artisan serve