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 16, 2019
1 parent d32d3a8 commit dc0133c
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 67 deletions.
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
21 changes: 21 additions & 0 deletions internal/e2e/rollup_code_splitting/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ 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"],
enable_code_splitting = False,
entry_point = ":main1.js",
license_banner = ":license.txt",
)
Expand All @@ -15,12 +27,14 @@ 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",
],
Expand Down Expand Up @@ -49,13 +63,20 @@ 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",
],
deps = [
"@npm//jasmine",
Expand Down
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');
});
});
4 changes: 4 additions & 0 deletions internal/e2e/rollup_fine_grained_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ load("//:defs.bzl", "jasmine_node_test", "nodejs_binary", "rollup_bundle")
# and no fine grained deps
rollup_bundle(
name = "bundle_no_deps",
enable_code_splitting = False,
entry_point = ":no-deps.js",
)

# You can have a rollup_bundle with no node_modules attribute
# and fine grained deps
rollup_bundle(
name = "bundle",
enable_code_splitting = False,
entry_point = ":has-deps.js",
deps = [
"@fine_grained_deps_yarn//@gregmagolan/test-a",
Expand All @@ -22,6 +24,7 @@ rollup_bundle(
# and no fine grained deps
rollup_bundle(
name = "bundle_legacy",
enable_code_splitting = False,
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)
Expand All @@ -30,6 +33,7 @@ rollup_bundle(
# and fine grained deps so long as they come from the same root
rollup_bundle(
name = "bundle_hybrid",
enable_code_splitting = False,
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
deps = [
Expand Down
Loading

0 comments on commit dc0133c

Please sign in to comment.