blob: ee5771a7dff07de475516c8de08b19f4f8c108cf [file] [log] [blame]
Takuto Ikuta2548a6c2022-02-01 12:10:021// 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
7const path = require('path');
8
9const devtools_paths = require('../devtools_paths.js');
10const devtools_plugin = require('./devtools_plugin.js');
11
12// esbuild module uses binary in this path.
13process.env.ESBUILD_BINARY_PATH = path.join(devtools_paths.devtoolsRootPath(), 'third_party', 'esbuild', 'esbuild');
14
15const entryPoints = [process.argv[2]];
16const outfile = process.argv[3];
17
18const outdir = path.dirname(outfile);
19
20const 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
50require('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 });