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';
19
20const path = require('path');
21const ROOT_DIR = path.dirname(path.dirname(__dirname));  // The repo root.
22const OUT_SYMLINK = path.join(ROOT_DIR, 'ui/out');
23
24function defBundle(bundle, distDir) {
25  return {
26    input: `${OUT_SYMLINK}/tsc/${bundle}/index.js`,
27    output: {
28      name: bundle,
29      format: 'iife',
30      esModule: false,
31      file: `${OUT_SYMLINK}/${distDir}/${bundle}_bundle.js`,
32      sourcemap: true,
33    },
34    plugins: [
35      nodeResolve({
36        mainFields: ['browser'],
37        browser: true,
38        preferBuiltins: false,
39      }),
40      // emscripten conditionally executes require('fs') (likewise for
41      // others), when running under node. Rollup can't find those libraries
42      // so expects these to be present in the global scope, which then fails
43      // at runtime. To avoid this we ignore require('fs') and the like.
44      commonjs({
45        ignore: [
46          'fs',
47          'path',
48          'crypto',
49        ]
50      }),
51      // Protobufjs's inquire() uses eval but that's not really needed in
52      // the browser.
53      // See https://github.com/protobufjs/protobuf.js/issues/593
54      replace({
55        patterns: [{test: /eval\(.*\(moduleName\);/g, replace: 'undefined;'}]
56      }),
57      // Translate source maps to point back to the .ts sources.
58      sourcemaps(),
59    ],
60  };
61}
62
63function defServiceWorkerBundle() {
64  return {
65    input: `${OUT_SYMLINK}/tsc/service_worker/service_worker.js`,
66    output: {
67      name: 'service_worker',
68      format: 'iife',
69      esModule: false,
70      file: `${OUT_SYMLINK}/dist/service_worker.js`,
71      sourcemap: true,
72    },
73    plugins: [
74      nodeResolve({
75        mainFields: ['browser'],
76        browser: true,
77        preferBuiltins: false,
78      }),
79      commonjs(),
80      sourcemaps(),
81    ],
82  };
83}
84
85export default [
86  defBundle('frontend', 'dist_version'),
87  defBundle('controller', 'dist_version'),
88  defBundle('engine', 'dist_version'),
89  defBundle('chrome_extension', 'chrome_extension'),
90  defServiceWorkerBundle(),
91]
92