File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ load ("//:index.bzl" , "prerender_pages" , "web_resources_devserver" )
2+ load ("//tools/jasmine:defs.bzl" , "jasmine_node_test" )
3+ load ("//tools/typescript:defs.bzl" , "ts_project" )
4+
5+ prerender_pages (
6+ name = "site" ,
7+ src = "site.ts" ,
8+ scripts = [":hello" , ":goodbye" ],
9+ tsconfig = "//:tsconfig" ,
10+ source_map = True ,
11+ lib_deps = ["//:node_modules/rules_prerender" ],
12+ )
13+
14+ ts_project (
15+ name = "hello" ,
16+ srcs = ["hello.ts" ],
17+ tsconfig = "//:tsconfig_client" ,
18+ deps = [":shared" ],
19+ )
20+
21+ ts_project (
22+ name = "goodbye" ,
23+ srcs = ["goodbye.ts" ],
24+ tsconfig = "//:tsconfig_client" ,
25+ deps = [":shared" ],
26+ )
27+
28+ ts_project (
29+ name = "shared" ,
30+ srcs = ["shared.ts" ],
31+ tsconfig = "//:tsconfig_client" ,
32+ )
33+
34+ web_resources_devserver (
35+ name = "devserver" ,
36+ resources = ":site" ,
37+ )
38+
39+ ts_project (
40+ name = "test_lib" ,
41+ srcs = ["test.ts" ],
42+ testonly = True ,
43+ data = [":site" ],
44+ deps = [
45+ "//:node_modules/@types/jasmine" ,
46+ "//:node_modules/@types/node" ,
47+ ],
48+ )
49+
50+ jasmine_node_test (
51+ name = "test" ,
52+ deps = [":test_lib" ],
53+ )
Original file line number Diff line number Diff line change 1+ # Shared Chunks
2+
3+ An example which bundles multiple entry points using a common dependency and
4+ verifies that the dependency is not duplicated.
Original file line number Diff line number Diff line change 1+ import { name } from './shared' ;
2+
3+ console . log ( `Goodbye, ${ name } !` ) ;
Original file line number Diff line number Diff line change 1+ import { name } from './shared' ;
2+
3+ console . log ( `Hello, ${ name } !` ) ;
Original file line number Diff line number Diff line change 1+ export const name = 'World' ;
Original file line number Diff line number Diff line change 1+ import { includeScript , PrerenderResource } from 'rules_prerender' ;
2+
3+ export default function * ( ) : Generator < PrerenderResource , void , void > {
4+ yield PrerenderResource . of ( '/index.html' , `
5+ <!DOCTYPE html>
6+ <html>
7+ <head>
8+ <title>Shared Chunks</title>
9+ <meta charset="utf8">
10+ </head>
11+ <body>
12+ <ul>
13+ <li><a href="/hello.html">Hello</a></li>
14+ <li><a href="/goodbye.html">Goodbye</a></li>
15+ </ul>
16+ </body>
17+ </html>
18+ ` . trim ( ) ) ;
19+
20+ yield PrerenderResource . of ( '/hello.html' , `
21+ <!DOCTYPE html>
22+ <html>
23+ <head>
24+ <title>Shared Chunks</title>
25+ <meta charset="utf8">
26+ </head>
27+ <body>
28+ <h2>Check console</h2>
29+ ${ includeScript ( 'examples/shared_chunks/hello' ) }
30+ </body>
31+ </html>
32+ ` . trim ( ) ) ;
33+
34+ yield PrerenderResource . of ( '/goodbye.html' , `
35+ <!DOCTYPE html>
36+ <html>
37+ <head>
38+ <title>Shared Chunks</title>
39+ <meta charset="utf8">
40+ </head>
41+ <body>
42+ <h2>Check console</h2>
43+ ${ includeScript ( 'examples/shared_chunks/goodbye' ) }
44+ </body>
45+ </html>
46+ ` . trim ( ) ) ;
47+ }
Original file line number Diff line number Diff line change 1+ import { promises as fs } from 'fs' ;
2+
3+ const site = 'examples/shared_chunks/site' ;
4+
5+ describe ( 'Shared Chunks' , ( ) => {
6+ it ( 'generates `/hello.js` without inlining `shared.js`' , async ( ) => {
7+ const hello = await fs . readFile ( `${ site } /hello.js` , 'utf8' ) ;
8+ expect ( hello ) . not . toContain ( 'World' ) ;
9+ } ) ;
10+
11+ it ( 'generates `/goodbye.js` without inlining `shared.js`' , async ( ) => {
12+ const goodbye = await fs . readFile ( `${ site } /goodbye.js` , 'utf8' ) ;
13+ expect ( goodbye ) . not . toContain ( 'World' ) ;
14+ } ) ;
15+
16+ it ( 'generates a shared chunk containing `shared.js`' , async ( ) => {
17+ // Should generate exactly three JavaScript files: `hello.js`,
18+ // `goodbye.js`, and one other shared chunk with a generated name.
19+ const entries = await fs . readdir ( site ) ;
20+ const sharedChunks = entries
21+ . filter ( ( entry ) => entry . endsWith ( '.js' ) )
22+ . filter ( ( entry ) => entry !== 'hello.js' && entry !== 'goodbye.js' ) ;
23+ expect ( sharedChunks . length ) . toBe ( 1 ) ;
24+ const [ shared ] = sharedChunks ;
25+
26+ const sharedJs = await fs . readFile ( `${ site } /${ shared } ` , 'utf8' ) ;
27+ expect ( sharedJs ) . toContain ( 'World' ) ;
28+ } ) ;
29+ } ) ;
You can’t perform that action at this time.
0 commit comments