@jitar/plugin-vite 0.10.6 → 0.11.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/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { PluginOption } from 'vite';
2
2
  type PluginConfig = {
3
- sourceDir: string;
4
- targetDir: string;
5
- jitarDir: string;
3
+ projectRoot: string;
4
+ sourceRoot: string;
5
+ configurationFile?: string;
6
+ environmentFile?: string;
6
7
  jitarUrl: string;
7
8
  segments?: string[];
8
9
  middleware?: string[];
package/dist/index.js CHANGED
@@ -1,38 +1,44 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { ConfigurationManager, BuildHelper } from 'jitar';
3
4
  import { normalizePath } from 'vite';
4
5
  const JITAR_SOURCE_ID = 'jitar';
5
6
  const JITAR_CLIENT_ID = 'jitar/client';
6
7
  const JITAR_BUNDLE_ID = 'jitar-bundle';
7
- function formatDir(dir) {
8
- dir = normalizePath(dir);
9
- if (dir.startsWith('/')) {
10
- dir = dir.substring(1);
11
- }
12
- if (dir.endsWith('/')) {
13
- dir = dir.substring(0, dir.length - 1);
14
- }
15
- return dir;
16
- }
17
8
  function assureExtension(filename) {
18
9
  if (filename.endsWith('.js')) {
19
10
  return filename;
20
11
  }
21
12
  return `${filename}.js`;
22
13
  }
14
+ function createJitarBundle(middlewares, targetPath) {
15
+ const middlewareFiles = middlewares.map(name => assureExtension(`${targetPath}/${name}`));
16
+ const jitarImport = `import { ClientBuilder, HttpRemoteBuilder } from "${JITAR_CLIENT_ID}";`;
17
+ const middlewareImports = middlewareFiles.map((filename, index) => `import { default as $M${index} } from "${filename}";`).join('');
18
+ const imports = [jitarImport, middlewareImports].join('\n');
19
+ const remoteUrl = 'const remoteUrl = document.location.origin;';
20
+ const segmentsArray = `const segments = [];`;
21
+ const middlewareItems = middlewares.map((_, index) => `$M${index}`).join(', ');
22
+ const middlewareArray = `const middleware = [${middlewareItems}];`;
23
+ const declarations = [remoteUrl, segmentsArray, middlewareArray].join('\n');
24
+ const remoteBuilder = 'const remoteBuilder = new HttpRemoteBuilder();';
25
+ const clientBuilder = 'const clientBuilder = new ClientBuilder(remoteBuilder);';
26
+ const build = 'const client = clientBuilder.build({remoteUrl, segments, middleware});';
27
+ const start = 'client.start();';
28
+ const client = [remoteBuilder, clientBuilder, build, start].join('\n');
29
+ const exports = `export * from "${JITAR_CLIENT_ID}";`;
30
+ return [imports, declarations, client, exports].join('\n');
31
+ }
23
32
  export default function viteJitar(pluginConfig) {
24
- const sourceDir = formatDir(pluginConfig.sourceDir);
25
- const targetDir = formatDir(pluginConfig.targetDir);
26
- const jitarDir = formatDir(pluginConfig.jitarDir);
27
33
  const jitarUrl = pluginConfig.jitarUrl;
28
34
  const segments = pluginConfig.segments ?? [];
29
35
  const middlewares = pluginConfig.middleware ?? [];
30
- const scopes = [...segments, 'remote'];
31
- let rootPath;
32
- let sourcePath;
33
- let targetPath;
34
- let outputPath;
35
- let jitarPath;
36
+ const paths = {
37
+ vite: { input: undefined, output: undefined, assetOutput: undefined },
38
+ project: { root: undefined, source: undefined },
39
+ jitar: { input: undefined, output: undefined }
40
+ };
41
+ let buildHelper;
36
42
  let jitarBundleFilename;
37
43
  let jitarBundleImported = false;
38
44
  return {
@@ -44,11 +50,22 @@ export default function viteJitar(pluginConfig) {
44
50
  };
45
51
  },
46
52
  configResolved(resolvedConfig) {
47
- rootPath = normalizePath(path.join(resolvedConfig.root));
48
- sourcePath = normalizePath(path.join(rootPath, sourceDir));
49
- targetPath = normalizePath(path.join(rootPath, targetDir));
50
- outputPath = normalizePath(path.join(targetPath, resolvedConfig.build.assetsDir));
51
- jitarPath = normalizePath(path.join(sourcePath, jitarDir));
53
+ paths.vite.input = normalizePath(path.join(resolvedConfig.root));
54
+ paths.vite.output = normalizePath(path.join(paths.vite.input, resolvedConfig.build.outDir));
55
+ paths.vite.assetOutput = normalizePath(path.join(paths.vite.input, resolvedConfig.build.assetsDir));
56
+ paths.project.root = normalizePath(path.join(paths.vite.input, pluginConfig.projectRoot));
57
+ paths.project.source = normalizePath(path.join(paths.vite.input, pluginConfig.sourceRoot));
58
+ },
59
+ async buildStart() {
60
+ const configurationManager = new ConfigurationManager(paths.project.root);
61
+ if (pluginConfig.environmentFile !== undefined) {
62
+ await configurationManager.configureEnvironment(pluginConfig.environmentFile);
63
+ }
64
+ const configuration = await configurationManager.getRuntimeConfiguration(pluginConfig.configurationFile);
65
+ paths.jitar.input = normalizePath(path.join(paths.project.root, configuration.source));
66
+ paths.jitar.output = normalizePath(path.join(paths.project.root, configuration.target));
67
+ buildHelper = new BuildHelper(configuration);
68
+ await buildHelper.readApplication();
52
69
  },
53
70
  options(options) {
54
71
  if (options.input === undefined) {
@@ -67,7 +84,7 @@ export default function viteJitar(pluginConfig) {
67
84
  },
68
85
  resolveId: {
69
86
  order: 'pre',
70
- async handler(source, importer, options) {
87
+ async handler(source, importer) {
71
88
  if (source === JITAR_BUNDLE_ID) {
72
89
  return source;
73
90
  }
@@ -77,47 +94,31 @@ export default function viteJitar(pluginConfig) {
77
94
  }
78
95
  return JITAR_BUNDLE_ID;
79
96
  }
80
- const resolution = await this.resolve(source, importer, options);
81
- if (resolution === null || jitarPath === undefined || resolution.id.includes(jitarPath) === false) {
97
+ }
98
+ },
99
+ load(id) {
100
+ if (id === JITAR_BUNDLE_ID) {
101
+ return createJitarBundle(middlewares, paths.vite.output);
102
+ }
103
+ if (id.startsWith(paths.project.source)) {
104
+ if (id.startsWith(paths.vite.input)) {
82
105
  return null;
83
106
  }
84
- const cacheId = resolution.id.replace(sourcePath, targetPath);
85
- for (const scope of scopes) {
86
- const scopeId = cacheId.replace('.ts', `.${scope}.js`);
87
- if (fs.existsSync(scopeId)) {
88
- return scopeId;
107
+ const relativeId = id
108
+ .replace(paths.project.source, '')
109
+ .replace('.ts', '.js');
110
+ if (relativeId.endsWith('.js')) {
111
+ try {
112
+ return buildHelper.generateModuleCode(relativeId, segments);
113
+ }
114
+ catch (error) {
115
+ const message = error instanceof Error ? error.message : String(error);
116
+ console.error('ERROR:', message);
117
+ return null;
89
118
  }
90
119
  }
91
- const scopeId = cacheId.replace('.ts', '.js');
92
- if (fs.existsSync(scopeId)) {
93
- return scopeId;
94
- }
95
- return resolution.id;
96
- }
97
- },
98
- load(id) {
99
- if (id !== JITAR_BUNDLE_ID) {
100
- return null;
101
120
  }
102
- const segmentFiles = segments.map(name => `${targetPath}/${name}.segment.js`);
103
- const middlewareFiles = middlewares.map(name => assureExtension(`${targetPath}/${name}`));
104
- const jitarImport = `import { ClientBuilder, HttpRemoteBuilder } from "${JITAR_CLIENT_ID}";`;
105
- const segmentImports = segmentFiles.map((filename, index) => `import { default as $S${index} } from "${filename}";`).join('');
106
- const middlewareImports = middlewareFiles.map((filename, index) => `import { default as $M${index} } from "${filename}";`).join('');
107
- const imports = [jitarImport, segmentImports, middlewareImports].join('\n');
108
- const remoteUrl = 'const remoteUrl = document.location.origin;';
109
- const segmentsItems = segments.map((_, index) => `$S${index}`).join(', ');
110
- const segmentsArray = `const segments = [${segmentsItems}];`;
111
- const middlewareItems = middlewares.map((_, index) => `$M${index}`).join(', ');
112
- const middlewareArray = `const middleware = [${middlewareItems}];`;
113
- const declarations = [remoteUrl, segmentsArray, middlewareArray].join('\n');
114
- const remoteBuilder = 'const remoteBuilder = new HttpRemoteBuilder();';
115
- const clientBuilder = 'const clientBuilder = new ClientBuilder(remoteBuilder);';
116
- const build = 'const client = clientBuilder.build({remoteUrl, segments, middleware});';
117
- const start = 'client.start();';
118
- const client = [remoteBuilder, clientBuilder, build, start].join('\n');
119
- const exports = `export * from "${JITAR_CLIENT_ID}";`;
120
- return [imports, declarations, client, exports].join('\n');
121
+ return null;
121
122
  },
122
123
  generateBundle(options, bundle) {
123
124
  const bundles = Object.entries(bundle);
@@ -133,17 +134,17 @@ export default function viteJitar(pluginConfig) {
133
134
  return html;
134
135
  }
135
136
  if (jitarBundleFilename === undefined) {
136
- if (outputPath === undefined) {
137
+ if (paths.vite.assetOutput === undefined) {
137
138
  console.warn('Output path not found!');
138
139
  return html;
139
140
  }
140
- const filenames = fs.readdirSync(outputPath);
141
+ const filenames = fs.readdirSync(paths.vite.assetOutput);
141
142
  const jitarFilename = filenames.find(fileName => fileName.startsWith(JITAR_BUNDLE_ID) && fileName.endsWith('.js'));
142
143
  if (jitarFilename === undefined) {
143
144
  console.warn('Jitar bundle not found! Did you build the application first?');
144
145
  return html;
145
146
  }
146
- const jitarBundle = fs.readFileSync(path.join(outputPath, jitarFilename), 'utf-8');
147
+ const jitarBundle = fs.readFileSync(path.join(paths.vite.assetOutput, jitarFilename), 'utf-8');
147
148
  return html.replace('<script', `<script type="module">${jitarBundle}</script>\n <script`);
148
149
  }
149
150
  return html.replace('<script', `<script type="module" crossorigin src="/${jitarBundleFilename}"></script>\n <script`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitar/plugin-vite",
3
- "version": "0.10.6",
3
+ "version": "0.11.0",
4
4
  "description": "Vite plugin for Jitar.",
5
5
  "author": "Masking Technology <info@masking.tech> (https://jitar.dev)",
6
6
  "license": "MIT",
@@ -31,6 +31,7 @@
31
31
  "vite": "^7.3.0"
32
32
  },
33
33
  "peerDependencies": {
34
+ "jitar": "^0.11.0",
34
35
  "vite": ">=4.0.0 || >=5.0.0 || >=6.0.0 || >=7.0.0"
35
36
  },
36
37
  "peerDependenciesMeta": {