13
13
// limitations under the License.
14
14
15
15
import { readFile , mkdir , copyFile , stat } from 'fs/promises' ;
16
- import { dirname , extname , join , relative , resolve , sep } from 'path' ;
16
+ import { dirname , extname , join } from 'path' ;
17
17
import type { Header , Rewrite , Redirect } from 'next/dist/lib/load-custom-routes' ;
18
18
import type { NextConfig } from 'next' ;
19
19
import nextBuild from 'next/dist/build' ;
20
+ import nextExport from 'next/dist/export' ;
20
21
import { copy } from 'fs-extra' ;
21
- import * as webpack from 'webpack' ;
22
- import { lt } from 'semver' ;
22
+ import { trace } from 'next/dist/trace' ;
23
23
24
- import { DeployConfig , PathFactory , exec , findDependency } from '../../utils' ;
24
+ import { DeployConfig , PathFactory , exec , findDependency , getWebpackPlugin } from '../../utils' ;
25
25
26
26
export const build = async ( config : DeployConfig | Required < DeployConfig > , getProjectPath : PathFactory ) => {
27
27
@@ -44,46 +44,39 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
44
44
webpack : ( config , context ) => {
45
45
let newConfig = config ;
46
46
if ( nextConfig . webpack ) newConfig = nextConfig . webpack ( config , context ) ;
47
- const plugin = new webpack . NormalModuleReplacementPlugin ( / ^ f i r e b a s e \/ ( a u t h ) $ / , ( resource : any ) => {
48
- // Don't allow firebase-frameworks to recurse
49
- const frameworksRoot = resolve ( `${ dirname ( require . resolve ( '../..' ) ) } ${ sep } ..` ) ;
50
- if ( resource . context . startsWith ( frameworksRoot ) ) return ;
51
- // Don't mutate their node_modules
52
- if ( relative ( getProjectPath ( ) , resource . context ) . startsWith ( `node_modules${ sep } ` ) ) return ;
53
- const client = resource . request . split ( 'firebase/' ) [ 1 ] ;
54
- // auth requires beforeAuthStateChanged, released in 9.7.1
55
- if ( client === 'auth' && lt ( firebaseDependency . version , '9.7.1' ) ) return ;
56
- resource . request = require . resolve ( `../../client/${ client } ` ) ;
57
- } ) ;
47
+ const plugin = getWebpackPlugin ( getProjectPath ( ) ) ;
58
48
newConfig . plugins ||= [ ] ;
59
49
newConfig . plugins . push ( plugin ) ;
60
50
return newConfig ;
61
51
}
62
52
}
63
53
}
64
54
65
- await nextBuild ( getProjectPath ( ) , overrideConfig as any , false , false , true ) ;
66
- // TODO be a bit smarter about this
67
- await exec ( `${ getProjectPath ( 'node_modules' , '.bin' , 'next' ) } export` , { cwd : getProjectPath ( ) } ) . catch ( ( ) => { } ) ;
68
55
69
56
// SEMVER these defaults are only needed for Next 11
70
57
const { distDir= '.next' , basePath= '' } = nextConfig ;
71
58
72
59
const deployPath = ( ...args : string [ ] ) => config . dist ? join ( config . dist , ...args ) : getProjectPath ( '.deploy' , ...args ) ;
73
60
const getHostingPath = ( ...args : string [ ] ) => deployPath ( 'hosting' , ...basePath . split ( '/' ) , ...args ) ;
74
61
75
- await mkdir ( getHostingPath ( '_next' , 'static' ) , { recursive : true } ) ;
62
+ await nextBuild ( getProjectPath ( ) , overrideConfig as any , false , false , true ) ;
63
+
64
+ await nextExport (
65
+ getProjectPath ( ) ,
66
+ { silent : true , outdir : getHostingPath ( ) } ,
67
+ trace ( 'next-export-cli' )
68
+ ) . catch ( ( ) => {
69
+ console . warn ( '\nUnable to export the app, treating as SSR.\n\n' ) ;
70
+ } ) ;
76
71
77
72
let usingCloudFunctions = ! ! config . function ;
78
73
const asyncSteps : Array < Promise < any > > = [ ] ;
79
74
80
75
const exportDetailJson = await readFile ( getProjectPath ( distDir , 'export-detail.json' ) ) . then ( it => JSON . parse ( it . toString ( ) ) , ( ) => { success : false } ) ;
81
76
if ( exportDetailJson . success ) {
82
77
usingCloudFunctions = false ;
83
- asyncSteps . push (
84
- copy ( exportDetailJson . outDirectory , getHostingPath ( ) )
85
- ) ;
86
78
} else {
79
+ await mkdir ( getHostingPath ( '_next' , 'static' ) , { recursive : true } ) ;
87
80
await copy ( getProjectPath ( 'public' ) , getHostingPath ( ) ) ;
88
81
await copy ( getProjectPath ( distDir , 'static' ) , getHostingPath ( '_next' , 'static' ) ) ;
89
82
@@ -152,11 +145,11 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
152
145
return { source, destination } ;
153
146
} ) . filter ( it => it ) ;
154
147
148
+ // TODO use this better detection for usesFirebaseConfig
155
149
return { usingCloudFunctions, usesFirebaseConfig, headers, redirects, rewrites, framework : 'next.js' , packageJson, bootstrapScript : null } ;
156
150
}
157
151
158
-
159
- export type Manifest = {
152
+ type Manifest = {
160
153
distDir ?: string ,
161
154
basePath ?: string ,
162
155
headers ?: ( Header & { regex : string } ) [ ] ,
@@ -166,4 +159,4 @@ export type Manifest = {
166
159
afterFiles ?: ( Rewrite & { regex : string } ) [ ] ,
167
160
fallback ?: ( Rewrite & { regex : string } ) [ ] ,
168
161
} ,
169
- } ;
162
+ } ;
0 commit comments