# 6、设置页面只在开发模式下打包

你可能会有这样的需求

在开发时,有一些测试页面,而这些测试页面不想在正式上线时被用户访问到。

以设置 pages_template 目录内的所有页面仅在开发模式下生效为例。

步骤

  • 1、在项目根目录新建文件 pages-dev.json
{
  "subPackages": [{
    "root": "pages_template",
    "pages": [{
      "path": "db-test/db-test",
      "style": {
        "navigationBarTitleText": "数据库调用示例"
      }
    },
    {
      "path": "db-test/list/list",
      "style": {
        "navigationBarTitleText": "列表渲染 - 分页加载"
      }
    },
    {
      "path": "uni-id/index/index",
      "style": {
        "navigationBarTitleText": "云函数调用示例-uni-id"
      }
    },
    {
      "path": "uni-id/password/password",
      "style": {}
    },
    {
      "path": "uni-id/mobile/mobile",
      "style": {}
    },
    {
      "path": "uni-id/univerify/univerify",
      "style": {}
    },
    {
      "path": "uni-id/weixin/weixin",
      "style": {}
    },
    {
      "path": "uni-id/alipay/alipay",
      "style": {}
    },
    {
      "path": "uni-id/util/util",
      "style": {}
    },
    {
      "path": "uni-id/email/email",
      "style": {}
    },
    {
      "path": "uni-id/login/index/index",
      "style": {
        "navigationBarTitleText": "登录"
      }
    },
    {
      "path": "uni-id/login/register/register",
      "style": {
        "navigationBarTitleText": "注册"
      }
    },
    {
      "path": "uni-id/login/forget/forget",
      "style": {
        "navigationBarTitleText": "找回密码"
      }
    },
    {
      "path": "vk-vuex/vk-vuex",
      "style": {
        "navigationBarTitleText": "vuex演示示例"
      }
    },
    {
      "path": "openapi/weixin/weixin",
      "style": {}
    },
    {
      "path": "openapi/weixin/msgSecCheck/msgSecCheck",
      "style": {}
    },
    {
      "path": "openapi/weixin/imgSecCheck/imgSecCheck",
      "style": {}
    },
    {
      "path": "openapi/weixin/sendMessage/sendMessage",
      "style": {}
    },
    {
      "path": "openapi/baidu/baidu",
      "style": {}
    }]
  }]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  • 2、删除 pages.json 内原有的 pages_template 分包

若删除后需要保留 "subPackages": [],

若删除后需要保留 "subPackages": [],

若删除后需要保留 "subPackages": [],

  • 3、项目根目录新建文件 pages.js
const debug = process.env.NODE_ENV !== 'production';
var devPages;
try {
	// 只在开发环境中才会被HBX打包的页面
	devPages = require('./pages-dev.json');
} catch (err) {
	console.error("检测到pages-dev.json文件异常,请检查(json文件内不可以有注释,每个{}是否全部对配,是否多写了逗号,)");
	console.error(err);
	console.error("检测到pages-dev.json文件异常,请检查(json文件内不可以有注释,每个{}是否全部对配,是否多写了逗号,)");
	throw Error(err)
}
module.exports = function(pagesJson) {
	try {
		let newDevPages = JSON.parse(JSON.stringify(devPages));
		if (debug && newDevPages) {
			for (let keyName in newDevPages) {
				let item = newDevPages[keyName];
				if (Object.prototype.toString.call(item) === "[object Array]") {
					pagesJson[keyName].push(...item);
				} else if (Object.prototype.toString.call(item) === '[object Object]') {
					pagesJson[keyName] = Object.assign(pagesJson[keyName], item);
				}
			}
		}
	} catch (err) {
		console.error("pages.js运行异常,请检查");
		console.error(err);
		console.error("pages.js运行异常,请检查");
		throw Error(err)
	}
	return pagesJson;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  • 4、重新编译(完成)

此时你可以尝试点击hbx上方菜单 发行 微信-小程序

此时是发行(正式)模式,可以看到 pages_template 目录并没有被打包到小程序源码中。

而点击hbx上方菜单 运行 运行到小程序模拟器

此时是调式(开发)模式,可以看到 pages_template 目录被打包到小程序源码中。