setupin 2.2.3 → 2.3.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 (2) hide show
  1. package/dist/main.js +93 -73
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -1,50 +1,6 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- function when(gist, verifyer = gist) {
5
- return function(classify) {
6
- const sym = Object.getOwnPropertySymbols(classify).find((sym2) => sym2.description === "default");
7
- const handler = classify[verifyer] ?? (sym && classify[sym]);
8
- return handler?.(gist);
9
- };
10
- }
11
- function loaded(callback) {
12
- document.addEventListener("DOMContentLoaded", callback);
13
- }
14
-
15
- function observe(SorM, callback) {
16
- return when(SorM, typeof SorM)({
17
- string: (selector) => new Promise((resolve, reject) => {
18
- let isFound = false;
19
- new MutationObserver((mutations, observer) => {
20
- for (const mutation of mutations) {
21
- const target = mutation.target?.querySelector(selector);
22
- if (target) {
23
- isFound = true;
24
- observer.disconnect();
25
- const result = callback?.(target);
26
- result instanceof Error ? reject(result) : resolve(result);
27
- break;
28
- }
29
- }
30
- loaded(() => {
31
- if (isFound)
32
- return;
33
- observer.disconnect();
34
- resolve(callback?.(void 0));
35
- console.warn(`No ${selector} found`);
36
- });
37
- }).observe(document, {
38
- childList: true,
39
- subtree: true
40
- });
41
- }),
42
- object: (map) => Object.fromEntries(
43
- Object.entries(map).map(([key, fn]) => [key, observe(key, fn)])
44
- )
45
- });
46
- }
47
-
48
4
  var lib = {};
49
5
 
