当死代码引用了不存在的模块时,使用webpack进行构建会失败-这是不可避免的行为吗? index.js package.json someFunc.js someOtherFunc.js index.js

我正在环境之间共享一些代码,并且在一个环境中不存在依赖项-但是该代码没有导入到项目中,并且模块使用sideEffects: false-我认为我应该能够来构建项目,但是如果我错了,我对摇树和sideEffects有什么误解?

我得到的错误是: Result of build attempt: Module not found: Error: Can't resolve 'unused-dep' in '...'

我将示例代码分为两部分:

  1. my-project-我从my-module导入函数并通过webpack构建的主要代码库
  2. my-module-我制作的模块,试图从中导入功能

https://webpack.js.org/guides/tree-shaking/说,@babel/preset-env的默认行为可能会造成干扰,因此我已经在文档中解决了这一问题以及其他要点。我猜我只是误解了局限性?


我的项目:

index.js

import { someFunc } from 'my-module'

someFunc();

我的模块:

package.json

{
  "sideEffects": false,"name": "my-module","version": "1.0.0","description": "","main": "src/index.js","author": "","license": "","dependencies": {
    "unused-dep": "*"
  }
}

someFunc.js

const someFunc = () => {}

someOtherFunc.js

import { someDep } from 'unused-dep'
const someOtherFunc = () => { 
  someDep()
}

index.js

export * from './someFunc'
export * from './someOtherFunc'
shanlinghai 回答:当死代码引用了不存在的模块时,使用webpack进行构建会失败-这是不可避免的行为吗? index.js package.json someFunc.js someOtherFunc.js index.js

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3121163.html

大家都在问