简体   繁体   English

如何混淆 Firebase 部署生成的静态文件?

[英]How do I obfuscate static files generated by firebase deployment?

Just wondering how I would go about obfuscating the data generated by the firebase deployment.只是想知道我将如何混淆 firebase 部署生成的数据。 When I run the command当我运行命令时

npm firebase deploy

it appears to rebuild my code and then deploy it to my web app.它似乎重建了我的代码,然后将其部署到我的网络应用程序。 So this means whenever I make changes or obfuscate the code manually on the build files it becomes overwritten when I decide to deploy it.因此,这意味着每当我在构建文件上手动更改或混淆代码时,当我决定部署它时,它就会被覆盖。

file screenshot文件截图

Tried lots of options, managed to hide src in the console but the static still exposes the JSON data I am using.尝试了很多选项,设法在控制台中隐藏 src 但静态仍然公开我正在使用的 JSON 数据。 I am looking to have this be hidden/obfuscated from the user.我希望对用户隐藏/混淆它。

Thanks谢谢

By default, Firebase Tools will deploy the public folder in your project directory as-is.默认情况下,Firebase 工具会按原样在您的项目目录中部署public It does not have a build step.它没有构建步骤。

// firebase.json
{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  ...
}

Assuming your file structure looks like this:假设您的文件结构如下所示:

$PROJECT_DIR/
|-- functions/
|-- public/
| |-- dist/
| | |-- css/
| | | `-- ...
| | |-- js/
| | | `-- ...
| | |-- media/
| | | `-- ...
| | |-- 404.html
| | `-- index.html
| `-- src/
|   |-- components/
|   |-- assets/
|   `-- index.ts
|-- .firebaserc
|-- database.rules.json
|-- firebase.json
|-- firestore.indexes.json
`-- firestore.rules

Then you can change the deployed directory to the build directory itself:然后您可以将部署目录更改为构建目录本身:

{
  "hosting": {
    "public": "public/dist",
    ...
  },
  ...
}

However, if your code is inside your functions/ directory (as it might be if using SSR), the functions do have a predeploy build step by default (when using TypeScript as your build language or you enabled linting during setup), as defined in firebase.json .但是,如果您的代码位于您的functions/目录中(可能是使用 SSR),则默认情况下,这些函数确实具有预部署构建步骤(当使用 TypeScript 作为构建语言或您在设置期间启用了 linting 时),如firebase.json As firebase deploy deploys functions before hosting, this would mean that the files are overwritten by that build first.由于firebase deploy在托管之前部署功能,这意味着文件首先被该构建覆盖。 You can either remove all of the predeploy hooks to not use the at-deploy build step (not recommended), add your obfuscation step to your project's build script (recommended) or add the obfuscation step as its own item in the predeploy hooks (less recommended).您可以删除所有预部署挂钩以不使用 at-deploy 构建步骤(不推荐),将混淆步骤添加到项目的构建脚本(推荐)或将混淆步骤作为自己的项目添加到预部署挂钩中(更少推荐的)。

// firebase.json
{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint", // runs "npm run lint" in your project's functions directory
      "npm --prefix \"$RESOURCE_DIR\" run build" // runs "npm run build" in your project's functions directory
    ]
  },
  ...
}
// functions/package.json
{
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "npm run build:src && npm run build:obfuscate",
    "build:src": "tsc",
    "build:obfuscate": "...",
    ...
  },
  ...
}

If your hosting files are not in the functions directory, you should check for the presence of predeploy hooks there too as they can be manually added in the same way:如果您的托管文件不在 functions 目录中,您也应该检查那里是否存在预部署挂钩,因为它们可以以相同的方式手动添加:

// firebase.json
{
  "hosting": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\..\" run lint", // runs "npm run lint" in your project's public directory
      "npm --prefix \"$RESOURCE_DIR\..\" run build" // runs "npm run build" in your project's public directory
    ],
    "public": "public/dist",
    ...
  },
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM