简体   繁体   English

运行 tsc(TypeScript 编译器)时如何将 package.json 复制到 dist 或 build 文件夹中

[英]How to copy package.json in dist or build folder when running tsc (TypeScript compiler)

I have a TypeScript React component that uses a package.json file (see screenshot) and I run tsc in order to transpile it to es5 in my dist folder but the package.json file doesn't get copied.我有一个使用package.json文件的 TypeScript React 组件(参见屏幕截图),我运行tsc以将其转换为dist文件夹中的 es5,但package.json文件没有被复制。 Please note that in the screenshot I manually copied it in the dist folder.请注意,在屏幕截图中,我手动将其复制到了dist文件夹中。

组件截图

So my question: Is there a way to do this via tsc/tsconfig ... without having to add a copy script (ran via yarn build ).所以我的问题:有没有办法通过tsc/tsconfig做到这tsc/tsconfig ......而不必添加复制脚本(通过yarn build )。 Because I would like this to be updated also when running tsc --watch因为我希望在运行tsc --watch时也能更新

Also I do NOT wish to rename my Component.tsx to index.tsx in the component folders.此外,我不希望在组件文件夹中将我的 Component.tsx 重命名为 index.tsx。 I don't want to have 200 index.tsx files in my project and using the package.json 's main allow me to have import SomeComponent from './components/SomeComponent' instead of doing import SomeComponent from './components/SomeComponent/SomeComponent' so it's great.我不想在我的项目中有 200 个 index.tsx 文件,并且使用package.jsonmain允许我import SomeComponent from './components/SomeComponent' import SomeComponent from './components/SomeComponent/SomeComponent'而不是import SomeComponent from './components/SomeComponent' import SomeComponent from './components/SomeComponent/SomeComponent'所以它很棒。

Here is my tsconfig file:这是我的tsconfig文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "declaration": true,
    "jsx": "react"
  },
  "include": ["src"]
}

Many thanks for your time and your help or suggestions.非常感谢您的时间和您的帮助或建议。

Just include it in your tsconfig.json file and enable the resolveJsonModule compiler option.只需将它包含在您的tsconfig.json文件中并启用resolveJsonModule编译器选项。 You can do this by merging the following config into your tsconfig.json file:您可以通过将以下配置合并到您的tsconfig.json文件中来做到这一点:

{
    "compilerOptions": {
        "resolveJsonModule": true
    },
    "include": ["./package.json"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM