typescript νλ‘μ νΈμ κ²½λ‘ λ³μΉμ μ μ© ν λ κ²½λ‘λ λ΄ λ³΄λΈ μλ° μ€ν¬λ¦½νΈ νμΌμμ νμΈλμ§λ§ μ μΈ νμΌμμλ νμΈλμ§ μμ΅λλ€.
λ€μ νλ‘μ νΈμμλ ~/*
λ³μΉμ ./src/*
. κΈ°λ³Έ λλ ν 리μμ μ λ κ²½λ‘λ₯Ό μ¬μ©νμ¬ κ°μ Έ μ€κΈ°λ₯Ό νμ©ν©λλ€.
https://github.com/alshdavid-sandbox/rollup-typescript-library
npm install && make
cat dist/a/index.d.ts | grep "~"
cat
λ μ무κ²λ μΈμνμ§ μμ§λ§ νμ¬ μΈμλ©λλ€.
import { B } from '~/b';
μ΄κ²μ μ μΈ νμΌμ κ²½λ‘ νμΈμ μ μ©νμ§ μμμ 보μ¬μ€λλ€.
typescript μ¬λλ€μ λ°λ₯΄λ©΄, λΉμ μ κ·Έκ²μ κ±°κΎΈλ‘ μ¬μ©νκ³ μμ΅λλ€.
μ°λ¦¬μ μΌλ°μ μΈ κ²¬ν΄λ TSμ λν΄ μ¦μ μλνλ κ°μ Έ μ€κΈ°λ₯Ό μμ±νκ³ μλνλ λμ λ°νμμ μλνλ κ°μ Έ μ€κΈ° κ²½λ‘λ₯Ό μμ±νκ³ μ»΄νμΌλ¬μ λͺ¨λ νμΈ λ¨κ³λ₯Ό μΆ©μ‘±νλλ‘ TS νλκ·Έλ₯Ό μ€μ ν΄μΌνλ€λ κ²μ λλ€. λ°νμμ μλνλ κ²½λ‘λ₯Ό "μμ "νλ λ€λ₯Έ λ¨κ³κ° μμ΅λλ€.
https://github.com/microsoft/TypeScript/issues/15479
νμ¬μ΄ νλ¬κ·ΈμΈμ typescriptμ μν΄ λ°©μΆλλ©΄ μ ν μ μΈμ μ²λ¦¬νκ±°λ ꡬ문 λΆμνμ§ μμ΅λλ€.
κ²μ / λ°κΎΈκΈ°λ³΄λ€ μ½κ° λ λμ μμ μ μν ν μ μμ§λ§ μΌλ°μ μΌλ‘μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ €λ©΄ κΈ°λ³Έμ μΌλ‘ μ ν μ μΈμ λν λ‘€μ μ λ€μ ꡬνν΄μΌν©λλ€ (λ΄κ° λ³΄μ§ μλ λμ λ²λ€ μ μΈμ μμ±νκΈ° μν΄ μΈμ΄ μλΉμ€ APIλ₯Ό μ΄λ―Έ μΆκ°νμ§ μμ κ²½μ°).
λ΅μ₯μ λ³΄λ΄ μ£Όμ μ κ°μ¬ν©λλ€. μ‘°μ‘ν κ²μ / κ΅μ²΄ ν μ²λ¦¬ λ¨κ³λ₯Ό μ°κ²°νλ λ° μ¬μ©ν μμλ κ°μ λͺ¨λμ λΉλ λͺ¨λ λͺ¨λμμ μ€νλλ "ν¬μ€νΈ λ°©μΆ"νν¬κ° μμ΅λκΉ?
d.ts νμΌμ λ‘€μ
νμ΄ν λΌμΈμΌλ‘ μ§μ λ°©μΆλλ―λ‘ ( useTsconfigDeclarationDir: false
μ΅μ
μ΄ μ¬μ©λλ κ²½μ°)μ΄λ₯Ό λ³ννλ λ‘€μ
νλ¬κ·ΈμΈμ λ§λ€ μ μμ΅λλ€.
λ²λ€μ΄ μμ± λ λ λ°©μΆλλ―λ‘ transform
λμ generateBundle
νν¬λ₯Ό μ¬μ©ν΄μΌ ν μλ μμ΅λλ€.
μλνμ§ μμΌλ©΄. rpt2 μ체μ μ¬μ μ°κΈ° νν¬λ₯Ό μΆκ° ν μ μμ΅λλ€.
λμΌν λ¬Έμ κ° λ°μνμ¬ μ¬κΈ° μμ ν΄κ²° λ°©λ²μ μ°Ύμμ΅λλ€
νμ¬ ttsc
λ₯Ό typescript-transform-paths
ttsc
μ ν¨κ» μ¬μ©νμ¬ λ‘€μ
λ ts νμΌμ λ‘€μ
ν ν μ μΈ νμΌμ μμ±ν©λλ€. μΌμ’
μ μ€λ³΅μ΄μ§λ§ μλν©λλ€ ...
μ, ttscλ₯Ό μ¬μ©νμ¬ μ΄κ²μ λ¬μ±νκ³ μμ΅λλ€. rollup-plugin-typescript2 transformers
μ΅μ
μΌλ‘ νΈλμ€ν¬λ¨Έλ₯Ό μ§μ μλμν¬ μ μμκΈ° λλ¬Έμ ttsc
λ₯Ό μ¬μ©νκΈ°λ‘νμ΅λλ€.
λ΄ λ‘€μ ꡬμ±μ λ€μκ³Ό κ°μ΅λλ€.
import typescript from 'rollup-plugin-typescript2'
export default {
input: `src/index.ts`,
preserveModules: true,
output: {
format: 'esm',
dir: './dist'
},
external: [],
watch: {
include: 'src/**',
},
plugins: [
typescript({
typescript: require('ttypescript'),
tsconfigDefaults: {
compilerOptions: {
plugins: [
{ "transform": "typescript-transform-paths" },
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
}
}
}),
],
}
κ°μ¬ν©λλ€ @alshdavid ! λΉμ μ λ΄ μλͺ
μ ꡬνμ΅λλ€! μ΄μ λλ κ°μ λ¬Έμ κ° μμκ³ typescript-transform-paths
λ₯Ό μ¬μ©νμ¬ ν΄κ²°νμ΅λλ€! κ°μ¬ν©λλ€ πͺ
// webpack.config.js
module: {
rules: [
{
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader'),
options: {
compiler: 'ttypescript',
},
},
{
loader: require.resolve('react-docgen-typescript-loader'),
},
],
exclude: /(node_modules)/,
},
],
},
// tsconfig.json
compilerOptions: {
"plugins": [
{ "transform": "typescript-transform-paths" },
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
}
μ΄ μ루μ μ κ³Όκ±°μ μ μκ² ν¨κ³Όμ μ΄μμ§λ§ μ§κΈμ λ€μκ³Ό κ°μ μ€λ₯κ° λ°μνλ λ§μ μν©μ μ°Ύκ³ μμ΅λλ€.
UnhandledPromiseRejectionWarning : TypeError : μ μλμ§ μμ 'text'μμ±μ μ½μ μ μμ΅λλ€.
λ³μΉ (λ° μ΄λ¬ν νλ¬κ·ΈμΈ) μ¬μ©μμ λ²μ΄λλ©΄ λ¬Έμ μμ΄ μλν©λλ€. λ€λ₯Έ μ¬λμ΄μ΄ λ¬Έμ κ° μμ΅λκΉ?
μ΄ μ루μ μ κ³Όκ±°μ μ μκ² ν¨κ³Όμ μ΄μμ§λ§ μ§κΈμ λ€μκ³Ό κ°μ μ€λ₯κ° λ°μνλ λ§μ μν©μ μ°Ύκ³ μμ΅λλ€.
UnhandledPromiseRejectionWarning : TypeError : μ μλμ§ μμ 'text'μμ±μ μ½μ μ μμ΅λλ€.
λ³μΉ (λ° μ΄λ¬ν νλ¬κ·ΈμΈ) μ¬μ©μμ λ²μ΄λλ©΄ λ¬Έμ μμ΄ μλν©λλ€. λ€λ₯Έ μ¬λμ΄μ΄ λ¬Έμ κ° μμ΅λκΉ?
κ°μ μ€λ₯κ° μμ΅λλ€
μ΄ μ루μ μ κ³Όκ±°μ μ μκ² ν¨κ³Όμ μ΄μμ§λ§ μ§κΈμ λ€μκ³Ό κ°μ μ€λ₯κ° λ°μνλ λ§μ μν©μ μ°Ύκ³ μμ΅λλ€.
UnhandledPromiseRejectionWarning : TypeError : μ μλμ§ μμ 'text'μμ±μ μ½μ μ μμ΅λλ€.
λ³μΉ (λ° μ΄λ¬ν νλ¬κ·ΈμΈ) μ¬μ©μμ λ²μ΄λλ©΄ λ¬Έμ μμ΄ μλν©λλ€. λ€λ₯Έ μ¬λμ΄μ΄ λ¬Έμ κ° μμ΅λκΉ?
μλ
νμΈμ, μ λ λμΌν λ¬Έμ λ₯Ό λ°μμν€κ³ μμ§λ§ λ€λ₯Έ λ§₯λ½μμ : Vue λ° Typescriptλ‘ μμ±λ κ΅¬μ± μμ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ²λ€λ‘ λ¬ΆκΈ° μν΄ λ‘€μ
μ μ¬μ©νκ³ μμ΅λλ€. λ€μμ rollup.config.js
νμΌμ
λλ€.
import path from "path";
import alias from "@rollup/plugin-alias";
import cleaner from "rollup-plugin-cleaner";
import css from "rollup-plugin-css-only";
import scss from "rollup-plugin-scss";
import typescript from "rollup-plugin-typescript2";
import vue from "rollup-plugin-vue";
export default [
{
input: "./src/index.ts",
preserveModules: true,
output: {
format: "esm",
dist: "./dist"
},
plugins: [
cleaner({ targets: ["dist/"] }),
alias({
resolve: [".ts", ".vue"],
entries: { "@/": path.resolve(__dirname, "./src/") }
}),
css({ output: "dist/bundle.css" }),
scss(),
typescript({
typescript: require("ttypescript"),
tsconfigDefaults: {
compilerOptions: {
plugins: [
{ transform: "typescript-transform-paths" },
{
transform: "typescript-transform-paths",
afterDeclarations: true
}
]
}
}
}),
vue({ css: false })
]
}
];
λ³μΉμ ν΄κ²°νκΈ° μν΄ rollup --config
ttsc --emitDeclarationOnly
λͺ
λ Ή λ€μμ μ΄λ―Έ .vue.d.ts
νμΌμ μ²λ¦¬λμ§ μμ΅λλ€ (λ¬Όλ‘ ).
ttypescript
λ° typescript-transofrm-paths
κ³Ό κ°μ λ¬Έμ μ΄μ§λ§ μλνμ§ μμμ΅λλ€. λΉλλ₯Ό μ€νν λ :
[!] (plugin rpt2) TypeError: Cannot read property 'text' of undefined
νμ§λ§ @zerollup/ts-transform-paths
λ° ttypescript
νλ©΄ μλν©λλ€ π
μ¬κΈ°μ λ©μ§ μ€μ μ΄ μμ΅λλ€ : https://www.npmjs.com/package/@zerollup/ts -transform-paths # setup-for-rollup-plugin-typescript2
_rollup.config.js_
import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'
export default {
input: 'src/lib.ts',
output: [{ file : 'dist/lib.js', name : 'mylib', format : 'iife', sourcemap : true }],
plugins: [
tsPlugin({
typescript: ttypescript
})
]
}
_tsconfig.json_
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"my-lib/*": ["src/*"]
},
"plugins": [
{
"transform": "@zerollup/ts-transform-paths",
"exclude": ["*"]
}
]
}
}
ν°! λ€λ₯Έ λ°©λ²μΌλ‘ ν΄κ²°νμ§λ§μ΄ νλ¬κ·ΈμΈκ³Ό κ΄λ ¨μ΄ μμ΅λλ€. typescript-transform-path λ° ttypescriptλ₯Ό μ κ±°νκ³ νμ¬ νλ¬κ·ΈμΈμ κ·Έλλ‘ λμμ΅λλ€ (typescript ()). λ‘€μ ν tsc-aliasλ₯Ό μ¬μ©νμ¬ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€. μμ± ν .d.ts νμΌμ μ²λ¦¬νκΈ° λλ¬Έμ vue νμΌλ λ체ν©λλ€.
λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ λ‘€μ ν
tsc-alias
λ₯Ό μ¬μ©νμ΅λλ€. μμ± ν .d.ts νμΌμ μ²λ¦¬νκΈ° λλ¬Έμ vue νμΌλ λ체ν©λλ€.
@mantlebee tsconfig.json
λ° λ‘€μ
ꡬμ±μ μλ₯Ό κ²μ ν΄ μ£Όμκ² μ΅λκΉ?
νμ§λ§
@zerollup/ts-transform-paths
λ°ttypescript
νλ©΄ μλν©λλ€ π
@luanorlandi κ° λ°νμμ ts-transform-path
μλνμ§ μμ΅λκΉ? μλ§λ λΌμ΄λΈλ¬λ¦¬ μ½λμ λν μ’μ μκ°μ μλ κ²μ
λλ€.
λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ λ‘€μ ν
tsc-alias
λ₯Ό μ¬μ©νμ΅λλ€. μμ± ν .d.ts νμΌμ μ²λ¦¬νκΈ° λλ¬Έμ vue νμΌλ λ체ν©λλ€.@mantlebee
tsconfig.json
λ° λ‘€μ ꡬμ±μ μλ₯Ό κ²μ ν΄ μ£Όμκ² μ΅λκΉ?
μ€μν μμ±μ declaration
, declarationDir
λ° outDir
// tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"declaration": true,
"declarationDir": "dist/src",
"baseUrl": ".",
"outDir": "dist/src",
"types": [
"webpack-env",
"jest"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
κ°λ¨ν rollup.config.js
νμΌ
import path from "path";
import alias from "@rollup/plugin-alias";
import cleaner from "rollup-plugin-cleaner";
import scss from "rollup-plugin-scss";
import typescript from "rollup-plugin-typescript2";
import vue from "rollup-plugin-vue";
export default [
{
input: "src/index.ts",
output: {
format: "esm",
file: "dist/elegere-ui.js"
},
plugins: [
cleaner({ targets: ["dist/"] }),
alias({
resolve: [".ts", ".vue"],
entries: { "@/": path.resolve(__dirname, "src/") }
}),
scss(),
typescript({
useTsconfigDeclarationDir: true
}),
vue()
//{ css: false }
]
}
];
κ·Έλ° λ€μ λͺ¨λ κ²μ prepublishOnly
λͺ
λ Ήμ λ£μ΅λλ€ (λμκ² μ μ©νκΈ° λλ¬Έμ)
"prepublishOnly": "npm run rollup && tsc-alias && npm run docs"
TSC-ALIASλ dist/src
ν΄λ λ΄μμ κ΅μ²΄λ₯Ό μνν©λλ€. ts-aliasκ° src ν΄λ μμ μλ€κ³ μκ°νλ λ³μΉμ λ체νκΈ° λλ¬Έμ λͺ¨λ κ²μ κ·Έ μμ λ£κΈ°λ‘ μ νν©λλ€.
μ΅μ μ κ²°λ‘ μ μλμ§λ§ μλν©λλ€. μ΅λν 빨리 @luanorlandi μ μ루μ μ νμΈ
λ¬Όλ‘ jest
μ κ°μ dist/src
ν΄λλ₯Ό νΌνλλ‘ λ€λ₯Έ λͺ¨λ libλ₯Ό ꡬμ±ν΄μΌν©λλ€.
νμ§λ§
@zerollup/ts-transform-paths
λ°ttypescript
νλ©΄ μλν©λλ€ π@luanorlandi κ° λ°νμμ
ts-transform-path
μλνμ§ μμ΅λκΉ? μλ§λ λΌμ΄λΈλ¬λ¦¬ μ½λμ λν μ’μ μκ°μ μλ κ²μ λλ€.
λλ κ·Έλ κ² μκ°νμ§ μλλ€. λ³νκΈ°λ λΌμ΄λΈλ¬λ¦¬κ° μ€λͺ νλ―μ΄ λΉλ λ¨κ³λ§μμν κ²μ΄λ€.
_... tsconfig baseUrl + λ²λ€ λ° μ μΈ νμΌμμ λ³μΉ μ¬ μμ± κ²½λ‘. κ·Έλ€ λͺ¨λλ transpiled jsμ d.ts νμΌμμ relativeλ‘ λ€μ μμ±λ©λλ€.
https://www.npmjs.com/package/@zerollup/ts -transform-paths
κ°μ₯ μ μ©ν λκΈ
μ, ttscλ₯Ό μ¬μ©νμ¬ μ΄κ²μ λ¬μ±νκ³ μμ΅λλ€. rollup-plugin-typescript2
transformers
μ΅μ μΌλ‘ νΈλμ€ν¬λ¨Έλ₯Ό μ§μ μλμν¬ μ μμκΈ° λλ¬Έμttsc
λ₯Ό μ¬μ©νκΈ°λ‘νμ΅λλ€.λ΄ λ‘€μ ꡬμ±μ λ€μκ³Ό κ°μ΅λλ€.