0

When using vanilla JS

module.js

console.log("modulejs");
const  a =1;
class Test{

}
export  {Test}

index.js

import {Test} from "./module.js"

I get the console.log message just via importing a class.

But when working with parcel on my TypeScript, the console.log is not executed.

J. Doe
  • 522
  • 6
  • 22

1 Answers1

-1

Update (June 2021):

This issue seems to be fixed with the latest versions of parcel2.

See the example above in the Parcel REPL.


What might be happening is that parcel is determining that the Test class import is actually unused in index.js, so it's tree-shaking the module.js entirely.

If you change index.js to:

import { Test } from "./module.js";
const myInstance = new Test();

You should see that it does not get tree-shaken, and the console.log statement is executed.

If the code you want to import is under your control, I wouldn't recommend having side effects like that console.log statements. If it's unavoidable, I think you can tell parcel not to tree-shake either by setting sideEffects: true in your package.json (see docs).

(This answer is assuming you are using Parcel 2).

Andrew Stegmaier
  • 3,429
  • 2
  • 14
  • 26