Skip to content

Commit a7bdbd0

Browse files
author
zhangjingyuan02
committed
feat(build): add PWA feature
1 parent 8554f1d commit a7bdbd0

File tree

18 files changed

+164
-13
lines changed

18 files changed

+164
-13
lines changed

packages/mars-build/src/compiler/script/script-h5.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ exports.compileMain = function (content, options) {
7676
window,
7777
mars: {
7878
navigationBarHomeColor: mars && mars.navigationBarHomeColor || 'dark',
79-
showNavigationBorder: mars ? !!mars.showNavigationBorder : true,
80-
useTransition: mars ? !!mars.useTransition : true,
81-
homePage: `/${tabBar && tabBar.list && tabBar.list.length > 0 ? tabBar.list[0].pagePath : pages[0]}`
79+
showNavigationBorder: mars && mars.showNavigationBorder ? mars.showNavigationBorder : true,
80+
useTransition: mars && mars.useTransition ? mars.useTransition : true,
81+
homePage: `/${tabBar && tabBar.list && tabBar.list.length > 0 ? tabBar.list[0].pagePath : pages[0]}`,
82+
supportPWA: !!mars.supportPWA
8283
},
8384
componentSet,
8485
pagesInfo

packages/mars-build/src/gulp-mars-h5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ async function compile(file, opt) {
165165
mars: opt._config.h5 || null
166166
});
167167
fs.writeFileSync(opt.dest + '/main.js', content);
168+
process.env.MARS_PWA = opt._config.h5 && opt._config.h5.supportPWA;
168169

169170
// 处理style
170171
const h5StylesArr = styles.filter(item =>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @file 注册service worker
3+
* @author mars
4+
*/
5+
6+
function register(file) {
7+
// ,service worker脚本文件为sw.js
8+
if ('serviceWorker' in navigator) {
9+
navigator.serviceWorker.register(`${process.env.BASE_URL}${file}`).then(function () {
10+
console.log('Service Worker 注册成功');
11+
});
12+
}
13+
}
14+
/* eslint-disable fecs-export-on-declare */
15+
export default register;
16+

packages/mars-build/src/h5/transform/plugins/transformMainPlugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,19 @@ module.exports = function getVisitor(options = {}) {
150150
// 插入 import basicComponents from './components.js';
151151
insertImportDeclaration('basicComponents', './components.js');
152152
}
153+
// 注册 service worker
154+
if (!!mars.supportPWA) {
155+
path.node.body.unshift(t.expressionStatement(t.callExpression(
156+
t.identifier('registerServiceWorker'),
157+
[t.stringLiteral('sw.js')]
158+
)));
159+
insertImportDeclaration('registerServiceWorker', './registerServiceWorker.js');
160+
}
153161
// 插入 import Vue from 'vue';
154162
insertImportDeclaration('Vue', 'vue');
155163
}
156164
}
157165
}
158166
};
159167
};
160-
}
168+
};

packages/mars-cli-template/bin/mars-cli-service.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ process.env.MARS_CLI_OPTIONS = process.env.MARS_CLI_OPTIONS || JSON.stringify({
7373
if (!fs.existsSync(process.cwd() + '/vue.config.js')) {
7474
console.error(chalk.red('vue.config.js 文件未找到,请确认当前所在工程支持编译到 h5。'));
7575
}
76-
await fs.copy(process.cwd() + '/vue.config.js', context + '/vue.config.js');
76+
if (!process.env.VUE_CLI_SERVICE_CONFIG_PATH) {
77+
process.env.VUE_CLI_SERVICE_CONFIG_PATH = process.cwd() + '/vue.config.js';
78+
}
79+
// await fs.copy(process.cwd() + '/vue.config.js', context + '/vue.config.js');
7780

7881
const plugins = [
7982
idToPlugin('@marsjs/vue-cli-plugin-mars-web'),
80-
idToPlugin('@vue/cli-plugin-babel')
83+
idToPlugin('@vue/cli-plugin-babel'),
84+
idToPlugin('@marsjs/vue-cli-plugin-pwa')
8185
];
8286
const service = new Service(context, {
8387
plugins

packages/mars-cli-template/generator/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55

66
module.exports = async (api, options) => {
7-
7+
const {
8+
noH5,
9+
needPWA
10+
} = options;
811

912
if (options.isDistH5) {
1013
api.render('./dist-h5');
@@ -15,10 +18,10 @@ module.exports = async (api, options) => {
1518
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript')
1619
});
1720

18-
if (!options.noH5) {
19-
api.render('./template-h5', {
20-
doesCompile: api.hasPlugin('babel') || api.hasPlugin('typescript')
21-
});
21+
if (!noH5) {
22+
api.render('./template-h5', null, {delimiter: '$'});
23+
24+
needPWA && api.render('./template-pwa', null, {delimiter: '$'});
2225

2326
// 添加 h5 所需的依赖
2427
api.extendPackage({
@@ -36,6 +39,7 @@ module.exports = async (api, options) => {
3639
'atom-web-compiler': '^2.2.0',
3740
'atom2vue-loader': '^1.0.0',
3841
'@marsjs/vue-cli-plugin-mars-web': '^0.0.9',
42+
'@marsjs/vue-cli-plugin-pwa': '^0.0.1',
3943
'@vue/cli-plugin-babel': '^3.0.0',
4044
'@vue/cli-service': '^3.5.0',
4145
'less': '^3.0.4',

packages/mars-cli-template/generator/template-h5/mars/mars.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = function (target) {
77
h5: {
88
navigationBarHomeColor: 'light',
99
showNavigationBorder: true,
10-
mode: 'hash'
10+
mode: 'hash',
11+
useTransition: true
1112
}
1213
};
1314

packages/mars-cli-template/generator/template-h5/mars/vue.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
.plugin('html')
1414
.tap(args => {
1515
args[0].title = 'Mars demo';
16+
args[0].template = process.cwd() + '/public/index.html';
1617
return args;
1718
});
1819
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @file config file
3+
*/
4+
5+
module.exports = function (target) {
6+
const config = {
7+
h5: {
8+
navigationBarHomeColor: 'light',
9+
showNavigationBorder: true,
10+
mode: 'hash',
11+
useTransition: true,
12+
supportPWA: true
13+
}
14+
};
15+
16+
return config;
17+
};

0 commit comments

Comments
 (0)