san*_*der 5 typescript rollupjs svelte
我有一个自定义依赖项,它是一个.ts
文件。它包含enums
,interfaces
和consts
,我像这样导入:
import type { inteface1, interface2} from "common";
Run Code Online (Sandbox Code Playgroud)
这工作完全正常,编译器不会给我任何错误。
如果我尝试从相同的依赖项进行导入,如下所示:
import { paths } from "common";
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
[!] 错误:意外的标记(请注意,您需要插件来导入非 JavaScript 的文件)node_modules/common/types.ts (1:7)
这两个导入都来自同一个文件,但由于某种原因,尝试导入 aconst
或enum
不起作用,而导入 ainterface
有效。唯一的区别似乎是 import 时interface
有关键字type
。
rollup.config.js
typescript({
sourceMap: !production,
rootDir: "./src",
exclude: ['node_modules/**']
}),
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"extends": "@tsconfig/svelte/tsconfig.json",
"include": ["src/**/*"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*"],
}
Run Code Online (Sandbox Code Playgroud)
我正在使用命令运行
rollup -c -w
Run Code Online (Sandbox Code Playgroud)
版本:
"@rollup/plugin-typescript": "^9.0.0",
Run Code Online (Sandbox Code Playgroud)
我正在导入的文件 ( types.ts
):
export interface interface1{
field1: string
field2: string
}
export interface interface2{
status: "OK" | "NOK",
field3: string;
}
export const paths = {
path1: "/path1"
}
Run Code Online (Sandbox Code Playgroud)
这是依赖项中的 package.json
{
"name": "common",
"version": "1.0.0",
"description": "",
"main": "types.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.8.3"
}
}
Run Code Online (Sandbox Code Playgroud)
长话短说,我做错了什么。经过各种测试后,这最终对我有用:
我index.ts
在项目的根目录中添加了一个文件,该文件显式导出我所需的依赖项,如下所示:
export {interface1} from "./types";
Run Code Online (Sandbox Code Playgroud)
添加了一个tsconfig.json
{
"compilerOptions": {
"strict": true,
"module": "ES6",
"target": "ES6",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"declaration": true,
"outDir": "./dist/lib/es6",
"moduleResolution": "node"
},
"include": ["src/**/*"]
}
Run Code Online (Sandbox Code Playgroud)
现在,当我在根目录中运行npx tsc
命令时,它将创建一个/dist/lib/es6
包含已编译的 javascript 的文件夹。
在依赖项目中,我修改了 package.json 以引用编译后的 javascript:
"main": "./dist/lib/es6/index.js",
"types": "./dist/lib/es6/index.d.ts",
Run Code Online (Sandbox Code Playgroud)
我仍然收到这样的警告,但到目前为止没有错误。
(!) `this` has been rewritten to `undefined`
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4542 次 |
最近记录: |