@jitar/plugin-vite 0.10.7 → 0.11.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/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,42 +1,48 @@
1
- import fs from 'node:fs';
2
1
  import path from 'node:path';
2
+ import { ConfigurationManager, BuildHelper } from 'jitar';
3
3
  import { normalizePath } from 'vite';
4
4
  const JITAR_SOURCE_ID = 'jitar';
5
5
  const JITAR_CLIENT_ID = 'jitar/client';
6
6
  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
- }
7
+ const JITAR_BUNDLE_RESOLVE_ID = `\0${JITAR_BUNDLE_ID}`;
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(path.join(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
- let jitarBundleFilename;
37
- let jitarBundleImported = false;
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;
42
+ let jitarImported = false;
38
43
  return {
39
44
  name: 'jitar-plugin-vite',
45
+ enforce: 'pre',
40
46
  config() {
41
47
  return {
42
48
  build: { target: 'esnext' },
@@ -44,109 +50,66 @@ 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.output, 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));
52
58
  },
53
- options(options) {
54
- if (options.input === undefined) {
55
- options.input = JITAR_BUNDLE_ID;
56
- }
57
- else if (typeof options.input === 'string') {
58
- options.input = [options.input, JITAR_BUNDLE_ID];
59
+ async buildStart() {
60
+ jitarImported = false;
61
+ const configurationManager = new ConfigurationManager(paths.project.root);
62
+ if (pluginConfig.environmentFile !== undefined) {
63
+ await configurationManager.configureEnvironment(pluginConfig.environmentFile);
59
64
  }
60
- else if (Array.isArray(options.input)) {
61
- options.input.push(JITAR_BUNDLE_ID);
65
+ const configuration = await configurationManager.getRuntimeConfiguration(pluginConfig.configurationFile);
66
+ paths.jitar.input = normalizePath(path.join(paths.project.root, configuration.source));
67
+ paths.jitar.output = normalizePath(path.join(paths.project.root, configuration.target));
68
+ buildHelper = new BuildHelper(configuration);
69
+ await buildHelper.readApplication();
70
+ },
71
+ resolveId(id) {
72
+ if (id === JITAR_BUNDLE_ID) {
73
+ return JITAR_BUNDLE_RESOLVE_ID;
62
74
  }
63
- else if (typeof options.input === 'object') {
64
- options.input.additionalEntry = JITAR_BUNDLE_ID;
75
+ if (id === JITAR_SOURCE_ID) {
76
+ return JITAR_BUNDLE_RESOLVE_ID;
65
77
  }
66
- return options;
78
+ return null;
67
79
  },
68
- resolveId: {
69
- order: 'pre',
70
- async handler(source, importer, options) {
71
- if (source === JITAR_BUNDLE_ID) {
72
- return source;
73
- }
74
- if (source === JITAR_SOURCE_ID) {
75
- if (importer?.endsWith('.segment.js') === false) {
76
- jitarBundleImported = true;
77
- }
78
- return JITAR_BUNDLE_ID;
79
- }
80
- const resolution = await this.resolve(source, importer, options);
81
- if (resolution === null || jitarPath === undefined || resolution.id.includes(jitarPath) === false) {
80
+ load(id) {
81
+ if (id === JITAR_BUNDLE_RESOLVE_ID) {
82
+ jitarImported = true;
83
+ return createJitarBundle(middlewares, paths.vite.output);
84
+ }
85
+ if (id.startsWith(paths.project.source)) {
86
+ if (id.startsWith(paths.vite.input)) {
82
87
  return null;
83
88
  }
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;
89
+ const relativeId = id
90
+ .replace(paths.project.source, '')
91
+ .replace('.ts', '.js');
92
+ if (relativeId.endsWith('.js')) {
93
+ try {
94
+ return buildHelper.generateModuleCode(relativeId, segments);
95
+ }
96
+ catch (error) {
97
+ const message = error instanceof Error ? error.message : String(error);
98
+ console.error('ERROR:', message);
99
+ return null;
89
100
  }
90
- }
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
- }
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
- },
122
- generateBundle(options, bundle) {
123
- const bundles = Object.entries(bundle);
124
- for (const [fileName, chunk] of bundles) {
125
- if (chunk.type === 'chunk' && chunk.name === JITAR_BUNDLE_ID) {
126
- jitarBundleFilename = fileName;
127
- break;
128
101
  }
129
102
  }
103
+ return null;
130
104
  },
131
- transformIndexHtml(html) {
132
- if (jitarBundleImported === true) {
133
- return html;
134
- }
135
- if (jitarBundleFilename === undefined) {
136
- if (outputPath === undefined) {
137
- console.warn('Output path not found!');
138
- return html;
139
- }
140
- const filenames = fs.readdirSync(outputPath);
141
- const jitarFilename = filenames.find(fileName => fileName.startsWith(JITAR_BUNDLE_ID) && fileName.endsWith('.js'));
142
- if (jitarFilename === undefined) {
143
- console.warn('Jitar bundle not found! Did you build the application first?');
144
- return html;
145
- }
146
- const jitarBundle = fs.readFileSync(path.join(outputPath, jitarFilename), 'utf-8');
147
- return html.replace('<script', `<script type="module">${jitarBundle}</script>\n <script`);
105
+ transform(code, id) {
106
+ const isFirstAppComponent = jitarImported === false
107
+ && id.startsWith(paths.project.source)
108
+ && (id.endsWith('.js') || id.endsWith('.ts') || id.endsWith('.jsx') || id.endsWith('.tsx'));
109
+ if (isFirstAppComponent) {
110
+ return `import '${JITAR_BUNDLE_ID}';\n${code}`;
148
111
  }
149
- return html.replace('<script', `<script type="module" crossorigin src="/${jitarBundleFilename}"></script>\n <script`);
112
+ return code;
150
113
  }
151
114
  };
152
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitar/plugin-vite",
3
- "version": "0.10.7",
3
+ "version": "0.11.1",
4
4
  "description": "Vite plugin for Jitar.",
5
5
  "author": "Masking Technology <info@masking.tech> (https://jitar.dev)",
6
6
  "license": "MIT",
@@ -31,7 +31,8 @@
31
31
  "vite": "^7.3.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "vite": ">=4.0.0 || >=5.0.0 || >=6.0.0 || >=7.0.0"
34
+ "jitar": "^0.11.0",
35
+ "vite": ">=7"
35
36
  },
36
37
  "peerDependenciesMeta": {
37
38
  "vite": {