blob: c3a134087856a7fa7c813a021cc8d0f151d5ec7e [file] [log] [blame]
Martin Waitzb6cb30db2021-11-05 19:44:26 +01001/**
2 * @license
3 * Copyright (C) 2021 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18const path = require('path');
19
20// In this file word "plugin" refers to rollup plugin, not Gerrit plugin.
21// By default, require(plugin_name) tries to find module plugin_name starting
22// from the folder where this file (rollup.config.js) is located
23// (see https://www.typescriptlang.org/docs/handbook/module-resolution.html#node
24// and https://nodejs.org/api/modules.html#modules_all_together).
25// So, rollup.config.js can't be in polygerrit-ui/app dir and it should be in
26// tools/node_tools directory (where all plugins are installed).
27// But rollup_bundle rule copy this .config.js file to another directory,
28// so require(plugin_name) can't find a plugin.
29// To fix it, requirePlugin tries:
30// 1. resolve module id using default behavior, i.e. it starts from __dirname
31// 2. if module not found - it tries to resolve module starting from rollupBin
32// location.
33// This workaround also gives us additional power - we can place .config.js
34// file anywhere in a source tree and add all plugins in the same package.json
35// file as rollup node module.
36function requirePlugin(id) {
37 const rollupBinDir = path.dirname(process.argv[1]);
38 const pluginPath = require.resolve(id, {paths: [__dirname, rollupBinDir] });
39 return require(pluginPath);
40}
41
42const cjs = requirePlugin('rollup-plugin-commonjs');
43const nodeResolve = requirePlugin('rollup-plugin-node-resolve');
44const {terser} = requirePlugin('rollup-plugin-terser');
45
46export default {
47 onwarn: warning => {
48 // No warnings from rollupjs are allowed.
49 // Most of the warnings are real error in our code (for example,
50 // if some import couldn't be resolved we can't continue, but rollup
51 // reports it as a warning)
52 throw new Error(warning.message);
53 },
54 output: {
55 format: 'iife',
56 compact: true,
57 strict: true,
58 exports: 'auto',
59 name: 'hljs',
60 footer: '',
61 plugins: [
62 terser({
63 output: {
64 comments: false
65 },
66 compress: {
67 ecma: 2015,
68 unsafe_arrows: true,
69 passes: 2,
70 unsafe: true,
71 warnings: true,
72 dead_code: true,
73 toplevel: "funcs"
74 }
75 })
76 ]
77 },
78 plugins: [
79 nodeResolve({
80 customResolveOptions: {
81 moduleDirectory: 'external/ui_npm/node_modules'
82 }
83 }),
84 cjs(),
85 ]
86};