@theia/bundle-plugin 1.72.0-next.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import * as path from 'path';
18
+ import * as fs from 'fs';
19
+ import * as os from 'os';
20
+
21
+ import type { Compiler } from 'webpack';
22
+
23
+ const REQUIRE_RIPGREP = '@vscode/ripgrep';
24
+ const REQUIRE_BINDINGS = 'bindings';
25
+ const REQUIRE_PARCEL_WATCHER = './build/Release/watcher.node';
26
+
27
+ export interface NativeWebpackPluginOptions {
28
+ out: string;
29
+ trash: boolean;
30
+ ripgrep: boolean;
31
+ pty: boolean;
32
+ replacements?: Record<string, string>;
33
+ nativeBindings?: Record<string, string>;
34
+ }
35
+
36
+ export class NativeWebpackPlugin {
37
+
38
+ private bindings = new Map<string, string>();
39
+ private options: NativeWebpackPluginOptions;
40
+
41
+ constructor(options: NativeWebpackPluginOptions) {
42
+ this.options = options;
43
+ for (const [name, value] of Object.entries(options.nativeBindings ?? {})) {
44
+ this.nativeBinding(name, value);
45
+ }
46
+ }
47
+
48
+ nativeBinding(dependency: string, nodePath: string): void {
49
+ this.bindings.set(dependency, nodePath);
50
+ }
51
+
52
+ apply(compiler: Compiler): void {
53
+ let replacements: Record<string, (issuer: string) => Promise<string>> = {};
54
+ let nodePtyIssuer: string | undefined;
55
+ let trashHelperIssuer: string | undefined;
56
+ let ripgrepIssuer: string | undefined;
57
+ compiler.hooks.initialize.tap(NativeWebpackPlugin.name, async () => {
58
+ const directory = path.resolve(compiler.outputPath, 'native-webpack-plugin');
59
+ await fs.promises.mkdir(directory, { recursive: true });
60
+ const bindingsFile = (issuer: string) => buildFile(directory, 'bindings.js', bindingsReplacement(issuer, Array.from(this.bindings.entries())));
61
+ const ripgrepFile = () => buildFile(directory, 'ripgrep.js', ripgrepReplacement(this.options.out));
62
+ replacements = {
63
+ ...(this.options.replacements ?? {}),
64
+ [REQUIRE_RIPGREP]: ripgrepFile,
65
+ [REQUIRE_BINDINGS]: bindingsFile,
66
+ [REQUIRE_PARCEL_WATCHER]: issuer => Promise.resolve(findNativeWatcherFile(issuer))
67
+ };
68
+ });
69
+ compiler.hooks.normalModuleFactory.tap(
70
+ NativeWebpackPlugin.name,
71
+ nmf => {
72
+ nmf.hooks.beforeResolve.tapPromise(NativeWebpackPlugin.name, async result => {
73
+ if (result.request === REQUIRE_RIPGREP) {
74
+ ripgrepIssuer = result.contextInfo.issuer;
75
+ } else if (result.request === 'node-pty') {
76
+ nodePtyIssuer = result.contextInfo.issuer;
77
+ } else if (result.request === 'trash') {
78
+ trashHelperIssuer = result.contextInfo.issuer;
79
+ }
80
+ for (const [file, replacement] of Object.entries(replacements)) {
81
+ if (result.request === file) {
82
+ result.request = await replacement(result.contextInfo.issuer);
83
+ }
84
+ }
85
+ });
86
+ }
87
+ );
88
+ compiler.hooks.afterEmit.tapPromise(NativeWebpackPlugin.name, async () => {
89
+ if (this.options.trash && trashHelperIssuer) {
90
+ await this.copyTrashHelper(trashHelperIssuer, compiler);
91
+ }
92
+ if (this.options.ripgrep && ripgrepIssuer) {
93
+ await this.copyRipgrep(ripgrepIssuer, compiler);
94
+ }
95
+ if (this.options.pty && nodePtyIssuer) {
96
+ await this.copyNodePtyNativeDeps(nodePtyIssuer, compiler);
97
+ }
98
+ });
99
+ }
100
+
101
+ protected async copyRipgrep(issuer: string, compiler: Compiler): Promise<void> {
102
+ const suffix = process.platform === 'win32' ? '.exe' : '';
103
+ const sourceFile = require.resolve(`@vscode/ripgrep/bin/rg${suffix}`, { paths: [issuer] });
104
+ const targetFile = path.join(compiler.outputPath, this.options.out, `rg${suffix}`);
105
+ await this.copyExecutable(sourceFile, targetFile);
106
+ }
107
+
108
+ protected async copyNodePtyNativeDeps(issuer: string, compiler: Compiler): Promise<void> {
109
+ const dist = `${process.platform}-${process.arch}`;
110
+ const src = `node-pty/prebuilds/${dist}`;
111
+ const targetDirectory = path.resolve(compiler.outputPath, '..', 'prebuilds', dist);
112
+
113
+ const copyFile = async (source: string): Promise<void> => {
114
+ const file = require.resolve(`${src}/${source}`, { paths: [issuer] });
115
+ const targetFile = path.join(targetDirectory, source);
116
+ await this.copyExecutable(file, targetFile);
117
+ };
118
+
119
+ if (process.platform === 'win32') {
120
+ await copyFile('conpty.node');
121
+ await copyFile('conpty_console_list.node');
122
+ await copyFile('conpty/conpty.dll');
123
+ await copyFile('conpty/OpenConsole.exe');
124
+ } else if (process.platform === 'darwin') {
125
+ await copyFile('spawn-helper');
126
+ }
127
+ // On non-windows platforms
128
+ if (process.platform !== 'win32') {
129
+ await copyFile('pty.node');
130
+ }
131
+ }
132
+
133
+ protected async copyTrashHelper(issuer: string, compiler: Compiler): Promise<void> {
134
+ let sourceFile: string | undefined;
135
+ let targetFile: string | undefined;
136
+ if (process.platform === 'win32') {
137
+ sourceFile = require.resolve('trash/lib/windows-trash.exe', { paths: [issuer] });
138
+ targetFile = path.join(compiler.outputPath, 'windows-trash.exe');
139
+ } else if (process.platform === 'darwin') {
140
+ sourceFile = require.resolve('trash/lib/macos-trash', { paths: [issuer] });
141
+ targetFile = path.join(compiler.outputPath, 'macos-trash');
142
+ }
143
+ if (sourceFile && targetFile) {
144
+ await this.copyExecutable(sourceFile, targetFile);
145
+ }
146
+ }
147
+
148
+ protected async copyExecutable(source: string, target: string): Promise<void> {
149
+ const targetDirectory = path.dirname(target);
150
+ await fs.promises.mkdir(targetDirectory, { recursive: true });
151
+ await fs.promises.copyFile(source, target);
152
+ await fs.promises.chmod(target, 0o777);
153
+ }
154
+ }
155
+
156
+ function findNativeWatcherFile(issuer: string): string {
157
+ let name = `@parcel/watcher-${process.platform}-${process.arch}`;
158
+ if (process.platform === 'linux') {
159
+ const { MUSL, family } = require('detect-libc');
160
+ if (family === MUSL) {
161
+ name += '-musl';
162
+ } else {
163
+ name += '-glibc';
164
+ }
165
+ }
166
+ return require.resolve(name, {
167
+ paths: [issuer]
168
+ });
169
+ }
170
+
171
+ async function buildFile(root: string, name: string, content: string): Promise<string> {
172
+ const tmpFile = path.join(root, name);
173
+ let write = true;
174
+ try {
175
+ const existing = await fs.promises.readFile(tmpFile, 'utf8');
176
+ if (existing === content) {
177
+ // prevent writing the same content again
178
+ // this would trigger the watch mode repeatedly
179
+ write = false;
180
+ }
181
+ } catch {
182
+ // ignore
183
+ }
184
+ if (write) {
185
+ await fs.promises.writeFile(tmpFile, content);
186
+ }
187
+ return tmpFile;
188
+ }
189
+
190
+ const ripgrepReplacement = (nativePath: string = '.'): string => `
191
+ const path = require('path');
192
+
193
+ exports.rgPath = path.join(__dirname, \`./${nativePath}/rg\${process.platform === 'win32' ? '.exe' : ''}\`);
194
+ `;
195
+
196
+ const bindingsReplacement = (issuer: string, entries: [string, string][]): string => {
197
+ const cases: string[] = [];
198
+
199
+ for (const [module, node] of entries) {
200
+ const modulePath = require.resolve(node, {
201
+ paths: [issuer]
202
+ });
203
+ cases.push(`${' '.repeat(8)}case '${module}': return require('${modulePath.replace(/\\/g, '/')}');`);
204
+ }
205
+
206
+ return `
207
+ module.exports = function (jsModule) {
208
+ switch (jsModule) {
209
+ ${cases.join(os.EOL)}
210
+ }
211
+ throw new Error(\`unhandled module: "\${jsModule}"\`);
212
+ }`.trim();
213
+ };