Skip to content

feat(cli-service): allow override from option in pages config #5534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ Deprecated since Vue CLI 3.3, please use [`publicPath`](#publicPath) instead.
title: 'Index Page',
// chunks to include on this page, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'index']
chunks: ['chunk-vendors', 'chunk-common', 'index'],
// devServer.historyApiFallback.rewrites
// We can set advanced path regex when serve
// Notice: This option doesn't effect production.
from: /^\/m(\/.*)?$/
},
// when using the entry-only string format,
// template is inferred to be `public/subpage.html`
Expand Down
8 changes: 6 additions & 2 deletions docs/zh/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = {

在 multi-page 模式下构建应用。每个“page”应该有一个对应的 JavaScript 入口文件。其值应该是一个对象,对象的 key 是入口的名字,value 是:

- 一个指定了 `entry`, `template`, `filename`, `title` 和 `chunks` 的对象 (除了 `entry` 之外都是可选的);
- 一个指定了 `entry`, `template`, `filename`, `title`, `from` 和 `chunks` 的对象 (除了 `entry` 之外都是可选的);
- 或一个指定其 `entry` 的字符串。

``` js
Expand All @@ -121,7 +121,11 @@ module.exports = {
title: 'Index Page',
// 在这个页面中包含的块,默认情况下会包含
// 提取出来的通用 chunk 和 vendor chunk。
chunks: ['chunk-vendors', 'chunk-common', 'index']
chunks: ['chunk-vendors', 'chunk-common', 'index'],
// devServer.historyApiFallback.rewrites
// 当使用开发服务器时,能进阶控制匹配路径
// 注意: 这个选项不会影响生产环境
from: /^\/m(\/.*)?$/
},
// 当使用只有入口的字符串格式时,
// 模板会被推导为 `public/subpage.html`
Expand Down
26 changes: 26 additions & 0 deletions packages/@vue/cli-service/__tests__/multiPage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function makeProjectMultiPage (project) {
entry: 'src/main.js',
template: 'public/baz.html',
filename: 'qux.html'
},
m: {
from: /^\\/m(\\/.*)?$/,
entry: 'src/mobile.js',
}
},
chainWebpack: config => {
Expand Down Expand Up @@ -53,6 +57,13 @@ async function makeProjectMultiPage (project) {
render: h => h('h1', 'FooBar')
})
`)
await project.write('src/mobile.js', `
import Vue from 'vue'
new Vue({
el: '#app',
render: h => h('h1', 'Mobile')
})
`)
const app = await project.read('src/App.vue')
await project.write('src/App.vue', app.replace(
`import HelloWorld from './components/HelloWorld.vue'`,
Expand Down Expand Up @@ -84,6 +95,18 @@ test('serve w/ multi page', async () => {

await page.goto(`${url}foobar`)
expect(await helpers.getText('h1')).toMatch(`FooBar`)

await page.goto(`${url}m`)
expect(await helpers.getText('h1')).toMatch(`Mobile`)

await page.goto(`${url}m/`)
expect(await helpers.getText('h1')).toMatch(`Mobile`)

await page.goto(`${url}m/oao`)
expect(await helpers.getText('h1')).toMatch(`Mobile`)

await page.goto(`${url}message`)
expect(await helpers.getText('h1')).not.toMatch(`Mobile`)
}
)
})
Expand Down Expand Up @@ -193,6 +216,9 @@ test('build w/ multi page', async () => {

await page.goto(`${url}bar.html`)
expect(await getH1Text()).toMatch('Welcome to Your Vue.js App')

await page.goto(`${url}m.html`)
expect(await getH1Text()).toMatch('Mobile')
})

afterAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function genHistoryApiFallbackRewrites (baseUrl, pages = {}) {
// eg. 'page11' should appear in front of 'page1'
.sort((a, b) => b.length - a.length)
.map(name => ({
from: new RegExp(`^/${name}`),
from: pages[name].from || new RegExp(`^/${name}`),
to: path.posix.join(baseUrl, pages[name].filename || `${name}.html`)
}))
return [
Expand Down