dismantle 0.2.0

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 (73) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +40 -0
  3. package/dist/cjs/development/index.cjs +1484 -0
  4. package/dist/cjs/development/index.cjs.map +7 -0
  5. package/dist/cjs/development/runtime.cjs +1484 -0
  6. package/dist/cjs/development/runtime.cjs.map +7 -0
  7. package/dist/cjs/production/index.cjs +1 -0
  8. package/dist/cjs/production/runtime.cjs +1 -0
  9. package/dist/esm/development/index.mjs +1453 -0
  10. package/dist/esm/development/index.mjs.map +7 -0
  11. package/dist/esm/development/runtime.mjs +1453 -0
  12. package/dist/esm/development/runtime.mjs.map +7 -0
  13. package/dist/esm/production/index.mjs +1 -0
  14. package/dist/esm/production/runtime.mjs +1 -0
  15. package/dist/types/hidden-imports.d.ts +4 -0
  16. package/dist/types/hidden-imports.d.ts.map +1 -0
  17. package/dist/types/index.d.ts +9 -0
  18. package/dist/types/index.d.ts.map +1 -0
  19. package/dist/types/plugin.d.ts +8 -0
  20. package/dist/types/plugin.d.ts.map +1 -0
  21. package/dist/types/split.d.ts +7 -0
  22. package/dist/types/split.d.ts.map +1 -0
  23. package/dist/types/transform-block.d.ts +5 -0
  24. package/dist/types/transform-block.d.ts.map +1 -0
  25. package/dist/types/transform-call.d.ts +5 -0
  26. package/dist/types/transform-call.d.ts.map +1 -0
  27. package/dist/types/types.d.ts +62 -0
  28. package/dist/types/types.d.ts.map +1 -0
  29. package/dist/types/utils/assert.d.ts +2 -0
  30. package/dist/types/utils/assert.d.ts.map +1 -0
  31. package/dist/types/utils/generator-shim.d.ts +4 -0
  32. package/dist/types/utils/generator-shim.d.ts.map +1 -0
  33. package/dist/types/utils/get-descriptive-name.d.ts +3 -0
  34. package/dist/types/utils/get-descriptive-name.d.ts.map +1 -0
  35. package/dist/types/utils/get-foreign-bindings.d.ts +3 -0
  36. package/dist/types/utils/get-foreign-bindings.d.ts.map +1 -0
  37. package/dist/types/utils/get-identifiers-from-lval.d.ts +3 -0
  38. package/dist/types/utils/get-identifiers-from-lval.d.ts.map +1 -0
  39. package/dist/types/utils/get-import-identifier.d.ts +5 -0
  40. package/dist/types/utils/get-import-identifier.d.ts.map +1 -0
  41. package/dist/types/utils/get-import-specifier-name.d.ts +3 -0
  42. package/dist/types/utils/get-import-specifier-name.d.ts.map +1 -0
  43. package/dist/types/utils/get-module-definition.d.ts +4 -0
  44. package/dist/types/utils/get-module-definition.d.ts.map +1 -0
  45. package/dist/types/utils/get-root-statement-path.d.ts +3 -0
  46. package/dist/types/utils/get-root-statement-path.d.ts.map +1 -0
  47. package/dist/types/utils/register-import-specifiers.d.ts +5 -0
  48. package/dist/types/utils/register-import-specifiers.d.ts.map +1 -0
  49. package/dist/types/utils/unwrap.d.ts +11 -0
  50. package/dist/types/utils/unwrap.d.ts.map +1 -0
  51. package/dist/types/utils/xxhash32.d.ts +7 -0
  52. package/dist/types/utils/xxhash32.d.ts.map +1 -0
  53. package/package.json +90 -0
  54. package/src/hidden-imports.ts +13 -0
  55. package/src/index.ts +90 -0
  56. package/src/plugin.ts +26 -0
  57. package/src/split.ts +1074 -0
  58. package/src/transform-block.ts +115 -0
  59. package/src/transform-call.ts +100 -0
  60. package/src/types.ts +73 -0
  61. package/src/utils/assert.ts +5 -0
  62. package/src/utils/errors.ts +21 -0
  63. package/src/utils/generator-shim.ts +22 -0
  64. package/src/utils/get-descriptive-name.ts +42 -0
  65. package/src/utils/get-foreign-bindings.ts +60 -0
  66. package/src/utils/get-identifiers-from-lval.ts +44 -0
  67. package/src/utils/get-import-identifier.ts +23 -0
  68. package/src/utils/get-import-specifier-name.ts +10 -0
  69. package/src/utils/get-module-definition.ts +53 -0
  70. package/src/utils/get-root-statement-path.ts +14 -0
  71. package/src/utils/register-import-specifiers.ts +77 -0
  72. package/src/utils/unwrap.ts +63 -0
  73. package/src/utils/xxhash32.ts +214 -0
