Ben Rohlfs | b3e7cd5 | 2021-08-10 11:42:54 +0200 | [diff] [blame] | 1 | /** |
| 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 | const path = require('path'); |
| 18 | |
| 19 | // In this file word "plugin" refers to rollup plugin, not Gerrit plugin. |
| 20 | // By default, require(plugin_name) tries to find module plugin_name starting |
| 21 | // from the folder where this file (rollup.config.js) is located |
| 22 | // (see https://www.typescriptlang.org/docs/handbook/module-resolution.html#node |
| 23 | // and https://nodejs.org/api/modules.html#modules_all_together). |
| 24 | // So, rollup.config.js can't be in polygerrit-ui/app dir and it should be in |
| 25 | // tools/node_tools directory (where all plugins are installed). |
| 26 | // But rollup_bundle rule copy this .config.js file to another directory, |
| 27 | // so require(plugin_name) can't find a plugin. |
| 28 | // To fix it, requirePlugin tries: |
| 29 | // 1. resolve module id using default behavior, i.e. it starts from __dirname |
| 30 | // 2. if module not found - it tries to resolve module starting from rollupBin |
| 31 | // location. |
| 32 | // This workaround also gives us additional power - we can place .config.js |
| 33 | // file anywhere in a source tree and add all plugins in the same package.json |
| 34 | // file as rollup node module. |
| 35 | function requirePlugin(id) { |
| 36 | const rollupBinDir = path.dirname(process.argv[1]); |
| 37 | const pluginPath = require.resolve(id, {paths: [__dirname, rollupBinDir] }); |
| 38 | return require(pluginPath); |
| 39 | } |
| 40 | |
| 41 | const resolve = requirePlugin('rollup-plugin-node-resolve'); |
| 42 | |
| 43 | export default { |
| 44 | treeshake: false, |
| 45 | onwarn: warning => { |
| 46 | // No warnings from rollupjs are allowed. |
| 47 | // Most of the warnings are real error in our code (for example, |
| 48 | // if some import couldn't be resolved we can't continue, but rollup |
| 49 | // reports it as a warning) |
| 50 | throw new Error(warning.message); |
| 51 | }, |
| 52 | // Context must be set to window to correctly process global variables |
| 53 | context: 'window', |
| 54 | plugins: [resolve({ |
| 55 | customResolveOptions: { |
| 56 | moduleDirectory: 'external/plugins_npm/node_modules', |
| 57 | }, |
| 58 | })], |
| 59 | }; |