xref: /aosp_15_r20/external/perfetto/ui/config/rollup.config.js (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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
15const {uglify} = require('rollup-plugin-uglify');
16const commonjs = require('@rollup/plugin-commonjs');
17const nodeResolve = require('@rollup/plugin-node-resolve');
18const path = require('path');
19const replace = require('rollup-plugin-re');
20const sourcemaps = require('rollup-plugin-sourcemaps');
21
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      if (warning.code === 'CIRCULAR_DEPENDENCY') {
64        // Ignore circular dependency warnings coming from third party code.
65        if (warning.message.includes('node_modules')) {
66          return;
67        }
68
69        // Treat all other circular dependency warnings as errors.
70        throw new Error(
71          `Circular dependency detected in ${warning.importer}:\n\n  ${warning.cycle.join('\n  ')}`,
72        );
73      }
74
75      // Call the default warning handler for all remaining warnings.
76      warn(warning);
77    },
78  };
79}
80
81function defServiceWorkerBundle() {
82  return {
83    input: `${OUT_SYMLINK}/tsc/service_worker/service_worker.js`,
84    output: {
85      name: 'service_worker',
86      format: 'iife',
87      esModule: false,
88      file: `${OUT_SYMLINK}/dist/service_worker.js`,
89      sourcemap: true,
90    },
91    plugins: [
92      nodeResolve({
93        mainFields: ['browser'],
94        browser: true,
95        preferBuiltins: false,
96      }),
97      commonjs(),
98      sourcemaps(),
99    ],
100  };
101}
102
103function maybeUglify() {
104  const minifyEnv = process.env['MINIFY_JS'];
105  if (!minifyEnv) return [];
106  const opts =
107    minifyEnv === 'preserve_comments' ? {output: {comments: 'all'}} : undefined;
108  return [uglify(opts)];
109}
110
111const maybeBigtrace = process.env['ENABLE_BIGTRACE']
112  ? [defBundle('tsc/bigtrace', 'bigtrace', 'dist_version/bigtrace')]
113  : [];
114
115const maybeOpenPerfettoTrace = process.env['ENABLE_OPEN_PERFETTO_TRACE']
116  ? [defBundle('tsc', 'open_perfetto_trace', 'dist/open_perfetto_trace')]
117  : [];
118
119module.exports = [
120  defBundle('tsc', 'frontend', 'dist_version'),
121  defBundle('tsc', 'engine', 'dist_version'),
122  defBundle('tsc', 'traceconv', 'dist_version'),
123  defBundle('tsc', 'chrome_extension', 'chrome_extension'),
124  defServiceWorkerBundle(),
125]
126  .concat(maybeBigtrace)
127  .concat(maybeOpenPerfettoTrace);
128