From cfef773cafe0a68762adb37a200e8423e34996b4 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 5 Sep 2019 15:14:19 -0700 Subject: [PATCH] feat(rollup): tests and docs for new rollup_bundle --- BUILD.bazel | 2 + docs/BUILD.bazel | 1 + e2e/BUILD.bazel | 7 ++ e2e/index.bzl | 1 + e2e/rollup/.bazelrc | 1 + e2e/rollup/BUILD.bazel | 28 ++++++++ e2e/rollup/WORKSPACE | 38 +++++++++++ e2e/rollup/golden.js_ | 6 ++ e2e/rollup/input.js | 3 + e2e/rollup/msg.js | 3 + e2e/rollup/package.json | 12 ++++ e2e/rollup/yarn.lock | 0 internal/golden_file_test/BUILD.bazel | 5 ++ internal/rollup/test/rollup/BUILD.bazel | 3 + package.json | 2 +- packages/rollup/src/index.bzl | 2 +- packages/rollup/src/rollup_bundle.bzl | 3 +- .../rollup/test/code_splitting/BUILD.bazel | 20 ++++++ packages/rollup/test/code_splitting/input.js | 1 + .../test/code_splitting/rollup.config.js | 6 ++ packages/rollup/test/code_splitting/spec.js | 8 +++ .../rollup/test/code_splitting/strings.js | 3 + packages/rollup/test/globals/BUILD.bazel | 16 +++++ packages/rollup/test/globals/golden.js_ | 9 +++ packages/rollup/test/globals/input.js | 3 + .../test/integration_e2e_rollup/BUILD.bazel | 66 +++++++++++++++++++ .../rollup/test/integration_e2e_rollup/bar.js | 1 + .../test/integration_e2e_rollup/file.js | 3 + .../rollup/test/integration_e2e_rollup/foo.js | 32 +++++++++ .../integration_e2e_rollup/fum/BUILD.bazel | 12 ++++ .../test/integration_e2e_rollup/fum/index.js | 1 + .../integration_e2e_rollup/rollup.config.js | 34 ++++++++++ packages/rollup/test/plugins/BUILD.bazel | 17 +++++ packages/rollup/test/plugins/golden.js_ | 3 + packages/rollup/test/plugins/input.js | 3 + packages/rollup/test/plugins/rollup.config.js | 7 ++ packages/rollup/test/plugins/some.json | 3 + packages/rollup/test/sourcemaps/BUILD.bazel | 15 +++++ packages/rollup/test/sourcemaps/input.js | 2 + packages/rollup/test/sourcemaps/s.js | 1 + packages/rollup/test/sourcemaps/spec.js | 19 ++++++ .../rollup/test/version_stamp/BUILD.bazel | 17 +++++ packages/rollup/test/version_stamp/golden.js_ | 7 ++ packages/rollup/test/version_stamp/input.js | 2 + .../test/version_stamp/rollup.config.js | 22 +++++++ yarn.lock | 22 +++---- 46 files changed, 458 insertions(+), 14 deletions(-) create mode 100644 e2e/rollup/.bazelrc create mode 100644 e2e/rollup/BUILD.bazel create mode 100644 e2e/rollup/WORKSPACE create mode 100644 e2e/rollup/golden.js_ create mode 100644 e2e/rollup/input.js create mode 100644 e2e/rollup/msg.js create mode 100644 e2e/rollup/package.json create mode 100644 e2e/rollup/yarn.lock create mode 100644 packages/rollup/test/code_splitting/BUILD.bazel create mode 100644 packages/rollup/test/code_splitting/input.js create mode 100644 packages/rollup/test/code_splitting/rollup.config.js create mode 100644 packages/rollup/test/code_splitting/spec.js create mode 100644 packages/rollup/test/code_splitting/strings.js create mode 100644 packages/rollup/test/globals/BUILD.bazel create mode 100644 packages/rollup/test/globals/golden.js_ create mode 100644 packages/rollup/test/globals/input.js create mode 100644 packages/rollup/test/integration_e2e_rollup/BUILD.bazel create mode 100644 packages/rollup/test/integration_e2e_rollup/bar.js create mode 100644 packages/rollup/test/integration_e2e_rollup/file.js create mode 100644 packages/rollup/test/integration_e2e_rollup/foo.js create mode 100644 packages/rollup/test/integration_e2e_rollup/fum/BUILD.bazel create mode 100644 packages/rollup/test/integration_e2e_rollup/fum/index.js create mode 100644 packages/rollup/test/integration_e2e_rollup/rollup.config.js create mode 100644 packages/rollup/test/plugins/BUILD.bazel create mode 100644 packages/rollup/test/plugins/golden.js_ create mode 100644 packages/rollup/test/plugins/input.js create mode 100644 packages/rollup/test/plugins/rollup.config.js create mode 100644 packages/rollup/test/plugins/some.json create mode 100644 packages/rollup/test/sourcemaps/BUILD.bazel create mode 100644 packages/rollup/test/sourcemaps/input.js create mode 100644 packages/rollup/test/sourcemaps/s.js create mode 100644 packages/rollup/test/sourcemaps/spec.js create mode 100644 packages/rollup/test/version_stamp/BUILD.bazel create mode 100644 packages/rollup/test/version_stamp/golden.js_ create mode 100644 packages/rollup/test/version_stamp/input.js create mode 100644 packages/rollup/test/version_stamp/rollup.config.js diff --git a/BUILD.bazel b/BUILD.bazel index 8493e824b7..6a022787a9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -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", @@ -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", diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index a35014b3b4..2c00ea5c53 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -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", diff --git a/e2e/BUILD.bazel b/e2e/BUILD.bazel index 9c52d8e154..7792312bf8 100644 --- a/e2e/BUILD.bazel +++ b/e2e/BUILD.bazel @@ -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 = { diff --git a/e2e/index.bzl b/e2e/index.bzl index fac899d9fc..4e1b7764b5 100644 --- a/e2e/index.bzl +++ b/e2e/index.bzl @@ -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", diff --git a/e2e/rollup/.bazelrc b/e2e/rollup/.bazelrc new file mode 100644 index 0000000000..3431057af6 --- /dev/null +++ b/e2e/rollup/.bazelrc @@ -0,0 +1 @@ +import %workspace%/../../common.bazelrc diff --git a/e2e/rollup/BUILD.bazel b/e2e/rollup/BUILD.bazel new file mode 100644 index 0000000000..1c65b4c6a0 --- /dev/null +++ b/e2e/rollup/BUILD.bazel @@ -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"], +) diff --git a/e2e/rollup/WORKSPACE b/e2e/rollup/WORKSPACE new file mode 100644 index 0000000000..4f630385e7 --- /dev/null +++ b/e2e/rollup/WORKSPACE @@ -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() diff --git a/e2e/rollup/golden.js_ b/e2e/rollup/golden.js_ new file mode 100644 index 0000000000..1346571ff4 --- /dev/null +++ b/e2e/rollup/golden.js_ @@ -0,0 +1,6 @@ +function hello(place) { + return `Hello, ${place}`; +} + +console.log(hello('world')); +//# sourceMappingURL=input.js.map diff --git a/e2e/rollup/input.js b/e2e/rollup/input.js new file mode 100644 index 0000000000..dd8347ea84 --- /dev/null +++ b/e2e/rollup/input.js @@ -0,0 +1,3 @@ +import {hello} from './msg'; + +console.log(hello('world')); diff --git a/e2e/rollup/msg.js b/e2e/rollup/msg.js new file mode 100644 index 0000000000..6e80112e38 --- /dev/null +++ b/e2e/rollup/msg.js @@ -0,0 +1,3 @@ +export function hello(place) { + return `Hello, ${place}`; +} diff --git a/e2e/rollup/package.json b/e2e/rollup/package.json new file mode 100644 index 0000000000..ac54be9439 --- /dev/null +++ b/e2e/rollup/package.json @@ -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 ..." + } +} diff --git a/e2e/rollup/yarn.lock b/e2e/rollup/yarn.lock new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/golden_file_test/BUILD.bazel b/internal/golden_file_test/BUILD.bazel index 0d67a500e4..df711b8cee 100644 --- a/internal/golden_file_test/BUILD.bazel +++ b/internal/golden_file_test/BUILD.bazel @@ -1,3 +1,8 @@ package(default_visibility = ["//visibility:public"]) exports_files(["bin.js"]) + +filegroup( + name = "package_contents", + srcs = glob(["*"]), +) diff --git a/internal/rollup/test/rollup/BUILD.bazel b/internal/rollup/test/rollup/BUILD.bazel index 57f024622d..5b5eba89d8 100644 --- a/internal/rollup/test/rollup/BUILD.bazel +++ b/internal/rollup/test/rollup/BUILD.bazel @@ -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"], diff --git a/package.json b/package.json index 24a257abd8..af86125e0f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/rollup/src/index.bzl b/packages/rollup/src/index.bzl index bb894187f8..a2810db84d 100644 --- a/packages/rollup/src/index.bzl +++ b/packages/rollup/src/index.bzl @@ -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") diff --git a/packages/rollup/src/rollup_bundle.bzl b/packages/rollup/src/rollup_bundle.bzl index 01d58ec11c..dc9c9d057c 100644 --- a/packages/rollup/src/rollup_bundle.bzl +++ b/packages/rollup/src/rollup_bundle.bzl @@ -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 diff --git a/packages/rollup/test/code_splitting/BUILD.bazel b/packages/rollup/test/code_splitting/BUILD.bazel new file mode 100644 index 0000000000..9bac6eff23 --- /dev/null +++ b/packages/rollup/test/code_splitting/BUILD.bazel @@ -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"], +) diff --git a/packages/rollup/test/code_splitting/input.js b/packages/rollup/test/code_splitting/input.js new file mode 100644 index 0000000000..1e29b6e0b3 --- /dev/null +++ b/packages/rollup/test/code_splitting/input.js @@ -0,0 +1 @@ +import('./strings').then(m => {const msg = document.createElement('span'); msg.innerText = m.hello(); document.body.appendChild(msg);}); \ No newline at end of file diff --git a/packages/rollup/test/code_splitting/rollup.config.js b/packages/rollup/test/code_splitting/rollup.config.js new file mode 100644 index 0000000000..8ee83f6f42 --- /dev/null +++ b/packages/rollup/test/code_splitting/rollup.config.js @@ -0,0 +1,6 @@ +module.exports = { + output: { + // make chunk filenames predictable + chunkFileNames: '[name].js', + } +} \ No newline at end of file diff --git a/packages/rollup/test/code_splitting/spec.js b/packages/rollup/test/code_splitting/spec.js new file mode 100644 index 0000000000..0d81686903 --- /dev/null +++ b/packages/rollup/test/code_splitting/spec.js @@ -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(); + }); +}); diff --git a/packages/rollup/test/code_splitting/strings.js b/packages/rollup/test/code_splitting/strings.js new file mode 100644 index 0000000000..c0bc83eec0 --- /dev/null +++ b/packages/rollup/test/code_splitting/strings.js @@ -0,0 +1,3 @@ +export function hello() { + return 'Hello World'; +} diff --git a/packages/rollup/test/globals/BUILD.bazel b/packages/rollup/test/globals/BUILD.bazel new file mode 100644 index 0000000000..d84d6e51b2 --- /dev/null +++ b/packages/rollup/test/globals/BUILD.bazel @@ -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_", +) diff --git a/packages/rollup/test/globals/golden.js_ b/packages/rollup/test/globals/golden.js_ new file mode 100644 index 0000000000..dffc89999d --- /dev/null +++ b/packages/rollup/test/globals/golden.js_ @@ -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); + +})); diff --git a/packages/rollup/test/globals/input.js b/packages/rollup/test/globals/input.js new file mode 100644 index 0000000000..d04b0edbc5 --- /dev/null +++ b/packages/rollup/test/globals/input.js @@ -0,0 +1,3 @@ +import {thing} from 'some_global_var'; + +console.error(thing); \ No newline at end of file diff --git a/packages/rollup/test/integration_e2e_rollup/BUILD.bazel b/packages/rollup/test/integration_e2e_rollup/BUILD.bazel new file mode 100644 index 0000000000..f118dd6bf4 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/BUILD.bazel @@ -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 diff --git a/packages/rollup/test/integration_e2e_rollup/bar.js b/packages/rollup/test/integration_e2e_rollup/bar.js new file mode 100644 index 0000000000..5195f60d91 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/bar.js @@ -0,0 +1 @@ +export const name = 'Alice'; diff --git a/packages/rollup/test/integration_e2e_rollup/file.js b/packages/rollup/test/integration_e2e_rollup/file.js new file mode 100644 index 0000000000..e7d978a6b2 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/file.js @@ -0,0 +1,3 @@ +/** + * @fileoverview Description of this file. + */ diff --git a/packages/rollup/test/integration_e2e_rollup/foo.js b/packages/rollup/test/integration_e2e_rollup/foo.js new file mode 100644 index 0000000000..61f7374531 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/foo.js @@ -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'); diff --git a/packages/rollup/test/integration_e2e_rollup/fum/BUILD.bazel b/packages/rollup/test/integration_e2e_rollup/fum/BUILD.bazel new file mode 100644 index 0000000000..12968f9d98 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/fum/BUILD.bazel @@ -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", +) diff --git a/packages/rollup/test/integration_e2e_rollup/fum/index.js b/packages/rollup/test/integration_e2e_rollup/fum/index.js new file mode 100644 index 0000000000..82c2219a00 --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/fum/index.js @@ -0,0 +1 @@ +export const fum = 'Wonderland'; diff --git a/packages/rollup/test/integration_e2e_rollup/rollup.config.js b/packages/rollup/test/integration_e2e_rollup/rollup.config.js new file mode 100644 index 0000000000..05f2d796fe --- /dev/null +++ b/packages/rollup/test/integration_e2e_rollup/rollup.config.js @@ -0,0 +1,34 @@ +const nodeResolve = require('rollup-plugin-node-resolve'); + +// Parse the stamp file produced by Bazel from the version control system +let version = ''; +if (bazel_stamp_file) { + const versionTag = require('fs') + .readFileSync(bazel_stamp_file, {encoding: 'utf-8'}) + .split('\n') + .find(s => s.startsWith('BUILD_SCM_VERSION')); + // Don't assume BUILD_SCM_VERSION exists + if (versionTag) { + version = 'v' + versionTag.split(' ')[1].trim(); + } +} + +let banner = `/** + * @license A dummy license banner that goes at the top of the file. + * This is version ${version} + */ +`; + + +module.exports = { + onwarn: (warning) => { + // Always fail on warnings, assuming we don't know which are harmless. + // We can add exclusions here based on warning.code, if we discover some + // types of warning should always be ignored under bazel. + throw new Error(warning.message); + }, + output: {name: 'bundle', banner}, + plugins: [ + nodeResolve(), + ], +}; diff --git a/packages/rollup/test/plugins/BUILD.bazel b/packages/rollup/test/plugins/BUILD.bazel new file mode 100644 index 0000000000..07c2446c05 --- /dev/null +++ b/packages/rollup/test/plugins/BUILD.bazel @@ -0,0 +1,17 @@ +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 = "plugins", + srcs = ["some.json"], + config_file = "rollup.config.js", + entry_point = "input.js", + output_dir = "bundle", + sourcemap = False, +) + +golden_file_test( + name = "test", + actual = "bundle/input.js", + golden = "golden.js_", +) diff --git a/packages/rollup/test/plugins/golden.js_ b/packages/rollup/test/plugins/golden.js_ new file mode 100644 index 0000000000..fe2887c5bf --- /dev/null +++ b/packages/rollup/test/plugins/golden.js_ @@ -0,0 +1,3 @@ +const json_key = "json_value"; + +console.log(json_key); diff --git a/packages/rollup/test/plugins/input.js b/packages/rollup/test/plugins/input.js new file mode 100644 index 0000000000..37b76ce254 --- /dev/null +++ b/packages/rollup/test/plugins/input.js @@ -0,0 +1,3 @@ +import {json_key} from './some.json'; + +console.log(json_key); diff --git a/packages/rollup/test/plugins/rollup.config.js b/packages/rollup/test/plugins/rollup.config.js new file mode 100644 index 0000000000..de40fdafba --- /dev/null +++ b/packages/rollup/test/plugins/rollup.config.js @@ -0,0 +1,7 @@ +import json from 'rollup-plugin-json'; + +module.exports = { + plugins: [ + json({preferConst: true}), + ], +}; diff --git a/packages/rollup/test/plugins/some.json b/packages/rollup/test/plugins/some.json new file mode 100644 index 0000000000..813ac25bd9 --- /dev/null +++ b/packages/rollup/test/plugins/some.json @@ -0,0 +1,3 @@ +{ + "json_key": "json_value" +} diff --git a/packages/rollup/test/sourcemaps/BUILD.bazel b/packages/rollup/test/sourcemaps/BUILD.bazel new file mode 100644 index 0000000000..3ef8f3961c --- /dev/null +++ b/packages/rollup/test/sourcemaps/BUILD.bazel @@ -0,0 +1,15 @@ +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 = ["s.js"], + entry_point = "input.js", +) + +jasmine_node_test( + name = "test", + srcs = ["spec.js"], + data = ["@npm//source-map"], + deps = [":bundle/input.js.map"], +) diff --git a/packages/rollup/test/sourcemaps/input.js b/packages/rollup/test/sourcemaps/input.js new file mode 100644 index 0000000000..90d4f8071b --- /dev/null +++ b/packages/rollup/test/sourcemaps/input.js @@ -0,0 +1,2 @@ +import {s} from './s' +console.error(s); diff --git a/packages/rollup/test/sourcemaps/s.js b/packages/rollup/test/sourcemaps/s.js new file mode 100644 index 0000000000..4efa86030e --- /dev/null +++ b/packages/rollup/test/sourcemaps/s.js @@ -0,0 +1 @@ +export const s = 'hello'; \ No newline at end of file diff --git a/packages/rollup/test/sourcemaps/spec.js b/packages/rollup/test/sourcemaps/spec.js new file mode 100644 index 0000000000..1d97f01cfb --- /dev/null +++ b/packages/rollup/test/sourcemaps/spec.js @@ -0,0 +1,19 @@ +const fs = require('fs'); +const sm = require('source-map'); + +describe('rollup sourcemap handling', () => { + it('should produce a sourcemap output', async () => { + const file = require.resolve(__dirname + '/bundle/input.js.map'); + const rawSourceMap = JSON.parse(fs.readFileSync(file, 'utf-8')); + await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => { + const pos = consumer.originalPositionFor({line: 1, column: 12}); + // This doesn't work because the output dir is different from input + // so it actually starts with a bunch of '/../..' + // expect(pos.source).toBe('s.js'); + + expect(pos.source.endsWith('s.js')).toBeTruthy(); + expect(pos.line).toBe(1); + expect(pos.column).toBe(17); + }); + }); +}); diff --git a/packages/rollup/test/version_stamp/BUILD.bazel b/packages/rollup/test/version_stamp/BUILD.bazel new file mode 100644 index 0000000000..92b493d51d --- /dev/null +++ b/packages/rollup/test/version_stamp/BUILD.bazel @@ -0,0 +1,17 @@ +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 = "version_stamp", + config_file = "rollup.config.js", + entry_point = "input.js", + # Disable sourcemap so we only get a single output + sourcemap = False, +) + +golden_file_test( + name = "test", + # Leave off the ".js" extension to test that it's the default output + actual = "version_stamp", + golden = "golden.js_", +) diff --git a/packages/rollup/test/version_stamp/golden.js_ b/packages/rollup/test/version_stamp/golden.js_ new file mode 100644 index 0000000000..94076a90d6 --- /dev/null +++ b/packages/rollup/test/version_stamp/golden.js_ @@ -0,0 +1,7 @@ +/** + * @license A dummy license banner that goes at the top of the file. + * This is version v1.2.3 + */ + +// License banner with version stamp will appear here +console.error('stamp'); diff --git a/packages/rollup/test/version_stamp/input.js b/packages/rollup/test/version_stamp/input.js new file mode 100644 index 0000000000..ba39bcd800 --- /dev/null +++ b/packages/rollup/test/version_stamp/input.js @@ -0,0 +1,2 @@ +// License banner with version stamp will appear here +console.error('stamp'); diff --git a/packages/rollup/test/version_stamp/rollup.config.js b/packages/rollup/test/version_stamp/rollup.config.js new file mode 100644 index 0000000000..6442d3f7c7 --- /dev/null +++ b/packages/rollup/test/version_stamp/rollup.config.js @@ -0,0 +1,22 @@ +// Parse the stamp file produced by Bazel from the version control system +let version = ''; +if (bazel_stamp_file) { + const versionTag = require('fs') + .readFileSync(bazel_stamp_file, {encoding: 'utf-8'}) + .split('\n') + .find(s => s.startsWith('BUILD_SCM_VERSION')); + // Don't assume BUILD_SCM_VERSION exists + if (versionTag) { + version = 'v' + versionTag.split(' ')[1].trim(); + } +} + +const banner = `/** + * @license A dummy license banner that goes at the top of the file. + * This is version ${version} + */ +`; + +module.exports = { + output: {banner}, +}; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 222a3b50cc..68285372e5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1300,7 +1300,7 @@ combined-stream@^1.0.6, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -commander@^2.13.0, commander@^2.20.0, commander@~2.20.0: +commander@^2.13.0, commander@^2.19.0, commander@^2.20.0, commander@~2.20.0: version "2.20.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422" integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ== @@ -4813,7 +4813,7 @@ source-map-support@~0.4.0: dependencies: source-map "^0.5.6" -source-map-support@~0.5.12: +source-map-support@~0.5.10, source-map-support@~0.5.12: version "0.5.13" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932" integrity sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w== @@ -5146,6 +5146,15 @@ terser-webpack-plugin@^1.1.0: webpack-sources "^1.4.0" worker-farm "^1.7.0" +terser@3.17.0: + version "3.17.0" + resolved "https://registry.yarnpkg.com/terser/-/terser-3.17.0.tgz#f88ffbeda0deb5637f9d24b0da66f4e15ab10cb2" + integrity sha512-/FQzzPJmCpjAH9Xvk2paiWrFq+5M6aVOf+2KRbwhByISDX/EujxsK+BAvrhb6H+2rtrLCHK9N01wO014vrIwVQ== + dependencies: + commander "^2.19.0" + source-map "~0.6.1" + source-map-support "~0.5.10" + terser@^4.1.2: version "4.1.4" resolved "https://registry.yarnpkg.com/terser/-/terser-4.1.4.tgz#4478b6a08bb096a61e793fea1a4434408bab936c" @@ -5155,15 +5164,6 @@ terser@^4.1.2: source-map "~0.6.1" source-map-support "~0.5.12" -terser@^4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/terser/-/terser-4.2.1.tgz#1052cfe17576c66e7bc70fcc7119f22b155bdac1" - integrity sha512-cGbc5utAcX4a9+2GGVX4DsenG6v0x3glnDi5hx8816X1McEAwPlPgRtXPJzSBsbpILxZ8MQMT0KvArLuE0HP5A== - dependencies: - commander "^2.20.0" - source-map "~0.6.1" - source-map-support "~0.5.12" - test-exclude@^5.2.2: version "5.2.3" resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-5.2.3.tgz#c3d3e1e311eb7ee405e092dac10aefd09091eac0"