@dative-gpi/foundation-shared-loader 0.0.2

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.
@@ -0,0 +1,4 @@
1
+ export declare function generateImports(source: string): {
2
+ code: string;
3
+ source: string;
4
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateImports = void 0;
4
+ const getImports_1 = require("./getImports");
5
+ function generateImports(source) {
6
+ const { imports, components, directives } = (0, getImports_1.getImports)(source);
7
+ let code = '';
8
+ if (components.length || directives.length) {
9
+ code += '\n\n/* Foundation */\n';
10
+ Array.from(imports).sort((a, b) => a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0))
11
+ .forEach(([from, names]) => {
12
+ code += `import ${names.join(', ')} from "${from}"\n`;
13
+ });
14
+ code += '\n';
15
+ source = [...components, ...directives].reduce((acc, v) => {
16
+ return acc.slice(0, v.index) + ' '.repeat(v.length) + acc.slice(v.index + v.length);
17
+ }, source);
18
+ }
19
+ return { code, source };
20
+ }
21
+ exports.generateImports = generateImports;
@@ -0,0 +1,6 @@
1
+ import { type TemplateMatch } from './parseTemplate';
2
+ export declare function getImports(source: string): {
3
+ imports: Map<string, string[]>;
4
+ components: TemplateMatch[];
5
+ directives: TemplateMatch[];
6
+ };
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getImports = void 0;
27
+ const parseTemplate_1 = require("./parseTemplate");
28
+ const sharedImportMap = __importStar(require("@dative-gpi/foundation-shared-components/importMap.json"));
29
+ const coreImportMap = __importStar(require("@dative-gpi/foundation-core-components/importMap.json"));
30
+ const adminImportMap = __importStar(require("@dative-gpi/foundation-admin-components/importMap.json"));
31
+ function getImports(source) {
32
+ const { components, directives } = (0, parseTemplate_1.parseTemplate)(source);
33
+ const resolvedComponents = [];
34
+ const resolvedDirectives = [];
35
+ const imports = new Map();
36
+ if (components.size || directives.size) {
37
+ components.forEach(component => {
38
+ if (component.name in sharedImportMap.components) {
39
+ resolvedComponents.push(component);
40
+ addImport(imports, component.name, component.symbol, sharedImportMap.components[component.name].from);
41
+ }
42
+ else if (component.name in coreImportMap.components) {
43
+ resolvedComponents.push(component);
44
+ addImport(imports, component.name, component.symbol, coreImportMap.components[component.name].from);
45
+ }
46
+ else if (component.name in adminImportMap.components) {
47
+ resolvedComponents.push(component);
48
+ addImport(imports, component.name, component.symbol, adminImportMap.components[component.name].from);
49
+ }
50
+ });
51
+ // directives.forEach(directive => {
52
+ // if (importMap.directives.includes(directive.name)) {
53
+ // resolvedDirectives.push(directive)
54
+ // }
55
+ // })
56
+ }
57
+ // resolvedComponents.forEach(component => {
58
+ // addImport(imports, component.name, component.symbol, (sharedImportMap.components as any)[component.name].from)
59
+ // })
60
+ // resolvedDirectives.forEach(directive => {
61
+ // addImport(imports, directive.name, directive.symbol, 'foundation/lib/directives/index.mjs')
62
+ // })
63
+ return {
64
+ imports,
65
+ components: resolvedComponents,
66
+ directives: resolvedDirectives,
67
+ };
68
+ }
69
+ exports.getImports = getImports;
70
+ function addImport(imports, name, as, from) {
71
+ if (!imports.has(from)) {
72
+ imports.set(from, []);
73
+ }
74
+ imports.get(from).push(`${as}`);
75
+ }
@@ -0,0 +1,2 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function importPlugin(): Plugin;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.importPlugin = void 0;
4
+ const path_1 = require("path");
5
+ const generateImports_1 = require("./generateImports");
6
+ const url_1 = require("url");
7
+ function parseId(id) {
8
+ const { query, pathname } = (0, url_1.parse)(id);
9
+ return {
10
+ query: query ? Object.fromEntries(new url_1.URLSearchParams(query)) : null,
11
+ path: pathname ?? id
12
+ };
13
+ }
14
+ function importPlugin() {
15
+ return {
16
+ name: 'foundation:import',
17
+ configResolved(config) {
18
+ if (config.plugins.findIndex(plugin => plugin.name === 'foundation:import') < config.plugins.findIndex(plugin => plugin.name === 'vite:vue')) {
19
+ throw new Error('Foundation plugin must be loaded after the vue plugin');
20
+ }
21
+ },
22
+ async transform(code, id) {
23
+ const { query, path } = parseId(id);
24
+ if (((!query || !('vue' in query)) && (0, path_1.extname)(path) === '.vue' && !/^import { render as _sfc_render } from ".*"$/m.test(code)) ||
25
+ (query && 'vue' in query && (query.type === 'template' || (query.type === 'script' && query.setup === 'true')))) {
26
+ const { code: imports, source } = (0, generateImports_1.generateImports)(code);
27
+ return {
28
+ code: source + imports,
29
+ map: null,
30
+ };
31
+ }
32
+ return null;
33
+ }
34
+ };
35
+ }
36
+ exports.importPlugin = importPlugin;
@@ -0,0 +1,3 @@
1
+ import { Plugin } from 'vite';
2
+ declare function FoundationSharedAutoImport(): Plugin[];
3
+ export default FoundationSharedAutoImport;
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const importPlugin_1 = require("./importPlugin");
4
+ function FoundationSharedAutoImport() {
5
+ const plugins = [];
6
+ plugins.push((0, importPlugin_1.importPlugin)());
7
+ return plugins;
8
+ }
9
+ module.exports = FoundationSharedAutoImport;
10
+ exports.default = FoundationSharedAutoImport;
@@ -0,0 +1,10 @@
1
+ export declare function parseTemplate(source: string): {
2
+ components: Set<TemplateMatch>;
3
+ directives: Set<TemplateMatch>;
4
+ };
5
+ export interface TemplateMatch {
6
+ symbol: string;
7
+ name: string;
8
+ index: number;
9
+ length: number;
10
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTemplate = void 0;
4
+ const vue_1 = require("vue");
5
+ function parseTemplate(source) {
6
+ const components = createSet(source.matchAll(/(?:var|const) (\w+) = _resolveComponent\("([\w-.]+)"\);?/gm));
7
+ const directives = createSet(source.matchAll(/(?:var|const) (\w+) = _resolveDirective\("([\w-.]+)"\);?/gm));
8
+ return { components, directives };
9
+ }
10
+ exports.parseTemplate = parseTemplate;
11
+ function createSet(matches) {
12
+ return new Set(Array.from(matches, i => ({
13
+ symbol: i[1],
14
+ name: (0, vue_1.capitalize)((0, vue_1.camelize)(i[2])),
15
+ index: i.index,
16
+ length: i[0].length,
17
+ })));
18
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@dative-gpi/foundation-shared-loader",
3
+ "version": "0.0.2",
4
+ "main": "dist/index.js",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "scripts": {
9
+ "build": "npx tsc --resolveJsonModule"
10
+ },
11
+ "types": "dist/index.d.ts",
12
+ "keywords": [
13
+ "vite-plugin"
14
+ ],
15
+ "description": "",
16
+ "devDependencies": {
17
+ "@babel/types": "^7.23.0",
18
+ "@dative-gpi/foundation-admin-components": "0.0.2",
19
+ "@dative-gpi/foundation-core-components": "0.0.2",
20
+ "@dative-gpi/foundation-shared-components": "0.0.2",
21
+ "@types/node": "^20.8.9",
22
+ "typescript": "^5.2.2",
23
+ "vite": "^4.5.0"
24
+ },
25
+ "gitHead": "44e9629c836e0cd06289b208b53120f07775ea5e"
26
+ }
@@ -0,0 +1,22 @@
1
+ import { getImports } from './getImports'
2
+
3
+ export function generateImports (source: string) {
4
+ const { imports, components, directives } = getImports(source)
5
+
6
+ let code = ''
7
+
8
+ if (components.length || directives.length) {
9
+ code += '\n\n/* Foundation */\n'
10
+
11
+ Array.from(imports).sort((a, b) => a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0))
12
+ .forEach(([from, names]) => {
13
+ code += `import ${names.join(', ')} from "${from}"\n`
14
+ })
15
+ code += '\n'
16
+
17
+ source = [...components, ...directives].reduce((acc, v) => {
18
+ return acc.slice(0, v.index) + ' '.repeat(v.length) + acc.slice(v.index + v.length)
19
+ }, source)
20
+ }
21
+ return { code, source }
22
+ }
@@ -0,0 +1,53 @@
1
+ import { parseTemplate, type TemplateMatch } from './parseTemplate'
2
+ import * as sharedImportMap from '@dative-gpi/foundation-shared-components/importMap.json'
3
+ import * as coreImportMap from '@dative-gpi/foundation-core-components/importMap.json'
4
+ import * as adminImportMap from '@dative-gpi/foundation-admin-components/importMap.json'
5
+
6
+ export function getImports (source: string) {
7
+ const { components, directives } = parseTemplate(source)
8
+ const resolvedComponents: TemplateMatch[] = []
9
+ const resolvedDirectives: TemplateMatch[] = []
10
+ const imports = new Map<string, string[]>()
11
+
12
+ if (components.size || directives.size) {
13
+ components.forEach(component => {
14
+ if (component.name in sharedImportMap.components) {
15
+ resolvedComponents.push(component)
16
+ addImport(imports, component.name, component.symbol, (sharedImportMap.components as any)[component.name].from)
17
+ }
18
+ else if (component.name in coreImportMap.components) {
19
+ resolvedComponents.push(component)
20
+ addImport(imports, component.name, component.symbol, (coreImportMap.components as any)[component.name].from)
21
+ }
22
+ else if (component.name in adminImportMap.components) {
23
+ resolvedComponents.push(component)
24
+ addImport(imports, component.name, component.symbol, (adminImportMap.components as any)[component.name].from)
25
+ }
26
+ })
27
+ // directives.forEach(directive => {
28
+ // if (importMap.directives.includes(directive.name)) {
29
+ // resolvedDirectives.push(directive)
30
+ // }
31
+ // })
32
+ }
33
+
34
+ // resolvedComponents.forEach(component => {
35
+ // addImport(imports, component.name, component.symbol, (sharedImportMap.components as any)[component.name].from)
36
+ // })
37
+ // resolvedDirectives.forEach(directive => {
38
+ // addImport(imports, directive.name, directive.symbol, 'foundation/lib/directives/index.mjs')
39
+ // })
40
+
41
+ return {
42
+ imports,
43
+ components: resolvedComponents,
44
+ directives: resolvedDirectives,
45
+ }
46
+ }
47
+
48
+ function addImport (imports: Map<string, string[]>, name: string, as: string, from: string) {
49
+ if (!imports.has(from)) {
50
+ imports.set(from, [])
51
+ }
52
+ imports.get(from)!.push(`${as}`)
53
+ }
@@ -0,0 +1,40 @@
1
+ import { extname } from 'path'
2
+ import type { Plugin } from 'vite'
3
+ import { generateImports } from './generateImports'
4
+ import { parse as parseUrl, URLSearchParams } from 'url'
5
+
6
+ function parseId(id: string) {
7
+ const { query, pathname } = parseUrl(id)
8
+
9
+ return {
10
+ query: query ? Object.fromEntries(new URLSearchParams(query)) : null,
11
+ path: pathname ?? id
12
+ }
13
+ }
14
+
15
+ export function importPlugin(): Plugin {
16
+ return {
17
+ name: 'foundation:import',
18
+ configResolved(config) {
19
+ if (config.plugins.findIndex(plugin => plugin.name === 'foundation:import') < config.plugins.findIndex(plugin => plugin.name === 'vite:vue')) {
20
+ throw new Error('Foundation plugin must be loaded after the vue plugin')
21
+ }
22
+ },
23
+ async transform(code, id) {
24
+ const { query, path } = parseId(id)
25
+
26
+ if (
27
+ ((!query || !('vue' in query)) && extname(path) === '.vue' && !/^import { render as _sfc_render } from ".*"$/m.test(code)) ||
28
+ (query && 'vue' in query && (query.type === 'template' || (query.type === 'script' && query.setup === 'true')))
29
+ ) {
30
+ const { code: imports, source } = generateImports(code)
31
+ return {
32
+ code: source + imports,
33
+ map: null,
34
+ }
35
+ }
36
+ return null
37
+ }
38
+
39
+ }
40
+ }
@@ -0,0 +1,13 @@
1
+ import { Plugin } from 'vite'
2
+
3
+ import { importPlugin } from './importPlugin'
4
+
5
+ function FoundationSharedAutoImport ( ): Plugin[] {
6
+ const plugins: Plugin[] = []
7
+ plugins.push(importPlugin())
8
+
9
+ return plugins
10
+ }
11
+
12
+ module.exports = FoundationSharedAutoImport
13
+ export default FoundationSharedAutoImport
@@ -0,0 +1,24 @@
1
+ import { camelize, capitalize } from 'vue'
2
+
3
+ export function parseTemplate (source: string) {
4
+ const components = createSet(source.matchAll(/(?:var|const) (\w+) = _resolveComponent\("([\w-.]+)"\);?/gm))
5
+ const directives = createSet(source.matchAll(/(?:var|const) (\w+) = _resolveDirective\("([\w-.]+)"\);?/gm))
6
+
7
+ return { components, directives }
8
+ }
9
+
10
+ export interface TemplateMatch {
11
+ symbol: string,
12
+ name: string,
13
+ index: number,
14
+ length: number,
15
+ }
16
+
17
+ function createSet (matches: IterableIterator<RegExpMatchArray>): Set<TemplateMatch> {
18
+ return new Set(Array.from(matches, i => ({
19
+ symbol: i[1],
20
+ name: capitalize(camelize(i[2])),
21
+ index: i.index!,
22
+ length: i[0].length,
23
+ })))
24
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "CommonJS",
5
+ "outDir": "./dist",
6
+ "rootDir": "./plugin",
7
+ "strict": false,
8
+ "declaration": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true
11
+ },
12
+ "include": ["plugin/**/*.ts", "plugin/importMap.json"]
13
+ }
14
+