@@ -10,7 +10,9 @@ import * as path from 'path'
10
10
import { rspack , type Configuration } from '@rspack/core'
11
11
import { merge } from 'webpack-merge'
12
12
import webpackConfig from '../webpack/webpack-config'
13
- import { getProjectPath } from './commands-lib/get-project-path'
13
+ import {
14
+ getProjectPath ,
15
+ } from './commands-lib/get-project-path'
14
16
import * as messages from './commands-lib/messages'
15
17
import { loadCustomWebpackConfig } from './commands-lib/get-extension-config'
16
18
import { PreviewOptions } from './commands-lib/config-types'
@@ -20,6 +22,19 @@ export async function extensionPreview(
20
22
previewOptions : PreviewOptions
21
23
) {
22
24
const projectPath = await getProjectPath ( pathOrRemoteUrl )
25
+ const distPath = path . join ( projectPath , 'dist' , previewOptions . browser )
26
+
27
+ // Output path defaults to extensionPreview config.
28
+ // The start command will use the build directory.
29
+ // The preview command will use the build directory if it exists,
30
+ // otherwise it will use the project path.
31
+ // This is useful for remote packages that don't have a build directory.
32
+ // but are ready for manual browser testing.
33
+ const outputPath = previewOptions . outputPath
34
+ ? previewOptions . outputPath
35
+ : fs . existsSync ( distPath )
36
+ ? distPath
37
+ : projectPath
23
38
24
39
if (
25
40
! pathOrRemoteUrl ?. startsWith ( 'http' ) &&
@@ -30,7 +45,7 @@ export async function extensionPreview(
30
45
)
31
46
process . exit ( 1 )
32
47
}
33
-
48
+
34
49
try {
35
50
const browser = previewOptions . browser || 'chrome'
36
51
const baseConfig : Configuration = webpackConfig ( projectPath , {
@@ -43,7 +58,8 @@ export async function extensionPreview(
43
58
// Preview needs a build before running so
44
59
// we don't want to clean the output directory.
45
60
output : {
46
- clean : false
61
+ clean : false ,
62
+ path : outputPath
47
63
}
48
64
} )
49
65
@@ -66,9 +82,7 @@ export async function extensionPreview(
66
82
}
67
83
68
84
if ( ! stats ?. hasErrors ( ) ) {
69
- console . log (
70
- messages . runningInProduction ( path . join ( projectPath , 'dist' , browser ) )
71
- )
85
+ console . log ( messages . runningInProduction ( projectPath ) )
72
86
} else {
73
87
console . log ( stats . toString ( { colors : true } ) )
74
88
process . exit ( 1 )
0 commit comments