Skip to content

Commit

Permalink
feat(rollup): tests and docs for new rollup_bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 10, 2019
1 parent 3873715 commit cfef773
Show file tree
Hide file tree
Showing 46 changed files with 458 additions and 14 deletions.
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bzl_library(
"//internal/history-server:bzl",
"//internal/http-server:bzl",
"//internal/jasmine_node_test:bzl",
"//internal/linker:bzl",
"//internal/npm_package:bzl",
"//internal/rollup:bzl",
"//toolchains/node:bzl",
Expand Down Expand Up @@ -83,6 +84,7 @@ npm_package(
"//third_party/npm/node_modules/named-amd:package_contents",
"//internal:package_contents",
"//internal/bazel_integration_test:package_contents",
"//internal/golden_file_test:package_contents",
"//internal/common:package_contents",
"//internal/copy_repository:package_contents",
"//internal/history-server:package_contents",
Expand Down
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ _PACKAGE_READMES = {
"Karma": "//packages/karma:README.md",
"Less": "//packages/less:README.md",
"Protractor": "//packages/protractor:README.md",
"Rollup": "//packages/rollup:README.md",
"Stylus": "//packages/stylus:README.md",
"Terser": "//packages/terser:README.md",
"TypeScript": "//packages/typescript:README.md",
Expand Down
7 changes: 7 additions & 0 deletions e2e/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ e2e_integration_test(
},
)

e2e_integration_test(
name = "e2e_rollup",
npm_packages = {
"//packages/rollup:npm_package": "@bazel/rollup",
},
)

e2e_integration_test(
name = "e2e_ts_devserver",
npm_packages = {
Expand Down
1 change: 1 addition & 0 deletions e2e/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ALL_E2E = [
"node_loader_no_preserve_symlinks",
"node_loader_preserve_symlinks",
"packages",
"rollup",
"stylus",
"symlinked_node_modules_npm",
"symlinked_node_modules_yarn",
Expand Down
1 change: 1 addition & 0 deletions e2e/rollup/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import %workspace%/../../common.bazelrc
28 changes: 28 additions & 0 deletions e2e/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@build_bazel_rules_nodejs//internal/golden_file_test:golden_file_test.bzl", "golden_file_test")
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")

rollup_bundle(
name = "bundle",
srcs = ["msg.js"],
entry_point = "input.js",
)

golden_file_test(
name = "test",
actual = "bundle/input.js",
golden = "golden.js_",
)

# For testing from the root workspace of this repository with bazel_integration_test.
filegroup(
name = "all_files",
srcs = glob(
include = ["**/*"],
exclude = [
"bazel-out/**/*",
"dist/**/*",
"node_modules/**/*",
],
),
visibility = ["//visibility:public"],
)
38 changes: 38 additions & 0 deletions e2e/rollup/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "e2e_rollup",
managed_directories = {"@npm": ["node_modules"]},
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "da72ea53fa1cb8ab5ef7781ba06b97259b7d579a431ce480476266bc81bdf21d",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.36.2/rules_nodejs-0.36.2.tar.gz"],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")

yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()
6 changes: 6 additions & 0 deletions e2e/rollup/golden.js_
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function hello(place) {
return `Hello, ${place}`;
}

console.log(hello('world'));
//# sourceMappingURL=input.js.map
3 changes: 3 additions & 0 deletions e2e/rollup/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {hello} from './msg';

console.log(hello('world'));
3 changes: 3 additions & 0 deletions e2e/rollup/msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hello(place) {
return `Hello, ${place}`;
}
12 changes: 12 additions & 0 deletions e2e/rollup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "e2e-rollup",
"private": true,
"devDependencies": {
"@bazel/rollup": "latest",
"rollup": "1.20.3",
"unidiff": "1.0.1"
},
"scripts": {
"test": "bazel test ..."
}
}
Empty file added e2e/rollup/yarn.lock
Empty file.
5 changes: 5 additions & 0 deletions internal/golden_file_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
package(default_visibility = ["//visibility:public"])

exports_files(["bin.js"])

filegroup(
name = "package_contents",
srcs = glob(["*"]),
)
3 changes: 3 additions & 0 deletions internal/rollup/test/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
load("//:defs.bzl", "rollup_bundle")

# Tests in the new implementation want to reuse the golden files
exports_files(glob(["*.js_"]))

rollup_bundle(
name = "bundle",
srcs = ["bar.js"],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"source-map": "^0.7.3",
"source-map-support": "0.5.9",
"stylus": "~0.54.5",
"terser": "^4.2.1",
"terser": "3.17.0",
"testy": "file:./tools/npm_packages/testy",
"tmp": "0.1.0",
"tsickle": "0.33.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup/src/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"Public API for terser rules"
"Public API for rollup rules"

load(":rollup_bundle.bzl", _rollup_bundle = "rollup_bundle")

Expand Down
3 changes: 2 additions & 1 deletion packages/rollup/src/rollup_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ _ROLLUP_ATTRS = {
The file passed to entry_point is automatically added.
""",
allow_files = [".js", ".json"],
# Don't try to constrain the filenames, could be json, svg, whatever
allow_files = True,
),
"config_file": attr.label(
doc = """A rollup.config.js file
Expand Down
20 changes: 20 additions & 0 deletions packages/rollup/test/code_splitting/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
load("@npm_bazel_rollup//:index.from_src.bzl", "rollup_bundle")

rollup_bundle(
name = "bundle",
srcs = ["strings.js"],
config_file = "rollup.config.js",
entry_point = "input.js",
# TODO: if this isn't here, the error is
# [!] Error: You must set output.dir instead of output.file when generating multiple chunks.
# which will confuse users because it references output.dir instead of output_dir
output_dir = "chunks",
)

jasmine_node_test(
name = "test",
srcs = ["spec.js"],
data = ["@npm//source-map"],
deps = [":bundle"],
)
1 change: 1 addition & 0 deletions packages/rollup/test/code_splitting/input.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/rollup/test/code_splitting/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
output: {
// make chunk filenames predictable
chunkFileNames: '[name].js',
}
}
8 changes: 8 additions & 0 deletions packages/rollup/test/code_splitting/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fs = require('fs');

describe('rollup code splitting', () => {
it('should produce a chunk for lazy loaded code', () => {
expect(fs.existsSync(require.resolve(__dirname + '/chunks/bundle.js'))).toBeTruthy();
expect(fs.existsSync(require.resolve(__dirname + '/chunks/chunk.js'))).toBeTruthy();
});
});
3 changes: 3 additions & 0 deletions packages/rollup/test/code_splitting/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hello() {
return 'Hello World';
}
16 changes: 16 additions & 0 deletions packages/rollup/test/globals/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@build_bazel_rules_nodejs//internal/golden_file_test:golden_file_test.bzl", "golden_file_test")
load("@npm_bazel_rollup//:index.from_src.bzl", "rollup_bundle")

rollup_bundle(
name = "bundle",
entry_point = "input.js",
format = "umd",
globals = {"some_global_var": "runtime_name_of_global_var"},
sourcemap = False,
)

golden_file_test(
name = "test",
actual = "bundle/input.js",
golden = "golden.js_",
)
9 changes: 9 additions & 0 deletions packages/rollup/test/globals/golden.js_
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('some_global_var')) :
typeof define === 'function' && define.amd ? define(['some_global_var'], factory) :
(global = global || self, factory(global.runtime_name_of_global_var));
}(this, function (some_global_var) { 'use strict';

console.error(some_global_var.thing);

}));
3 changes: 3 additions & 0 deletions packages/rollup/test/globals/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {thing} from 'some_global_var';

console.error(thing);
66 changes: 66 additions & 0 deletions packages/rollup/test/integration_e2e_rollup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
load("@build_bazel_rules_nodejs//internal/golden_file_test:golden_file_test.bzl", "golden_file_test")
load("@npm_bazel_rollup//:index.from_src.bzl", "rollup_bundle")
load("@npm_bazel_terser//:index.from_src.bzl", "terser_minified")

[
rollup_bundle(
name = "bundle.%s" % format,
srcs = ["bar.js"],
config_file = "rollup.config.js",
entry_point = "foo.js",
format = format,
globals = {"some_global_var": "runtime_name_of_global_var"},
deps = [
"//%s/fum:fumlib" % package_name(),
"@npm//hello",
],
)
for format in [
"cjs",
"umd",
]
]

# Adapt the sourcemap line
# In the prior golden test the output was called bundle.umd.js
# so the sourcemap included that filename

[
genrule(
name = "fix_%s_sourcemap" % format,
srcs = ["//internal/rollup/test/rollup:bundle-%s_golden.js_" % format],
outs = ["%s_golden.js_" % format],
cmd = "sed s/bundle.%s.js.map/foo.js.map/ $< > $@" % format,
)
for format in [
"cjs",
"umd",
]
]

[
golden_file_test(
name = "test_%s" % format,
actual = "bundle.%s/foo.js" % format,
golden = "%s_golden.js_" % format,
)
for format in [
"cjs",
"umd",
]
]

terser_minified(
name = "bundle.debug.min",
src = "bundle.umd/foo.js",
debug = True,
)

golden_file_test(
name = "bundle-min-debug",
actual = "bundle.debug.min.js",
golden = "//internal/rollup/test/rollup:bundle-min-debug_golden.js_",
tags = ["manual"], # not working yet, needs something to do transpilation to es5
)

# TODO(alexeagle): verify against remaining golden files
1 change: 1 addition & 0 deletions packages/rollup/test/integration_e2e_rollup/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = 'Alice';
3 changes: 3 additions & 0 deletions packages/rollup/test/integration_e2e_rollup/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @fileoverview Description of this file.
*/
32 changes: 32 additions & 0 deletions packages/rollup/test/integration_e2e_rollup/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {fum} from 'fumlib';
import hello from 'hello';
import {thing} from 'some_global_var';

import {name} from './bar';

console.log(`${hello}, ${name} in ${fum}`);

// Tests for @PURE annotations
/*@__PURE__*/
console.log('side-effect');

class Impure {
constructor() {
console.log('side-effect')
}
}

/*@__PURE__*/ new Impure();

// Test for sequences = false
export class A {
a() {
return document.a;
}
}
function inline_me() {
return 'abc';
}
console.error(new A().a(), inline_me(), thing, ngDevMode, ngI18nClosureMode);
ngDevMode && console.log('ngDevMode is truthy');
ngI18nClosureMode && console.log('ngI18nClosureMode is truthy');
12 changes: 12 additions & 0 deletions packages/rollup/test/integration_e2e_rollup/fum/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//internal/js_library:js_library.bzl", "js_library")

package(default_visibility = ["//packages/rollup:__subpackages__"])

js_library(
name = "fumlib",
srcs = ["index.js"],
module_name = "fumlib",
# Don't allow deep imports under here,
# and give it the AMD name "fumlib", not "fumlib/index"
module_root = "index.d.ts",
)
1 change: 1 addition & 0 deletions packages/rollup/test/integration_e2e_rollup/fum/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fum = 'Wonderland';
Loading

0 comments on commit cfef773

Please sign in to comment.