简体   繁体   English

与 Rollup 和 rollup-plugin-typescript2 捆绑的 Typescript 变坏了

[英]Typescript bundling with Rollup and rollup-plugin-typescript2 gone bad

I'm trying to extract small part of monorepo to web client library and running into some unexpected hurdles, in here.我正在尝试将 monorepo 的一小部分提取到 Web 客户端库中,并在这里遇到了一些意想不到的障碍。 So, quite straight forward rollup config, like this:因此,非常直接的汇总配置,如下所示:

import resolve from "rollup-plugin-node-resolve";
import typescript from "rollup-plugin-typescript2";
import sourceMaps from 'rollup-plugin-sourcemaps';

export default {
    input: "../main.ts",
    output: {
        file: './bundle/rollup-bundle.js',
        format: 'iife',
        name: 'WebClient',
        sourcemap: true
    },
    plugins: [
        typescript(),
        resolve({browser: true, mainFields: ['module']}),
        sourceMaps(),
    ]
};

Produces:产生:

../main.ts → ./bundle/rollup-bundle.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
..\main.ts (3:32)
1: [...]
2:
3: export const createClient = (url: string) => new ClientImpl([...]);
                                   ^
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)

It just looks like rollup-plugin-typescript2 doesn't do it's job of transpiling typescript and that just seems way too basic to not to work.它只是看起来像rollup-plugin-typescript2 typescript2 并没有完成它的转换打字稿的工作,而且这似乎太基本了,不能工作。 I'm confused!我糊涂了!

Thanks in advance!提前致谢!

It appears that rollup is possibly built with idea, that it will be always located in project root, in mind.看起来rollup可能是用想法构建的,记住它总是位于项目根目录中。 And once I attempted to place web client builder as a separate app in it's dedicated folder with it's dedicated tsconfig and rollup.config it all gone mad with no clear indications on why that was taking place.一旦我尝试将 Web 客户端构建器作为一个单独的app放置在它的专用文件夹中,它有专用的 tsconfig 和 rollup.config,这一切都变得疯狂,没有明确说明为什么会发生这种情况。 Only obvious indication is that rollup-plugin-typescript2 isn't capable to transpile (it's not even attempting to) any *.ts files located in higher folders and it took hell of a time to find this out.唯一明显的迹象是rollup-plugin-typescript2 typescript2 无法转译(它甚至没有尝试)位于更高文件夹中的任何 *.ts 文件,并且花了很长时间才发现这一点。

This is probably caused by typescript2 and node-resolve being called in the wrong order:这可能是由typescript2node-resolve以错误的顺序调用引起的:

[@rollup/plugin-node-resolve] Must be before rollup-plugin-typescript2 in the plugin list, especially when browser: true option is used [@rollup/plugin-node-resolve] 插件列表中必须在 rollup-plugin-typescript2 之前,尤其是当使用browser: true选项时

(from rollup-plugin-typescript2 's documentation ) (来自rollup-plugin-typescript2文档


Update更新

The main issue turned out to be the positioning of some source files in higher folders with respect to the tsconfig root.结果证明,主要问题是将某些源文件放置在相对于 tsconfig 根目录的更高文件夹中。 By default tsconfig excluded them from the compilation:默认情况下 tsconfig 将它们从编译中排除:

If the "files" and "include" are both left unspecified, the compiler defaults to including all TypeScript (.ts, .d.ts and .tsx) files in the containing directory and subdirectories如果 "files" 和 "include" 都未指定,则编译器默认将所有 TypeScript(.ts、.d.ts 和 .tsx)文件包含包含目录和子目录中

( tsconfig docs ) ( tsconfig 文档)

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

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