@creopse/bridge 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Noé Gnanih
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Creopse Bridge
@@ -0,0 +1,2 @@
1
+ export declare const DASHBOARD_MODULE_NAME = "PluginDashboard";
2
+ export declare const SETTINGS_MODULE_NAME = "PluginSettings";
@@ -0,0 +1,3 @@
1
+ // Modules
2
+ export const DASHBOARD_MODULE_NAME = 'PluginDashboard';
3
+ export const SETTINGS_MODULE_NAME = 'PluginSettings';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Returns the base URL of the current window location.
3
+ *
4
+ * @return {string} The base URL of the current window location.
5
+ */
6
+ export declare const getBaseUrl: () => string;
7
+ /**
8
+ * Returns the URL of the current page by appending the base URL with the pathname.
9
+ *
10
+ * @return {string} The URL of the current page.
11
+ */
12
+ export declare const getPageUrl: () => string;
13
+ /**
14
+ * Removes the trailing slash from a given path.
15
+ *
16
+ * @param {string} path - The path to remove the trailing slash from.
17
+ * @return {string} The path without the trailing slash.
18
+ */
19
+ export declare const removeTrailingSlash: (path: string) => string;
20
+ /**
21
+ * Determines if a given string is a valid JSON string representing an object.
22
+ *
23
+ * @param {string} str - The string to be checked.
24
+ * @return {boolean} Returns true if the string is a valid JSON string of an object, false otherwise.
25
+ */
26
+ export declare const isStringifiedObject: (str: string) => boolean;
27
+ /**
28
+ * Sanitizes an ID by removing any characters that are not a letter, number, slash, or dash and replacing any slashes with dashes.
29
+ *
30
+ * @param {string} id - The ID to be sanitized.
31
+ * @return {string} The sanitized ID.
32
+ */
33
+ export declare const sanitizeId: (id: string) => string;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Returns the base URL of the current window location.
3
+ *
4
+ * @return {string} The base URL of the current window location.
5
+ */
6
+ export const getBaseUrl = () => {
7
+ const { protocol, hostname, port } = window.location;
8
+ return protocol + '//' + hostname + (port ? `:${port}` : '');
9
+ };
10
+ /**
11
+ * Returns the URL of the current page by appending the base URL with the pathname.
12
+ *
13
+ * @return {string} The URL of the current page.
14
+ */
15
+ export const getPageUrl = () => {
16
+ return getBaseUrl() + window.location.pathname;
17
+ };
18
+ /**
19
+ * Removes the trailing slash from a given path.
20
+ *
21
+ * @param {string} path - The path to remove the trailing slash from.
22
+ * @return {string} The path without the trailing slash.
23
+ */
24
+ export const removeTrailingSlash = (path) => {
25
+ if (path.length > 1 && path.charAt(path.length - 1) === '/') {
26
+ return path.substring(0, path.length - 1);
27
+ }
28
+ return path;
29
+ };
30
+ /**
31
+ * Determines if a given string is a valid JSON string representing an object.
32
+ *
33
+ * @param {string} str - The string to be checked.
34
+ * @return {boolean} Returns true if the string is a valid JSON string of an object, false otherwise.
35
+ */
36
+ export const isStringifiedObject = (str) => {
37
+ try {
38
+ const parsed = JSON.parse(str);
39
+ return (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed));
40
+ }
41
+ catch (e) {
42
+ return false;
43
+ }
44
+ };
45
+ /**
46
+ * Sanitizes an ID by removing any characters that are not a letter, number, slash, or dash and replacing any slashes with dashes.
47
+ *
48
+ * @param {string} id - The ID to be sanitized.
49
+ * @return {string} The sanitized ID.
50
+ */
51
+ export const sanitizeId = (id) => {
52
+ return id.replace(/[^a-z0-9\/-]/g, '').replace(/\//g, '-');
53
+ };
@@ -0,0 +1,2 @@
1
+ export * from './vue';
2
+ export * from './react';
@@ -0,0 +1,2 @@
1
+ export * from './vue';
2
+ export * from './react';
@@ -0,0 +1,45 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface CreopseReactPluginOptions {
3
+ /**
4
+ * Path to the manifest.jsonc file, relative to the project root.
5
+ * @default './manifest.jsonc'
6
+ */
7
+ manifestPath?: string;
8
+ /**
9
+ * Source directory, relative to the project root.
10
+ * @default './src'
11
+ */
12
+ srcDir?: string;
13
+ /**
14
+ * Output directory for the build.
15
+ * @default 'frontend'
16
+ */
17
+ outDir?: string;
18
+ /**
19
+ * Path to the i18n locales directory, relative to the project root.
20
+ * @default './src/i18n/locales'
21
+ */
22
+ i18nLocalesPath?: string;
23
+ /**
24
+ * Directories to scan for auto-imports, relative to the project root.
25
+ * @default ['src/hooks']
26
+ */
27
+ hooksDirs?: string[];
28
+ /**
29
+ * Shared modules for Module Federation.
30
+ * 'react', 'react-dom' and 'zustand' are always included.
31
+ * @default []
32
+ */
33
+ shared?: string[];
34
+ /**
35
+ * Whether to enable UnoCSS.
36
+ * @default false
37
+ */
38
+ unocss?: boolean;
39
+ /**
40
+ * Whether to generate an eslint auto-import config file.
41
+ * @default false
42
+ */
43
+ eslintAutoImport?: boolean;
44
+ }
45
+ export declare function CreopseReactPlugin(options?: CreopseReactPluginOptions): Plugin;
@@ -0,0 +1,152 @@
1
+ import federation from '@originjs/vite-plugin-federation';
2
+ import basicSsl from '@vitejs/plugin-basic-ssl';
3
+ import react from '@vitejs/plugin-react';
4
+ import { parse, stringify } from 'comment-json';
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import copy from 'rollup-plugin-copy';
8
+ import UnoCSS from 'unocss/vite';
9
+ import AutoImportPlugin from 'vite-plugin-auto-import-lite';
10
+ import i18nextLoader from 'vite-plugin-i18next-loader';
11
+ import json5Plugin from 'vite-plugin-json5';
12
+ import topLevelAwait from 'vite-plugin-top-level-await';
13
+ import { DASHBOARD_MODULE_NAME, SETTINGS_MODULE_NAME } from '../utils/constants';
14
+ import { sanitizeId } from '../utils/functions';
15
+ // ——— Plugin ——————————————————————————————————————————————————————————————————
16
+ export function CreopseReactPlugin(options = {}) {
17
+ const { manifestPath = './manifest.jsonc', srcDir = './src', outDir = 'frontend', i18nLocalesPath, hooksDirs = ['src/hooks'], shared = [], unocss = false, eslintAutoImport = false, } = options;
18
+ let root;
19
+ let manifest;
20
+ let pluginId;
21
+ let pluginEntry;
22
+ let exposes;
23
+ let previewPort;
24
+ let serverPort;
25
+ let sslEnabled;
26
+ function loadManifest() {
27
+ const fullPath = path.resolve(root, manifestPath);
28
+ const raw = fs.readFileSync(fullPath, 'utf-8');
29
+ manifest = parse(raw || '{}');
30
+ exposes = {};
31
+ if (manifest.pages && Array.isArray(manifest.pages)) {
32
+ for (const page of manifest.pages) {
33
+ exposes[`./${page.name}`] = page.module;
34
+ }
35
+ }
36
+ const dashboard = manifest.dashboard;
37
+ if (dashboard?.module) {
38
+ exposes[`./${DASHBOARD_MODULE_NAME}`] = dashboard.module;
39
+ }
40
+ const settings = manifest.settings;
41
+ if (settings?.module) {
42
+ exposes[`./${SETTINGS_MODULE_NAME}`] = settings.module;
43
+ }
44
+ const dev = manifest.development;
45
+ pluginId = sanitizeId(String(manifest.id)) || 'creopse-plugin';
46
+ pluginEntry = (manifest.entry || 'remoteEntry.js');
47
+ previewPort = (dev?.previewPort || 2160);
48
+ serverPort = (dev?.serverPort || 2161);
49
+ sslEnabled = (dev?.ssl || false);
50
+ }
51
+ return {
52
+ name: 'creopse-react-plugin',
53
+ config(config) {
54
+ root = config.root || process.cwd();
55
+ loadManifest();
56
+ const isPreview = process.env.APP_IS_PREVIEW === 'true';
57
+ const localesPath = i18nLocalesPath
58
+ ? path.resolve(root, i18nLocalesPath)
59
+ : path.resolve(root, srcDir, 'i18n/locales');
60
+ const sharedModules = ['react', 'react-dom', 'zustand', ...shared];
61
+ return {
62
+ envPrefix: 'APP_',
63
+ plugins: [
64
+ react(),
65
+ federation({
66
+ name: pluginId,
67
+ filename: pluginEntry,
68
+ exposes,
69
+ shared: sharedModules,
70
+ }),
71
+ topLevelAwait({
72
+ promiseExportName: '__tla',
73
+ promiseImportName: (i) => `__tla_${i}`,
74
+ }),
75
+ json5Plugin(),
76
+ i18nextLoader({
77
+ paths: [localesPath],
78
+ include: ['**/*.json'],
79
+ }),
80
+ AutoImportPlugin({
81
+ dirs: hooksDirs,
82
+ imports: ['react'],
83
+ vueTemplate: false,
84
+ ...(eslintAutoImport
85
+ ? {
86
+ eslintrc: {
87
+ enabled: true,
88
+ filepath: './.eslintrc-auto-import.json',
89
+ globalsPropValue: true,
90
+ },
91
+ }
92
+ : {}),
93
+ }),
94
+ ...(unocss ? [UnoCSS()] : []),
95
+ ...(sslEnabled ? [basicSsl()] : []),
96
+ ],
97
+ resolve: {
98
+ alias: [
99
+ {
100
+ find: '@',
101
+ replacement: path.resolve(root, srcDir),
102
+ },
103
+ ],
104
+ dedupe: ['react', 'react-dom', 'zustand'],
105
+ },
106
+ build: {
107
+ target: 'esnext',
108
+ outDir,
109
+ rollupOptions: {
110
+ plugins: [
111
+ copy({
112
+ targets: [
113
+ {
114
+ src: path.resolve(root, manifestPath),
115
+ dest: outDir,
116
+ transform() {
117
+ if (manifest?.mode) {
118
+ manifest.mode = isPreview ? 'development' : 'production';
119
+ }
120
+ const addBlankLineAfterCommas = (jsonc) => jsonc.replace(/(,)\s*\n/g, ',\n\n');
121
+ return addBlankLineAfterCommas(stringify(manifest, null, 2));
122
+ },
123
+ },
124
+ ],
125
+ hook: 'writeBundle',
126
+ verbose: false,
127
+ flatten: true,
128
+ }),
129
+ ],
130
+ },
131
+ assetsInlineLimit: Infinity,
132
+ cssCodeSplit: false,
133
+ },
134
+ preview: {
135
+ port: previewPort,
136
+ strictPort: true,
137
+ cors: true,
138
+ headers: {
139
+ 'Access-Control-Allow-Origin': '*',
140
+ },
141
+ },
142
+ server: {
143
+ port: serverPort,
144
+ cors: true,
145
+ headers: {
146
+ 'Access-Control-Allow-Origin': '*',
147
+ },
148
+ },
149
+ };
150
+ },
151
+ };
152
+ }
@@ -0,0 +1,40 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface CreopseVuePluginOptions {
3
+ /**
4
+ * Path to the manifest.jsonc file, relative to the project root.
5
+ * @default './manifest.jsonc'
6
+ */
7
+ manifestPath?: string;
8
+ /**
9
+ * Source directory, relative to the project root.
10
+ * @default './src'
11
+ */
12
+ srcDir?: string;
13
+ /**
14
+ * Output directory for the build.
15
+ * @default 'frontend'
16
+ */
17
+ outDir?: string;
18
+ /**
19
+ * Path to the i18n locales directory, relative to the project root.
20
+ * @default './src/i18n/locales'
21
+ */
22
+ i18nLocalesPath?: string;
23
+ /**
24
+ * Directories to scan for auto-imports, relative to the project root.
25
+ * @default ['src/composables']
26
+ */
27
+ composablesDirs?: string[];
28
+ /**
29
+ * Shared modules for Module Federation.
30
+ * The 'vue' and 'pinia' are always included.
31
+ * @default []
32
+ */
33
+ shared?: string[];
34
+ /**
35
+ * Whether to enable UnoCSS.
36
+ * @default false
37
+ */
38
+ unocss?: boolean;
39
+ }
40
+ export declare function CreopseVuePlugin(options?: CreopseVuePluginOptions): Plugin;
@@ -0,0 +1,143 @@
1
+ import federation from '@originjs/vite-plugin-federation';
2
+ import basicSsl from '@vitejs/plugin-basic-ssl';
3
+ import vue from '@vitejs/plugin-vue';
4
+ import { parse, stringify } from 'comment-json';
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import copy from 'rollup-plugin-copy';
8
+ import UnoCSS from 'unocss/vite';
9
+ import AutoImportPlugin from 'vite-plugin-auto-import-lite';
10
+ import i18nextLoader from 'vite-plugin-i18next-loader';
11
+ import json5Plugin from 'vite-plugin-json5';
12
+ import topLevelAwait from 'vite-plugin-top-level-await';
13
+ import { DASHBOARD_MODULE_NAME, SETTINGS_MODULE_NAME } from '../utils/constants';
14
+ import { sanitizeId } from '../utils/functions';
15
+ // ——— Plugin ——————————————————————————————————————————————————————————————————
16
+ export function CreopseVuePlugin(options = {}) {
17
+ const { manifestPath = './manifest.jsonc', srcDir = './src', outDir = 'frontend', i18nLocalesPath, composablesDirs = ['src/composables'], shared = [], unocss = false, } = options;
18
+ let root;
19
+ let manifest;
20
+ let pluginId;
21
+ let pluginEntry;
22
+ let exposes;
23
+ let previewPort;
24
+ let serverPort;
25
+ let sslEnabled;
26
+ function loadManifest() {
27
+ const fullPath = path.resolve(root, manifestPath);
28
+ const raw = fs.readFileSync(fullPath, 'utf-8');
29
+ manifest = parse(raw || '{}');
30
+ exposes = {};
31
+ if (manifest.pages && Array.isArray(manifest.pages)) {
32
+ for (const page of manifest.pages) {
33
+ exposes[`./${page.name}`] = page.module;
34
+ }
35
+ }
36
+ const dashboard = manifest.dashboard;
37
+ if (dashboard?.module) {
38
+ exposes[`./${DASHBOARD_MODULE_NAME}`] = dashboard.module;
39
+ }
40
+ const settings = manifest.settings;
41
+ if (settings?.module) {
42
+ exposes[`./${SETTINGS_MODULE_NAME}`] = settings.module;
43
+ }
44
+ const dev = manifest.development;
45
+ pluginId = sanitizeId(String(manifest.id)) || 'creopse-plugin';
46
+ pluginEntry = (manifest.entry || 'remoteEntry.js');
47
+ previewPort = (dev?.previewPort || 2160);
48
+ serverPort = (dev?.serverPort || 2161);
49
+ sslEnabled = (dev?.ssl || false);
50
+ }
51
+ return {
52
+ name: 'creopse-vue-plugin',
53
+ config(config) {
54
+ root = config.root || process.cwd();
55
+ loadManifest();
56
+ const isPreview = process.env.APP_IS_PREVIEW === 'true';
57
+ const localesPath = i18nLocalesPath
58
+ ? path.resolve(root, i18nLocalesPath)
59
+ : path.resolve(root, srcDir, 'i18n/locales');
60
+ const sharedModules = ['vue', 'pinia', ...shared];
61
+ return {
62
+ envPrefix: 'APP_',
63
+ plugins: [
64
+ vue(),
65
+ federation({
66
+ name: pluginId,
67
+ filename: pluginEntry,
68
+ exposes,
69
+ shared: sharedModules,
70
+ }),
71
+ topLevelAwait({
72
+ promiseExportName: '__tla',
73
+ promiseImportName: (i) => `__tla_${i}`,
74
+ }),
75
+ json5Plugin(),
76
+ i18nextLoader({
77
+ paths: [localesPath],
78
+ include: ['**/*.json'],
79
+ }),
80
+ AutoImportPlugin({
81
+ dirs: composablesDirs,
82
+ imports: ['vue', 'pinia'],
83
+ vueTemplate: true,
84
+ }),
85
+ ...(unocss ? [UnoCSS()] : []),
86
+ ...(sslEnabled ? [basicSsl()] : []),
87
+ ],
88
+ resolve: {
89
+ alias: [
90
+ {
91
+ find: '@',
92
+ replacement: path.resolve(root, srcDir),
93
+ },
94
+ ],
95
+ dedupe: ['vue'],
96
+ },
97
+ build: {
98
+ target: 'esnext',
99
+ outDir,
100
+ rollupOptions: {
101
+ plugins: [
102
+ copy({
103
+ targets: [
104
+ {
105
+ src: path.resolve(root, manifestPath),
106
+ dest: outDir,
107
+ transform() {
108
+ if (manifest?.mode) {
109
+ manifest.mode = isPreview ? 'development' : 'production';
110
+ }
111
+ const addBlankLineAfterCommas = (jsonc) => jsonc.replace(/(,)\s*\n/g, ',\n\n');
112
+ return addBlankLineAfterCommas(stringify(manifest, null, 2));
113
+ },
114
+ },
115
+ ],
116
+ hook: 'writeBundle',
117
+ verbose: false,
118
+ flatten: true,
119
+ }),
120
+ ],
121
+ },
122
+ assetsInlineLimit: Infinity,
123
+ cssCodeSplit: false,
124
+ },
125
+ preview: {
126
+ port: previewPort,
127
+ strictPort: true,
128
+ cors: true,
129
+ headers: {
130
+ 'Access-Control-Allow-Origin': '*',
131
+ },
132
+ },
133
+ server: {
134
+ port: serverPort,
135
+ cors: true,
136
+ headers: {
137
+ 'Access-Control-Allow-Origin': '*',
138
+ },
139
+ },
140
+ };
141
+ },
142
+ };
143
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@creopse/bridge",
3
+ "description": "Creopse Bridge Toolkit",
4
+ "version": "0.0.1",
5
+ "private": false,
6
+ "author": "Noé Gnanih <noegnanih@gmail.com>",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "main": "dist/index.js",
10
+ "exports": {
11
+ "./vite": "./dist/vite/index.js",
12
+ "./vite/vue": "./dist/vite/vue.js",
13
+ "./vite/react": "./dist/vite/react.js",
14
+ "./vue": "./dist/vue/index.js",
15
+ "./react": "./dist/react/index.js",
16
+ "./shared": "./dist/shared/index.js"
17
+ },
18
+ "types": "dist/index.d.ts",
19
+ "files": [
20
+ "dist",
21
+ "README.md"
22
+ ],
23
+ "devDependencies": {
24
+ "@originjs/vite-plugin-federation": "^1.4.1",
25
+ "@vitejs/plugin-basic-ssl": "^2.3.0",
26
+ "@vitejs/plugin-react": "^4.6.0",
27
+ "@vitejs/plugin-vue": "^6.0.0",
28
+ "comment-json": "^4.6.2",
29
+ "rollup-plugin-copy": "^3.5.0",
30
+ "tsc-alias": "^1.8.16",
31
+ "typescript": "~5.8.3",
32
+ "unocss": "^66.6.7",
33
+ "vite": "^7.0.5",
34
+ "vite-plugin-auto-import-lite": "^0.0.2",
35
+ "vite-plugin-i18next-loader": "^3.1.3",
36
+ "vite-plugin-json5": "^1.2.0",
37
+ "vite-plugin-top-level-await": "^1.6.0",
38
+ "@creopse/utils": "0.0.16"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/creopse/toolkit.git"
43
+ },
44
+ "keywords": [],
45
+ "bugs": {
46
+ "url": "https://github.com/creopse/toolkit/issues"
47
+ },
48
+ "homepage": "https://github.com/creopse/toolkit#readme",
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "build": "tsc -b && tsc-alias"
54
+ }
55
+ }