-1

I create a typescript project and use Rollup to build it, but error:

1

and then I find this bug in the builded file carefully, and discover the class Text has defined before class Carobj

var newcar = (function (exports){
  // ......
  class Text extends Carobj {
    // ......
  }
  
  class Carobj {
    // ......
  }
})({})

So how to make class Carobj in front of Text when use Rollup to build project?

I wanna Rollup can use right order to build codes.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Acbox Sky
  • 3
  • 2
  • How do your source files look? Especially their imports and exports? – Bergi Feb 18 '23 at 08:41
  • Does your project work if you execute the modules directly without bundling? Do you have any circular dependencies? – Bergi Feb 18 '23 at 08:43
  • @Bergi The source code about this bug is a little huge, so if you don't mind, visit https://github.com/Bug-Duck/newcar/tree/develop. and I use rollup to build `packages/core`, The export code was in index.ts – Acbox Sky Feb 18 '23 at 09:00
  • I found the [*index.ts*](https://github.com/Bug-Duck/newcar/blob/develop/packages/core/src/index.ts) that you are using as the entry point, but where are `CarObj` and `Text`? All you need to add to your question are the `import` and `export` declarations of the involved modules, you can omit the body of all the `class` definitions (just how you did it in the snippet of the output). – Bergi Feb 19 '23 at 00:05
  • @Bergi `Carobj` and `Text` is in the `packages/objects`, I use pnpm to create a monorepo, in `packages/core`, the `import` and `export` declaration like this: `import {Carobj} from "@newcar/core" // ...... export {Carobj}; export {Text} from "@newcar/objects"` – Acbox Sky Feb 19 '23 at 03:45
  • 1
    You can [edit] your question to include those details as formatted code. – Bergi Feb 19 '23 at 04:15

1 Answers1

1

You have a circular dependency between the module exporting CarObj and re-export Text (index.ts) and the module importing CarObj and exporting Text (text.ts).

When you attempt to import anything from index.ts (like your entrypoint does) - it doesn't matter whether that's CarObj or Text or nothing - then it will first load index.ts, then load its dependencies, including text.ts, which in turn loads its dependencies that were not already loaded. After all variables are declared and imports/exports are set up, when all of text.ts' dependencies are met - again excluding index.ts which is already in the process of being imported - it is executed, attempting to initialise the class Text, and runs into an error because the CarObj it attempts to extend is still in the temporal dead zone. If it had finished, the index.ts module could have been executed after all its dependencies were met, and so on, back until the entry point.

So this is not a problem of rollup, it just bundles the code in the same order, with the same scopes, as it would have been executed with individual modules. Which leads to the same exception.

The fix is simple: put the class CarObj into a separate module, and import only that in text.ts, not the index.ts that imports text.ts.

// index.ts
export { CarObj } from "./lib/obj";
export { Text } from "./lib/text";
// lib/obj.ts
export class CarObj {
  …
}
// lib/text.ts
import { CarObj } from "./obj";
//                     ^^^^^^^ - not "../index"!
export class Text extends CarObj {
  …
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • But I need use `Carobj` as a type, so I must import it in `index.ts`, what should I do? – Acbox Sky Feb 19 '23 at 06:59
  • Sure you can import and re-export the `CarObj` in *index.ts*, you just must not import that one into the *text.ts*. (Or you can, if it's only `import type …`, which will get stripped out and not actually loaded/executed). – Bergi Feb 19 '23 at 07:08
  • Okay, now the bug has been fixed! – Acbox Sky Feb 19 '23 at 07:32