bare-stow 0.1.0 → 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/README.md CHANGED
@@ -144,6 +144,10 @@ opts = {
144
144
 
145
145
  Any additional options are forwarded to `bare-pack`. See <https://github.com/holepunchto/bare-pack> for the full set, including `builtins`, `imports`, `defer`, and `resolve`.
146
146
 
147
+ #### TypeScript
148
+
149
+ TypeScript modules are supported out of the box: `.ts`, `.mts`, and `.cts` sources are stripped of their type syntax with [`bare-type-stripper`](https://github.com/holepunchto/bare-type-stripper) as they are read, and their extensions are aliased to `.js`, `.mjs`, and `.cjs` respectively for module-type detection. The stripper only erases types, so the module system follows the aliased JavaScript extension. Because stripping is purely lexical, non-erasable constructs (`enum`, `namespace` with a body, parameter properties) throw, and JSX (`.tsx`) is not supported.
150
+
147
151
  ### Targets and RPC providers
148
152
 
149
153
  The harness and RPC code generators are decoupled from the bundler: `target`, `client`, and `server` each accept a provider object directly, so generators can live outside `bare-stow`. Passing a name instead resolves it through `resolveTarget` / `resolveRPC`, which default to the built-in registries.
package/bin.js CHANGED
@@ -2,7 +2,6 @@
2
2
  const path = require('path')
3
3
  const { pathToFileURL } = require('url')
4
4
  const { command, flag, arg, summary } = require('paparam')
5
- const { resolve } = require('bare-module-traverse')
6
5
  const pkg = require('./package')
7
6
  const stow = require('.')
8
7
 
@@ -72,7 +71,6 @@ const cmd = command(
72
71
  hosts,
73
72
  resolveTarget,
74
73
  resolveRPC,
75
- resolve: resolve.bare,
76
74
  builtins,
77
75
  imports,
78
76
  defer
package/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  const path = require('path')
2
2
  const { fileURLToPath } = require('url')
3
3
  const pack = require('bare-pack')
4
- const { resolve } = require('bare-module-traverse')
5
4
  const id = require('bare-bundle-id')
5
+ const strip = require('bare-type-stripper')
6
6
  const fs = require('./lib/fs')
7
7
  const shim = require('./lib/shim')
8
8
  const harness = require('./lib/target')
9
9
  const rpc = require('./lib/rpc')
10
+ const resolve = require('./lib/resolve')
10
11
 
11
12
  module.exports = async function* stow(entry, target, out, opts = {}) {
12
13
  if (!target) throw new Error("'target' is required")
@@ -64,12 +65,17 @@ module.exports = async function* stow(entry, target, out, opts = {}) {
64
65
  const bundle = await pack(
65
66
  shimURL,
66
67
  {
67
- resolve: resolve.bare,
68
68
  ...packOpts,
69
+ resolve,
70
+ aliases: {
71
+ '.ts': '.js',
72
+ '.mts': '.mjs',
73
+ '.cts': '.cjs'
74
+ },
75
+ base,
69
76
  hosts,
70
77
  linked: t.linked,
71
- offload: t.offload,
72
- base: base
78
+ offload: t.offload
73
79
  },
74
80
  readModule,
75
81
  fs.listPrefix,
@@ -130,10 +136,16 @@ function isOffloadEnabled(offload) {
130
136
  }
131
137
 
132
138
  function wrapReadModule(readModule, shimURL, shimSource) {
133
- return function (url) {
139
+ return async function (url) {
134
140
  if (url.href === shimURL.href) return shimSource
135
141
 
136
- return readModule(url)
142
+ const source = await readModule(url)
143
+
144
+ if (source === null) return null
145
+
146
+ if (/\.(c|m)?ts$/.test(url.pathname)) return strip(source)
147
+
148
+ return source
137
149
  }
138
150
  }
139
151
 
package/lib/resolve.js ADDED
@@ -0,0 +1,40 @@
1
+ const lex = require('bare-module-lexer')
2
+ const { resolve } = require('bare-module-traverse')
3
+
4
+ module.exports = function bare(entry, parentURL, opts = {}) {
5
+ const { linked = false, hosts } = opts
6
+
7
+ let extensions
8
+ let conditions = hosts.map((host) => ['bare', 'node', ...host.split('-')])
9
+
10
+ if (entry.type & lex.constants.ADDON) {
11
+ extensions = linked ? [] : ['.bare', '.node']
12
+ conditions = conditions.map((conditions) => ['addon', ...conditions])
13
+
14
+ return resolve.addon(entry.specifier || '.', parentURL, {
15
+ ...opts,
16
+ extensions,
17
+ conditions,
18
+ hosts,
19
+ linked
20
+ })
21
+ }
22
+
23
+ if (entry.type & lex.constants.ASSET) {
24
+ conditions = conditions.map((conditions) => ['asset', ...conditions])
25
+ } else {
26
+ extensions = ['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.json', '.bare', '.node']
27
+
28
+ if (entry.type & lex.constants.REQUIRE) {
29
+ conditions = conditions.map((conditions) => ['require', ...conditions])
30
+ } else if (entry.type & lex.constants.IMPORT) {
31
+ conditions = conditions.map((conditions) => ['import', ...conditions])
32
+ }
33
+ }
34
+
35
+ return resolve.module(entry.specifier, parentURL, {
36
+ ...opts,
37
+ extensions,
38
+ conditions
39
+ })
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-stow",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Module bundling and harness generation for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -61,10 +61,12 @@
61
61
  "bare-bundle": "^1.8.3",
62
62
  "bare-bundle-id": "^1.0.0",
63
63
  "bare-fs": "^4.2.1",
64
+ "bare-module-lexer": "^1.5.2",
64
65
  "bare-module-traverse": "^2.1.1",
65
66
  "bare-pack": "^2.1.0",
66
67
  "bare-path": "^3.0.0",
67
68
  "bare-stream": "^2.13.1",
69
+ "bare-type-stripper": "^0.1.2",
68
70
  "paparam": "^1.5.0"
69
71
  },
70
72
  "devDependencies": {