@backtickjs/node-plugin 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +61 -2
- package/dist/hooks.d.ts +3 -0
- package/dist/hooks.js +46 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -3
- package/src/hooks.ts +56 -0
- package/src/index.ts +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jean-François Bisson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @backtickjs/node-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Runs a Backtick server on Node without a build step. It's a loader, registered with `--import`, that compiles your server's TypeScript and JSX files as they load, including their `cs` client scripts and the framework compile steps your project names. Use it in development and in production when Node is your runtime; on Bun, use [`@backtickjs/bun-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/bun-plugin) instead.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @backtickjs/node-plugin typescript
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
It's a runtime dependency: the server needs it whenever it starts. Requires Node 22.15 or later (it uses `module.registerHooks`) and TypeScript 6.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
node --enable-source-maps --import @backtickjs/node-plugin server/index.tsx
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Run it from the project root: the plugin reads `package.json` from the current directory. In development, add `--watch` to restart the server when a file changes:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
node --watch --enable-source-maps --import @backtickjs/node-plugin server/index.tsx
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
What it does:
|
|
26
|
+
|
|
27
|
+
- Compiles every `.ts` and `.tsx` file outside `node_modules`, each with the `tsconfig.json` nearest to it, so a server and an app beside it can each have their own.
|
|
28
|
+
- Compiles each `cs` script in those files, with the framework compile steps named in `package.json` (below). A script the compiler refuses throws its error, and the file doesn't load.
|
|
29
|
+
- Resolves a relative `./Home.js` import to `./Home.ts` or `./Home.tsx` when no `.js` file exists, as TypeScript writes imports of files it compiles.
|
|
30
|
+
- Answers `require` as well as `import`, so a server file starts in a package without `"type": "module"`.
|
|
31
|
+
|
|
32
|
+
## Configuration
|
|
33
|
+
|
|
34
|
+
Name your framework's compile steps in the project's `package.json`:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"backtick": {
|
|
39
|
+
"plugins": ["@backtickjs/react/plugin"]
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Use `@backtickjs/solid-js/plugin` for Solid. The steps are loaded once, at startup, with the project's own `require`.
|
|
45
|
+
|
|
46
|
+
Compiler options come from the nearest `tsconfig.json`, following `extends`. The plugin always emits ES modules (`ESNext`) with an inline source map, and ignores `outDir`, `rootDir` and other output-location options.
|
|
47
|
+
|
|
48
|
+
## Notes
|
|
49
|
+
|
|
50
|
+
- The plugin compiles; it doesn't type-check. Run [`backtick-tsc`](https://github.com/backtickjs/backtick/tree/main/packages/tsc) for that.
|
|
51
|
+
- Pass `--enable-source-maps` for stack traces that point at your `.ts`/`.tsx` lines rather than the compiled output.
|
|
52
|
+
- Files under `node_modules` are left to Node.
|
|
53
|
+
|
|
54
|
+
## Related
|
|
55
|
+
|
|
56
|
+
- [`@backtickjs/bun-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/bun-plugin): the same for Bun
|
|
57
|
+
- [`@backtickjs/tsc`](https://github.com/backtickjs/backtick/tree/main/packages/tsc): type-checks scripts and their splices
|
|
58
|
+
- [Documentation](https://backtickjs.com/docs)
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
package/dist/hooks.d.ts
ADDED
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire, } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { compileModule, pluginsFrom } from "@backtickjs/compiler";
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
// Node's module hooks, registered by `index.ts`. The framework's compile steps
|
|
8
|
+
// are read once, from the project's `package.json`.
|
|
9
|
+
const packageJson = path.join(process.cwd(), "package.json");
|
|
10
|
+
const plugins = pluginsFrom(readFileSync(packageJson, "utf8"), createRequire(packageJson));
|
|
11
|
+
// The project's own TypeScript, not what it installed.
|
|
12
|
+
const compiled = (url) => url.startsWith("file:") &&
|
|
13
|
+
/\.tsx?$/.test(url) &&
|
|
14
|
+
!url.includes("/node_modules/");
|
|
15
|
+
// `./Home.js` for `./Home.tsx`, as TypeScript writes an import of a file it
|
|
16
|
+
// compiles: tried as written first, then as the source it stands for.
|
|
17
|
+
export const resolve = (specifier, context, nextResolve) => {
|
|
18
|
+
try {
|
|
19
|
+
return nextResolve(specifier, context);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
const relative = specifier.startsWith("./") || specifier.startsWith("../");
|
|
23
|
+
if (!relative || !specifier.endsWith(".js")) {
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
for (const extension of [".ts", ".tsx"]) {
|
|
27
|
+
try {
|
|
28
|
+
return nextResolve(specifier.slice(0, -3) + extension, context);
|
|
29
|
+
}
|
|
30
|
+
catch { }
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export const load = (url, context, nextLoad) => {
|
|
36
|
+
if (!compiled(url)) {
|
|
37
|
+
return nextLoad(url, context);
|
|
38
|
+
}
|
|
39
|
+
const file = fileURLToPath(url);
|
|
40
|
+
const fileName = path.relative(process.cwd(), file).split(path.sep).join("/") || file;
|
|
41
|
+
const source = compileModule(ts, fileName, readFileSync(file, "utf8"), {
|
|
42
|
+
plugins,
|
|
43
|
+
});
|
|
44
|
+
return { format: "module", source, shortCircuit: true };
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EACL,aAA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AAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,OAAO,CAAC,CAAC;YAClE,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,IAAI,GAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;IAC3D,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACnB,OAAO,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAChC,CAAC;IACD,MAAM,IAAI,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC;IAChC,MAAM,QAAQ,GACZ,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC;IACvE,MAAM,MAAM,GAAG,aAAa,CAAC,EAAE,EAAE,QAAQ,EAAE,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE;QACrE,OAAO;KACR,CAAC,CAAC;IACH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { registerHooks } from "node:module";
|
|
2
|
+
import { load, resolve } from "./hooks.js";
|
|
3
|
+
/**
|
|
4
|
+
* Use it from the command line, as Node's `--import`:
|
|
5
|
+
*
|
|
6
|
+
* ```sh
|
|
7
|
+
* node --import @backtickjs/node-plugin ./server/index.tsx
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* TypeScript and JSX files outside `node_modules` are compiled as they load,
|
|
11
|
+
* their `cs` scripts with them. The framework's compile steps are the
|
|
12
|
+
* project's, named in its `package.json`:
|
|
13
|
+
*
|
|
14
|
+
* ```json
|
|
15
|
+
* "backtick": { "plugins": ["@backtickjs/react/plugin"] }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
//
|
|
19
|
+
// The hooks run on this thread, synchronously, so they answer `require` as well
|
|
20
|
+
// as `import`: an app's package.json has no `"type": "module"`, so Node starts
|
|
21
|
+
// a server file as CommonJS before it sees the `import`s.
|
|
22
|
+
registerHooks({ resolve, load });
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAC/E,0DAA0D;AAC1D,aAAa,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backtickjs/node-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Runs a Backtick server on Node without a build step, compiling its files and client scripts as they load",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"backtick",
|
|
7
|
+
"react-native",
|
|
8
|
+
"server-driven-ui",
|
|
9
|
+
"server-components",
|
|
10
|
+
"node",
|
|
11
|
+
"loader",
|
|
12
|
+
"module-hooks"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://backtickjs.com",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/backtickjs/backtick/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/backtickjs/backtick.git",
|
|
21
|
+
"directory": "packages/node-plugin"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Jean-François Bisson <jf@backtickjs.com>",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"!dist/._cache",
|
|
36
|
+
"src"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@backtickjs/compiler": "0.1.1"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"typescript": "^6.0.3"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^25.9.1",
|
|
49
|
+
"prettier": "^3.6.2",
|
|
50
|
+
"typescript": "^6.0.3",
|
|
51
|
+
"@backtickjs/core": "0.1.1"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22.15"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsc -b",
|
|
58
|
+
"dev": "tsc -b --watch",
|
|
59
|
+
"test": "node --enable-source-maps --import ./dist/index.js --test \"test/**/*.test.ts\"",
|
|
60
|
+
"format": "prettier --write --ignore-unknown .",
|
|
61
|
+
"format:check": "prettier --check --ignore-unknown .",
|
|
62
|
+
"clean": "rm -rf node_modules dist .turbo"
|
|
63
|
+
}
|
|
6
64
|
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import {
|
|
3
|
+
createRequire,
|
|
4
|
+
type LoadHookSync,
|
|
5
|
+
type ResolveHookSync,
|
|
6
|
+
} from "node:module";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { compileModule, pluginsFrom } from "@backtickjs/compiler";
|
|
10
|
+
import ts from "typescript";
|
|
11
|
+
|
|
12
|
+
// Node's module hooks, registered by `index.ts`. The framework's compile steps
|
|
13
|
+
// are read once, from the project's `package.json`.
|
|
14
|
+
const packageJson = path.join(process.cwd(), "package.json");
|
|
15
|
+
const plugins = pluginsFrom(
|
|
16
|
+
readFileSync(packageJson, "utf8"),
|
|
17
|
+
createRequire(packageJson),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// The project's own TypeScript, not what it installed.
|
|
21
|
+
const compiled = (url: string) =>
|
|
22
|
+
url.startsWith("file:") &&
|
|
23
|
+
/\.tsx?$/.test(url) &&
|
|
24
|
+
!url.includes("/node_modules/");
|
|
25
|
+
|
|
26
|
+
// `./Home.js` for `./Home.tsx`, as TypeScript writes an import of a file it
|
|
27
|
+
// compiles: tried as written first, then as the source it stands for.
|
|
28
|
+
export const resolve: ResolveHookSync = (specifier, context, nextResolve) => {
|
|
29
|
+
try {
|
|
30
|
+
return nextResolve(specifier, context);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
const relative = specifier.startsWith("./") || specifier.startsWith("../");
|
|
33
|
+
if (!relative || !specifier.endsWith(".js")) {
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
for (const extension of [".ts", ".tsx"]) {
|
|
37
|
+
try {
|
|
38
|
+
return nextResolve(specifier.slice(0, -3) + extension, context);
|
|
39
|
+
} catch {}
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const load: LoadHookSync = (url, context, nextLoad) => {
|
|
46
|
+
if (!compiled(url)) {
|
|
47
|
+
return nextLoad(url, context);
|
|
48
|
+
}
|
|
49
|
+
const file = fileURLToPath(url);
|
|
50
|
+
const fileName =
|
|
51
|
+
path.relative(process.cwd(), file).split(path.sep).join("/") || file;
|
|
52
|
+
const source = compileModule(ts, fileName, readFileSync(file, "utf8"), {
|
|
53
|
+
plugins,
|
|
54
|
+
});
|
|
55
|
+
return { format: "module", source, shortCircuit: true };
|
|
56
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { registerHooks } from "node:module";
|
|
2
|
+
import { load, resolve } from "./hooks.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Use it from the command line, as Node's `--import`:
|
|
6
|
+
*
|
|
7
|
+
* ```sh
|
|
8
|
+
* node --import @backtickjs/node-plugin ./server/index.tsx
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* TypeScript and JSX files outside `node_modules` are compiled as they load,
|
|
12
|
+
* their `cs` scripts with them. The framework's compile steps are the
|
|
13
|
+
* project's, named in its `package.json`:
|
|
14
|
+
*
|
|
15
|
+
* ```json
|
|
16
|
+
* "backtick": { "plugins": ["@backtickjs/react/plugin"] }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
//
|
|
20
|
+
// The hooks run on this thread, synchronously, so they answer `require` as well
|
|
21
|
+
// as `import`: an app's package.json has no `"type": "module"`, so Node starts
|
|
22
|
+
// a server file as CommonJS before it sees the `import`s.
|
|
23
|
+
registerHooks({ resolve, load });
|