forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdevtools_plugin.js
More file actions
157 lines (137 loc) · 5.23 KB
/
Copy pathdevtools_plugin.js
File metadata and controls
157 lines (137 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-check
const path = require('path');
/**
* `path.dirname` does not include trailing slashes. If we would always
* use `path.dirname` and then later perform comparisons on the paths that
* it returns, we could run into false positives. For example, given the
* the following two paths:
*
* front_end/timeline_model/TimelineModel.js
* front_end/timeline/Timeline.js
*
* And that would have the following values for `path.dirname`:
*
* front_end/timeline_model
* front_end/timeline
*
* If we would do a simple `.startswith` on the `path.dirname` of both of
* these paths, then the first path would start with the dirname of the
* second. However, they *are* part of different folders. To fix that problem,
* we need to force a path separator after each folder. That makes sure we
* and up with the following comparison of path dirnames:
*
* front_end/timeline_model/
* front_end/timeline/
*
* Now, the first path does *not* start with the second one, as expected.
*
* @param {string} file
* @return {string}
*/
function dirnameWithSeparator(file) {
return path.dirname(file) + path.sep;
}
function devtoolsPlugin(source, importer) {
if (!importer) {
return null;
}
if (source === '../../lib/codemirror' || !source.startsWith('.')) {
// These are imported via require(...), but we don't use
// @rollup/plugin-commonjs. So this check is not necessary for rollup. But
// need to have this for esbuild as it doesn't ignore require(...).
return {
id: source,
external: true,
};
}
const currentDirectory = path.normalize(dirnameWithSeparator(importer));
const importedFilelocation = path.normalize(path.join(currentDirectory, source));
const importedFileDirectory = dirnameWithSeparator(importedFilelocation);
// Generated files are part of other directories, as they are only imported once
if (path.basename(importedFileDirectory) === 'generated') {
return null;
}
// An import is considered external (and therefore a separate
// bundle) if its filename matches its immediate parent's folder
// name (without the extension). For example:
// `import * as Components from './components/components.js'` = external
// `import * as UI from '../ui/ui.js'` = external
// `import * as Lit from '../third_party/lit/lit.js'` = external
// `import {DataGrid} from './components/DataGrid.js'` = not external
// `import * as Components from './components/foo.js'` = not external
// Note that we can't do a simple check for only `third_party`, as in Chromium
// our full path is `third_party/devtools-frontend/src/`, which thus *always*
// includes third_party. It also not possible to use the current directory
// as a check for the import, as the import will be different in Chromium and
// would therefore not match the path of `__dirname`.
// These should be removed because the new heuristic _should_ deal with these
// e.g. it'll pick up third_party/lit/lit.js is its own entrypoint
// The CodeMirror addons look like bundles (addon/comment/comment.js) but are not.
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'codemirror', 'package'))) {
return null;
}
// The LightHouse bundle shouldn't be processed by `terser` again, as it is uniquely built
if (importedFilelocation.includes(path.join('front_end', 'third_party', 'lighthouse', 'lighthouse-dt-bundle.js'))) {
return {
id: importedFilelocation,
external: true,
};
}
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'puppeteer', 'package'))) {
// Ignore possible dynamic imports from the Node folder.
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'puppeteer', 'package', 'lib', 'esm', 'puppeteer', 'node'))) {
return {
id: importedFilelocation,
external: true,
};
}
return {
id: importedFilelocation,
external: false,
};
}
if (importedFileDirectory.includes(path.join('front_end', 'third_party', 'puppeteer-replay', 'package'))) {
return {
id: importedFilelocation,
external: false,
};
}
const importedFileName = path.basename(importedFilelocation, '.js');
const importedFileParentDirectory = path.basename(path.dirname(importedFilelocation));
const isExternal = importedFileName === importedFileParentDirectory;
return {
id: importedFilelocation,
external: isExternal,
};
}
function esbuildPlugin(outdir) {
return args => {
// args.importer is absolute path in esbuild.
const res = devtoolsPlugin(args.path, args.importer);
if (!res) {
return null;
}
if (res.external) {
// res.id can be both of absolutized local JavaScript path or node's
// builtin module (e.g. 'fs', 'path'), and only relativize the path in
// former case.
if (path.isAbsolute(res.id)) {
res.id = './' + path.relative(outdir, res.id);
}
return {
external: res.external,
path: res.id,
};
}
return {
path: res.id,
};
};
}
module.exports = {
devtoolsPlugin,
esbuildPlugin
};