@ohos-ports/bun-repl 0.0.0-stage → 2.1.1-beta.1

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.
package/dist/swcrc.js ADDED
@@ -0,0 +1,66 @@
1
+ const REPL_FILENAME = '$bun$repl.ts';
2
+ const swcrc = {
3
+ inlineSourcesContent: false,
4
+ sourceMaps: true,
5
+ isModule: !process.argv.includes('--sloppy'),
6
+ minify: false,
7
+ swcrc: false,
8
+ filename: REPL_FILENAME,
9
+ module: {
10
+ type: 'commonjs',
11
+ lazy: false,
12
+ strict: true,
13
+ strictMode: false,
14
+ ignoreDynamic: true,
15
+ importInterop: 'none'
16
+ },
17
+ jsc: {
18
+ target: 'esnext',
19
+ parser: {
20
+ syntax: 'typescript',
21
+ dynamicImport: true,
22
+ decorators: true
23
+ },
24
+ transform: {
25
+ useDefineForClassFields: false,
26
+ treatConstEnumAsEnum: false,
27
+ optimizer: {
28
+ simplify: false,
29
+ globals: {
30
+ vars: {
31
+ 'import.meta': /*js*/ `((cwd) => {
32
+ const {
33
+ join, dirname, basename, resolve, getcwd,
34
+ pathToFileURL, fileURLToPath,
35
+ StringStartsWith, ObjectFreeze
36
+ } = (0, eval)('this')['@@bunReplRuntimeHelpers'];
37
+ cwd = join(getcwd(), '${REPL_FILENAME}');
38
+ return ObjectFreeze({
39
+ url: pathToFileURL(cwd).href,
40
+ main: true,
41
+ path: cwd,
42
+ dir: dirname(cwd),
43
+ file: basename(cwd),
44
+ require: require,
45
+ async resolve(id, parent) {
46
+ return this.resolveSync(id, parent);
47
+ },
48
+ resolveSync(id, parent) {
49
+ return require.resolve(id, {
50
+ paths: typeof parent === "string" ? [
51
+ resolve(StringStartsWith(parent, 'file://') ? fileURLToPath(parent) : parent, '.')
52
+ ] : void 0
53
+ });
54
+ }
55
+ });
56
+ })()`
57
+ }
58
+ }
59
+ }
60
+ },
61
+ preserveAllComments: true,
62
+ keepClassNames: true,
63
+ loose: false
64
+ }
65
+ };
66
+ export default swcrc;
@@ -0,0 +1,86 @@
1
+ import swc from '@ohos-ports/swc-core';
2
+ import swcDefaultConfig from './swcrc.js';
3
+ const StringReplaceAll = Function.prototype.call.bind(String.prototype.replaceAll);
4
+ const randomUUID = crypto.randomUUID.bind(crypto);
5
+ const safeUUID = ()=>StringReplaceAll(randomUUID(), '-', '');
6
+ const SLOPPY_MODE = process.argv.includes('--sloppy');
7
+ export default class Transpiler extends swc.Compiler {
8
+ constructor(config = {}){
9
+ super();
10
+ this.#config = Object.assign(swcDefaultConfig, config);
11
+ }
12
+ transpile(code) {
13
+ return super.transformSync(code, this.#config).code;
14
+ }
15
+ // REPL-specific adjustments needed for the code to work in a REPL context. (Ran before transpile)
16
+ preprocess(code) {
17
+ return code.replaceAll(/import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*\{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+\})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/g, ($0, defaultVar, destructuredVars, wildcardVar, moduleIdentifier = '')=>{
18
+ let str = `${$0};/*$replTranspiledImport:`;
19
+ let info = {
20
+ moduleIdentifier
21
+ };
22
+ if (defaultVar) info.varname = defaultVar.trim();
23
+ if (wildcardVar) info.varname = wildcardVar.trim();
24
+ if (destructuredVars) {
25
+ info.destructuredVars = destructuredVars.trim().slice(1, -1).trim().split(',').map((s)=>s.trim());
26
+ }
27
+ info.wildcard = !!wildcardVar;
28
+ str += JSON.stringify(info) + '*/';
29
+ if (info.varname) str += `void ${info.varname};`;
30
+ if (info.destructuredVars) info.destructuredVars.forEach((variable)=>{
31
+ const match = variable.match(/[^ \n\t]+[ \n\t]+as[ \n\t]+([^ \n\t]+)/);
32
+ str += `void ${match ? match[1] : variable};`;
33
+ });
34
+ return str;
35
+ });
36
+ }
37
+ // REPL-specific adjustments needed for the code to work in a REPL context. (Ran after transpile)
38
+ postprocess(code) {
39
+ let importsData = [];
40
+ code = (SLOPPY_MODE ? '' : '"use strict";void 0;\n') + code.replaceAll(/(?:var|let|const) (_.+?) = require\("(.+?)"\);[ \t\n;]*\/\*\$replTranspiledImport:({.+?})\*\//g, ($0, requireVar, requireStr, infoStr)=>{
41
+ const info = JSON.parse(infoStr);
42
+ const uuid = safeUUID();
43
+ importsData.push({
44
+ requireVar,
45
+ requireStr,
46
+ info,
47
+ uuid
48
+ });
49
+ let str = `const ${requireVar}${uuid} = require("${requireStr}");`;
50
+ if (info.varname) {
51
+ str += `const ${info.varname}=${requireVar}${uuid}`;
52
+ if (!info.wildcard) str += `.default??${requireVar}${uuid};`;
53
+ str += ';';
54
+ }
55
+ if (info.destructuredVars) {
56
+ let ifstr = 'true.valueOf["@@replTemp"]={};if(!(';
57
+ let delstr = ')){';
58
+ info.destructuredVars.forEach((variable)=>{
59
+ let exportStr = variable;
60
+ const match = variable.match(/([^ \n\t]+)[ \n\t]+as[ \n\t]+([^ \n\t]+)/);
61
+ if (match) {
62
+ variable = match[2];
63
+ exportStr = match[1];
64
+ str += `const{${exportStr}:${variable}}=${requireVar}${uuid};`;
65
+ } else str += `const{${variable}}=${requireVar}${uuid};`;
66
+ ifstr += `(true.valueOf['@@replTemp'].$v="${exportStr}") in ${requireVar}${uuid}&&`;
67
+ delstr += `delete true.valueOf['@@replTemp']["${variable}"];`;
68
+ });
69
+ const errmsg = `Import named '\${true.valueOf["@@replTemp"].$v}' not found in module '${requireStr}'.`;
70
+ delstr += `{const $err=new SyntaxError(\`${errmsg}\`);$err.stack='';throw $err;}};`;
71
+ ifstr = ifstr.slice(0, -2);
72
+ str += ifstr + delstr;
73
+ }
74
+ return str + '\n';
75
+ });
76
+ for (const importData of importsData){
77
+ // node port: rewrite every remaining reference to the renamed require
78
+ // variable — the @ohos-ports/swc-core (1.16) emit uses namespace-style
79
+ // usages like `(0, _mod.fn)(...)`, not only the `void _mod` markers.
80
+ // \b keeps the already-renamed `${requireVar}${uuid}` intact.
81
+ code = code.replaceAll(new RegExp(`\\b${importData.requireVar}\\b`, 'g'), `${importData.requireVar}${importData.uuid}`);
82
+ }
83
+ return code;
84
+ }
85
+ #config;
86
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ @license
3
+ Extracted from https://github.com/DefinitelyTyped/DefinitelyTyped
4
+
5
+ This project is licensed under the MIT license.
6
+ Copyrights are respective of each contributor listed at the beginning of each definition file.
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
9
+ to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
+ */ /**
19
+ * The `util` module supports the needs of Node.js internal APIs. Many of the
20
+ * utilities are useful for application and module developers as well.
21
+ * @see [source](https://github.com/nodejs/node/blob/v17.6.0/lib/util.js)
22
+ */ (function(inspect) {
23
+ let colors;
24
+ let styles;
25
+ let defaultOptions;
26
+ /** Allows changing inspect settings from the repl. */ let replDefaults;
27
+ /** That can be used to declare custom inspect functions. */ const custom;
28
+ })(inspect || (inspect = {}));
29
+ export const $Proxy; //! modified
30
+ export var inspect;