Skip to content

Commit 34e6fd5

Browse files
committed
feat(command/dev): implement support for router.base setting
BREAKING CHANGE: change file-path logic in command/build
1 parent c3493d7 commit 34e6fd5

File tree

10 files changed

+116
-109
lines changed

10 files changed

+116
-109
lines changed

dist/classes/nuxtCommand.js

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/subcommands/build.js

+15-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/subcommands/dev.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/classes/nuxtCommand.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export class NuxtLaravelCommand extends NuxtCommand {
4040
// get Nuxt from cli imports
4141
const { Nuxt } = await imports.core()
4242

43-
// disable auto init
44-
options._ready = false
43+
// initialize the nuxt instance
4544
const nuxt = new Nuxt(options)
45+
await nuxt.ready()
4646

4747
// apply nuxt hooks if they have been provided
4848
if (typeof this.cmd._nuxtHooks === 'object') {
@@ -55,9 +55,6 @@ export class NuxtLaravelCommand extends NuxtCommand {
5555
}
5656
}
5757

58-
// initialize the nuxt instance
59-
await nuxt.ready()
60-
6158
return nuxt
6259
}
6360

src/subcommands/build.ts

+25-28
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,29 @@ const config: NuxtLaravelCommandConfig = {
7171
},
7272
'file-path': {
7373
type: 'string',
74-
default: process.env.NUXT_URL || 'storage/app/index.html',
74+
default: process.env.NUXT_URL,
7575
description: 'Location for the SPA index file',
7676
prepare: (cmd, _, argv) => {
7777
// add hook to output index file
7878
cmd.cmd.addNuxtHook!('build:done', async ({ options, nuxt }) => {
79+
// get html for single page app
7980
const { html } = await nuxt.server.renderRoute('/', {
8081
url: '/'
8182
})
8283

84+
let filePath = argv['file-path'] as string
85+
86+
// fallback to public path if file path option is not set
87+
if (!filePath) {
88+
filePath = path.join(
89+
`${argv['public-path']}`,
90+
options.router.base,
91+
'index.html'
92+
)
93+
}
94+
8395
// resolve the file path relative to configured rootDir
84-
const destination = path.resolve(
85-
options.rootDir,
86-
`${argv['file-path']}`
87-
)
96+
const destination = path.resolve(options.rootDir, filePath)
8897

8998
// create directory if it does not exist
9099
const dir = path.dirname(destination)
@@ -104,45 +113,33 @@ const config: NuxtLaravelCommandConfig = {
104113
if (argv['public-path']) {
105114
// add hook move built assets to public path
106115
cmd.cmd.addNuxtHook!('build:done', ({ options }) => {
107-
// resolve public root for static assets
108-
const publicRoot = path.resolve(
109-
options.rootDir,
110-
`${argv['public-path']}`
111-
)
112-
113-
// resolve public path for compiled assets
114-
let assetsPath = ''
115-
if (options.router && options.router.base) {
116-
assetsPath = options.router.base.replace(/^\/$/g, '') + '/'
117-
}
118-
assetsPath += options.build.publicPath.replace(/^\//, '')
119-
const publicPath = path.resolve(
120-
publicRoot,
121-
assetsPath.replace(/^\//, '')
116+
// resolve public path for assets
117+
const publicPath = path.join(
118+
path.resolve(options.rootDir, `${argv['public-path']}`),
119+
options.router.base,
120+
options.build.publicPath
122121
)
123122

124123
// create directory if it does not exist
125124
if (!fs.existsSync(publicPath)) {
126125
fs.mkdirpSync(publicPath)
127126
}
128127

129-
// copy static assets to public root
128+
// resolve static assets path
130129
const staticDir = path.resolve(
131130
options.rootDir,
132131
options.srcDir,
133132
options.dir.static
134133
)
134+
// copy static assets to public path if folder exists
135135
if (fs.existsSync(staticDir)) {
136-
fs.copySync(staticDir, publicRoot)
136+
fs.copySync(staticDir, publicPath)
137137
}
138138

139-
// move compiled assets to public path
140-
fs.moveSync(
139+
// copy compiled assets to public path
140+
fs.copySync(
141141
path.resolve(options.rootDir, options.buildDir, 'dist', 'client'),
142-
publicPath,
143-
{
144-
overwrite: true
145-
}
142+
publicPath
146143
)
147144
})
148145
}

src/subcommands/dev.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ const config: NuxtLaravelCommandConfig = {
137137
const options = await loadNuxtConfig(cmd.argv)
138138

139139
// retrieve dev server URL
140+
const basePrefix = (options.router && options.router.base) || '/'
140141
const nuxtUrl = new URL(
141-
cmd.argv['render-path'] as string,
142+
path.join(basePrefix, cmd.argv['render-path'] as string),
142143
`http://${options.server!.host}:${options.server!.port}`
143144
)
144145

tests/unit/__mocks__/@nuxt/cli.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const config = jest.requireActual('@nuxt/config')
66
export const setup = cli.setup
77
export const commands = cli.commands
88
export const NuxtCommand = cli.NuxtCommand
9-
export const loadNuxtConfig = cli.loadNuxtConfig
9+
export const loadNuxtConfig = jest
10+
.fn()
11+
.mockImplementation(argv => cli.loadNuxtConfig(argv))
1012

1113
export const helpSpy = jest.fn()
1214
export const versionSpy = jest.fn()
@@ -21,6 +23,7 @@ NuxtCommand.prototype.getBuilder = jest.fn().mockReturnValue({
2123
NuxtCommand.prototype.getGenerator = jest.fn().mockReturnValue({
2224
generate: generateSpy
2325
})
26+
2427
export const configSpy = jest.spyOn(NuxtCommand.prototype, 'getNuxtConfig')
2528

2629
export const hookSpy = jest.fn()

0 commit comments

Comments
 (0)