@medusajs/admin-bundler 0.0.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/README.md ADDED
@@ -0,0 +1 @@
1
+ # cli
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ function start() {
3
+ return import("../dist/cli/index.mjs");
4
+ }
5
+
6
+ start();
@@ -0,0 +1,257 @@
1
+ // src/api/build.ts
2
+ import { resolve } from "path";
3
+ import { build as command } from "vite";
4
+
5
+ // src/api/create-vite-config.ts
6
+ import inject from "@medusajs/vite-plugin-extension";
7
+ import react from "@vitejs/plugin-react";
8
+ import deepmerge from "deepmerge";
9
+ import { createRequire } from "module";
10
+ import path from "path";
11
+ import { createLogger, mergeConfig } from "vite";
12
+ var require2 = createRequire(import.meta.url);
13
+ async function createViteConfig(inline) {
14
+ const root = process.cwd();
15
+ const logger = createCustomLogger();
16
+ let dashboardRoot = null;
17
+ try {
18
+ dashboardRoot = path.dirname(require2.resolve("@medusajs/dashboard"));
19
+ } catch (err) {
20
+ dashboardRoot = null;
21
+ }
22
+ if (!dashboardRoot) {
23
+ logger.error(
24
+ "Unable to find @medusajs/dashboard. Please install it in your project, or specify the root directory."
25
+ );
26
+ return null;
27
+ }
28
+ const { plugins, userConfig } = await loadConfig(root, logger) ?? {};
29
+ let viteConfig = mergeConfig(inline, {
30
+ plugins: [
31
+ react(),
32
+ inject({
33
+ sources: plugins
34
+ })
35
+ ],
36
+ configFile: false,
37
+ root: dashboardRoot,
38
+ css: {
39
+ postcss: {
40
+ plugins: [
41
+ require2("tailwindcss")({
42
+ config: createTwConfig(process.cwd(), dashboardRoot)
43
+ }),
44
+ require2("autoprefixer")
45
+ ]
46
+ }
47
+ }
48
+ });
49
+ if (userConfig) {
50
+ viteConfig = await userConfig(viteConfig);
51
+ }
52
+ return viteConfig;
53
+ }
54
+ function mergeTailwindConfigs(config1, config2) {
55
+ const content1 = config1.content;
56
+ const content2 = config2.content;
57
+ let mergedContent;
58
+ if (Array.isArray(content1) && Array.isArray(content2)) {
59
+ mergedContent = [...content1, ...content2];
60
+ } else if (!Array.isArray(content1) && !Array.isArray(content2)) {
61
+ mergedContent = {
62
+ files: [...content1.files, ...content2.files],
63
+ relative: content1.relative || content2.relative,
64
+ extract: { ...content1.extract, ...content2.extract },
65
+ transform: { ...content1.transform, ...content2.transform }
66
+ };
67
+ } else {
68
+ throw new Error("Cannot merge content fields of different types");
69
+ }
70
+ const mergedConfig = deepmerge(config1, config2);
71
+ mergedConfig.content = mergedContent;
72
+ console.log(config1.presets, config2.presets);
73
+ mergedConfig.presets = config1.presets || [];
74
+ return mergedConfig;
75
+ }
76
+ function createTwConfig(root, dashboardRoot) {
77
+ const uiRoot = path.join(
78
+ path.dirname(require2.resolve("@medusajs/ui")),
79
+ "**/*.{js,jsx,ts,tsx}"
80
+ );
81
+ const baseConfig = {
82
+ presets: [require2("@medusajs/ui-preset")],
83
+ content: [
84
+ `${root}/src/admin/**/*.{js,jsx,ts,tsx}`,
85
+ `${dashboardRoot}/src/**/*.{js,jsx,ts,tsx}`,
86
+ uiRoot
87
+ ],
88
+ darkMode: "class",
89
+ theme: {
90
+ extend: {}
91
+ },
92
+ plugins: []
93
+ };
94
+ let userConfig = null;
95
+ const extensions = ["js", "cjs", "mjs", "ts", "cts", "mts"];
96
+ for (const ext of extensions) {
97
+ try {
98
+ userConfig = require2(path.join(root, `tailwind.config.${ext}`));
99
+ break;
100
+ } catch (err) {
101
+ console.log("Failed to load tailwind config with extension", ext, err);
102
+ userConfig = null;
103
+ }
104
+ }
105
+ if (!userConfig) {
106
+ return baseConfig;
107
+ }
108
+ return mergeTailwindConfigs(baseConfig, userConfig);
109
+ }
110
+ function createCustomLogger() {
111
+ const logger = createLogger("info", {
112
+ prefix: "medusa-admin"
113
+ });
114
+ const loggerInfo = logger.info;
115
+ logger.info = (msg, opts) => {
116
+ if (msg.includes("hmr invalidate") && msg.includes(
117
+ "Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports"
118
+ )) {
119
+ return;
120
+ }
121
+ loggerInfo(msg, opts);
122
+ };
123
+ return logger;
124
+ }
125
+ async function loadConfig(root, logger) {
126
+ const configPath = path.resolve(root, "medusa-config.js");
127
+ const config = await import(configPath).then((c) => c).catch((e) => {
128
+ if (e.code === "ERR_MODULE_NOT_FOUND") {
129
+ logger.warn(
130
+ "Root 'medusa-config.js' file not found; extensions won't load. If running Admin UI as a standalone app, use the 'standalone' option.",
131
+ {
132
+ timestamp: true
133
+ }
134
+ );
135
+ } else {
136
+ logger.error(
137
+ `An error occured while attempting to load '${configPath}':
138
+ ${e}`,
139
+ {
140
+ timestamp: true
141
+ }
142
+ );
143
+ }
144
+ return null;
145
+ });
146
+ if (!config) {
147
+ return;
148
+ }
149
+ if (!config.plugins?.length) {
150
+ logger.info(
151
+ "No plugins in 'medusa-config.js', no extensions will load. To enable Admin UI extensions, add them to the 'plugins' array in 'medusa-config.js'.",
152
+ {
153
+ timestamp: true
154
+ }
155
+ );
156
+ return;
157
+ }
158
+ const uiPlugins = config.plugins.filter((p) => typeof p !== "string" && p.options?.enableUI).map((p) => {
159
+ return typeof p === "string" ? p : p.resolve;
160
+ });
161
+ const extensionSources = uiPlugins.map((p) => {
162
+ return path.resolve(require2.resolve(p), "dist", "admin");
163
+ });
164
+ const rootSource = path.resolve(process.cwd(), "src", "admin");
165
+ extensionSources.push(rootSource);
166
+ const adminPlugin = config.plugins.find(
167
+ (p) => typeof p === "string" ? p === "@medusajs/admin" : p.resolve === "@medusajs/admin"
168
+ );
169
+ if (!adminPlugin) {
170
+ logger.info(
171
+ "No @medusajs/admin in 'medusa-config.js', no extensions will load. To enable Admin UI extensions, add it to the 'plugins' array in 'medusa-config.js'.",
172
+ {
173
+ timestamp: true
174
+ }
175
+ );
176
+ return;
177
+ }
178
+ const adminPluginOptions = typeof adminPlugin !== "string" && !!adminPlugin.options ? adminPlugin.options : {};
179
+ const viteConfig = adminPluginOptions.withFinal;
180
+ return {
181
+ plugins: extensionSources,
182
+ userConfig: viteConfig
183
+ };
184
+ }
185
+
186
+ // src/api/build.ts
187
+ async function build({ root }) {
188
+ const config = await createViteConfig({
189
+ build: {
190
+ outDir: resolve(process.cwd(), "build")
191
+ }
192
+ });
193
+ if (!config) {
194
+ return;
195
+ }
196
+ await command(config);
197
+ }
198
+
199
+ // src/api/bundle.ts
200
+ import { readFileSync } from "fs";
201
+ import glob from "glob";
202
+ import { relative, resolve as resolve2 } from "path";
203
+ import { build as command2 } from "vite";
204
+ async function bundle({ watch, root }) {
205
+ const resolvedRoot = root ? resolve2(process.cwd(), root) : resolve2(process.cwd(), "src", "admin");
206
+ const files = glob.sync(`${resolvedRoot}/**/*.{ts,tsx,js,jsx}`);
207
+ const input = {};
208
+ for (const file of files) {
209
+ const relativePath = relative(resolvedRoot, file);
210
+ input[relativePath] = file;
211
+ }
212
+ const packageJson = JSON.parse(
213
+ readFileSync(resolve2(process.cwd(), "package.json"), "utf-8")
214
+ );
215
+ const external = [
216
+ ...Object.keys(packageJson.dependencies),
217
+ "@medusajs/ui",
218
+ "@medusajs/ui-preset",
219
+ "react",
220
+ "react-dom",
221
+ "react-router-dom",
222
+ "react-hook-form"
223
+ ];
224
+ await command2({
225
+ build: {
226
+ watch: watch ? {} : void 0,
227
+ rollupOptions: {
228
+ input,
229
+ external
230
+ }
231
+ }
232
+ });
233
+ }
234
+
235
+ // src/api/dev.ts
236
+ import { createServer } from "vite";
237
+ async function dev({ port = 5173, host }) {
238
+ const config = await createViteConfig({
239
+ server: {
240
+ port,
241
+ host
242
+ }
243
+ });
244
+ if (!config) {
245
+ return;
246
+ }
247
+ const server = await createServer(config);
248
+ await server.listen();
249
+ server.printUrls();
250
+ server.bindCLIShortcuts({ print: true });
251
+ }
252
+
253
+ export {
254
+ build,
255
+ bundle,
256
+ dev
257
+ };
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,22 @@
1
+ import {
2
+ build,
3
+ bundle,
4
+ dev
5
+ } from "../chunk-UG4OGQKM.mjs";
6
+
7
+ // src/cli/create-cli.ts
8
+ import { Command } from "commander";
9
+ async function createCli() {
10
+ const program = new Command();
11
+ program.name("medusa-admin");
12
+ program.command("dev").description("Starts the development server").action(dev);
13
+ program.command("build").description("Builds the admin dashboard").action(build);
14
+ program.command("bundle").description("Bundles the admin dashboard").action(bundle);
15
+ return program;
16
+ }
17
+
18
+ // src/cli/index.ts
19
+ createCli().then(async (cli) => cli.parseAsync(process.argv)).catch((err) => {
20
+ console.error(err);
21
+ process.exit(1);
22
+ });
@@ -0,0 +1,18 @@
1
+ type BuildArgs = {
2
+ root?: string;
3
+ };
4
+ declare function build({ root }: BuildArgs): Promise<void>;
5
+
6
+ type BundleArgs = {
7
+ root?: string | undefined;
8
+ watch?: boolean | undefined;
9
+ };
10
+ declare function bundle({ watch, root }: BundleArgs): Promise<void>;
11
+
12
+ type DevArgs = {
13
+ port?: number | undefined;
14
+ host?: string | boolean | undefined;
15
+ };
16
+ declare function dev({ port, host }: DevArgs): Promise<void>;
17
+
18
+ export { build, bundle, dev };
package/dist/index.mjs ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ build,
3
+ bundle,
4
+ dev
5
+ } from "./chunk-UG4OGQKM.mjs";
6
+ export {
7
+ build,
8
+ bundle,
9
+ dev
10
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@medusajs/admin-bundler",
3
+ "version": "0.0.0",
4
+ "scripts": {
5
+ "build": "rimraf dist && tsup"
6
+ },
7
+ "bin": {
8
+ "medusa-admin": "./bin/medusa-admin.js"
9
+ },
10
+ "main": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "module": "dist/index.mjs",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "devDependencies": {
17
+ "rimraf": "5.0.1",
18
+ "tsup": "^8.0.1",
19
+ "typescript": "^5.3.3"
20
+ },
21
+ "dependencies": {
22
+ "@medusajs/ui-preset": "1.0.3-next-20240108145510",
23
+ "@medusajs/vite-plugin-extension": "*",
24
+ "@vitejs/plugin-react": "^4.2.1",
25
+ "autoprefixer": "^10.4.16",
26
+ "commander": "^11.1.0",
27
+ "deepmerge": "^4.3.1",
28
+ "glob": "^7.1.6",
29
+ "postcss": "^8.4.32",
30
+ "tailwindcss": "^3.3.6",
31
+ "vite": "5.0.10"
32
+ },
33
+ "packageManager": "yarn@3.2.1"
34
+ }