【问题标题】:Imported ES6 module is not executed after processing in parcel导入的ES6模块在包中处理后不执行
【发布时间】:2020-09-21 20:54:32
【问题描述】:

使用原生 JS 时

模块.js

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

}
export  {Test}

index.js

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

我只是通过导入一个类得到了 console.log 消息。

但是在我的 TypeScript 上使用 parcel 时,console.log 没有被执行。

【问题讨论】:

    标签: typescript es6-modules parceljs


    【解决方案1】:

    更新(2021 年 6 月):

    这个问题似乎已经用最新版本的 parcel2 解决了。

    the example above in the Parcel REPL


    可能发生的情况是,parcel 正在确定 Test 类导入实际上在 index.js 中未使用,因此它完全摇晃了 module.js

    如果您将index.js 更改为:

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

    您应该会看到它没有进行 tree-shaking,并且执行了 console.log 语句。

    如果您要导入的代码在您的控制之下,我不建议您使用像 console.log 语句这样的副作用。如果这是不可避免的,我认为您可以通过在您的package.json 中设置sideEffects: true 来告诉parcel 不要摇树(请参阅docs)。

    (此答案假设您使用的是Parcel 2)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2018-11-06
      • 2017-01-04
      • 1970-01-01
      • 2018-11-12
      • 2019-03-27
      • 2016-06-10
      相关资源
      最近更新 更多