如何从项目文件夹外部导入打字稿文件

时间:2020-09-15 10:09:35

标签: javascript typescript svelte rollup svelte-3

所以我正在制作一个精巧的打字稿库,文件夹结构如下

|- dist/
|- src/
  |- components/
  |- index.ts
|- tests/ 
  |- src/
  |- components/
  |- index.ts
  |- package.json
  |- tsconfig.json
  |- rollup.config.js
|- package.json
|- tsconfig.json
|- rollup.config.js

tests文件夹中,我有一个单独的苗条打字稿项目来测试该库,并且我试图将文件从tests文件夹之外直接导入到tests项目中,我这个错误

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
..\src\utilities\navbarStore.ts (1:12)
1: import type { NavThemes } from "src/interfaces";
               ^
2: import { writable } from "svelte/store";

我已经尝试了几种方法,例如在tests项目tsconfig.json中使用路径并添加rootDirs,仍然不能解决问题,有人知道如何解决此问题吗?谢谢您的帮助

这是主项目的tsconfig.json:

{
    "include": ["src/**/*"],
    "exclude": ["node_modules/*", "public/*", "tests/*", "docs/*", "demo/*"],
    "compilerOptions": {
        "baseUrl": ".",
        "lib": ["es2017", "dom"],
        "target": "es2017",
        "module": "esnext",
        "sourceMap": true,
        "rootDir": ".",
        "composite": true,
        "noEmitOnError": true,
        "noErrorTruncation": true,
        "declaration": true,
        "declarationMap": true,
        "noEmit": false,
        "strict": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "types": ["svelte", "node"],
        "allowSyntheticDefaultImports": true,
        "importsNotUsedAsValues": "error",
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "esModuleInterop": true
    }
}

这是tests项目tsconfig.json:

{
    "include": ["src/**/*", "../src/**/*"],
    "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
    "compilerOptions": {
        "lib": ["es2017", "dom"],
        "target": "es2017",
        "noEmitOnError": true,
        "noErrorTruncation": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "types": ["svelte", "node"],
        "forceConsistentCasingInFileNames": true,
        "noImplicitThis": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "rootDirs": [".", "../"],
        "baseUrl": ".",
        "paths": {
            "svelteprojectts/*": ["../src/*"],
            "svelteprojectjs/*": ["../dist/es/*"]
        }
    }
}

0 个答案:

没有答案