Skip to content

Commit f51c129

Browse files
Jesse-Goodalexeagle
authored andcommitted
feat(builtin): do code splitting even if only one entry point
This separates handling of multiple entry points from code splitting
1 parent 780dfb4 commit f51c129

18 files changed

Lines changed: 203 additions & 66 deletions

File tree

examples/app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ts_devserver(
1919

2020
rollup_bundle(
2121
name = "bundle",
22+
enable_code_splitting = False,
2223
entry_point = ":app.ts",
2324
deps = [":app"],
2425
)

examples/protocol_buffers/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ ts_devserver(
7070
# Test for production mode
7171
rollup_bundle(
7272
name = "bundle",
73+
enable_code_splitting = False,
7374
entry_point = ":app.ts",
7475
# TODO(alexeagle): we should be able to get this from //:protobufjs_bootstrap_scripts
7576
# and automatically plumb it through to Rollup.

internal/e2e/rollup/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("//:defs.bzl", "rollup_bundle")
44
rollup_bundle(
55
name = "bundle",
66
srcs = ["bar.js"],
7+
enable_code_splitting = False,
78
entry_point = ":foo.js",
89
globals = {"some_global_var": "runtime_name_of_global_var"},
910
license_banner = ":license.txt",

internal/e2e/rollup_code_splitting/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ rollup_bundle(
77
["*.js"],
88
exclude = ["*.spec.js"],
99
),
10+
enable_code_splitting = True,
11+
entry_point = ":main1.js",
12+
license_banner = ":license.txt",
13+
)
14+
15+
rollup_bundle(
16+
name = "bundle_multi_entry",
17+
srcs = glob(
18+
["*.js"],
19+
exclude = ["*.spec.js"],
20+
),
1021
additional_entry_points = ["build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/additional_entry.js"],
1122
entry_point = ":main1.js",
1223
license_banner = ":license.txt",
@@ -16,15 +27,19 @@ jasmine_node_test(
1627
name = "test",
1728
srcs = [
1829
"additional_entry.spec.js",
30+
"code_splitting.spec.js",
1931
"main1.spec.js",
2032
],
2133
data = glob([
2234
"goldens/*",
2335
]) + [
2436
":bundle",
37+
":bundle_multi_entry",
2538
":bundle.min.js",
2639
":bundle.min.es2015.js",
2740
],
41+
# TODO(alexeagle): update the test logic for directory lookups vs runfiles manifest
42+
tags = ["fix-windows"],
2843
deps = [
2944
"//internal/e2e:check_lib",
3045
"@npm//unidiff",
@@ -49,12 +64,21 @@ filegroup(
4964
output_group = "es5_min_debug",
5065
)
5166

67+
filegroup(
68+
name = "bundle_multi_entry-outputgroups-es2015",
69+
srcs = [":bundle_multi_entry"],
70+
output_group = "es2015",
71+
)
72+
5273
jasmine_node_test(
5374
name = "test-outputgroups",
5475
srcs = ["outputgroups.spec.js"],
5576
data = [
5677
":bundle-outputgroups-es2015",
5778
":bundle-outputgroups-es5_min",
5879
":bundle-outputgroups-es5_min_debug",
80+
":bundle_multi_entry-outputgroups-es2015",
5981
],
82+
# TODO(alexeagle): update the test logic for directory lookups vs runfiles manifest
83+
tags = ["fix-windows"],
6084
)
Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
check = require('../check.js');
22
const fs = require('fs');
3-
const expected = 'lib1 fn,dep3 fn,lib2 fn,dep2 fn';
4-
const path = __dirname;
3+
const expected = 'dep4 fn';
4+
const path = require('path');
5+
6+
describe('bundling chunks', () => {
7+
function findChunk() {
8+
let chunks = fs.readdirSync(path.join(__dirname, 'bundle_chunks'))
9+
.filter(name => name.startsWith('chunk-') && name.endsWith('.js'));
10+
if (chunks.length != 1) {
11+
fail('Not 1 chunk ' + chunks);
12+
}
13+
return chunks[0];
14+
}
515

6-
describe('bundling additional entry point', () => {
716
it('should work', () => {
8-
check(path, 'bundle.min.js', 'goldens/bundle.min.js_');
9-
check(path, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
17+
check(__dirname, 'bundle.min.js', 'goldens/bundle.min.js_');
18+
check(__dirname, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
1019
});
1120

1221
// Disabled because native ESModules can't be loaded in current nodejs
1322
// see https://github.com/bazelbuild/rules_nodejs/issues/593
1423
xit('bundle_chunks_es6 should work', () => {
1524
const additional_entry = require(
16-
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/additional_entry.js');
17-
const actual = (new additional_entry()).test();
25+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/' +
26+
findChunk());
27+
const actual = additional_entry.fn();
1828
expect(actual).toEqual(expected);
1929
});
2030

2131
it('bundle_chunks should work', () => {
22-
const additional_entry =
23-
require(
24-
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/additional_entry.js')
25-
.default;
26-
const actual = (new additional_entry()).test();
32+
const additional_entry = require(
33+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/' + findChunk());
34+
const actual = additional_entry.fn();
2735
expect(actual).toEqual(expected);
2836
});
2937

3038
it('bundle_chunks_min should work', () => {
31-
const additional_entry =
32-
require(
33-
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/additional_entry.js')
34-
.default;
35-
const actual = (new additional_entry()).test();
39+
const additional_entry = require(
40+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/' +
41+
findChunk());
42+
const actual = additional_entry.fn();
3643
expect(actual).toEqual(expected);
3744
});
3845

3946
it('bundle_chunks_min_debug should work', () => {
40-
const additional_entry =
41-
require(
42-
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/additional_entry.js')
43-
.default;
44-
const actual = (new additional_entry()).test();
47+
const additional_entry = require(
48+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
49+
findChunk());
50+
const actual = additional_entry.fn();
4551
expect(actual).toEqual(expected);
4652
});
4753

4854
it('should have a license header', () => {
4955
const content = fs.readFileSync(
5056
require.resolve(
51-
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/additional_entry.js'),
57+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
58+
findChunk()),
5259
{encoding: 'utf-8'});
5360
expect(content).toContain('dummy license banner');
5461
});
55-
});
62+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
check = require('../check.js');
2+
const fs = require('fs');
3+
const expected = 'dep4 fn';
4+
const path = require('path');
5+
6+
describe('code splitting', () => {
7+
function findChunk() {
8+
let chunks = fs.readdirSync(path.join(__dirname, 'bundle_chunks'))
9+
.filter(name => name.startsWith('chunk-') && name.endsWith('.js'));
10+
if (chunks.length != 1) {
11+
fail('Not 1 chunk ' + chunks);
12+
}
13+
return chunks[0];
14+
}
15+
16+
it('should work', () => {
17+
check(__dirname, 'bundle.min.js', 'goldens/bundle.min.js_');
18+
check(__dirname, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
19+
check(__dirname, 'bundle_multi_entry.min.js', 'goldens/bundle_multi_entry.min.js_');
20+
});
21+
22+
// Disabled because native ESModules can't be loaded in current nodejs
23+
// see https://github.com/bazelbuild/rules_nodejs/issues/593
24+
xit('bundle_chunks_es6 should work', () => {
25+
const additional_entry = require(
26+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/' +
27+
findChunk());
28+
const actual = additional_entry.fn();
29+
expect(actual).toEqual(expected);
30+
});
31+
32+
it('bundle_chunks should work', () => {
33+
const additional_entry = require(
34+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/' + findChunk());
35+
const actual = additional_entry.fn();
36+
expect(actual).toEqual(expected);
37+
});
38+
39+
it('bundle_chunks_min should work', () => {
40+
const additional_entry = require(
41+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/' +
42+
findChunk());
43+
const actual = additional_entry.fn();
44+
expect(actual).toEqual(expected);
45+
});
46+
47+
it('bundle_chunks_min_debug should work', () => {
48+
const additional_entry = require(
49+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
50+
findChunk());
51+
const actual = additional_entry.fn();
52+
expect(actual).toEqual(expected);
53+
});
54+
55+
it('should have a license header', () => {
56+
const content = fs.readFileSync(
57+
require.resolve(
58+
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
59+
findChunk()),
60+
{encoding: 'utf-8'});
61+
expect(content).toContain('dummy license banner');
62+
});
63+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function fn() {
22
return 'dep4 fn';
3-
}
3+
}

internal/e2e/rollup_code_splitting/dynamic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export function dynamic() {
22
return import('./dep4.js').then(dep4 => {
33
return dep4.fn();
44
});
5-
}
5+
}

internal/e2e/rollup_code_splitting/goldens/bundle.min.js_

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(function(global) {
44
System.config({
55
packages: {
6-
'': {map: {"./main1": "bundle_chunks_min/main1", "./additional_entry.js": "bundle_chunks_min/additional_entry.js"}, defaultExtension: 'js'},
6+
'': {map: {"./main1": "bundle_chunks_min/main1"}, defaultExtension: 'js'},
77
}
88
});
99
System.import('main1.js').catch(function(err) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SystemJS boilerplate/entry point TEMPLATE for code-split rollup_bundle.
2+
// GENERATED BY Bazel
3+
(function(global) {
4+
System.config({
5+
packages: {
6+
'': {map: {"./main1": "bundle_multi_entry_chunks_min/main1", "./additional_entry.js": "bundle_multi_entry_chunks_min/additional_entry.js"}, defaultExtension: 'js'},
7+
}
8+
});
9+
System.import('main1.js').catch(function(err) {
10+
console.error(err);
11+
});
12+
})(this);

0 commit comments

Comments
 (0)