1
1
/* eslint-env jest */
2
2
import fs from 'fs-extra'
3
- import { join } from 'path'
4
- import { nextBuild , getPageFileFromBuildManifest } from 'next-test-utils'
3
+ import { dirname , join } from 'path'
4
+ import { nextBuild , getPageFilesFromBuildManifest } from 'next-test-utils'
5
5
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
6
6
7
7
const appDir = join ( __dirname , '../' )
8
8
9
- function runTests ( ) {
10
- it ( 'includes sourcemaps for all browser files' , async ( ) => {
11
- const browserFiles = await recursiveReadDir ( join ( appDir , '.next' , 'static' ) )
12
- const jsFiles = browserFiles . filter (
13
- ( file ) => file . endsWith ( '.js' ) && file . includes ( '/pages/' )
14
- )
15
- expect ( jsFiles ) . not . toBeEmpty ( )
16
-
17
- jsFiles . forEach ( ( file ) => {
18
- expect ( browserFiles . includes ( `${ file } .map` ) ) . toBe ( true )
19
- } )
20
- } )
21
-
22
- it ( 'correctly generated the source map' , async ( ) => {
23
- const map = JSON . parse (
24
- await fs . readFile (
25
- join (
26
- appDir ,
27
- '.next' ,
28
- ( await getPageFileFromBuildManifest ( appDir , '/static' ) ) + '.map'
29
- ) ,
30
- 'utf8'
31
- )
32
- )
9
+ function extractSourceMappingURL ( jsContent ) {
10
+ // Matches both //# and //@ sourceMappingURL=...
11
+ const match = jsContent . match ( / \/ \/ [ # @ ] s o u r c e M a p p i n g U R L = ( [ ^ \s ] + ) / )
12
+ return match ? match [ 1 ] : null
13
+ }
33
14
34
- expect ( map . sources ) . toContainEqual (
35
- expect . stringMatching ( / p a g e s [ / \\ ] s t a t i c \. j s / )
36
- )
37
- expect ( map . names ) . toContainEqual ( 'StaticPage' )
38
- } )
15
+ async function checkSourceMapExistsForFile ( jsFilePath ) {
16
+ const jsContent = await fs . readFile ( jsFilePath , 'utf8' )
17
+ const sourceMappingURL = extractSourceMappingURL ( jsContent )
18
+ if ( ! sourceMappingURL ) {
19
+ return
20
+ }
21
+ expect ( sourceMappingURL ) . toBeTruthy ( )
22
+ const mapPath = join ( dirname ( jsFilePath ) , sourceMappingURL )
23
+ expect ( await fs . pathExists ( mapPath ) ) . toBe ( true )
39
24
}
40
25
41
26
describe ( 'Production browser sourcemaps' , ( ) => {
@@ -47,7 +32,28 @@ describe('Production browser sourcemaps', () => {
47
32
await nextBuild ( appDir , [ ] , { } )
48
33
} )
49
34
50
- runTests ( )
35
+ it ( 'includes sourcemaps for all browser files' , async ( ) => {
36
+ const staticDir = join ( appDir , '.next' , 'static' )
37
+ const browserFiles = await recursiveReadDir ( staticDir )
38
+ const jsFiles = browserFiles . filter (
39
+ ( file ) => file . endsWith ( '.js' ) && file . includes ( '/pages/' )
40
+ )
41
+ expect ( jsFiles ) . not . toBeEmpty ( )
42
+
43
+ for ( const file of jsFiles ) {
44
+ const jsPath = join ( staticDir , file )
45
+ checkSourceMapExistsForFile ( jsPath )
46
+ }
47
+ } )
48
+
49
+ it ( 'correctly generated the source map' , async ( ) => {
50
+ const dotNextDir = join ( appDir , '.next' )
51
+ const jsFiles = getPageFilesFromBuildManifest ( appDir , '/static' )
52
+ for ( const file of jsFiles ) {
53
+ const jsPath = join ( dotNextDir , file )
54
+ checkSourceMapExistsForFile ( jsPath )
55
+ }
56
+ } )
51
57
} )
52
58
}
53
59
)
0 commit comments