50
6
  Object.defineProperty(lib, '__esModule', {
@@ -14068,9 +14024,54 @@
14068
14024
  lib.parseExpression = parseExpression;
14069
14025
  lib.tokTypes = tokTypes;
14070
14026
 
14071
- function getASTBody(code) {
14072
- return parse_1(code, { sourceType: "module" }).program.body;
14027
+ function extractImport(astBody, code) {
14028
+ const imports = astBody.filter(({ type }) => type === "ImportDeclaration").map((node) => code.slice(node.start, node.end));
14029
+ return {
14030
+ importText: imports.join("\n"),
14031
+ setupText: code.replace(new RegExp(imports.join("|"), "g"), "")
14032
+ };
14033
+ }
14034
+
14035
+ function observe(selector, callback) {
14036
+ return new Promise((resolve, reject) => {
14037
+ let isFound = false;
14038
+ new MutationObserver((mutations, observer) => {
14039
+ for (const mutation of mutations) {
14040
+ const target = mutation.target?.querySelector(selector);
14041
+ if (target) {
14042
+ isFound = true;
14043
+ observer.disconnect();
14044
+ resolve(callback?.(target));
14045
+ break;
14046
+ }
14047
+ }
14048
+ document.addEventListener("DOMContentLoaded", () => {
14049
+ if (isFound)
14050
+ return;
14051
+ observer.disconnect();
14052
+ const result = callback?.(void 0);
14053
+ if (result instanceof Error) {
14054
+ reject(result);
14055
+ } else {
14056
+ resolve(result);
14057
+ console.warn(`No ${selector} found`);
14058
+ }
14059
+ });
14060
+ }).observe(document, {
14061
+ childList: true,
14062
+ subtree: true
14063
+ });
14064
+ });
14065
+ }
14066
+
14067
+ function when(gist, verifyer = gist) {
14068
+ return function(classify) {
14069
+ const sym = Object.getOwnPropertySymbols(classify).find((sym2) => sym2.description === "default");
14070
+ const handler = classify[verifyer] ?? (sym && classify[sym]);
14071
+ return handler?.(gist);
14072
+ };
14073
14073
  }
14074
+
14074
14075
  function getGlobalVars(astBody) {
14075
14076
  return astBody.flatMap((node) => when(node, node.type)({
14076
14077
  FunctionDeclaration: ({ id }) => id ? [id.name] : [],
@@ -14095,43 +14096,62 @@
14095
14096
  });
14096
14097
  }
14097
14098
  }
14098
- function extractImport(code, astBody) {
14099
- const imports = astBody.filter(({ type }) => type === "ImportDeclaration").map((node) => code.slice(node.start, node.end));
14100
- return [imports.join("\n"), code.replace(new RegExp(imports.join("|"), "g"), "")];
14099
+
14100
+ function ast(code) {
14101
+ const astBody = parse_1(code, { sourceType: "module" }).program.body;
14102
+ return {
14103
+ getGlobalVars: () => getGlobalVars(astBody),
14104
+ extractImport: () => extractImport(astBody, code)
14105
+ };
14101
14106
  }
14102
14107
 
14103
- function parseSetup(scriptEl) {
14104
- const scriptText = scriptEl?.textContent ?? "";
14105
- const astBody = getASTBody(scriptText);
14106
- const [importText, setupText] = extractImport(scriptText, astBody);
14107
- if (scriptEl && importText) {
14108
- scriptEl.type = "module";
14109
- scriptEl.textContent = importText;
14108
+ function parseSetup(setupEl) {
14109
+ const scriptContent = setupEl?.textContent ?? "";
14110
+ if (setupEl) {
14111
+ setupEl.type = "module";
14112
+ setupEl.textContent = "";
14110
14113
  } else {
14111
- scriptEl?.remove();
14114
+ setupEl = document.createElement("script");
14115
+ setupEl.type = "module";
14116
+ document.head.appendChild(setupEl);
14112
14117
  }
14118
+ const { extractImport, getGlobalVars } = ast(scriptContent);
14113
14119
  return {
14114
- setupText,
14115
- retNames: getGlobalVars(astBody)
14120
+ ...extractImport(),
14121
+ retNames: getGlobalVars(),
14122
+ setupEl
14116
14123
  };
14117
14124
  }
14118
14125
 
14119
- function parseTemplate(body) {
14120
- const template = document.querySelector("head>template");
14121
- const templateContent = template?.content.cloneNode(true);
14122
- template?.remove();
14123
- body?.replaceChildren(templateContent ?? "");
14124
- return template ? body : new Error("No template found");
14126
+ function parseTemplate(templateEl) {
14127
+ if (!templateEl)
14128
+ return new Error("No template found");
14129
+ const templateContent = templateEl.innerHTML;
14130
+ templateEl.remove();
14131
+ return templateContent.replace(/[`"'$\\]/g, (s) => `\\${s}`);
14125
14132
  }
14126
14133
 
14127
- const oTemplate = observe("body", parseTemplate);
14134
+ const oTemplate = observe("head>template", parseTemplate);
14128
14135
  const oSetup = observe("script[setup]", parseSetup);
14129
- loaded(async () => {
14130
- const [{ setupText, retNames }, Template] = await Promise.all([oSetup, oTemplate]);
14131
- const Vue = window.Vue = await import('https://unpkg.com/vue/dist/vue.esm-browser.prod.js');
14132
- Vue.createApp({
14133
- setup: new Function(`const { ${Object.keys(Vue)} } = Vue; ${setupText} return { ${retNames} }`)
14134
- }).mount(Template);
14135
- });
14136
+ (async () => {
14137
+ const template = await oTemplate;
14138
+ const { setupEl, importText, setupText, retNames } = await oSetup;
14139
+ setupEl.textContent = `
14140
+ import * as Vue from 'https://unpkg.com/vue/dist/vue.esm-browser.prod.js'
14141
+ ${importText}
14142
+ for (const k in Vue) {
14143
+ window[k] = Vue[k]
14144
+ }
14145
+ Vue.createApp({
14146
+ template: \`${template}\`,
14147
+ setup(){
14148
+ ${setupText}
14149
+ return {
14150
+ ${retNames}
14151
+ }
14152
+ }
14153
+ }).mount(document.body)
14154
+ `;
14155
+ })();
14136
14156
 
14137
14157
  })();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "setupin",
3
3
  "type": "module",
4
- "version": "2.2.3",
4
+ "version": "2.3.0",
5
5
  "description": "Vue SFC? HTML! <script setup> in html",
6
6
  "author": "tofu-xx <tofu-xx@foxmail.com>",
7
7
  "license": "MIT",