@cedarjs/vite 1.1.1 → 1.1.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.
package/dist/cjs/index.js CHANGED
@@ -29,9 +29,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var index_exports = {};
30
30
  __export(index_exports, {
31
31
  cedar: () => cedar,
32
+ cedarAutoImportsPlugin: () => import_vite_plugin_cedar_auto_import.cedarAutoImportsPlugin,
32
33
  cedarCellTransform: () => import_vite_plugin_cedar_cell2.cedarCellTransform,
33
34
  cedarEntryInjectionPlugin: () => import_vite_plugin_cedar_entry_injection2.cedarEntryInjectionPlugin,
34
35
  cedarHtmlEnvPlugin: () => import_vite_plugin_cedar_html_env2.cedarHtmlEnvPlugin,
36
+ cedarImportDirPlugin: () => import_vite_plugin_cedar_import_dir.cedarImportDirPlugin,
35
37
  cedarMergedConfig: () => import_vite_plugin_merged_config2.cedarMergedConfig,
36
38
  cedarNodePolyfills: () => import_vite_plugin_cedar_node_polyfills2.cedarNodePolyfills,
37
39
  cedarRemoveFromBundle: () => import_vite_plugin_cedar_remove_from_bundle2.cedarRemoveFromBundle,
@@ -54,9 +56,11 @@ var import_vite_plugin_cedar_remove_from_bundle = require("./plugins/vite-plugin
54
56
  var import_vite_plugin_jsx_loader = require("./plugins/vite-plugin-jsx-loader.js");
55
57
  var import_vite_plugin_merged_config = require("./plugins/vite-plugin-merged-config.js");
56
58
  var import_vite_plugin_swap_apollo_provider = require("./plugins/vite-plugin-swap-apollo-provider.js");
59
+ var import_vite_plugin_cedar_auto_import = require("./plugins/vite-plugin-cedar-auto-import.js");
57
60
  var import_vite_plugin_cedar_cell2 = require("./plugins/vite-plugin-cedar-cell.js");
58
61
  var import_vite_plugin_cedar_entry_injection2 = require("./plugins/vite-plugin-cedar-entry-injection.js");
59
62
  var import_vite_plugin_cedar_html_env2 = require("./plugins/vite-plugin-cedar-html-env.js");
63
+ var import_vite_plugin_cedar_import_dir = require("./plugins/vite-plugin-cedar-import-dir.js");
60
64
  var import_vite_plugin_cedar_node_polyfills2 = require("./plugins/vite-plugin-cedar-node-polyfills.js");
61
65
  var import_vite_plugin_cedar_remove_from_bundle2 = require("./plugins/vite-plugin-cedar-remove-from-bundle.js");
62
66
  var import_vite_plugin_cedarjs_directory_named_import = require("./plugins/vite-plugin-cedarjs-directory-named-import.js");
@@ -97,9 +101,11 @@ var index_default = cedar;
97
101
  // Annotate the CommonJS export names for ESM import in node:
98
102
  0 && (module.exports = {
99
103
  cedar,
104
+ cedarAutoImportsPlugin,
100
105
  cedarCellTransform,
101
106
  cedarEntryInjectionPlugin,
102
107
  cedarHtmlEnvPlugin,
108
+ cedarImportDirPlugin,
103
109
  cedarMergedConfig,
104
110
  cedarNodePolyfills,
105
111
  cedarRemoveFromBundle,
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var vite_plugin_cedar_auto_import_exports = {};
30
+ __export(vite_plugin_cedar_auto_import_exports, {
31
+ cedarAutoImportsPlugin: () => cedarAutoImportsPlugin
32
+ });
33
+ module.exports = __toCommonJS(vite_plugin_cedar_auto_import_exports);
34
+ var import_node_path = __toESM(require("node:path"), 1);
35
+ var import_unplugin = __toESM(require("unimport/unplugin"), 1);
36
+ var import_vite = __toESM(require("unplugin-auto-import/vite"), 1);
37
+ var import_project_config = require("@cedarjs/project-config");
38
+ function cedarAutoImportsPlugin() {
39
+ const config = (0, import_project_config.getConfig)();
40
+ const cedarPaths = (0, import_project_config.getPaths)();
41
+ const useTrustedDocumentsGqlTag = config?.graphql?.trustedDocuments;
42
+ return [
43
+ (0, import_vite.default)({
44
+ // targets to transform
45
+ include: [
46
+ /web\/.*\.[tj]sx?$/,
47
+ // Assume we want the trusted documents gql tag import in all .jsx and
48
+ // .tsx files
49
+ /\.[tj]sx$/
50
+ ],
51
+ // global imports to register
52
+ imports: [
53
+ // import gql from 'graphql-tag'
54
+ !useTrustedDocumentsGqlTag && {
55
+ "graphql-tag": ["gql"]
56
+ },
57
+ // import { gql } from 'src/graphql/gql'
58
+ useTrustedDocumentsGqlTag && {
59
+ [(0, import_project_config.importStatementPath)(
60
+ import_node_path.default.join(cedarPaths.web.base, "src", "graphql", "gql")
61
+ )]: ["gql"]
62
+ }
63
+ ].filter((v) => Boolean(v)),
64
+ // Types aren't important since the output is ephemeral
65
+ dts: false
66
+ }),
67
+ (0, import_vite.default)({
68
+ // targets to transform
69
+ include: [/\.[tj]sx?$/],
70
+ // global imports to register
71
+ imports: [
72
+ {
73
+ "@cedarjs/context": ["context"]
74
+ }
75
+ ],
76
+ // Types aren't important since the output is ephemeral
77
+ dts: false
78
+ }),
79
+ (0, import_vite.default)({
80
+ // targets to transform
81
+ include: [/api\/.*\.[tj]sx?$/],
82
+ // global imports to register
83
+ imports: [
84
+ // import gql from 'graphql-tag'
85
+ {
86
+ "graphql-tag": ["gql"]
87
+ }
88
+ ],
89
+ // Types aren't important since the output is ephemeral
90
+ dts: false
91
+ }),
92
+ import_unplugin.default.vite({
93
+ imports: [
94
+ // import React from 'react'
95
+ {
96
+ name: "default",
97
+ as: "React",
98
+ from: "react"
99
+ }
100
+ ]
101
+ })
102
+ ];
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ cedarAutoImportsPlugin
107
+ });
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var vite_plugin_cedar_import_dir_exports = {};
30
+ __export(vite_plugin_cedar_import_dir_exports, {
31
+ cedarImportDirPlugin: () => cedarImportDirPlugin
32
+ });
33
+ module.exports = __toCommonJS(vite_plugin_cedar_import_dir_exports);
34
+ var import_node_path = __toESM(require("node:path"), 1);
35
+ var import_napi = require("@ast-grep/napi");
36
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
37
+ var import_project_config = require("@cedarjs/project-config");
38
+ function cedarImportDirPlugin() {
39
+ return {
40
+ name: "vite-plugin-cedar-import-dir",
41
+ enforce: "pre",
42
+ async transform(code, id) {
43
+ if (!code.includes("/**/")) {
44
+ return null;
45
+ }
46
+ const ext = import_node_path.default.extname(id);
47
+ const language = ext === ".ts" || ext === ".tsx" ? import_napi.Lang.TypeScript : import_napi.Lang.JavaScript;
48
+ let ast;
49
+ try {
50
+ ast = (0, import_napi.parse)(language, code);
51
+ } catch (error) {
52
+ console.warn("Failed to parse file:", id);
53
+ console.warn(error);
54
+ return null;
55
+ }
56
+ const root = ast.root();
57
+ let hasTransformations = false;
58
+ const edits = [];
59
+ const globImports = root.findAll({
60
+ rule: {
61
+ pattern: "import $DEFAULT_IMPORT from $SOURCE"
62
+ }
63
+ });
64
+ for (const importNode of globImports) {
65
+ const sourceNode = importNode.getMatch("SOURCE");
66
+ const defaultImportNode = importNode.getMatch("DEFAULT_IMPORT");
67
+ if (!sourceNode || !defaultImportNode) {
68
+ continue;
69
+ }
70
+ const sourceValue = sourceNode.text().slice(1, -1);
71
+ if (!sourceValue.includes("/**/")) {
72
+ continue;
73
+ }
74
+ hasTransformations = true;
75
+ const importName = defaultImportNode.text();
76
+ const importGlob = (0, import_project_config.importStatementPath)(sourceValue);
77
+ const cwd = importGlob.startsWith("src/") ? (0, import_project_config.getPaths)().api.base : import_node_path.default.dirname(id);
78
+ try {
79
+ const dirFiles = import_fast_glob.default.sync(importGlob, { cwd }).filter(
80
+ (n) => !n.includes(".test.") && !n.includes(".scenarios.") && !n.includes(".d.ts")
81
+ );
82
+ const staticGlob = importGlob.split("*")[0];
83
+ const filePathToVarName = (filePath) => {
84
+ return filePath.replace(staticGlob, "").replace(/\.(js|ts)$/, "").replace(/[^a-zA-Z0-9]/g, "_");
85
+ };
86
+ let replacement = `let ${importName} = {};
87
+ `;
88
+ for (const filePath of dirFiles) {
89
+ const { dir: fileDir, name: fileName } = import_node_path.default.parse(filePath);
90
+ const fileImportPath = fileDir + "/" + fileName;
91
+ const filePathVarName = filePathToVarName(filePath);
92
+ const namespaceImportName = `${importName}_${filePathVarName}`;
93
+ replacement += `import * as ${namespaceImportName} from '${fileImportPath}';
94
+ `;
95
+ replacement += `${importName}.${filePathVarName} = ${namespaceImportName};
96
+ `;
97
+ }
98
+ edits.push(importNode.replace(replacement.trim()));
99
+ } catch (error) {
100
+ console.warn(`Failed to process glob import: ${sourceValue}`, error);
101
+ }
102
+ }
103
+ if (hasTransformations && edits.length > 0) {
104
+ const transformedCode = root.commitEdits(edits);
105
+ return {
106
+ code: transformedCode,
107
+ map: null
108
+ // For simplicity, not generating source maps
109
+ };
110
+ }
111
+ return null;
112
+ }
113
+ };
114
+ }
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ cedarImportDirPlugin
118
+ });
@@ -197,7 +197,6 @@ async function parseExportNamesIntoNames(code, body, names) {
197
197
  async function transformClientModule(code, body, url, clientEntryFiles) {
198
198
  const names = [];
199
199
  await parseExportNamesIntoNames(code, body, names);
200
- console.log("transformClientModule names", names);
201
200
  const entryRecord = Object.entries(clientEntryFiles).find(
202
201
  ([_key, value]) => value === url
203
202
  );
File without changes
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import type { PluginOption } from 'vite';
2
+ export { cedarAutoImportsPlugin } from './plugins/vite-plugin-cedar-auto-import.js';
2
3
  export { cedarCellTransform } from './plugins/vite-plugin-cedar-cell.js';
3
4
  export { cedarEntryInjectionPlugin } from './plugins/vite-plugin-cedar-entry-injection.js';
4
5
  export { cedarHtmlEnvPlugin } from './plugins/vite-plugin-cedar-html-env.js';
6
+ export { cedarImportDirPlugin } from './plugins/vite-plugin-cedar-import-dir.js';
5
7
  export { cedarNodePolyfills } from './plugins/vite-plugin-cedar-node-polyfills.js';
6
8
  export { cedarRemoveFromBundle } from './plugins/vite-plugin-cedar-remove-from-bundle.js';
7
9
  export { cedarjsDirectoryNamedImportPlugin } from './plugins/vite-plugin-cedarjs-directory-named-import.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAmBxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,kBAAkB,EAAE,MAAM,+CAA+C,CAAA;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,iCAAiC,EAAE,MAAM,yDAAyD,CAAA;AAC3G,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AAEvF,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAqClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAmBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,+CAA+C,CAAA;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,iCAAiC,EAAE,MAAM,yDAAyD,CAAA;AAC3G,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AAEvF,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAqClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
package/dist/index.js CHANGED
@@ -14,9 +14,11 @@ import { cedarRemoveFromBundle } from "./plugins/vite-plugin-cedar-remove-from-b
14
14
  import { cedarTransformJsAsJsx } from "./plugins/vite-plugin-jsx-loader.js";
15
15
  import { cedarMergedConfig } from "./plugins/vite-plugin-merged-config.js";
16
16
  import { cedarSwapApolloProvider } from "./plugins/vite-plugin-swap-apollo-provider.js";
17
+ import { cedarAutoImportsPlugin } from "./plugins/vite-plugin-cedar-auto-import.js";
17
18
  import { cedarCellTransform as cedarCellTransform2 } from "./plugins/vite-plugin-cedar-cell.js";
18
19
  import { cedarEntryInjectionPlugin as cedarEntryInjectionPlugin2 } from "./plugins/vite-plugin-cedar-entry-injection.js";
19
20
  import { cedarHtmlEnvPlugin as cedarHtmlEnvPlugin2 } from "./plugins/vite-plugin-cedar-html-env.js";
21
+ import { cedarImportDirPlugin } from "./plugins/vite-plugin-cedar-import-dir.js";
20
22
  import { cedarNodePolyfills as cedarNodePolyfills2 } from "./plugins/vite-plugin-cedar-node-polyfills.js";
21
23
  import { cedarRemoveFromBundle as cedarRemoveFromBundle2 } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
22
24
  import { cedarjsDirectoryNamedImportPlugin } from "./plugins/vite-plugin-cedarjs-directory-named-import.js";
@@ -56,9 +58,11 @@ function cedar({ mode } = {}) {
56
58
  var index_default = cedar;
57
59
  export {
58
60
  cedar,
61
+ cedarAutoImportsPlugin,
59
62
  cedarCellTransform2 as cedarCellTransform,
60
63
  cedarEntryInjectionPlugin2 as cedarEntryInjectionPlugin,
61
64
  cedarHtmlEnvPlugin2 as cedarHtmlEnvPlugin,
65
+ cedarImportDirPlugin,
62
66
  cedarMergedConfig2 as cedarMergedConfig,
63
67
  cedarNodePolyfills2 as cedarNodePolyfills,
64
68
  cedarRemoveFromBundle2 as cedarRemoveFromBundle,
@@ -0,0 +1,2 @@
1
+ export declare function cedarAutoImportsPlugin(): (import("vite").Plugin<any> | import("vite").Plugin<any>[])[];
2
+ //# sourceMappingURL=vite-plugin-cedar-auto-import.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-cedar-auto-import.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-auto-import.ts"],"names":[],"mappings":"AAWA,wBAAgB,sBAAsB,kEA2ErC"}
@@ -0,0 +1,77 @@
1
+ import path from "node:path";
2
+ import unimportPlugin from "unimport/unplugin";
3
+ import autoImport from "unplugin-auto-import/vite";
4
+ import {
5
+ getConfig,
6
+ getPaths,
7
+ importStatementPath
8
+ } from "@cedarjs/project-config";
9
+ function cedarAutoImportsPlugin() {
10
+ const config = getConfig();
11
+ const cedarPaths = getPaths();
12
+ const useTrustedDocumentsGqlTag = config?.graphql?.trustedDocuments;
13
+ return [
14
+ autoImport({
15
+ // targets to transform
16
+ include: [
17
+ /web\/.*\.[tj]sx?$/,
18
+ // Assume we want the trusted documents gql tag import in all .jsx and
19
+ // .tsx files
20
+ /\.[tj]sx$/
21
+ ],
22
+ // global imports to register
23
+ imports: [
24
+ // import gql from 'graphql-tag'
25
+ !useTrustedDocumentsGqlTag && {
26
+ "graphql-tag": ["gql"]
27
+ },
28
+ // import { gql } from 'src/graphql/gql'
29
+ useTrustedDocumentsGqlTag && {
30
+ [importStatementPath(
31
+ path.join(cedarPaths.web.base, "src", "graphql", "gql")
32
+ )]: ["gql"]
33
+ }
34
+ ].filter((v) => Boolean(v)),
35
+ // Types aren't important since the output is ephemeral
36
+ dts: false
37
+ }),
38
+ autoImport({
39
+ // targets to transform
40
+ include: [/\.[tj]sx?$/],
41
+ // global imports to register
42
+ imports: [
43
+ {
44
+ "@cedarjs/context": ["context"]
45
+ }
46
+ ],
47
+ // Types aren't important since the output is ephemeral
48
+ dts: false
49
+ }),
50
+ autoImport({
51
+ // targets to transform
52
+ include: [/api\/.*\.[tj]sx?$/],
53
+ // global imports to register
54
+ imports: [
55
+ // import gql from 'graphql-tag'
56
+ {
57
+ "graphql-tag": ["gql"]
58
+ }
59
+ ],
60
+ // Types aren't important since the output is ephemeral
61
+ dts: false
62
+ }),
63
+ unimportPlugin.vite({
64
+ imports: [
65
+ // import React from 'react'
66
+ {
67
+ name: "default",
68
+ as: "React",
69
+ from: "react"
70
+ }
71
+ ]
72
+ })
73
+ ];
74
+ }
75
+ export {
76
+ cedarAutoImportsPlugin
77
+ };
@@ -0,0 +1,19 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * This Vite plugin will search for import statements that include a glob double
4
+ * star `**` in the source part of the statement. The files that are matched are
5
+ * imported and appended to an object.
6
+ *
7
+ * @example
8
+ * Given a directory "src/services" that contains "a.js", "b.ts" and
9
+ * "nested/c.js" will produce the following results:
10
+ * ```js
11
+ * import services from 'src/services/**\/*.{js,ts}'
12
+ * console.log(services)
13
+ * // services.a = import('src/services/a')
14
+ * // services.b = import('src/services/b')
15
+ * // services.nested_c = import('src/services/nested/c')
16
+ * ```
17
+ */
18
+ export declare function cedarImportDirPlugin(): Plugin;
19
+ //# sourceMappingURL=vite-plugin-cedar-import-dir.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-cedar-import-dir.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-import-dir.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAgH7C"}
@@ -0,0 +1,84 @@
1
+ import path from "node:path";
2
+ import { parse, Lang } from "@ast-grep/napi";
3
+ import fg from "fast-glob";
4
+ import { importStatementPath, getPaths } from "@cedarjs/project-config";
5
+ function cedarImportDirPlugin() {
6
+ return {
7
+ name: "vite-plugin-cedar-import-dir",
8
+ enforce: "pre",
9
+ async transform(code, id) {
10
+ if (!code.includes("/**/")) {
11
+ return null;
12
+ }
13
+ const ext = path.extname(id);
14
+ const language = ext === ".ts" || ext === ".tsx" ? Lang.TypeScript : Lang.JavaScript;
15
+ let ast;
16
+ try {
17
+ ast = parse(language, code);
18
+ } catch (error) {
19
+ console.warn("Failed to parse file:", id);
20
+ console.warn(error);
21
+ return null;
22
+ }
23
+ const root = ast.root();
24
+ let hasTransformations = false;
25
+ const edits = [];
26
+ const globImports = root.findAll({
27
+ rule: {
28
+ pattern: "import $DEFAULT_IMPORT from $SOURCE"
29
+ }
30
+ });
31
+ for (const importNode of globImports) {
32
+ const sourceNode = importNode.getMatch("SOURCE");
33
+ const defaultImportNode = importNode.getMatch("DEFAULT_IMPORT");
34
+ if (!sourceNode || !defaultImportNode) {
35
+ continue;
36
+ }
37
+ const sourceValue = sourceNode.text().slice(1, -1);
38
+ if (!sourceValue.includes("/**/")) {
39
+ continue;
40
+ }
41
+ hasTransformations = true;
42
+ const importName = defaultImportNode.text();
43
+ const importGlob = importStatementPath(sourceValue);
44
+ const cwd = importGlob.startsWith("src/") ? getPaths().api.base : path.dirname(id);
45
+ try {
46
+ const dirFiles = fg.sync(importGlob, { cwd }).filter(
47
+ (n) => !n.includes(".test.") && !n.includes(".scenarios.") && !n.includes(".d.ts")
48
+ );
49
+ const staticGlob = importGlob.split("*")[0];
50
+ const filePathToVarName = (filePath) => {
51
+ return filePath.replace(staticGlob, "").replace(/\.(js|ts)$/, "").replace(/[^a-zA-Z0-9]/g, "_");
52
+ };
53
+ let replacement = `let ${importName} = {};
54
+ `;
55
+ for (const filePath of dirFiles) {
56
+ const { dir: fileDir, name: fileName } = path.parse(filePath);
57
+ const fileImportPath = fileDir + "/" + fileName;
58
+ const filePathVarName = filePathToVarName(filePath);
59
+ const namespaceImportName = `${importName}_${filePathVarName}`;
60
+ replacement += `import * as ${namespaceImportName} from '${fileImportPath}';
61
+ `;
62
+ replacement += `${importName}.${filePathVarName} = ${namespaceImportName};
63
+ `;
64
+ }
65
+ edits.push(importNode.replace(replacement.trim()));
66
+ } catch (error) {
67
+ console.warn(`Failed to process glob import: ${sourceValue}`, error);
68
+ }
69
+ }
70
+ if (hasTransformations && edits.length > 0) {
71
+ const transformedCode = root.commitEdits(edits);
72
+ return {
73
+ code: transformedCode,
74
+ map: null
75
+ // For simplicity, not generating source maps
76
+ };
77
+ }
78
+ return null;
79
+ }
80
+ };
81
+ }
82
+ export {
83
+ cedarImportDirPlugin
84
+ };
@@ -164,7 +164,6 @@ async function parseExportNamesIntoNames(code, body, names) {
164
164
  async function transformClientModule(code, body, url, clientEntryFiles) {
165
165
  const names = [];
166
166
  await parseExportNamesIntoNames(code, body, names);
167
- console.log("transformClientModule names", names);
168
167
  const entryRecord = Object.entries(clientEntryFiles).find(
169
168
  ([_key, value]) => value === url
170
169
  );
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/vite",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Vite configuration package for CedarJS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -64,14 +64,14 @@
64
64
  "@babel/generator": "7.27.5",
65
65
  "@babel/parser": "7.27.5",
66
66
  "@babel/traverse": "7.27.4",
67
- "@cedarjs/auth": "1.1.1",
68
- "@cedarjs/babel-config": "1.1.1",
69
- "@cedarjs/cookie-jar": "1.1.1",
70
- "@cedarjs/internal": "1.1.1",
71
- "@cedarjs/project-config": "1.1.1",
72
- "@cedarjs/server-store": "1.1.1",
73
- "@cedarjs/testing": "1.1.1",
74
- "@cedarjs/web": "1.1.1",
67
+ "@cedarjs/auth": "1.1.2",
68
+ "@cedarjs/babel-config": "1.1.2",
69
+ "@cedarjs/cookie-jar": "1.1.2",
70
+ "@cedarjs/internal": "1.1.2",
71
+ "@cedarjs/project-config": "1.1.2",
72
+ "@cedarjs/server-store": "1.1.2",
73
+ "@cedarjs/testing": "1.1.2",
74
+ "@cedarjs/web": "1.1.2",
75
75
  "@swc/core": "1.13.5",
76
76
  "@vitejs/plugin-react": "4.3.4",
77
77
  "@whatwg-node/fetch": "0.9.21",
@@ -118,5 +118,5 @@
118
118
  "publishConfig": {
119
119
  "access": "public"
120
120
  },
121
- "gitHead": "0c21e9ed1de0c7302295f3ca284924e48dd81afe"
121
+ "gitHead": "2e526cd5ee2f4b5850f79cc841bf5b5e1dfdea37"
122
122
  }