reactjs - How do I uglify code when building Gatsby sites?
问题描述
I'm deploying with Gatsby and netlify, however I can see the complete pristine code in the Sources tab. I wish I could uglify this and make this a bit more obscure. Is there a way to uglify this webpack
folder? Thanks!
IMAGE: Code showing in webpack folder after build
PS: Even the official Gatsby website has the pristine code appearing, not sure if this is their design choice to make it more accessible or not.
Same for reactjs.org:
Same for Airbnb.io, with TODO comments:
Most sites you see in the Gatsby Showcase expose their code in webpack/./src
.
解决方案
您可以添加自定义 webpack 配置以在生产中禁用源映射。
1 - 删除/public
文件夹,以确保删除之前创建的源映射
2-将以下内容添加到您的gatsby-node.js
文件中:
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage === 'build-javascript') {
actions.setWebpackConfig({
devtool: false
})
}
};
或者,如果您更愿意使用 gatsby 插件,您可以使用: https ://www.gatsbyjs.org/packages/gatsby-plugin-no-sourcemaps/基本上做同样的事情。
(看看它的作用https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-no-sourcemaps/gatsby-node.js)
确保先删除/public
文件夹,然后运行gatsby build
.
了解更多:
推荐阅读
- mysql - Sql查询确定未分配时间
- python - 如何绘制 DataFrame 中包含的特定日期的时间序列数据,可能导致单个记录
- php - 如何在 wpdb 中编写正确的查询
- tortoisegit - TortoiseGit 中的修订图和 git log 中的分支图有什么区别?
- javascript - Onchange 选择选项在另一个选择选项上显示结果
- snowflake-cloud-data-platform - 从在 AWS EC2 上运行的 JupyterHub 使用 SSO 连接到 Snowflake
- salesforce - SFCC - 帮助将所有产品从 Salesforce Commerce Cloud (Demandware) 导出为 XML 或 CSV
- ember.js - Ember,更改当前 url 和 LinkTo 目标而不重新加载数据
- json - 我收到“未捕获的类型错误:无法读取未定义的属性 'get'”
- python - 无法将 azure.cognitiveservices.search.websearch 模块导入程序