Skip to content

Commit

Permalink
feat(builtin): do code splitting even if only one entry point
Browse files Browse the repository at this point in the history
This separates handling of multiple entry points from code splitting
  • Loading branch information
Jesse-Good authored and alexeagle committed Jul 31, 2019
1 parent 780dfb4 commit f51c129
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 66 deletions.
1 change: 1 addition & 0 deletions examples/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ts_devserver(

rollup_bundle(
name = "bundle",
enable_code_splitting = False,
entry_point = ":app.ts",
deps = [":app"],
)
Expand Down
1 change: 1 addition & 0 deletions examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ts_devserver(
# Test for production mode
rollup_bundle(
name = "bundle",
enable_code_splitting = False,
entry_point = ":app.ts",
# TODO(alexeagle): we should be able to get this from //:protobufjs_bootstrap_scripts
# and automatically plumb it through to Rollup.
Expand Down
1 change: 1 addition & 0 deletions internal/e2e/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("//:defs.bzl", "rollup_bundle")
rollup_bundle(
name = "bundle",
srcs = ["bar.js"],
enable_code_splitting = False,
entry_point = ":foo.js",
globals = {"some_global_var": "runtime_name_of_global_var"},
license_banner = ":license.txt",
Expand Down
24 changes: 24 additions & 0 deletions internal/e2e/rollup_code_splitting/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ rollup_bundle(
["*.js"],
exclude = ["*.spec.js"],
),
enable_code_splitting = True,
entry_point = ":main1.js",
license_banner = ":license.txt",
)

rollup_bundle(
name = "bundle_multi_entry",
srcs = glob(
["*.js"],
exclude = ["*.spec.js"],
),
additional_entry_points = ["build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/additional_entry.js"],
entry_point = ":main1.js",
license_banner = ":license.txt",
Expand All @@ -16,15 +27,19 @@ jasmine_node_test(
name = "test",
srcs = [
"additional_entry.spec.js",
"code_splitting.spec.js",
"main1.spec.js",
],
data = glob([
"goldens/*",
]) + [
":bundle",
":bundle_multi_entry",
":bundle.min.js",
":bundle.min.es2015.js",
],
# TODO(alexeagle): update the test logic for directory lookups vs runfiles manifest
tags = ["fix-windows"],
deps = [
"//internal/e2e:check_lib",
"@npm//unidiff",
Expand All @@ -49,12 +64,21 @@ filegroup(
output_group = "es5_min_debug",
)

filegroup(
name = "bundle_multi_entry-outputgroups-es2015",
srcs = [":bundle_multi_entry"],
output_group = "es2015",
)

jasmine_node_test(
name = "test-outputgroups",
srcs = ["outputgroups.spec.js"],
data = [
":bundle-outputgroups-es2015",
":bundle-outputgroups-es5_min",
":bundle-outputgroups-es5_min_debug",
":bundle_multi_entry-outputgroups-es2015",
],
# TODO(alexeagle): update the test logic for directory lookups vs runfiles manifest
tags = ["fix-windows"],
)
55 changes: 31 additions & 24 deletions internal/e2e/rollup_code_splitting/additional_entry.spec.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
check = require('../check.js');
const fs = require('fs');
const expected = 'lib1 fn,dep3 fn,lib2 fn,dep2 fn';
const path = __dirname;
const expected = 'dep4 fn';
const path = require('path');

describe('bundling chunks', () => {
function findChunk() {
let chunks = fs.readdirSync(path.join(__dirname, 'bundle_chunks'))
.filter(name => name.startsWith('chunk-') && name.endsWith('.js'));
if (chunks.length != 1) {
fail('Not 1 chunk ' + chunks);
}
return chunks[0];
}

describe('bundling additional entry point', () => {
it('should work', () => {
check(path, 'bundle.min.js', 'goldens/bundle.min.js_');
check(path, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
check(__dirname, 'bundle.min.js', 'goldens/bundle.min.js_');
check(__dirname, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
});

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

it('bundle_chunks should work', () => {
const additional_entry =
require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/additional_entry.js')
.default;
const actual = (new additional_entry()).test();
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/' + findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('bundle_chunks_min should work', () => {
const additional_entry =
require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/additional_entry.js')
.default;
const actual = (new additional_entry()).test();
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/' +
findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('bundle_chunks_min_debug should work', () => {
const additional_entry =
require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/additional_entry.js')
.default;
const actual = (new additional_entry()).test();
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('should have a license header', () => {
const content = fs.readFileSync(
require.resolve(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/additional_entry.js'),
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
findChunk()),
{encoding: 'utf-8'});
expect(content).toContain('dummy license banner');
});
});
});
63 changes: 63 additions & 0 deletions internal/e2e/rollup_code_splitting/code_splitting.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
check = require('../check.js');
const fs = require('fs');
const expected = 'dep4 fn';
const path = require('path');

describe('code splitting', () => {
function findChunk() {
let chunks = fs.readdirSync(path.join(__dirname, 'bundle_chunks'))
.filter(name => name.startsWith('chunk-') && name.endsWith('.js'));
if (chunks.length != 1) {
fail('Not 1 chunk ' + chunks);
}
return chunks[0];
}

it('should work', () => {
check(__dirname, 'bundle.min.js', 'goldens/bundle.min.js_');
check(__dirname, 'bundle.min.es2015.js', 'goldens/bundle.min.es2015.js_');
check(__dirname, 'bundle_multi_entry.min.js', 'goldens/bundle_multi_entry.min.js_');
});

// Disabled because native ESModules can't be loaded in current nodejs
// see https://github.com/bazelbuild/rules_nodejs/issues/593
xit('bundle_chunks_es6 should work', () => {
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/' +
findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('bundle_chunks should work', () => {
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/' + findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('bundle_chunks_min should work', () => {
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/' +
findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('bundle_chunks_min_debug should work', () => {
const additional_entry = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
findChunk());
const actual = additional_entry.fn();
expect(actual).toEqual(expected);
});

it('should have a license header', () => {
const content = fs.readFileSync(
require.resolve(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/' +
findChunk()),
{encoding: 'utf-8'});
expect(content).toContain('dummy license banner');
});
});
2 changes: 1 addition & 1 deletion internal/e2e/rollup_code_splitting/dep4.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function fn() {
return 'dep4 fn';
}
}
2 changes: 1 addition & 1 deletion internal/e2e/rollup_code_splitting/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export function dynamic() {
return import('./dep4.js').then(dep4 => {
return dep4.fn();
});
}
}
2 changes: 1 addition & 1 deletion internal/e2e/rollup_code_splitting/goldens/bundle.min.js_
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(function(global) {
System.config({
packages: {
'': {map: {"./main1": "bundle_chunks_min/main1", "./additional_entry.js": "bundle_chunks_min/additional_entry.js"}, defaultExtension: 'js'},
'': {map: {"./main1": "bundle_chunks_min/main1"}, defaultExtension: 'js'},
}
});
System.import('main1.js').catch(function(err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SystemJS boilerplate/entry point TEMPLATE for code-split rollup_bundle.
// GENERATED BY Bazel
(function(global) {
System.config({
packages: {
'': {map: {"./main1": "bundle_multi_entry_chunks_min/main1", "./additional_entry.js": "bundle_multi_entry_chunks_min/additional_entry.js"}, defaultExtension: 'js'},
}
});
System.import('main1.js').catch(function(err) {
console.error(err);
});
})(this);
32 changes: 15 additions & 17 deletions internal/e2e/rollup_code_splitting/main1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@ const exportedTest1 = 'dep1 fn,lib2 fn,dep2 fn';
const exportedTest2 = 'dep4 fn';

describe('bundling main entry point', () => {
async function checkAllTests(main1) {
expect(main1.test()).toEqual(exportedTest1);
const actualTest2 = await main1.test2();
expect(actualTest2).toEqual(exportedTest2);
}

// Disabled since native ESModules can't be loaded in nodejs yet
// https://github.com/bazelbuild/rules_nodejs/issues/593
xit('bundle_chunks_es6 should work', async () => {
const main1 = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/main1.js')
expect(main1.test()).toEqual(exportedTest1);
const actualTest2 = await main1.test2();
expect(actualTest2).toEqual(exportedTest2);
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_es6/main1.js');
checkAllTests(main1);
});

it('bundle_chunks should work', async () => {
const main1 = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/main1.js')
expect(main1.test()).toEqual(exportedTest1);
const actualTest2 = await main1.test2();
expect(actualTest2).toEqual(exportedTest2);
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks/main1.js');
checkAllTests(main1);
});

it('bundle_chunks_min should work', async () => {
const main1 = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/main1.js')
expect(main1.test()).toEqual(exportedTest1);
const actualTest2 = await main1.test2();
expect(actualTest2).toEqual(exportedTest2);
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min/main1.js');
checkAllTests(main1);
});

it('bundle_chunks_min_debug should work', async () => {
const main1 = require(
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/main1.js')
expect(main1.test()).toEqual(exportedTest1);
const actualTest2 = await main1.test2();
expect(actualTest2).toEqual(exportedTest2);
'build_bazel_rules_nodejs/internal/e2e/rollup_code_splitting/bundle_chunks_min_debug/main1.js');
checkAllTests(main1);
});

it('should have a license header', () => {
Expand All @@ -44,4 +42,4 @@ describe('bundling main entry point', () => {
{encoding: 'utf-8'});
expect(content).toContain('dummy license banner');
});
});
});
28 changes: 22 additions & 6 deletions internal/e2e/rollup_code_splitting/outputgroups.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ function checkExists(name) {
}
}

// With enable_code_splitting=True there should be 1 chunk. We
// don't know its name ahead of time so just assert the count.
function checkChunkCount(name) {
expect(fs.readdirSync(path.join(__dirname, name))
.filter(name => name.startsWith('chunk-') && name.endsWith('.js'))
.length)
.toBe(1);
}

// TODO: the right assertions are to load up the source-map library
// and assert that the sourcemap actually maps back to the sources

describe('outputgroups', () => {
it('should produce a es2015 sourcemap', () => {
checkExists('bundle.es2015.js');
checkExists('bundle_chunks_es2015/additional_entry.js');
checkExists('bundle_chunks_es2015/additional_entry.js.map');
checkExists('bundle_chunks_es2015/main1.js');
checkExists('bundle_chunks_es2015/main1.js.map');
checkChunkCount('bundle_chunks_es2015');
});
it('should produce a es5_min sourcemap', () => {
checkExists('bundle.min.js');
checkExists('bundle_chunks_min/additional_entry.js');
checkExists('bundle_chunks_min/additional_entry.js.map');
checkExists('bundle_chunks_min/main1.js');
checkExists('bundle_chunks_min/main1.js.map');
checkChunkCount('bundle_chunks_min');
});
it('should produce a es5_min_debug sourcemap', () => {
checkExists('bundle.min_debug.js');
checkExists('bundle_chunks_min_debug/additional_entry.js');
checkExists('bundle_chunks_min_debug/additional_entry.js.map');
checkExists('bundle_chunks_min_debug/main1.js');
checkExists('bundle_chunks_min_debug/main1.js.map');
checkChunkCount('bundle_chunks_min_debug');
});
});

describe('outputgroups multi entry', () => {
it('should produce a es2015 sourcemap', () => {
checkExists('bundle_multi_entry.es2015.js');
checkExists('bundle_multi_entry_chunks_es2015/additional_entry.js');
checkExists('bundle_multi_entry_chunks_es2015/additional_entry.js.map');
checkExists('bundle_multi_entry_chunks_es2015/main1.js');
checkExists('bundle_multi_entry_chunks_es2015/main1.js.map');
});
});
Loading

0 comments on commit f51c129

Please sign in to comment.