Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit e2a46b7

Browse files
authored
build: Return array of resolved secondary entry points (#2149)
- Bump to latest `@rollup/plugin-alias` - Cleanup and compatibility fixes with rollup alias - Re-enable source maps during `terserjs.minify()` - Use variable to pass required MDC version during a build
1 parent cf63a30 commit e2a46b7

18 files changed

+351
-168
lines changed

build-config.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ const packageJson = require('./package.json');
1010
const buildVersion = packageJson.version;
1111

1212
/**
13-
* Required Angular version for all Angular Material packages. This version will be used
13+
* Required Angular version for all Angular MDC packages. This version will be used
1414
* as the peer dependency version for Angular in all release packages.
1515
*/
16-
const angularVersion = packageJson.requiredAngularVersion;
16+
const angularVersion = packageJson.requiredAngularVersion;
17+
18+
/**
19+
* Required MDC Web version for all Angular MDC packages. This version will be used
20+
* as the peer dependency version for MDC Web in all release packages that require MDC Web.
21+
*/
22+
const mdcVersion = packageJson.requiredMDCVersion;
1723

1824
/** License that will be placed inside of all created bundles. */
1925
const buildLicense = `/**
@@ -27,6 +33,7 @@ const buildLicense = `/**
2733
module.exports = {
2834
projectVersion: buildVersion,
2935
angularVersion: angularVersion,
36+
mdcVersion: mdcVersion,
3037
projectDir: __dirname,
3138
packagesDir: join(__dirname, 'packages'),
3239
outputDir: join(__dirname, 'dist'),

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"yarn": ">= 1.19.1"
1515
},
1616
"requiredAngularVersion": "^9.0.0",
17+
"requiredMDCVersion": "^5.1.0",
1718
"scripts": {
1819
"build": "gulp web:build",
1920
"build:release": "gulp web:build-release",
@@ -31,9 +32,8 @@
3132
"@angular/core": "^9.0.4",
3233
"@angular/forms": "^9.0.4",
3334
"@angular/platform-browser": "^9.0.4",
34-
"core-js": "2.6.9",
35+
"core-js": "^2.6.9",
3536
"material-components-web": "^5.1.0",
36-
"rollup-plugin-alias": "1.5.2",
3737
"rxjs": "^6.5.3",
3838
"tsickle": "0.38.0",
3939
"tslib": "1.11.1",
@@ -44,6 +44,7 @@
4444
"@angular/platform-browser-dynamic": "^9.0.4",
4545
"@babel/core": "^7.8.6",
4646
"@babel/preset-env": "^7.8.6",
47+
"@rollup/plugin-alias": "^3.0.1",
4748
"@rollup/plugin-node-resolve": "7.1.1",
4849
"@types/fs-extra": "^8.1.0",
4950
"@types/glob": "^7.1.1",

packages/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"private": false,
2424
"dependencies": {
2525
"@angular-mdc/theme": "0.0.0-PLACEHOLDER",
26-
"material-components-web": "^5.1.0",
26+
"material-components-web": "0.0.0-MDC",
2727
"@angular/cdk": "^9.1.0"
2828
},
2929
"peerDependencies": {

packages/tsconfig-build.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"types": [],
2525
"baseUrl": ".",
2626
"paths": {
27+
"@angular-mdc/web": ["../dist/packages/web"],
2728
"@angular-mdc/web/*": ["../dist/packages/web/*"]
2829
}
2930
},

tools/package-tools/build-bundles.ts

+29-28
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {remapSourcemap} from './sourcemap-remap';
88

99
// There are no type definitions available for these imports.
1010
const rollup = require('rollup');
11-
const rollupNodeResolutionPlugin = require('@rollup/plugin-node-resolve');
12-
const rollupAlias = require('rollup-plugin-alias');
11+
const rollupNodeResolve = require('@rollup/plugin-node-resolve');
12+
const rollupAlias = require('@rollup/plugin-alias');
1313
const babel = require('rollup-plugin-babel');
1414

1515
/** Directory where all bundles will be created in. */
@@ -20,13 +20,13 @@ export class PackageBundler {
2020
/** Name of the AMD module for the primary entry point of the build package. */
2121
private readonly primaryAmdModuleName: string;
2222

23-
constructor(private buildPackage: BuildPackage) {
24-
this.primaryAmdModuleName = this.getAmdModuleName(buildPackage.name);
23+
constructor(private _buildPackage: BuildPackage) {
24+
this.primaryAmdModuleName = this._getAmdModuleName(_buildPackage.name);
2525
}
2626

2727
/** Creates all bundles for the package and all associated entry points (UMD, ES5, ES2015). */
2828
async createBundles() {
29-
for (const entryPoint of this.buildPackage.secondaryEntryPoints) {
29+
for (const entryPoint of this._buildPackage.secondaryEntryPoints) {
3030
await this.bundleSecondaryEntryPoint(entryPoint);
3131
}
3232

@@ -35,11 +35,11 @@ export class PackageBundler {
3535

3636
/** Bundles the primary entry-point w/ given entry file, e.g. @angular-mdc/web */
3737
private async bundlePrimaryEntryPoint() {
38-
const packageName = this.buildPackage.name;
38+
const packageName = this._buildPackage.name;
3939

4040
return this.bundleEntryPoint({
41-
entryFile: this.buildPackage.entryFilePath,
42-
esm5EntryFile: join(this.buildPackage.esm5OutputDir, 'index.js'),
41+
entryFile: this._buildPackage.entryFilePath,
42+
esm5EntryFile: join(this._buildPackage.esm5OutputDir, 'index.js'),
4343
importName: `@angular-mdc/${packageName}`,
4444
moduleName: this.primaryAmdModuleName,
4545
esm2015Dest: join(bundlesDir, `${packageName}.js`),
@@ -51,15 +51,15 @@ export class PackageBundler {
5151

5252
/** Bundles a single secondary entry-point w/ given entry file, e.g. @angular-mdc/web/button */
5353
private async bundleSecondaryEntryPoint(entryPointName: string) {
54-
const packageName = this.buildPackage.name;
55-
const entryFile = join(this.buildPackage.outputDir, entryPointName, 'index.js');
56-
const esm5EntryFile = join(this.buildPackage.esm5OutputDir, entryPointName, 'index.js');
54+
const packageName = this._buildPackage.name;
55+
const entryFile = join(this._buildPackage.outputDir, entryPointName, 'index.js');
56+
const esm5EntryFile = join(this._buildPackage.esm5OutputDir, entryPointName, 'index.js');
5757

5858
return this.bundleEntryPoint({
5959
entryFile,
6060
esm5EntryFile,
6161
importName: `@angular-mdc/${packageName}/${entryPointName}`,
62-
moduleName: this.getAmdModuleName(packageName, entryPointName),
62+
moduleName: this._getAmdModuleName(packageName, entryPointName),
6363
esm2015Dest: join(bundlesDir, `${packageName}`, `${entryPointName}.js`),
6464
esm5Dest: join(bundlesDir, `${packageName}`, `${entryPointName}.es5.js`),
6565
umdDest: join(bundlesDir, `${packageName}-${entryPointName}.umd.js`),
@@ -70,7 +70,7 @@ export class PackageBundler {
7070
/**
7171
* Creates the ES5, ES2015, and UMD bundles for the specified entry-point.
7272
* @param config Configuration that specifies the entry-point, module name, and output
73-
* bundle paths.
73+
* bundle paths.
7474
*/
7575
private async bundleEntryPoint(config: BundlesConfig) {
7676
// Build FESM-2015 bundle file.
@@ -107,7 +107,6 @@ export class PackageBundler {
107107
await remapSourcemap(config.esm2015Dest);
108108
await remapSourcemap(config.esm5Dest);
109109
await remapSourcemap(config.umdDest);
110-
// await remapSourcemap(config.umdMinDest);
111110
}
112111

113112
/** Creates a rollup bundle of a specified JavaScript file.*/
@@ -164,7 +163,7 @@ export class PackageBundler {
164163
// For UMD bundles, we need to adjust the `external` bundle option in order to include
165164
// all necessary code in the bundle.
166165
if (config.format === 'umd') {
167-
bundleOptions.plugins.push(rollupNodeResolutionPlugin());
166+
bundleOptions.plugins.push(rollupNodeResolve());
168167
// For all UMD bundles, we want to exclude tslib from the `external` bundle option so that
169168
// it is inlined into the bundle.
170169
let external = Object.keys(rollupGlobals);
@@ -173,17 +172,18 @@ export class PackageBundler {
173172
// If each secondary entry-point is re-exported at the root, we want to exclude those
174173
// secondary entry-points from the rollup globals because we want the UMD for the
175174
// primary entry-point to include *all* of the sources for those entry-points.
176-
if (this.buildPackage.exportsSecondaryEntryPointsAtRoot &&
175+
if (this._buildPackage.exportsSecondaryEntryPointsAtRoot &&
177176
config.moduleName === this.primaryAmdModuleName) {
178177

179-
const importRegex = new RegExp(`@angular-mdc/${this.buildPackage.name}/.+`);
178+
const importRegex = new RegExp(`@angular-mdc/${this._buildPackage.name}/.+`);
180179
external = external.filter(e => !importRegex.test(e));
181180

182181
// Use the rollup-alias plugin to map imports of the form `@angular-mdc/web/button`
183182
// to the actual file location so that rollup can resolve the imports (otherwise they
184183
// will be treated as external dependencies and not included in the bundle).
185-
bundleOptions.plugins.push(
186-
rollupAlias(this.getResolvedSecondaryEntryPointImportPaths(config.dest)));
184+
bundleOptions.plugins.push(rollupAlias({
185+
entries: this._getResolvedSecondaryEntryPointImportPaths(config.dest)
186+
}));
187187
}
188188
bundleOptions.external = external;
189189
}
@@ -192,24 +192,25 @@ export class PackageBundler {
192192
}
193193

194194
/**
195-
* Gets mapping of import aliases (e.g. `@angular-mdc/web/button`) to the path of the es5
195+
* Gets array of import aliases (e.g. `@angular-mdc/web/button`) with the path of the es5
196196
* bundle output.
197197
* @param bundleOutputDir Path to the bundle output directory.
198-
* @returns Map of alias to resolved path.
198+
* @returns Array of alias with resolved path.
199199
*/
200-
private getResolvedSecondaryEntryPointImportPaths(bundleOutputDir: string) {
201-
return this.buildPackage.secondaryEntryPoints.reduce((map, p) => {
202-
map[`@angular-mdc/${this.buildPackage.name}/${p}`] =
203-
join(dirname(bundleOutputDir), this.buildPackage.name, `${p}.es5.js`);
204-
return map;
205-
}, {} as {[key: string]: string});
200+
private _getResolvedSecondaryEntryPointImportPaths(bundleOutputDir: string) {
201+
return this._buildPackage.secondaryEntryPoints.map(p => {
202+
return {
203+
find: `@angular-mdc/${this._buildPackage.name}/${p}`,
204+
replacement: join(dirname(bundleOutputDir), this._buildPackage.name, `${p}.es5.js`)
205+
};
206+
});
206207
}
207208

208209
/**
209210
* Gets the AMD module name for a package and an optional entry point. This is consistent
210211
* to the module name format being used in "angular/angular".
211212
*/
212-
private getAmdModuleName(packageName: string, entryPointName?: string) {
213+
private _getAmdModuleName(packageName: string, entryPointName?: string) {
213214
let amdModuleName = `ng.${dashCaseToCamelCase(packageName)}`;
214215

215216
if (entryPointName) {

tools/package-tools/build-config.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface BuildConfig {
55
projectVersion: string;
66
/** Required Angular version for the project. */
77
angularVersion: string;
8+
/** Required MDC Web version for the project. */
9+
mdcVersion: string;
810
/** Path to the root of the project. */
911
projectDir: string;
1012
/** Path to the directory where all packages are living. */
@@ -18,10 +20,5 @@ export interface BuildConfig {
1820
// Search for a build config by walking up the current working directory of the Node process.
1921
const buildConfigPath = findBuildConfig();
2022

21-
if (!buildConfigPath) {
22-
throw 'Angular MDC build tools were not able to find a build config. ' +
23-
'Please create a "build-config.js" file in your project.';
24-
}
25-
2623
// Load the config file using a basic CommonJS import.
2724
export const buildConfig = require(buildConfigPath) as BuildConfig;

tools/package-tools/compile-entry-point.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function addIdToGlob(outputPath: string, entryPointId: number): void {
5151
fileContent = fileContent.replace(/ɵ(ɵ)?[a-z]+/g,
5252
(match, isDoubleTheta) => {
5353
return isDoubleTheta ? match : match + entryPointId;
54-
}); writeFileSync(filePath, fileContent, 'utf-8');
54+
});
55+
writeFileSync(filePath, fileContent, 'utf-8');
5556
});
5657
}

tools/package-tools/copy-files.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { sync as glob } from 'glob';
2-
import { mkdirpSync, copySync } from 'fs-extra';
3-
import { join, dirname } from 'path';
1+
import {sync as glob} from 'glob';
2+
import {mkdirpSync, copySync} from 'fs-extra';
3+
import {join, dirname} from 'path';
44

55
/** Function to copy files that match a glob to another directory. */
66
export function copyFiles(fromPath: string, fileGlob: string, outDir: string) {
7-
glob(fileGlob, { cwd: fromPath }).forEach(filePath => {
7+
glob(fileGlob, {cwd: fromPath}).forEach(filePath => {
88
const fileDestPath = join(outDir, filePath);
99
mkdirpSync(dirname(fileDestPath));
1010
copySync(join(fromPath, filePath), fileDestPath);

tools/package-tools/entry-point-package-json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { join } from 'path';
2-
import { writeFileSync } from 'fs';
1+
import {join} from 'path';
2+
import {writeFileSync} from 'fs';
33

44
/** Creates a package.json for a secondary entry-point with the different bundle locations. */
55
export function createEntryPointPackageJson(destDir: string, packageName: string,

tools/package-tools/find-build-config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { resolve, dirname, join } from 'path';
2-
import { existsSync } from 'fs';
1+
import {resolve, dirname, join} from 'path';
2+
import {existsSync} from 'fs';
33

44
/** Name of the build config file. */
55
const BUILD_CONFIG_FILENAME = 'build-config.js';

tools/package-tools/metadata-inlining.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFileSync, writeFileSync } from 'fs';
2-
import { sync as glob } from 'glob';
3-
import { basename, join } from 'path';
1+
import {readFileSync, writeFileSync} from 'fs';
2+
import {sync as glob} from 'glob';
3+
import {basename, join} from 'path';
44

55
/**
66
* Recurse through a parsed metadata.json file and inline all html and css.

tools/package-tools/metadata-reexport.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { writeFileSync } from 'fs';
2-
import { join } from 'path';
1+
import {writeFileSync} from 'fs';
2+
import {join} from 'path';
33

44
/** Creates a metadata file that re-exports the metadata bundle inside of the typings. */
55
export function createMetadataReexportFile(destDir: string, from: string | string[],
@@ -10,7 +10,7 @@ export function createMetadataReexportFile(destDir: string, from: string | strin
1010
__symbolic: 'module',
1111
version: 3,
1212
metadata: {},
13-
exports: from.map(f => ({ from: f })),
13+
exports: from.map(f => ({from: f})),
1414
flatModuleIndexRedirect: true,
1515
importAs: importAsName
1616
}, null, 2);

tools/package-tools/minify-sources.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const terser = require('terser');
55

66
/** Minifies a JavaScript file by using terser-js. Also writes sourcemaps to the output. */
77
export function terserJs(inputPath: string, outputPath: string) {
8-
// const sourceMapPath = `${outputPath}.map`;
8+
const sourceMapPath = `${outputPath}.map`;
99

1010
const result = terser.minify(readFileSync(inputPath, 'utf8'), {
11-
// sourceMap: {
12-
// content: `${inputPath}.map`,
13-
// url: sourceMapPath
14-
// },
11+
sourceMap: {
12+
filename: `${inputPath}.map`,
13+
url: sourceMapPath
14+
},
1515
output: {
1616
comments: 'some'
1717
}
@@ -20,5 +20,5 @@ export function terserJs(inputPath: string, outputPath: string) {
2020
// console.log(result.error); // runtime error, `undefined` in this case
2121
// console.log(result.warnings); // [ 'Dropping unused variable u [0:1,18]' ]
2222
writeFileSync(outputPath, result.code);
23-
// writeFileSync(sourceMapPath, result.map);
23+
writeFileSync(sourceMapPath, result.map);
2424
}

tools/package-tools/secondary-entry-points.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { join } from 'path';
2-
import { readdirSync, lstatSync, existsSync } from 'fs';
3-
import { spawnSync } from 'child_process';
4-
import { BuildPackage } from './build-package';
5-
import { platform } from 'os';
1+
import {join} from 'path';
2+
import {readdirSync, lstatSync, existsSync} from 'fs';
3+
import {spawnSync} from 'child_process';
4+
import {BuildPackage} from './build-package';
5+
import {platform} from 'os';
66

77
/**
88
* Gets secondary entry-points for a given package in the order they should be built.
@@ -23,7 +23,7 @@ export function getSecondaryEntryPointsForPackage(pkg: BuildPackage) {
2323
.filter(d => existsSync(join(packageDir, d, 'tsconfig-build.json')));
2424

2525
// Create nodes that comprise the build graph.
26-
const buildNodes: BuildNode[] = entryPoints.map(p => ({ name: p, deps: [], depth: 0 }));
26+
const buildNodes: BuildNode[] = entryPoints.map(p => ({name: p, deps: [], depth: 0}));
2727

2828
// Create a lookup for name -> build graph node.
2929
const nodeLookup = buildNodes.reduce((lookup, node) => {

tools/package-tools/ts-compile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {spawn} from 'child_process';
77
* @param flags Command-line flags to be passed to binary.
88
* @returns Promise that resolves/rejects when the child process exits.
99
*/
10-
export function tsCompile(binary: 'tsc' | 'ngc', flags: string[]) {
10+
export function tsCompile(binary: 'ngc', flags: string[]) {
1111
return new Promise((resolve, reject) => {
1212
const binaryPath = resolvePath(`./node_modules/.bin/${binary}`);
1313
const childProcess = spawn(binaryPath, flags, {shell: true});

tools/package-tools/typings-reexport.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { writeFileSync } from 'fs';
2-
import { buildConfig } from './build-config';
3-
import { join } from 'path';
1+
import {writeFileSync} from 'fs';
2+
import {buildConfig} from './build-config';
3+
import {join} from 'path';
44

55
/** Create a typing file that links to the bundled definitions of NGC. */
66
export function createTypingsReexportFile(outDir: string, from: string, fileName: string) {

0 commit comments

Comments
 (0)