LoginSignup
3
1

More than 3 years have passed since last update.

Vue.jsをTypeScriptと使うメモ -設定-(@vue/cliは使わずに)

Last updated at Posted at 2019-09-29

設定

必要なpackageを取得

npm install vue-property-decorator
npm install --save-dev typescript webpack webpack-merge

tsconfig(基本的に推奨構成より)

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "strict": true,
    "moduleResolution": "node",
    "noImplicitAny": true,  // 暗黙のany
    "strictNullChecks": true,  //nullやundefinedを明示するように
    "noImplicitThis": true,
    "moduleResolution": "node",
    // importのpathを./src/...から@src/...に
    "paths": {
      "@src/*": ["src/*"]
    }, 
    "include": [
      "./src/**/*.ts"
    ]
  }
}

includeなどは自分の設定に合わせて…

webpack設定

webpack.common.js
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')

module.exports = {
  entry: {
    app: ["./src/index.ts"]
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "public/js")
  },
  resolve: {
    extensions: [".js", ".ts", ".vue", ".json", ".css"],
    alias: {
      vue$: "vue/dist/vue.esm.js",
      // importのpathを./src/...から@src/...に
      '@src': path.resolve(__dirname, './src')
    },
    modules: ["node_modules"]
  },
  plugins: [
    new VueLoaderPlugin(),
  ]
}

開発用のビルドと本番用のビルドで分けたりする。(sourcemap出力したり)

webpack.dev.js
// 開発ビルド用
const merge = require('webpack-merge')
const common = require('./webpack.common.js')

module.exports = merge(common, {
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.vue$/,
        exclude: /node_modules/,
        loader: 'vue-loader',
        options: {
          cssModules: {
            camelCase: true
          }
        }
      },
      {
        test: /\.ts$/,
        exclude: '/node_modules/',
        use: [
          {
            loader: 'ts-loader',
            options: {
              appendTsSuffixTo: [/\.vue$/]
            }
          }
        ]
      },
    ]
  }
})


3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1