@dxos/esbuild-plugins 0.1.14

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.
Files changed (34) hide show
  1. package/LICENSE +8 -0
  2. package/dist/src/fix-graceful-fs-plugin.d.ts +6 -0
  3. package/dist/src/fix-graceful-fs-plugin.d.ts.map +1 -0
  4. package/dist/src/fix-graceful-fs-plugin.js +28 -0
  5. package/dist/src/fix-graceful-fs-plugin.js.map +1 -0
  6. package/dist/src/fix-memdown-plugin.d.ts +6 -0
  7. package/dist/src/fix-memdown-plugin.d.ts.map +1 -0
  8. package/dist/src/fix-memdown-plugin.js +21 -0
  9. package/dist/src/fix-memdown-plugin.js.map +1 -0
  10. package/dist/src/index.d.ts +5 -0
  11. package/dist/src/index.d.ts.map +1 -0
  12. package/dist/src/index.js +24 -0
  13. package/dist/src/index.js.map +1 -0
  14. package/dist/src/node-globals-polyfill-plugin.d.ts +6 -0
  15. package/dist/src/node-globals-polyfill-plugin.d.ts.map +1 -0
  16. package/dist/src/node-globals-polyfill-plugin.js +28 -0
  17. package/dist/src/node-globals-polyfill-plugin.js.map +1 -0
  18. package/dist/src/node-modules-plugin.d.ts +6 -0
  19. package/dist/src/node-modules-plugin.d.ts.map +1 -0
  20. package/dist/src/node-modules-plugin.js +86 -0
  21. package/dist/src/node-modules-plugin.js.map +1 -0
  22. package/package.json +25 -0
  23. package/polyfills/Buffer.js +2169 -0
  24. package/polyfills/crypto.js +7 -0
  25. package/polyfills/empty-module-stub.js +4 -0
  26. package/polyfills/module.js +5 -0
  27. package/polyfills/process.js +261 -0
  28. package/project.json +32 -0
  29. package/src/fix-graceful-fs-plugin.ts +27 -0
  30. package/src/fix-memdown-plugin.ts +19 -0
  31. package/src/index.ts +8 -0
  32. package/src/node-globals-polyfill-plugin.ts +25 -0
  33. package/src/node-modules-plugin.ts +84 -0
  34. package/tsconfig.json +14 -0
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2022 DXOS.org
3
+ //
4
+
5
+ export * from 'crypto-browserify';
6
+ // eslint-disable-next-line no-undef
7
+ export const webcrypto = self.crypto;
@@ -0,0 +1,4 @@
1
+ /* eslint-disable */
2
+
3
+ // Stub for an empty module.
4
+ module.exports = {};
@@ -0,0 +1,5 @@
1
+ /* eslint-disable */
2
+
3
+ module.exports = {
4
+ builtinModules: [],
5
+ }
@@ -0,0 +1,261 @@
1
+ // shim for using process in browser
2
+ // based off https://github.com/defunctzombie/node-process/blob/master/browser.js
3
+ /* eslint-disable */
4
+
5
+ var _window = this || self || window;
6
+ _window.global = _window;
7
+
8
+ function defaultSetTimout () {
9
+ throw new Error('setTimeout has not been defined');
10
+ }
11
+
12
+ function defaultClearTimeout () {
13
+ throw new Error('clearTimeout has not been defined');
14
+ }
15
+
16
+ var cachedSetTimeout = defaultSetTimout;
17
+ var cachedClearTimeout = defaultClearTimeout;
18
+ if (typeof global.setTimeout === 'function') {
19
+ cachedSetTimeout = setTimeout;
20
+ }
21
+ if (typeof global.clearTimeout === 'function') {
22
+ cachedClearTimeout = clearTimeout;
23
+ }
24
+
25
+ function runTimeout (fun) {
26
+ if (cachedSetTimeout === setTimeout) {
27
+ //normal enviroments in sane situations
28
+ return setTimeout(fun, 0);
29
+ }
30
+ // if setTimeout wasn't available but was latter defined
31
+ if (
32
+ (cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) &&
33
+ setTimeout
34
+ ) {
35
+ cachedSetTimeout = setTimeout;
36
+ return setTimeout(fun, 0);
37
+ }
38
+ try {
39
+ // when when somebody has screwed with setTimeout but no I.E. maddness
40
+ return cachedSetTimeout(fun, 0);
41
+ } catch (e) {
42
+ try {
43
+ // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
44
+ return cachedSetTimeout.call(null, fun, 0);
45
+ } catch (e) {
46
+ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
47
+ return cachedSetTimeout.call(this, fun, 0);
48
+ }
49
+ }
50
+ }
51
+
52
+ function runClearTimeout (marker) {
53
+ if (cachedClearTimeout === clearTimeout) {
54
+ //normal enviroments in sane situations
55
+ return clearTimeout(marker);
56
+ }
57
+ // if clearTimeout wasn't available but was latter defined
58
+ if (
59
+ (cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) &&
60
+ clearTimeout
61
+ ) {
62
+ cachedClearTimeout = clearTimeout;
63
+ return clearTimeout(marker);
64
+ }
65
+ try {
66
+ // when when somebody has screwed with setTimeout but no I.E. maddness
67
+ return cachedClearTimeout(marker);
68
+ } catch (e) {
69
+ try {
70
+ // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
71
+ return cachedClearTimeout.call(null, marker);
72
+ } catch (e) {
73
+ // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
74
+ // Some versions of I.E. have different rules for clearTimeout vs setTimeout
75
+ return cachedClearTimeout.call(this, marker);
76
+ }
77
+ }
78
+ }
79
+
80
+ var queue = [];
81
+ var draining = false;
82
+ var currentQueue;
83
+ var queueIndex = -1;
84
+
85
+ function cleanUpNextTick () {
86
+ if (!draining || !currentQueue) {
87
+ return;
88
+ }
89
+ draining = false;
90
+ if (currentQueue.length) {
91
+ queue = currentQueue.concat(queue);
92
+ } else {
93
+ queueIndex = -1;
94
+ }
95
+ if (queue.length) {
96
+ drainQueue();
97
+ }
98
+ }
99
+
100
+ function drainQueue () {
101
+ if (draining) {
102
+ return;
103
+ }
104
+ var timeout = runTimeout(cleanUpNextTick);
105
+ draining = true;
106
+
107
+ var len = queue.length;
108
+ while (len) {
109
+ currentQueue = queue;
110
+ queue = [];
111
+ while (++queueIndex < len) {
112
+ if (currentQueue) {
113
+ currentQueue[queueIndex].run();
114
+ }
115
+ }
116
+ queueIndex = -1;
117
+ len = queue.length;
118
+ }
119
+ currentQueue = null;
120
+ draining = false;
121
+ runClearTimeout(timeout);
122
+ }
123
+
124
+ function nextTick (fun) {
125
+ var args = new Array(arguments.length - 1);
126
+ if (arguments.length > 1) {
127
+ for (var i = 1; i < arguments.length; i++) {
128
+ args[i - 1] = arguments[i];
129
+ }
130
+ }
131
+ queue.push(new Item(fun, args));
132
+ if (queue.length === 1 && !draining) {
133
+ runTimeout(drainQueue);
134
+ }
135
+ }
136
+
137
+ // v8 likes predictible objects
138
+ function Item (fun, array) {
139
+ this.fun = fun;
140
+ this.array = array;
141
+ }
142
+
143
+ Item.prototype.run = function () {
144
+ this.fun.apply(null, this.array);
145
+ };
146
+ var title = 'browser';
147
+ var platform = 'browser';
148
+ var browser = true;
149
+ var env = {};
150
+ var argv = [];
151
+ var version = ''; // empty string to avoid regexp issues
152
+ var versions = {};
153
+ var release = {};
154
+ var config = {};
155
+
156
+ function noop () {}
157
+
158
+ var on = noop;
159
+ var addListener = noop;
160
+ var once = noop;
161
+ var off = noop;
162
+ var removeListener = noop;
163
+ var removeAllListeners = noop;
164
+ var emit = noop;
165
+
166
+ function binding (name) {
167
+ throw new Error('process.binding is not supported');
168
+ }
169
+
170
+ function cwd () {
171
+ return '/';
172
+ }
173
+
174
+ function chdir (dir) {
175
+ throw new Error('process.chdir is not supported');
176
+ }
177
+
178
+ function umask () {
179
+ return 0;
180
+ }
181
+
182
+ // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js
183
+ var performance = global.performance || {};
184
+ var performanceNow =
185
+ performance.now ||
186
+ performance.mozNow ||
187
+ performance.msNow ||
188
+ performance.oNow ||
189
+ performance.webkitNow ||
190
+ function () {
191
+ return new Date().getTime();
192
+ };
193
+
194
+ // generate timestamp or delta
195
+ // see http://nodejs.org/api/process.html#process_process_hrtime
196
+ function hrtime (previousTimestamp) {
197
+ var clocktime = performanceNow.call(performance) * 1e-3;
198
+ var seconds = Math.floor(clocktime);
199
+ var nanoseconds = Math.floor((clocktime % 1) * 1e9);
200
+ if (previousTimestamp) {
201
+ seconds = seconds - previousTimestamp[0];
202
+ nanoseconds = nanoseconds - previousTimestamp[1];
203
+ if (nanoseconds < 0) {
204
+ seconds--;
205
+ nanoseconds += 1e9;
206
+ }
207
+ }
208
+ return [seconds, nanoseconds];
209
+ }
210
+
211
+ var startTime = new Date();
212
+
213
+ function uptime () {
214
+ var currentTime = new Date();
215
+ var dif = currentTime - startTime;
216
+ return dif / 1000;
217
+ }
218
+
219
+ _window.setImmediate = nextTick;
220
+
221
+ export var process = {
222
+ nextTick: nextTick,
223
+ title: title,
224
+ browser: browser,
225
+ env: env,
226
+ argv: argv,
227
+ version: version,
228
+ versions: versions,
229
+ on: on,
230
+ addListener: addListener,
231
+ once: once,
232
+ off: off,
233
+ removeListener: removeListener,
234
+ removeAllListeners: removeAllListeners,
235
+ emit: emit,
236
+ binding: binding,
237
+ cwd: cwd,
238
+ chdir: chdir,
239
+ umask: umask,
240
+ hrtime: hrtime,
241
+ platform: platform,
242
+ release: release,
243
+ config: config,
244
+ uptime: uptime,
245
+ };
246
+
247
+ // replace process.env.VAR with define
248
+
249
+ const defines = {};
250
+ Object.keys(defines).forEach((key) => {
251
+ const segs = key.split('.');
252
+ let target = process;
253
+ for (let i = 0; i < segs.length; i++) {
254
+ const seg = segs[i];
255
+ if (i === segs.length - 1) {
256
+ target[seg] = defines[key];
257
+ } else {
258
+ target = target[seg] || (target[seg] = {});
259
+ }
260
+ }
261
+ });
package/project.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "sourceRoot": "packages/common/esbuild-plugins/src",
3
+ "projectType": "library",
4
+ "targets": {
5
+ "compile": {
6
+ "executor": "@nrwl/js:tsc",
7
+ "options": {
8
+ "main": "packages/common/esbuild-plugins/src/index.ts",
9
+ "outputPath": "packages/common/esbuild-plugins/dist",
10
+ "transformers": [
11
+ "@dxos/log-hook/transformer"
12
+ ],
13
+ "tsConfig": "packages/common/esbuild-plugins/tsconfig.json"
14
+ },
15
+ "outputs": [
16
+ "{options.outputPath}"
17
+ ]
18
+ },
19
+ "lint": {
20
+ "executor": "@nrwl/linter:eslint",
21
+ "options": {
22
+ "format": "unix",
23
+ "lintFilePatterns": [
24
+ "packages/common/esbuild-plugins/**/*.{ts,js}"
25
+ ]
26
+ },
27
+ "outputs": [
28
+ "{options.outputFile}"
29
+ ]
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,27 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ import type { Plugin } from 'esbuild';
6
+
7
+ /**
8
+ * Replace graceful-fs with an empty module when bundling for browser.
9
+ */
10
+ export const FixGracefulFsPlugin = (): Plugin => ({
11
+ name: 'fix-graceful-fs-plugin',
12
+ setup: ({ onResolve, onLoad }) => {
13
+ onResolve({ filter: /^graceful-fs$/ }, (arg) => {
14
+ return {
15
+ path: 'graceful-fs',
16
+ namespace: 'fix-graceful-fs-plugin'
17
+ };
18
+ });
19
+
20
+ onLoad({ filter: /^graceful-fs$/, namespace: 'fix-graceful-fs-plugin' }, async (args) => {
21
+ return {
22
+ contents: 'module.exports = {};',
23
+ loader: 'js'
24
+ };
25
+ });
26
+ }
27
+ });
@@ -0,0 +1,19 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ import type { Plugin } from 'esbuild';
6
+
7
+ /**
8
+ * Some odd path resolution in memdown package.
9
+ */
10
+ export const FixMemdownPlugin = (): Plugin => ({
11
+ name: 'fix-memdown-plugin',
12
+ setup: ({ onResolve }) => {
13
+ onResolve({ filter: /^immediate$/ }, (arg) => {
14
+ return {
15
+ path: require.resolve(arg.path, { paths: [arg.resolveDir] })
16
+ };
17
+ });
18
+ }
19
+ });
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ export * from './fix-graceful-fs-plugin';
6
+ export * from './fix-memdown-plugin';
7
+ export * from './node-globals-polyfill-plugin';
8
+ export * from './node-modules-plugin';
@@ -0,0 +1,25 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ import type { Plugin } from 'esbuild';
6
+ import { resolve } from 'path';
7
+
8
+ /**
9
+ * Injects node globals such as `Buffer` or `process`.
10
+ */
11
+ export const NodeGlobalsPolyfillPlugin = (): Plugin => ({
12
+ name: 'node-globals-polyfill',
13
+ setup: ({ initialOptions }) => {
14
+ const polyfills = [
15
+ // TODO: Use `buffer` module from NPM.
16
+ resolve(__dirname, '../../polyfills/process.js'),
17
+ resolve(__dirname, '../../polyfills/Buffer.js')
18
+ ];
19
+ if (initialOptions.inject) {
20
+ initialOptions.inject.push(...polyfills);
21
+ } else {
22
+ initialOptions.inject = [...polyfills];
23
+ }
24
+ }
25
+ });
@@ -0,0 +1,84 @@
1
+ //
2
+ // Copyright 2021 DXOS.org
3
+ //
4
+
5
+ import type { Plugin } from 'esbuild';
6
+
7
+ /**
8
+ * Adds polyfills for node's built-in modules.
9
+ */
10
+ export const NodeModulesPlugin = (): Plugin => ({
11
+ name: 'node-modules-plugin',
12
+ setup: ({ onResolve }) => {
13
+ onResolve({ filter: /^(node:)?assert$/ }, (arg) => {
14
+ return {
15
+ path: require.resolve('assert/') // Appending slash makes node resolve the installed package from node_modules.
16
+ };
17
+ });
18
+ onResolve({ filter: /^(node:)?buffer$/ }, (arg) => {
19
+ return {
20
+ path: require.resolve('../../polyfills/Buffer.js')
21
+ };
22
+ });
23
+ onResolve({ filter: /^(node:)?domain$/ }, (arg) => {
24
+ return {
25
+ path: require.resolve('domain-browser')
26
+ };
27
+ });
28
+ onResolve({ filter: /^(node:)?stream$/ }, (arg) => {
29
+ return {
30
+ path: require.resolve('readable-stream')
31
+ };
32
+ });
33
+ onResolve({ filter: /^(node:)?path$/ }, (arg) => {
34
+ return {
35
+ path: require.resolve('path-browserify')
36
+ };
37
+ });
38
+ onResolve({ filter: /^(node:)?events$/ }, (arg) => {
39
+ return {
40
+ path: require.resolve('events/')
41
+ };
42
+ });
43
+ onResolve({ filter: /^(node:)?util$/ }, (arg) => {
44
+ return {
45
+ path: require.resolve('util/')
46
+ };
47
+ });
48
+ onResolve({ filter: /^(node:)?http$/ }, (arg) => {
49
+ return {
50
+ path: require.resolve('../../polyfills/empty-module-stub.js')
51
+ };
52
+ });
53
+ onResolve({ filter: /^(node:)?https$/ }, (arg) => {
54
+ return {
55
+ path: require.resolve('../../polyfills/empty-module-stub.js')
56
+ };
57
+ });
58
+ onResolve({ filter: /^(node:)?module$/ }, (arg) => {
59
+ return {
60
+ path: require.resolve('../../polyfills/module.js')
61
+ };
62
+ });
63
+ onResolve({ filter: /^(node:)?crypto$/ }, (arg) => {
64
+ return {
65
+ path: require.resolve('../../polyfills/crypto.js')
66
+ };
67
+ });
68
+ onResolve({ filter: /^(node:)?debug$/ }, (arg) => {
69
+ return {
70
+ path: require.resolve('debug') // Resolves to installed debug module.
71
+ };
72
+ });
73
+ onResolve({ filter: /^(node:)?tty$/ }, (arg) => {
74
+ return {
75
+ path: require.resolve('tty-browserify')
76
+ };
77
+ });
78
+ onResolve({ filter: /^(node:)?fs$/ }, (arg) => {
79
+ return {
80
+ path: require.resolve('../../polyfills/empty-module-stub.js')
81
+ };
82
+ });
83
+ }
84
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "emitDeclarationOnly": false,
5
+ "outDir": "dist",
6
+ "types": [
7
+ "node"
8
+ ]
9
+ },
10
+ "include": [
11
+ "src"
12
+ ],
13
+ "references": []
14
+ }