Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以强制导入依赖项为纯吗?_Javascript_Webpack_Tree Shaking - Fatal编程技术网

Javascript 我可以强制导入依赖项为纯吗?

Javascript 我可以强制导入依赖项为纯吗?,javascript,webpack,tree-shaking,Javascript,Webpack,Tree Shaking,例如,我在我的库中使用了mustache,但它只在可导出函数中使用 从“胡须”导入胡须 导出函数some(){ ... 胡子 ... } 导出函数其他(){ ... } 当我从这个库中只导入other并使用webpack构建包时,webpack包含mustache的代码,因为它认为mustache的代码是不纯粹的 我可以把小胡子导入标记为纯吗?我想当你导入时,Webpack总是会捆绑小胡子 也许您可以将库拆分为多个文件: some.js import mustache from 'mustac

例如,我在我的库中使用了
mustache
,但它只在可导出函数中使用

从“胡须”导入胡须
导出函数some(){
...
胡子
...
}
导出函数其他(){
...
}
当我从这个库中只导入
other
并使用webpack构建包时,webpack包含
mustache
的代码,因为它认为
mustache
的代码是不纯粹的


我可以把
小胡子
导入标记为纯吗?

我想当你导入时,Webpack总是会捆绑
小胡子

也许您可以将库拆分为多个文件:

some.js

import mustache from 'mustache'

export default function some() {
  ...
  mustache
  ...
}
export default function other() {
  ...
}
other.js

import mustache from 'mustache'

export default function some() {
  ...
  mustache
  ...
}
export default function other() {
  ...
}
并创建主
index.js
文件,该文件是默认导出,并结合了上述内容:

import some from './some';
import other from './other';

export default {
  some,
  other,
};
通过这种方式,您的库可以作为一个整体(通过
index.js
)或部分(部分
some.js
other.js
)使用(不包括
mustache


我认为Webpack在导入时总是会捆绑
胡须

也许您可以将库拆分为多个文件:

some.js

import mustache from 'mustache'

export default function some() {
  ...
  mustache
  ...
}
export default function other() {
  ...
}
other.js

import mustache from 'mustache'

export default function some() {
  ...
  mustache
  ...
}
export default function other() {
  ...
}
并创建主
index.js
文件,该文件是默认导出,并结合了上述内容:

import some from './some';
import other from './other';

export default {
  some,
  other,
};
通过这种方式,您的库可以作为一个整体(通过
index.js
)或部分(部分
some.js
other.js
)使用(不包括
mustache