Skip to content

Commit 00343d3

Browse files
author
longshihui
committed
feat(插件): 新增插件页面标题自动读取用户工程package.json的name
1 parent 7e7d33d commit 00343d3

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/vue-cli-plugin.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable*/
22
const path = require('path');
3+
const fs = require('fs');
34
const createPluginData = require('./createPluginData');
45
const PLUGIN_NAME = 'vue-cli-plugin-navigator';
56
/**
@@ -18,9 +19,21 @@ module.exports = (api, projectOptions) => {
1819
typeof pagesConfig === 'object' &&
1920
Object.keys(pagesConfig).length
2021
) {
22+
function readPackageConfig() {
23+
let packageConfig = fs.readFileSync(api.resolve('package.json'), {
24+
encoding: 'utf-8'
25+
});
26+
if (packageConfig) {
27+
packageConfig = JSON.parse(packageConfig);
28+
} else {
29+
packageConfig = {};
30+
}
31+
return packageConfig;
32+
}
2133
const resolve = path =>
2234
api.resolve(require('path').posix.join(PLUGIN_DIR, path));
2335
const navigatorData = createPluginData(projectOptions);
36+
const packageConfig = readPackageConfig();
2437

2538
api.chainWebpack(config => {
2639
// add navigator entry
@@ -38,7 +51,8 @@ module.exports = (api, projectOptions) => {
3851
},
3952
filename: `${PLUGIN_NAME}.html`,
4053
favicon: resolve('./public/favicon.ico'),
41-
title: 'welcome to navigator center',
54+
title: `welcome to ${packageConfig.name ||
55+
'navigator center'}`,
4256
template: resolve('./public/index.html'),
4357
chunks: [PLUGIN_NAME]
4458
}

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
7-
<title>vue-cli-plugin-navigator</title>
7+
<title><%= htmlWebpackPlugin.options.title %></title>
88
</head>
99
<body>
1010
<noscript>

0 commit comments

Comments
 (0)