gpt4 book ai didi

typescript - slim 的 : imported typescript files not recognized

转载 作者:行者123 更新时间:2023-12-03 23:01:33 28 4
gpt4 key购买 nike

所以...
我正在尝试使用 Rollup 构建一个带有 Svelte 和 Typescript 的应用程序,当我尝试构建我的 Svelte 组件时,我似乎无法让它编译我的 .ts.svelte 包含的文件成分。
我不断收到此错误:

[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/ui/pages/files-page/utils/mapPaths.ts (1:12)
1: import type { PathMapping } from '~/types/path.d';
^
这是我的 FilesPage.svelte包括 mapPaths.ts文件:
<script lang="ts">
import FileList from '~/ui/layouts/file-list/FileList.svelte';

import mapPaths from './utils/mapPaths';

export let paths: string[] = [];

$: mappedPaths = mapPaths(paths);
</script>

<FileList paths={mappedPaths} />
和我的 mapPaths.ts文件:
import type { PathMapping } from '~/types/path.d';

export default (paths: string[]): PathMapping => {
const mapping = paths.reduce(
(m, path) => {
const root = path.replace(/_\d+$/, '');
m.set(root, (m.get(root) || 0) + 1);
return m;
},
new Map() as Map<string, number>
);

return Array.from(mapping.entries());
};
这是我的 rollup.config.js :
import typescript from '@rollup/plugin-typescript';
import nodeResolve from '@rollup/plugin-node-resolve';
import alias from '@rollup/plugin-alias';
import commonjs from 'rollup-plugin-commonjs';
import svelte from 'rollup-plugin-svelte';
import sveltePreprocess from 'svelte-preprocess';

export default {
input: 'src/web.ts',

output: {
sourcemap: false,
format: 'iife',
name: 'app',
file: 'build/web.js'
},

plugins: [
svelte({
preprocess: sveltePreprocess(),
emitCss: false
}),

alias({
entries: [
{ find: '~', replacement: 'src' }
]
}),

nodeResolve({
dedupe: ['svelte']
}),

commonjs(),

typescript()
]
};
以及我的 tsconfig.json :
{
"extends": "@tsconfig/svelte/tsconfig.json",
"include": [
"src/**/*"
],
"exclude": [
"node_modules/*"
],
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"module": "es2020",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"~/*": [
"src/*"
]
}
}
}
我一直在玩插件的顺序,但无济于事。据我所知,它们应该按照推荐的顺序排列(除了 alias )。
非常感谢任何帮助,因为我对拒绝工作的构建有点疯狂。

最佳答案

我遇到了同样的问题。这似乎是一个复杂的错误,但这是为我解决的问题。 This comment in the rollup github说:

Quick fix for now is to manually set your rootDir compiler option to"src".


因此,我将此标志添加到 rollup.config.js 中 typescript 插件中的 ts 编译器选项(当然,我的源目录也称为 src,您可以更改它以适合您的目录结构):
plugins: [
...
typescript({
...
rootDir: './src',
}),
...
],
不幸的是,我不能保证它也适用于你,因为它是一个快速修复。问题 seems to be fixed in newer versions of typescript如果更新也是一种选择。

关于typescript - slim 的 : imported typescript files not recognized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65417201/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com