简体   繁体   English

如何将 JS 和 JSON 一起编译?

[英]How can I compile JS and JSON together?

I have a project where the prod machine specs are low so I'm trying something new and trying Gulp to compile everything into one compressed js file.我有一个项目,其中 prod 机器规格很低,所以我正在尝试一些新的东西并尝试 Gulp 将所有内容编译成一个压缩的 js 文件。

I have a config.json that's being used by main.js .我有一个config.json正在使用的main.js What does my gulpfile.js has to look like in order to turn those two files into one js file?为了将这两个文件变成一个 js 文件,我的gulpfile.js必须是什么样子?

You may declare a variable in JS to contain the JSON data as per following example您可以按照以下示例在JS中声明一个变量以包含 JSON 数据

var arr = [
       {
          data1: 'mydata01',
          data2: 'mydata02',
       },
       {
          data3: 'mydata03',
          data4: 'mydata04',
       }
     ]

But this defeats the spirit and purpose of JSON which is primarily for exchanging of Data between two entities (two webpages, client-server, webpage-database, etc).但这违背了 JSON 的精神和目的,主要用于在两个实体(两个网页、客户端-服务器、网页-数据库等)之间交换数据。 This technique will make the data hardcoded in the JS with lesser flexibility to change the data dynamically.这种技术将使JS中的数据硬编码,动态更改数据的灵活性较低。

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

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