2.5.13
https://github.com/80LevelElf/VueProblemApp
์ฑ์ ์ด์ด์ ์คํํ๊ธฐ๋ง ํ๋ฉด
/VueProblemApp/VueProblemApp/index.html ํ์ผ๋ก ์ด๋ํ๋ฉด ์ฝ์์ ์ค๋ฅ๊ฐ ํ์๋ฉ๋๋ค.
Typescript ์ปดํ์ผ๋ฌ๋ Vue ํ์ดํ์ ๋ฐ์ ์ฌ๋ฐ๋ฅธ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํฉ๋๋ค.
๊ฒฐ๊ณผ JS ์ฝ๋๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค(์์ธํ ์ค๋ช ์ ์ฃผ์ ์ฐธ์กฐ).
๋ฉฐ์น ์ ์ TypeScript ๋ฐ AMD(UMD) ๋ฒ๊ทธ์
@yyx990803 ์์ ๋ต๋ณ์ ์ป์์ง๋ง ์ ๋ JS์ ๋ฉ์ฒญํ ๋์ด๊ณ ์ฒ์๋ถํฐ ์ ๋๋ก ์ดํดํ์ง ๋ชปํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ด๊ฐ ์ง๊ธ ๊ทธ๊ฒ์ ์ป๋๋ค๋ฉด ๋ฌธ์ ๋ ์ฌ์ ํ ๊ด๋ จ์ด ์์ต๋๋ค.
์๋ฐ์ด ๋์๊ฒ ๋๋ตํ๋ค.
"vue" ๋ชจ๋์ ์ง์ ํฌํจ์ํจ vue.js UMD ๋น๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ค์ ์ด ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฐ๋ผ์ TS ์ปดํ์ผ๋ฌ๋ node_modules/vue์์ ํด๊ฒฐํ๊ณ CommonJS ๋น๋์ธ package.json์ "main" ํ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค. tsconfig์์ compilerOptions.paths๋ฅผ ์ฌ์ฉํ์ฌ ๋งคํ์ ์ง์ ํด์ผ ํฉ๋๋ค.
๊ทธ๋์ Evan์ด ๋ง๋ค๋ฉด ๊ทธ๋ RequireJS๊ฐ ์๋ชป๋ vue.js ํ์ผ์ ๋ค์ด๋ก๋ํ๋ค๊ณ ์๊ฐํ์ต๋๋ค.
์ค๋ฅธ์ชฝ์ UMD/AMD ๋ชจ๋ ์์คํ
์ฉ์ด์ง๋ง TS ์ปดํ์ผ๋ฌ๋ CommonJS ๋ชจ๋ ์์คํ
์ฉ vue.js ํ์ผ์ ์ฐธ์กฐํฉ๋๋ค. ์ด๊ฒ์ด ๋ด๊ฐ ์ค๋ฅ๋ฅผ ์ป์ ์ด์ ์
๋๋ค.
๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ ์ณ์ง ์์ต๋๋ค. TS ์ปดํ์ผ๋ฌ๋ ๋ค์์ ์ฐธ์กฐํฉ๋๋ค.
๋ง์ด์ฌ์ดํธ/
์ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
mySite/vue.js
vue.js ํ์ผ์ด ๋ฌด์์ด๋ ์๊ด์์ต๋๋ค. ๋ฐ๋ผ์ ์ด ๋งํฌ์ vue.js๊ฐ ์์ผ๋ฉด 404 ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ํ node_modules/vue/package.json์ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ "main": "dist/vue.js"์ ๊ฐ์ด ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด ๋์ผํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
RequireJS์์ ๋ถ๋ฌ์ค๋ vue.js ํ์ผ์ ํ์ธํด๋ณด๋ ๋ง๋ ํ์ผ ์ ๋๋ค. RequireJS๋ ๊ทธ๊ฒ์ ์ฌ๋ฐ๋ฅด๊ฒ ๊ตฌ๋ฌธ ๋ถ์ํฉ๋๋ค. ๋ฌธ์ ๋ ํ์ดํ์ ์์ต๋๋ค.
๋ํ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ์ํํ๋ฉด(์: @Micene09 ์ ๊ณต):
import { Vue } from "vue-property-decorator";
mySite/vue.js์ ๋์ผํ vue.js ํ์ผ์ ๋ก๋ํ๋ฉด ๋ชจ๋ ๊ฒ์ด ์ ์๋ํฉ๋๋ค.
๊ทธ๋์ ๋ด๊ฐ ๋น์ ์ ์ณ๊ฒ ์ดํดํ๋ค๋ฉด ๋ฌธ์ ๋ ์ฌ์ ํ ๋จ์ ์์ต๋๋ค.
์ฃ์กํฉ๋๋ค. ์ด๊ฒ์ ๋ฌธ์ ์์ ํด๊ฒฐ๋์ด์๋ ์ ๋๋ ๋งค์ฐ ๊ตฌ์ฒด์ ์ธ ์ค์ ์ ๋ํ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์ง๋ฌธ์ ๋๋ค. ๋ฌธ์ ๋ Vue ์์ฒด์์ ๋ฐ์ํ ๋ฒ๊ทธ ๋ณด๊ณ ์์๋ง ํด๋น๋ฉ๋๋ค.
@80LevelElf -- ๋ค์์ ์ํํ์ฌ (์ง๊ธ์) ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๋ค.
import { VueConstructor } from "vue";
const Vue = require("vue") as VueConstructor; // cast as VueConstructor to get typings.
const app = new Vue({
template: "<h1>Hello World</h1>
});
@yyx990803 -- ์ ๋ ๋น์ทํ์ง๋ง ๋ค๋ฅธ ์ํฉ์
๋๋ค. TypeScript์ commonjs
์ถ๋ ฅ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค(์นํฉ ์์). OP์ I์ ํ ๊ฐ์ง ์ ์ฌ์ ์ ๋ ๋ค TypeScript baseUrl
์ต์
์ ์ฌ์ฉํ๊ณ ์๋ค๋ ๊ฒ์
๋๋ค. TypeScript์ ํ์ค ๊ฐ์ ธ์ค๊ธฐ์ ํ์คํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
๋๋ Vue 2.5.13์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๋
ธ๋ 8.9.3 ๋ฐ TypeScript 2.6์์ SSR์ ์ค์ ํ๊ณ ์์ต๋๋ค. ๋ด tsconfig.json
๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
{
"compilerOptions": {
"outDir": "build",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"rootDir": "./src",
"baseUrl": "./src",
"noUnusedLocals": true
},
"exclude": [
"node_modules",
"typings"
]
}
compilerOptions.paths
์์ฑ์ ํตํด vue.esm.js ํ์ผ์ ๋ก๋ํ๋ ๊ฒ์ ํฌํจํ์ฌ ์ฌ๋ฌ ๋ค๋ฅธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๋ํ์ง๋ง ์ฑ๊ณตํ์ง ๋ชปํ์ต๋๋ค. OP์ ๋๊ฐ์ vue_1.default is not a constructor
์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
๋๋ ์ฌ๊ธฐ์ ์ฌํ์ ๋ชจ์์ ์๋ก์ด ๋ฌธ์ ๋ฅผ ์ด์์ต๋๋ค.
๋๋ ๊ฐ์ ๋ฌธ์ ์ ๋ถ๋ช์ณค๋ค.
๋ด ์ดํด์์ vue.js ํ์ผ์ vue-class-component์ ๊ฐ์ด exports.default
๋ณ์๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.
vue-class-component.js(192ํ):
exports['default'] = Component$1;
vue-class-component ํ์ผ์๋ Vue์์ ๋๋ฝ๋ ๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํน๋ณํ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก ์๋ํฉ๋๋ค.
vue-class-component.js(12ํ):
Vue = Vue && Vue.hasOwnProperty('default') ? Vue['default'] : Vue;
๋ถํํ๋ TypeScript 2.5 ์ปดํ์ผ๋ฌ๋ ๋๋ฝ๋ ๊ธฐ๋ณธ๊ฐ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ํน๋ณํ ๊ฒฝ์ฐ๋ฅผ ์ถ๋ ฅํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ TypeScript 2.7 ์๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์๋ก์ด ์ต์
( esModuleInterop
)์ด ์์ต๋๋ค.
{
"compilerOptions": {
"module": "amd",
"esModuleInterop": true,
...
},
...
}
esModuleInterop
๋ฅผ true
๋ก ์ค์ ํ๋ฉด ์ถ๋ ฅ์ __importDefault
๋ผ๋ ํจ์๊ฐ ํฌํจ๋์ด ๋๋ฝ๋ ๊ธฐ๋ณธ ์์ฑ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฒ๋ฆฌํฉ๋๋ค.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
define(["require", "exports", "vue"], function (require, exports, vue_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
vue_1 = __importDefault(vue_1);
var app = new vue_1.default({
...
});
});
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๊ฐ์ ๋ฌธ์ ์ ๋ถ๋ช์ณค๋ค.
๋ด ์ดํด์์ vue.js ํ์ผ์ vue-class-component์ ๊ฐ์ด
exports.default
๋ณ์๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.vue-class-component.js(192ํ):
exports['default'] = Component$1;
vue-class-component ํ์ผ์๋ Vue์์ ๋๋ฝ๋ ๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํน๋ณํ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก ์๋ํฉ๋๋ค.
vue-class-component.js(12ํ):
Vue = Vue && Vue.hasOwnProperty('default') ? Vue['default'] : Vue;
๋ถํํ๋ TypeScript 2.5 ์ปดํ์ผ๋ฌ๋ ๋๋ฝ๋ ๊ธฐ๋ณธ๊ฐ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ํน๋ณํ ๊ฒฝ์ฐ๋ฅผ ์ถ๋ ฅํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ TypeScript 2.7 ์๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์๋ก์ด ์ต์ (
esModuleInterop
)์ด ์์ต๋๋ค.esModuleInterop
๋ฅผtrue
๋ก ์ค์ ํ๋ฉด ์ถ๋ ฅ์__importDefault
๋ผ๋ ํจ์๊ฐ ํฌํจ๋์ด ๋๋ฝ๋ ๊ธฐ๋ณธ ์์ฑ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฒ๋ฆฌํฉ๋๋ค.