javascript - 我可以强制导入依赖为纯吗?
问题描述
例如,我mustache
在我的库中使用,但它仅用于可导出功能
import mustache from 'mustache'
export function some() {
...
mustache
...
}
export function other() {
...
}
当我只other
从这个库导入并使用 webpack 构建包时,webpack 包含mustache
的代码,因为它认为什么代码mustache
不纯。
我可以以某种方式将mustache
导入标记为纯吗?
解决方案
mustache
我认为 Webpack在您导入时总是会捆绑。
也许您可以将库拆分为几个文件:
some.js
import mustache from 'mustache'
export default function some() {
...
mustache
...
}
和other.js
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
)。
// you can use the whole library as
import library from 'library';
// or you can use any part, for example 'other' (without mustache)
import other from 'library/other';
推荐阅读
- python - 如何在 django 的管理页面中添加新用户?
- windows - 无法在自定义操作 ExeCommand 中展开属性 SourceDir
- php - 创建新的空白斜杠 (/) 路由时,Symfony 3.4 应用程序内的无限 HTTP 301 重定向循环
- azure-devops - 是否可以使用部署连接来移动文件或其他操作?
- node.js - Mern 堆栈应用程序在本地 heroku 上工作,但在部署时无法在线工作
- javascript - 使用 Javascript 或 Jquery 动态生成表单是否安全?
- javascript - 我可以将变量传递给ajax查询吗?
- php - 如何将php字符串转换为jquery数组?
- python - 在 for 循环之外访问多个动态变量 - Python 中的全局关键字
- oracle - Oracle Forms Clear Record 在When-Validate-Record 触发器中