Open1

rollupでtypescriptを使う

shiratorishiratori

@rollup/plugin-typescriptプラグインを使う

https://www.npmjs.com/package/@rollup/plugin-typescript

設定を追記

rollup.config.js
import typescript from '@rollup/plugin-typescript';

export default {
 + plugins: [typescript()]
};

これでビルドするとtslibが必要と怒られる

src/index.ts → dist/index.js, dist/index.min.js...
[!] (plugin typescript) RollupError: @rollup/plugin-typescript: Could not find module 'tslib', which is required by this plugin. Is it installed?
  ...
 ELIFECYCLE  Command failed with exit code 1.

tslibを入れる

https://www.npmjs.com/package/tslib

ビルドが成功

% pnpm build     
省略
src/index.ts → dist/index.js, dist/index.min.js...
(!) Plugin typescript: @rollup/plugin-typescript TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
created dist/index.js, dist/index.min.js in 769ms

tsconfig.jsonの設定

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["ES2020", "DOM"],
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "emitDeclarationOnly": true,
    "declaration": true,
    "declarationDir": "./types",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"]
}