@@ -0,0 +1,7 @@
1
+ /**
2
+ *
3
+ * @param buffer - byte array or string
4
+ * @param seed - optional seed (32-bit unsigned);
5
+ */
6
+ export default function xxHash32(buffer: Uint8Array | string, seed?: number): number;
7
+ //# sourceMappingURL=xxhash32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xxhash32.d.ts","sourceRoot":"","sources":["../../../src/utils/xxhash32.ts"],"names":[],"mappings":"AAkCA;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAC9B,MAAM,EAAE,UAAU,GAAG,MAAM,EAC3B,IAAI,SAAI,GACP,MAAM,CA2KR"}
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "dismantle",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "files": [
6
+ "dist",
7
+ "src"
8
+ ],
9
+ "engines": {
10
+ "node": ">=10"
11
+ },
12
+ "license": "MIT",
13
+ "keywords": [
14
+ "pridepack"
15
+ ],
16
+ "devDependencies": {
17
+ "@types/babel__core": "^7.20.5",
18
+ "@types/babel__generator": "^7.6.8",
19
+ "@types/babel__helper-module-imports": "^7.18.3",
20
+ "@types/babel__traverse": "^7.20.5",
21
+ "@types/node": "^20.11.6",
22
+ "pridepack": "2.6.0",
23
+ "tslib": "^2.6.2",
24
+ "typescript": "^5.3.3",
25
+ "vitest": "^1.2.1"
26
+ },
27
+ "dependencies": {
28
+ "@babel/core": "^7.23.7",
29
+ "@babel/generator": "^7.23.6",
30
+ "@babel/helper-module-imports": "^7.22.15",
31
+ "@babel/traverse": "^7.23.7",
32
+ "@babel/types": "^7.23.6"
33
+ },
34
+ "scripts": {
35
+ "prepublishOnly": "pridepack clean && pridepack build",
36
+ "build": "pridepack build",
37
+ "type-check": "pridepack check",
38
+ "lint": "pridepack lint",
39
+ "clean": "pridepack clean",
40
+ "watch": "pridepack watch",
41
+ "start": "pridepack start",
42
+ "dev": "pridepack dev",
43
+ "test": "vitest"
44
+ },
45
+ "description": "Universal \"use-server\" functions",
46
+ "repository": {
47
+ "url": "https://github.com/lxsmnsyc/dismantle.git",
48
+ "type": "git"
49
+ },
50
+ "homepage": "https://github.com/lxsmnsyc/dismantle/tree/main/packages/core",
51
+ "bugs": {
52
+ "url": "https://github.com/lxsmnsyc/dismantle/issues"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ },
57
+ "author": "Alexis Munsayac",
58
+ "private": false,
59
+ "typesVersions": {
60
+ "*": {
61
+ "runtime": [
62
+ "./dist/types/index.d.ts"
63
+ ]
64
+ }
65
+ },
66
+ "exports": {
67
+ ".": {
68
+ "development": {
69
+ "require": "./dist/cjs/development/index.cjs",
70
+ "import": "./dist/esm/development/index.mjs"
71
+ },
72
+ "require": "./dist/cjs/production/index.cjs",
73
+ "import": "./dist/esm/production/index.mjs",
74
+ "types": "./dist/types/index.d.ts"
75
+ },
76
+ "./runtime": {
77
+ "development": {
78
+ "require": "./dist/cjs/development/runtime.cjs",
79
+ "import": "./dist/esm/development/runtime.mjs"
80
+ },
81
+ "require": "./dist/cjs/production/runtime.cjs",
82
+ "import": "./dist/esm/production/runtime.mjs",
83
+ "types": "./dist/types/index.d.ts"
84
+ }
85
+ },
86
+ "types": "./dist/types/index.d.ts",
87
+ "main": "./dist/cjs/production/index.cjs",
88
+ "module": "./dist/esm/production/index.mjs",
89
+ "gitHead": "fb58a6463b2a78337822b1aeca5c64caf41c8daa"
90
+ }
@@ -0,0 +1,13 @@
1
+ import type { ImportDefinition } from './types';
2
+
3
+ export const HIDDEN_FUNC: ImportDefinition = {
4
+ kind: 'named',
5
+ source: 'dismantle/runtime',
6
+ name: '$$func',
7
+ };
8
+
9
+ export const HIDDEN_GENERATOR: ImportDefinition = {
10
+ kind: 'named',
11
+ source: 'dismantle/runtime',
12
+ name: '$$gen',
13
+ };
package/src/index.ts ADDED
@@ -0,0 +1,90 @@
1
+ import * as babel from '@babel/core';
2
+ import path from 'node:path';
3
+ import { plugin } from './plugin';
4
+ import type { CodeOutput, Options, StateContext } from './types';
5
+ import assert from './utils/assert';
6
+ import xxHash32 from './utils/xxhash32';
7
+
8
+ export interface Output extends CodeOutput {
9
+ files: Map<string, CodeOutput>;
10
+ entries: string[];
11
+ roots: string[];
12
+ }
13
+
14
+ export type {
15
+ CodeOutput,
16
+ DefaultImportDefinition,
17
+ DirectiveDefinition,
18
+ ImportDefinition,
19
+ NamedImportDefinition,
20
+ Options,
21
+ } from './types';
22
+
23
+ export async function compile(
24
+ id: string,
25
+ code: string,
26
+ options: Options,
27
+ ): Promise<Output> {
28
+ const parsedPath = path.parse(id);
29
+
30
+ const entries: string[] = [];
31
+ const roots: string[] = [];
32
+ const files = new Map<string, CodeOutput>();
33
+
34
+ const ctx: StateContext = {
35
+ id,
36
+ path: parsedPath,
37
+ imports: new Map(),
38
+ virtual: {
39
+ count: 0,
40
+ },
41
+ options,
42
+ bindings: new Map(),
43
+ blocks: {
44
+ hash: xxHash32(id).toString(16),
45
+ count: 0,
46
+ },
47
+ onVirtualFile(current, content, mode) {
48
+ const filePath = path.join(parsedPath.dir, current);
49
+ files.set(path.join(parsedPath.dir, current), content);
50
+ if (mode === 'entry') {
51
+ entries.push(filePath);
52
+ } else if (mode === 'root') {
53
+ roots.push(filePath);
54
+ }
55
+ },
56
+ registrations: {
57
+ identifiers: new Map(),
58
+ namespaces: new Map(),
59
+ },
60
+ };
61
+
62
+ const plugins: babel.ParserOptions['plugins'] = ['jsx'];
63
+
64
+ if (/\.[mc]?tsx?$/i.test(id)) {
65
+ plugins.push('typescript');
66
+ }
67
+
68
+ const result = await babel.transformAsync(code, {
69
+ plugins: [[plugin, ctx]],
70
+ parserOpts: {
71
+ plugins,
72
+ },
73
+ filename: parsedPath.base,
74
+ ast: false,
75
+ sourceFileName: id,
76
+ sourceMaps: true,
77
+ configFile: false,
78
+ babelrc: false,
79
+ });
80
+
81
+ assert(result, 'invariant');
82
+
83
+ return {
84
+ code: result.code,
85
+ map: result.map,
86
+ files,
87
+ entries,
88
+ roots,
89
+ };
90
+ }
package/src/plugin.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type * as babel from '@babel/core';
2
+ import { transformBlock } from './transform-block';
3
+ import { transformCall } from './transform-call';
4
+ import type { StateContext } from './types';
5
+ import { registerImportSpecifiers } from './utils/register-import-specifiers';
6
+
7
+ interface State extends babel.PluginPass {
8
+ opts: StateContext;
9
+ }
10
+
11
+ export function plugin(): babel.PluginObj<State> {
12
+ return {
13
+ name: 'dismantle',
14
+ visitor: {
15
+ Program(path, ctx) {
16
+ registerImportSpecifiers(ctx.opts, path);
17
+ },
18
+ BlockStatement(path, ctx) {
19
+ transformBlock(ctx.opts, path);
20
+ },
21
+ CallExpression(path, ctx) {
22
+ transformCall(ctx.opts, path);
23
+ },
24
+ },
25
+ };
26
+ }