@primate/solid 0.0.1 → 0.1.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.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@primate/solid",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "Primate Solid module",
5
5
  "homepage": "https://primatejs.com/modules/solid",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
7
7
  "license": "MIT",
8
8
  "files": [
9
9
  "src/**/*.js",
10
+ "src/errors.json",
10
11
  "!src/**/*.spec.js"
11
12
  ],
12
13
  "repository": {
@@ -7,13 +7,13 @@ const {groups: {code: hydration_script}} = generateHydrationScript()
7
7
 
8
8
  export default ({names, data}, options) => `
9
9
  import * as components from "app";
10
- import {hydrate_solid} from "app";
10
+ import {hydrate_solid, render_solid} from "app";
11
11
 
12
- window._$HY = { events: [], completed: new WeakSet(), r: {} };
12
+ window._$HY = {events: [], completed: new WeakSet(), r: {}};
13
13
 
14
14
  ${hydration_script}
15
15
 
16
- const root = hydrate_solid(() => components.${rootname}({
16
+ let dispose = hydrate_solid(() => components.${rootname}({
17
17
  components: [${names.map(name => `components.${name}`).join(", ")}],
18
18
  data: JSON.parse(${JSON.stringify(JSON.stringify(data))}),
19
19
  }), globalThis.window.document.body);
@@ -1,5 +1,4 @@
1
1
  export {default as client} from "./client.js";
2
2
  export {default as hydrate} from "./hydrate.js";
3
3
  export {default as create_root} from "./create-root.js";
4
- export {default as create_root2} from "./create-root2.js";
5
4
  export {default as rootname} from "./rootname.js";
@@ -1 +1,4 @@
1
- export default "export {hydrate as hydrate_solid} from \"solid-js/web\";";
1
+ export default `export {
2
+ hydrate as hydrate_solid,
3
+ render as render_solid,
4
+ } from "solid-js/web";`;
@@ -3,10 +3,9 @@ import rootname from "./rootname.js";
3
3
  export default `
4
4
  const {liveview} = components;
5
5
  window.addEventListener("DOMContentLoaded", _ => liveview((props, update) => {
6
- root.render(
7
- createElement(components.${rootname}, {
8
- components: props.names.map(name => components[name]),
9
- data: props.data,
10
- })
11
- );
6
+ dispose();
7
+ dispose = render_solid(() => components.${rootname}({
8
+ components: props.names.map(name => components[name]),
9
+ data: props.data,
10
+ }), globalThis.window.document.body);
12
11
  }));`;
@@ -0,0 +1,10 @@
1
+ {
2
+ "module": "primate/solid",
3
+ "errors": {
4
+ "MissingComponent": {
5
+ "message": "missing component {0}",
6
+ "fix": "create {1} or remove route function",
7
+ "level": "Error"
8
+ }
9
+ }
10
+ }
package/src/module.js CHANGED
@@ -6,7 +6,7 @@ import solid from "babel-preset-solid";
6
6
  import {renderToString} from "solid-js/web";
7
7
  import {tryreturn} from "runtime-compat/async";
8
8
  import errors from "./errors.js";
9
- import {client, create_root, create_root2, rootname, hydrate} from "./client/exports.js";
9
+ import {client, create_root, rootname, hydrate} from "./client/exports.js";
10
10
 
11
11
  // do not hard-depend on node
12
12
  const packager = import.meta.runtime?.packager ?? "package.json";
@@ -45,7 +45,7 @@ const type = "module";
45
45
 
46
46
  const load = async path =>
47
47
  tryreturn(async () => (await import(`${path}.js`)).default)
48
- .orelse(_ => errors.MissingComponent.throw(path.name, path));
48
+ .orelse(error => {console.log(error);return errors.MissingComponent.throw(path.name, path)});
49
49
 
50
50
  const make_component = base => async (name, props) =>
51
51
  ({name, component: await load(base.join(name)), props});
@@ -108,11 +108,6 @@ const options = {
108
108
  }],
109
109
  };
110
110
 
111
- const endings = {
112
- solid: ".solid",
113
- js: ".solid.js",
114
- };
115
-
116
111
  const transform = {
117
112
  async server(file) {
118
113
  return (await transformAsync(file, {presets: [options.server]})).code;
@@ -127,22 +122,28 @@ export default ({
127
122
  extension = "jsx",
128
123
  } = {}) => {
129
124
  const env = {};
125
+ const copy_re = new RegExp(`^.*.(?:${extension})$`, "u");
126
+ const collect_re = new RegExp(`^.*.${extension}$`, "u");
127
+ const extensions = {
128
+ from: `.${extension}`,
129
+ to: `.${extension}.js`,
130
+ };
130
131
 
131
132
  return {
132
133
  name: "@primate/solid",
133
- init(app, next) {
134
+ init(app, next/*0.21 compat, remove >0.21*/ = () => null) {
134
135
  env.source = app.build.paths.components;
135
136
 
136
137
  return next(app);
137
138
  },
138
139
  register(app, next) {
139
- app.register("solid", handler);
140
+ app.register(extension, handler);
140
141
  return next(app);
141
142
  },
142
143
  async compile(app, next) {
143
144
  // copy components to build/components
144
- await app.copy(app.paths.components, env.source, /^.*.(?:solid)$/u);
145
- const components = await env.source.collect(/^.*.solid$/u);
145
+ await app.copy(app.paths.components, env.source, copy_re);
146
+ const components = await env.source.collect(collect_re);
146
147
  const target = app.build.paths.server.join(app.config.build.app);
147
148
  await target.file.create();
148
149
 
@@ -151,10 +152,10 @@ export default ({
151
152
  const code = await transform.server(file);
152
153
  const to = target.join(`${component.path}.js`.replace(env.source, ""));
153
154
  await to.directory.file.create();
154
- await to.file.write(code.replaceAll(endings.solid, endings.js));
155
+ await to.file.write(code.replaceAll(extensions.from, extensions.to));
155
156
  }));
156
157
 
157
- const root = await transform.server(create_root2(app.layoutDepth,
158
+ const root = await transform.server(create_root(app.layoutDepth,
158
159
  dynamicProps));
159
160
  const to = app.build.paths.server.join(filename);
160
161
  await to.file.write(root);
@@ -166,7 +167,7 @@ export default ({
166
167
  await target.file.create();
167
168
 
168
169
  // create client components
169
- const components = await env.source.collect(/^.*.solid/u);
170
+ const components = await env.source.collect(collect_re);
170
171
  await Promise.all(components.map(async component => {
171
172
  const name = component.path.replace(`${env.source}/`, "");
172
173
  const file = await component.file.read();
@@ -178,7 +179,7 @@ export default ({
178
179
  {
179
180
  const src = `${path}.js`.replace(`${env.source}`, _ => build);
180
181
  await app.publish({src,
181
- code: code.replaceAll(endings.solid, endings.js),
182
+ code: code.replaceAll(extensions.from, extensions.to),
182
183
  type});
183
184
  }
184
185
  const imported = await normalize(name);
@@ -195,7 +196,7 @@ export default ({
195
196
  app.bootstrap({type: "script", code: hydrate});
196
197
 
197
198
  {
198
- const code = await transform.client(create_root2(app.layoutDepth,
199
+ const code = await transform.client(create_root(app.layoutDepth,
199
200
  dynamicProps));
200
201
  const src = new Path(app.config.http.static.root, filename);
201
202
  await app.publish({src, code, type});
@@ -1,22 +0,0 @@
1
- export default (length, data) => {
2
- const n = length - 1;
3
- const body = Array.from({length: n}, (_, i) => i - 1)
4
- .reduceRight((child, _, i) => `<Show
5
- when={components[${i + 1}] !== undefined}
6
- fallback={
7
- <>
8
- <Dynamic component={components[${i}]} ${data}={data[${i}]} />
9
- </>
10
- }
11
- >
12
- <Dynamic component={components[${i}]} ${data}={data[${i}]}>${child}</Dynamic>
13
- </Show>
14
- `, `<Dynamic component={components[${n}]} ${data}={data[${n}]} />`);
15
-
16
- return `
17
- import {Dynamic, Show} from "solid-js/web";
18
- export default function Root({components, data} = {}) {
19
- return ${body};
20
- };
21
- `;
22
- };