@@ -10,7 +10,9 @@ import * as path from 'path'
1010import { rspack , type Configuration } from '@rspack/core'
1111import { merge } from 'webpack-merge'
1212import 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'
1416import * as messages from './commands-lib/messages'
1517import { loadCustomWebpackConfig } from './commands-lib/get-extension-config'
1618import { PreviewOptions } from './commands-lib/config-types'
@@ -20,6 +22,19 @@ export async function extensionPreview(
2022 previewOptions : PreviewOptions
2123) {
2224 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
2338
2439 if (
2540 ! pathOrRemoteUrl ?. startsWith ( 'http' ) &&
@@ -30,7 +45,7 @@ export async function extensionPreview(
3045 )
3146 process . exit ( 1 )
3247 }
33-
48+
3449 try {
3550 const browser = previewOptions . browser || 'chrome'
3651 const baseConfig : Configuration = webpackConfig ( projectPath , {
@@ -43,7 +58,8 @@ export async function extensionPreview(
4358 // Preview needs a build before running so
4459 // we don't want to clean the output directory.
4560 output : {
46- clean : false
61+ clean : false ,
62+ path : outputPath
4763 }
4864 } )
4965
@@ -66,9 +82,7 @@ export async function extensionPreview(
6682 }
6783
6884 if ( ! stats ?. hasErrors ( ) ) {
69- console . log (
70- messages . runningInProduction ( path . join ( projectPath , 'dist' , browser ) )
71- )
85+ console . log ( messages . runningInProduction ( projectPath ) )
7286 } else {
7387 console . log ( stats . toString ( { colors : true } ) )
7488 process . exit ( 1 )
0 commit comments