@hybridly/vite 0.0.1-alpha.2 → 0.0.1-alpha.4

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/index.cjs CHANGED
@@ -27,29 +27,37 @@ const debug = {
27
27
  layout: makeDebugger__default(LAYOUT_PLUGIN_NAME)
28
28
  };
29
29
 
30
- const TEMPLATE_LAYOUT_REGEX = /<template +layout(?: *= *['"](?:(?:(\w+):)?(\w+))['"] *)?>/;
30
+ const TEMPLATE_LAYOUT_REGEX = /<template +layout(?: *= *['"]((?:[\w\/-_,](?:,\ )?)+)['"] *)?>/;
31
31
  const TYPESCRIPT_REGEX = /lang=['"]ts['"]/;
32
32
  const layout = (options = {}) => {
33
+ const defaultLayoutName = options?.defaultLayoutName ?? "default";
33
34
  const base = options?.directory ? options?.directory : path__default.resolve(process.cwd(), "resources", "views", "layouts");
34
- const layoutPath = options?.resolve ? options.resolve : (layoutName) => vite.normalizePath(path__default.resolve(base, `${layoutName ?? "default"}.vue`)).replaceAll("\\", "/");
35
+ const templateRegExp = options?.templateRegExp ? options?.templateRegExp : TEMPLATE_LAYOUT_REGEX;
36
+ const getLayoutPath = options?.resolve ? options.resolve : (layoutName) => vite.normalizePath(path__default.resolve(base, `${layoutName}.vue`)).replaceAll("\\", "/");
35
37
  debug.layout("Registered layout path:", base);
36
38
  return {
37
39
  name: LAYOUT_PLUGIN_NAME,
38
40
  transform: (code, id) => {
39
- if (!TEMPLATE_LAYOUT_REGEX.test(code)) {
41
+ if (!templateRegExp.test(code)) {
40
42
  return;
41
43
  }
42
- return code.replace(TEMPLATE_LAYOUT_REGEX, (_, __, layoutName) => {
44
+ return code.replace(templateRegExp, (_, layoutName) => {
43
45
  const isTypeScript = TYPESCRIPT_REGEX.test(code);
44
- const importPath = layoutPath(layoutName);
45
- debug.layout(`Resolved layout "${layoutName}":`, {
46
+ const layouts = layoutName?.toString()?.replaceAll(" ", "").split(",") ?? [defaultLayoutName];
47
+ const importName = (i) => `__hybridly_layout_${i}`;
48
+ const exports = layouts.map((_2, i) => importName(i));
49
+ const imports = layouts.reduce((imports2, layoutName2, i) => `
50
+ ${imports2}
51
+ import ${importName(i)} from '${getLayoutPath(layoutName2)}';
52
+ `, "").trim();
53
+ debug.layout(`Resolved layouts "${layouts.join(", ")}":`, {
46
54
  sourceFile: id,
47
- importPath
55
+ layouts
48
56
  });
49
57
  return `
50
58
  <script${isTypeScript ? ' lang="ts"' : ""}>
51
- import layout from '${importPath}'
52
- export default { layout }
59
+ ${imports}
60
+ export default { layout: [${exports.join(", ")}] }
53
61
  <\/script>
54
62
  <template>
55
63
  `;
@@ -103,6 +111,7 @@ async function writeDefinitions(options, collection) {
103
111
  }];
104
112
  }));
105
113
  const definitions = generateDefinitions().replace("__URL__", collection?.url ?? "").replace("__ROUTES__", JSON.stringify(routes).replaceAll('"__key_placeholder__"', "any"));
114
+ fs__default.mkdirSync(path__default.dirname(target), { recursive: true });
106
115
  fs__default.writeFileSync(target, definitions, { encoding: "utf-8" });
107
116
  }
108
117
  function generateDefinitions() {
package/dist/index.d.ts CHANGED
@@ -7,6 +7,10 @@ interface Options {
7
7
  router?: false | RouterOptions;
8
8
  }
9
9
  interface LayoutOptions {
10
+ /** Name of the layout used when no argument is provided to `layout`. */
11
+ defaultLayoutName?: string;
12
+ /** Custom RegExp for parsing the template string. */
13
+ templateRegExp?: RegExp;
10
14
  /** The directory in which layouts are stored. */
11
15
  directory?: string;
12
16
  /** Function that resolves the layout path given its name. */
package/dist/index.mjs CHANGED
@@ -17,29 +17,37 @@ const debug = {
17
17
  layout: makeDebugger(LAYOUT_PLUGIN_NAME)
18
18
  };
19
19
 
20
- const TEMPLATE_LAYOUT_REGEX = /<template +layout(?: *= *['"](?:(?:(\w+):)?(\w+))['"] *)?>/;
20
+ const TEMPLATE_LAYOUT_REGEX = /<template +layout(?: *= *['"]((?:[\w\/-_,](?:,\ )?)+)['"] *)?>/;
21
21
  const TYPESCRIPT_REGEX = /lang=['"]ts['"]/;
22
22
  const layout = (options = {}) => {
23
+ const defaultLayoutName = options?.defaultLayoutName ?? "default";
23
24
  const base = options?.directory ? options?.directory : path.resolve(process.cwd(), "resources", "views", "layouts");
24
- const layoutPath = options?.resolve ? options.resolve : (layoutName) => normalizePath(path.resolve(base, `${layoutName ?? "default"}.vue`)).replaceAll("\\", "/");
25
+ const templateRegExp = options?.templateRegExp ? options?.templateRegExp : TEMPLATE_LAYOUT_REGEX;
26
+ const getLayoutPath = options?.resolve ? options.resolve : (layoutName) => normalizePath(path.resolve(base, `${layoutName}.vue`)).replaceAll("\\", "/");
25
27
  debug.layout("Registered layout path:", base);
26
28
  return {
27
29
  name: LAYOUT_PLUGIN_NAME,
28
30
  transform: (code, id) => {
29
- if (!TEMPLATE_LAYOUT_REGEX.test(code)) {
31
+ if (!templateRegExp.test(code)) {
30
32
  return;
31
33
  }
32
- return code.replace(TEMPLATE_LAYOUT_REGEX, (_, __, layoutName) => {
34
+ return code.replace(templateRegExp, (_, layoutName) => {
33
35
  const isTypeScript = TYPESCRIPT_REGEX.test(code);
34
- const importPath = layoutPath(layoutName);
35
- debug.layout(`Resolved layout "${layoutName}":`, {
36
+ const layouts = layoutName?.toString()?.replaceAll(" ", "").split(",") ?? [defaultLayoutName];
37
+ const importName = (i) => `__hybridly_layout_${i}`;
38
+ const exports = layouts.map((_2, i) => importName(i));
39
+ const imports = layouts.reduce((imports2, layoutName2, i) => `
40
+ ${imports2}
41
+ import ${importName(i)} from '${getLayoutPath(layoutName2)}';
42
+ `, "").trim();
43
+ debug.layout(`Resolved layouts "${layouts.join(", ")}":`, {
36
44
  sourceFile: id,
37
- importPath
45
+ layouts
38
46
  });
39
47
  return `
40
48
  <script${isTypeScript ? ' lang="ts"' : ""}>
41
- import layout from '${importPath}'
42
- export default { layout }
49
+ ${imports}
50
+ export default { layout: [${exports.join(", ")}] }
43
51
  <\/script>
44
52
  <template>
45
53
  `;
@@ -93,6 +101,7 @@ async function writeDefinitions(options, collection) {
93
101
  }];
94
102
  }));
95
103
  const definitions = generateDefinitions().replace("__URL__", collection?.url ?? "").replace("__ROUTES__", JSON.stringify(routes).replaceAll('"__key_placeholder__"', "any"));
104
+ fs.mkdirSync(path.dirname(target), { recursive: true });
96
105
  fs.writeFileSync(target, definitions, { encoding: "utf-8" });
97
106
  }
98
107
  function generateDefinitions() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hybridly/vite",
3
- "version": "0.0.1-alpha.2",
3
+ "version": "0.0.1-alpha.4",
4
4
  "description": "A solution to develop server-driven, client-rendered applications",
5
5
  "keywords": [
6
6
  "hybridly",
@@ -39,7 +39,7 @@
39
39
  "vue": "^3.2.33"
40
40
  },
41
41
  "dependencies": {
42
- "@hybridly/core": "0.0.1-alpha.2",
42
+ "@hybridly/core": "0.0.1-alpha.4",
43
43
  "throttle-debounce": "^5.0.0"
44
44
  },
45
45
  "devDependencies": {