Skip to content

Commit 562b85c

Browse files
alexeaglehansl
authored andcommitted
feat(@angular-devkit/build-optimizer): add rollup plugin
Also add a bazel build file so angular/angular can have a source dependency on it.
1 parent 4001ea6 commit 562b85c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright Google Inc. All Rights Reserved.
2+
#
3+
# Use of this source code is governed by an MIT-style license that can be
4+
# found in the LICENSE file at https://angular.io/license
5+
6+
licenses(["notice"]) # MIT
7+
8+
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
9+
10+
package(default_visibility = ["//visibility:public"])
11+
12+
ts_library(
13+
name = "lib",
14+
srcs = glob(
15+
["**/*.ts"],
16+
# Currently, this library is used only with the rollup plugin.
17+
# To make it simpler for downstream repositories to compile this, we
18+
# neither provide compile-time deps as an `npm_install` rule, nor do we
19+
# expect the downstream repository to install @types/webpack[-*]
20+
# So we exclude files that depend on webpack typings.
21+
exclude = [
22+
"src/build-optimizer/webpack-loader.ts",
23+
"src/purify/**",
24+
"src/index.ts",
25+
"**/*_spec.ts",
26+
],
27+
),
28+
# Note, intentionally no node_modules attribute - so it will use the
29+
# compile-time deps in the downstream repository's //:node_modules.
30+
# This creates the assumption that every consumer does have that target.
31+
tsconfig = "//:tsconfig.json",
32+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* @fileoverview This adapts the buildOptimizer to run over each file as it is
11+
* processed by Rollup. We must do this since buildOptimizer expects to see the
12+
* ESModules in the input sources, and therefore cannot run on the rollup output
13+
*/
14+
15+
import * as path from 'path';
16+
import { RawSourceMap } from 'source-map';
17+
import { buildOptimizer } from './build-optimizer';
18+
19+
const DEBUG = false;
20+
21+
export interface Options {
22+
sideEffectFreeModules?: string[];
23+
}
24+
25+
export default function optimizer(options: Options) {
26+
return {
27+
name: 'build-optimizer',
28+
transform: (content: string, id: string): {code: string, map: RawSourceMap}|null => {
29+
const isSideEffectFree = options.sideEffectFreeModules &&
30+
options.sideEffectFreeModules.some(m => id.indexOf(m) >= 0);
31+
const { content: code, sourceMap: map } = buildOptimizer({
32+
content, inputFilePath: id, emitSourceMap: true, isSideEffectFree,
33+
});
34+
if (!code) {
35+
if (DEBUG) {
36+
console.error('no transforms produced by buildOptimizer for '
37+
+ path.relative(process.cwd(), id));
38+
}
39+
40+
return null;
41+
}
42+
if (!map) {
43+
throw new Error('no sourcemap produced by buildOptimizer');
44+
}
45+
46+
return { code, map };
47+
},
48+
};
49+
}

0 commit comments

Comments
 (0)