• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import commonjs from '@rollup/plugin-commonjs';
16import nodeResolve from '@rollup/plugin-node-resolve';
17import replace from 'rollup-plugin-re';
18import sourcemaps from 'rollup-plugin-sourcemaps';
19import {uglify} from 'rollup-plugin-uglify';
20
21const path = require('path');
22const ROOT_DIR = path.dirname(path.dirname(__dirname)); // The repo root.
23const OUT_SYMLINK = path.join(ROOT_DIR, 'ui/out');
24
25function defBundle(tsRoot, bundle, distDir) {
26  return {
27    input: `${OUT_SYMLINK}/${tsRoot}/${bundle}/index.js`,
28    output: {
29      name: bundle,
30      format: 'iife',
31      esModule: false,
32      file: `${OUT_SYMLINK}/${distDir}/${bundle}_bundle.js`,
33      sourcemap: true,
34    },
35    plugins: [
36      nodeResolve({
37        mainFields: ['browser'],
38        browser: true,
39        preferBuiltins: false,
40      }),
41
42      commonjs({
43        strictRequires: true,
44      }),
45
46      replace({
47        patterns: [
48          // Protobufjs's inquire() uses eval but that's not really needed in
49          // the browser. https://github.com/protobufjs/protobuf.js/issues/593
50          {test: /eval\(.*\(moduleName\);/g, replace: 'undefined;'},
51
52          // Immer entry point has a if (process.env.NODE_ENV === 'production')
53          // but |process| is not defined in the browser. Bypass.
54          // https://github.com/immerjs/immer/issues/557
55          {test: /process\.env\.NODE_ENV/g, replace: "'production'"},
56        ],
57      }),
58
59      // Translate source maps to point back to the .ts sources.
60      sourcemaps(),
61    ].concat(maybeUglify()),
62    onwarn: function (warning, warn) {
63      // Ignore circular dependency warnings coming from third party code.
64      if (
65        warning.code === 'CIRCULAR_DEPENDENCY' &&
66        warning.importer.includes('node_modules')
67      ) {
68        return;
69      }
70
71      // Call the default warning handler for all remaining warnings.
72      warn(warning);
73    },
74  };
75}
76
77function defServiceWorkerBundle() {
78  return {
79    input: `${OUT_SYMLINK}/tsc/service_worker/service_worker.js`,
80    output: {
81      name: 'service_worker',
82      format: 'iife',
83      esModule: false,
84      file: `${OUT_SYMLINK}/dist/service_worker.js`,
85      sourcemap: true,
86    },
87    plugins: [
88      nodeResolve({
89        mainFields: ['browser'],
90        browser: true,
91        preferBuiltins: false,
92      }),
93      commonjs(),
94      sourcemaps(),
95    ],
96  };
97}
98
99function maybeUglify() {
100  const minifyEnv = process.env['MINIFY_JS'];
101  if (!minifyEnv) return [];
102  const opts =
103    minifyEnv === 'preserve_comments' ? {output: {comments: 'all'}} : undefined;
104  return [uglify(opts)];
105}
106
107const maybeBigtrace = process.env['ENABLE_BIGTRACE']
108  ? [defBundle('tsc/bigtrace', 'bigtrace', 'dist_version/bigtrace')]
109  : [];
110
111const maybeOpenPerfettoTrace = process.env['ENABLE_OPEN_PERFETTO_TRACE']
112  ? [defBundle('tsc', 'open_perfetto_trace', 'dist/open_perfetto_trace')]
113  : [];
114
115export default [
116  defBundle('tsc', 'frontend', 'dist_version'),
117  defBundle('tsc', 'engine', 'dist_version'),
118  defBundle('tsc', 'traceconv', 'dist_version'),
119  defBundle('tsc', 'chrome_extension', 'chrome_extension'),
120  defServiceWorkerBundle(),
121]
122  .concat(maybeBigtrace)
123  .concat(maybeOpenPerfettoTrace);
124