Takuto Ikuta | 2548a6c | 2022-02-01 12:10:02 | [diff] [blame^] | 1 | // Copyright 2022 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // @ts-check |
| 6 | |
| 7 | const path = require('path'); |
| 8 | |
| 9 | const devtools_paths = require('../devtools_paths.js'); |
| 10 | const devtools_plugin = require('./devtools_plugin.js'); |
| 11 | |
| 12 | // esbuild module uses binary in this path. |
| 13 | process.env.ESBUILD_BINARY_PATH = path.join(devtools_paths.devtoolsRootPath(), 'third_party', 'esbuild', 'esbuild'); |
| 14 | |
| 15 | const entryPoints = [process.argv[2]]; |
| 16 | const outfile = process.argv[3]; |
| 17 | |
| 18 | const outdir = path.dirname(outfile); |
| 19 | |
| 20 | const plugin = { |
| 21 | name: 'devtools-plugin', |
| 22 | setup(build) { |
| 23 | // https://esbuild.github.io/plugins/#on-resolve |
| 24 | build.onResolve({filter: /.*/}, args => { |
| 25 | const res = devtools_plugin.devtoolsPlugin(args.path, args.importer); |
| 26 | if (!res) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | if (res.external && res.id) { |
| 31 | return { |
| 32 | external: res.external, |
| 33 | path: './' + path.relative(outdir, res.id), |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | if (res.external) { |
| 38 | return { |
| 39 | external: true, |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | return { |
| 44 | path: res.id, |
| 45 | }; |
| 46 | }); |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | require('esbuild') |
| 51 | .build({ |
| 52 | entryPoints, |
| 53 | outfile, |
| 54 | bundle: true, |
| 55 | format: 'esm', |
| 56 | platform: 'browser', |
| 57 | plugins: [plugin], |
| 58 | }) |
| 59 | .catch(err => { |
| 60 | console.error('failed to run esbuild:', err); |
| 61 | process.exit(1); |
| 62 | }); |