build(bazel): create script to run on top of ng_package output (#10776)

* build(bazel): create script to run on top of ng_package output

* Use bazel query to get targets and use ng_package replacements attr
This commit is contained in:
Jeremy Elbourn
2018-04-09 17:19:28 -07:00
committed by tinayuangao
parent 09a60bbc3e
commit 4211b39669
10 changed files with 777 additions and 346 deletions
+2 -2
View File
@@ -4,10 +4,10 @@
*/
const {join} = require('path');
const package = require('./package.json');
const packageJson = require('./package.json');
/** Current version of the project*/
const buildVersion = package.version;
const buildVersion = packageJson.version;
/**
* Required Angular version for all Angular Material packages. This version will be used
+678 -339
View File
File diff suppressed because it is too large Load Diff
+8
View File
@@ -61,6 +61,14 @@ MATERIAL_PACKAGES = [
MATERIAL_TARGETS = ["//src/lib:material"] + ["//src/lib/%s" % p for p in MATERIAL_PACKAGES]
# Each individual package uses a placeholder for the version of Angular to ensure they're
# all in-sync. This map is passed to each ng_package rule to stamp out the appropriate
# version for the placeholders.
ANGULAR_PACKAGE_VERSION = ">=6.0.0-beta.0 <7.0.0"
VERSION_PLACEHOLDER_REPLACEMENTS = {
"0.0.0-NG": ANGULAR_PACKAGE_VERSION,
}
# Base rollup globals for everything in the repo.
ROLLUP_GLOBALS = {
'tslib': 'tslib',
+37
View File
@@ -0,0 +1,37 @@
#!/bin/bash
# Exit if any of the builds fails
set -e
# Source directories for packages to build with bazel and copy into dist/
# We use the bazel tag "publish" as a marker for rules we care about here.
packages=$(bazel query --output=package 'attr(tags, publish, ...)' | xargs -n1 basename)
# The bazel-bin directory where the bazel output is written
bazel_bin="$(bazel info bazel-bin)"
# The dist/ directory where we want to copy the final result. Clear it out to avoid
# any artifacts from earlier builds being retained.
packages_dist="./dist/bazel-packages"
rm -rf ${packages_dist}
mkdir -p ${packages_dist}
for p in ${packages[@]}
do
bazel build src/${p}:npm_package
# The output for "lib" should go into a directory named "material"
[[ ${p} = "lib" ]] && out_dir="material" || out_dir="${p}"
# Copy without preserving the read-only mode from bazel so that we can make final modifications
# to the generated package.
mkdir -p ${packages_dist}/${out_dir}
cp -r --no-preserve=mode ${bazel_bin}/src/${p}/npm_package/* ${packages_dist}/${out_dir}
done
# Update the root @angular/material metadata file to re-export metadata from each entry-point.
./scripts/bazel/update-material-metadata-reexports.js
# Create a tgz for each package with `npm pack`. Change directory in a subshell because
# `npm pack` always outputs to the current working directory.
(cd ${packages_dist} ; find . -maxdepth 1 -mindepth 1 -type d | xargs npm pack)
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env node
/**
* Script to manually add metadata re-exports to the @angular/material package generated
* via the ng_package bazel rule. This is rather special to the @angular/material package,
* so it hasn't been built into the ng_package rule yet.
*
* This is necessary for being able to import Angular constructs from "@angular/material"
* while still bundling at a per-component level.
*/
const path = require('path');
const fs = require('fs');
const materialPackageDir = path.join(__dirname, '..', '..', 'dist', 'bazel-packages', 'material');
if (!fs.existsSync(materialPackageDir)) {
console.error('The @angular/material package has not been written to dist/bazel-packages.');
process.exit(1);
}
// We only add metadata re-exports for entry-points that *have* metadata (except the root,
// which we're going to overwrite). Create a set of export entries in the form:
// {"from": "./button/button_public_index"}.
const secondaryEntryPointExports = fs.readdirSync(materialPackageDir)
.filter(f => f.endsWith('metadata.json') && !f.includes('public_index'))
.map(f => path.basename(f, '.metadata.json'))
.map(e => ({from: `./${e}/${e}_public_index`}));
// Read the root metadata for @angular/material from the bazel output so we can modify it with
// the additonal exports.
const rootMetadataPath = path.join(materialPackageDir, 'material_public_index.metadata.json');
const rootMetadata = JSON.parse(fs.readFileSync(rootMetadataPath, 'utf-8'));
rootMetadata.exports = secondaryEntryPointExports;
fs.writeFileSync(rootMetadataPath, JSON.stringify(rootMetadata), 'utf-8');
+3 -1
View File
@@ -1,6 +1,6 @@
package(default_visibility=["//visibility:public"])
load("@angular//:index.bzl", "ng_module", "ng_package")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
ng_module(
@@ -17,5 +17,7 @@ ng_package(
srcs = ["package.json"],
entry_point = "src/cdk-experimental/public_api.js",
globals = ROLLUP_GLOBALS,
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
deps = [":cdk-experimental"],
tags = ["publish"],
)
+3 -1
View File
@@ -1,7 +1,7 @@
package(default_visibility=["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@angular//:index.bzl", "ng_package")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
# Export the CDK tsconfig so that subpackages can reference it directly.
exports_files(["tsconfig-build.json"])
@@ -21,5 +21,7 @@ ng_package(
srcs = ["package.json"],
entry_point = "src/cdk/public_api.js",
globals = ROLLUP_GLOBALS,
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
deps = CDK_TARGETS,
tags = ["publish"],
)
+4 -1
View File
@@ -2,7 +2,8 @@ package(default_visibility=["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@angular//:index.bzl", "ng_package", "ng_module")
load("//tools:sass_bundle.bzl", "sass_bundle")
load("//:packages.bzl", "MATERIAL_PACKAGES", "MATERIAL_TARGETS", "ROLLUP_GLOBALS")
load("//:packages.bzl", "MATERIAL_PACKAGES", "MATERIAL_TARGETS", "ROLLUP_GLOBALS",
"VERSION_PLACEHOLDER_REPLACEMENTS")
# Export the root material tsconfig so that subpackages can reference it directly.
@@ -47,5 +48,7 @@ ng_package(
"//src/lib/prebuilt-themes:purple-green",
],
packages = ["//src/lib/schematics:npm_package"],
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
deps = MATERIAL_TARGETS,
tags = ["publish"],
)
+3 -1
View File
@@ -1,6 +1,6 @@
package(default_visibility=["//visibility:public"])
load("@angular//:index.bzl", "ng_module", "ng_package")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
ng_module(
@@ -18,5 +18,7 @@ ng_package(
srcs = ["package.json"],
entry_point = "src/material-experimental/public_api.js",
globals = ROLLUP_GLOBALS,
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
deps = [":material-experimental"],
tags = ["publish"],
)
+3 -1
View File
@@ -1,7 +1,7 @@
package(default_visibility=["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@angular//:index.bzl", "ng_package")
load("//:packages.bzl", "ROLLUP_GLOBALS")
load("//:packages.bzl", "ROLLUP_GLOBALS", "VERSION_PLACEHOLDER_REPLACEMENTS")
ts_library(
@@ -17,5 +17,7 @@ ng_package(
srcs = ["package.json"],
entry_point = "src/material-moment-adapter/public_api.js",
globals = ROLLUP_GLOBALS,
replacements = VERSION_PLACEHOLDER_REPLACEMENTS,
deps = [":material-moment-adapter"],
tags = ["publish"],
)