@jitar/plugin-vite 0.11.0 → 0.11.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.
Files changed (2) hide show
  1. package/dist/index.js +21 -59
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import fs from 'node:fs';
2
1
  import path from 'node:path';
3
2
  import { ConfigurationManager, BuildHelper } from 'jitar';
4
3
  import { normalizePath } from 'vite';
5
4
  const JITAR_SOURCE_ID = 'jitar';
6
5
  const JITAR_CLIENT_ID = 'jitar/client';
7
6
  const JITAR_BUNDLE_ID = 'jitar-bundle';
7
+ const JITAR_BUNDLE_RESOLVE_ID = `\0${JITAR_BUNDLE_ID}`;
8
8
  function assureExtension(filename) {
9
9
  if (filename.endsWith('.js')) {
10
10
  return filename;
@@ -12,7 +12,7 @@ function assureExtension(filename) {
12
12
  return `${filename}.js`;
13
13
  }
14
14
  function createJitarBundle(middlewares, targetPath) {
15
- const middlewareFiles = middlewares.map(name => assureExtension(`${targetPath}/${name}`));
15
+ const middlewareFiles = middlewares.map(name => assureExtension(path.join(targetPath, name)));
16
16
  const jitarImport = `import { ClientBuilder, HttpRemoteBuilder } from "${JITAR_CLIENT_ID}";`;
17
17
  const middlewareImports = middlewareFiles.map((filename, index) => `import { default as $M${index} } from "${filename}";`).join('');
18
18
  const imports = [jitarImport, middlewareImports].join('\n');
@@ -39,10 +39,10 @@ export default function viteJitar(pluginConfig) {
39
39
  jitar: { input: undefined, output: undefined }
40
40
  };
41
41
  let buildHelper;
42
- let jitarBundleFilename;
43
- let jitarBundleImported = false;
42
+ let jitarImported = false;
44
43
  return {
45
44
  name: 'jitar-plugin-vite',
45
+ enforce: 'pre',
46
46
  config() {
47
47
  return {
48
48
  build: { target: 'esnext' },
@@ -52,11 +52,12 @@ export default function viteJitar(pluginConfig) {
52
52
  configResolved(resolvedConfig) {
53
53
  paths.vite.input = normalizePath(path.join(resolvedConfig.root));
54
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));
55
+ paths.vite.assetOutput = normalizePath(path.join(paths.vite.output, resolvedConfig.build.assetsDir));
56
56
  paths.project.root = normalizePath(path.join(paths.vite.input, pluginConfig.projectRoot));
57
57
  paths.project.source = normalizePath(path.join(paths.vite.input, pluginConfig.sourceRoot));
58
58
  },
59
59
  async buildStart() {
60
+ jitarImported = false;
60
61
  const configurationManager = new ConfigurationManager(paths.project.root);
61
62
  if (pluginConfig.environmentFile !== undefined) {
62
63
  await configurationManager.configureEnvironment(pluginConfig.environmentFile);
@@ -67,37 +68,18 @@ export default function viteJitar(pluginConfig) {
67
68
  buildHelper = new BuildHelper(configuration);
68
69
  await buildHelper.readApplication();
69
70
  },
70
- options(options) {
71
- if (options.input === undefined) {
72
- options.input = JITAR_BUNDLE_ID;
73
- }
74
- else if (typeof options.input === 'string') {
75
- options.input = [options.input, JITAR_BUNDLE_ID];
76
- }
77
- else if (Array.isArray(options.input)) {
78
- options.input.push(JITAR_BUNDLE_ID);
79
- }
80
- else if (typeof options.input === 'object') {
81
- options.input.additionalEntry = JITAR_BUNDLE_ID;
71
+ resolveId(id) {
72
+ if (id === JITAR_BUNDLE_ID) {
73
+ return JITAR_BUNDLE_RESOLVE_ID;
82
74
  }
83
- return options;
84
- },
85
- resolveId: {
86
- order: 'pre',
87
- async handler(source, importer) {
88
- if (source === JITAR_BUNDLE_ID) {
89
- return source;
90
- }
91
- if (source === JITAR_SOURCE_ID) {
92
- if (importer?.endsWith('.segment.js') === false) {
93
- jitarBundleImported = true;
94
- }
95
- return JITAR_BUNDLE_ID;
96
- }
75
+ if (id === JITAR_SOURCE_ID) {
76
+ return JITAR_BUNDLE_RESOLVE_ID;
97
77
  }
78
+ return null;
98
79
  },
99
80
  load(id) {
100
- if (id === JITAR_BUNDLE_ID) {
81
+ if (id === JITAR_BUNDLE_RESOLVE_ID) {
82
+ jitarImported = true;
101
83
  return createJitarBundle(middlewares, paths.vite.output);
102
84
  }
103
85
  if (id.startsWith(paths.project.source)) {
@@ -120,34 +102,14 @@ export default function viteJitar(pluginConfig) {
120
102
  }
121
103
  return null;
122
104
  },
123
- generateBundle(options, bundle) {
124
- const bundles = Object.entries(bundle);
125
- for (const [fileName, chunk] of bundles) {
126
- if (chunk.type === 'chunk' && chunk.name === JITAR_BUNDLE_ID) {
127
- jitarBundleFilename = fileName;
128
- break;
129
- }
130
- }
131
- },
132
- transformIndexHtml(html) {
133
- if (jitarBundleImported === true) {
134
- return html;
135
- }
136
- if (jitarBundleFilename === undefined) {
137
- if (paths.vite.assetOutput === undefined) {
138
- console.warn('Output path not found!');
139
- return html;
140
- }
141
- const filenames = fs.readdirSync(paths.vite.assetOutput);
142
- const jitarFilename = filenames.find(fileName => fileName.startsWith(JITAR_BUNDLE_ID) && fileName.endsWith('.js'));
143
- if (jitarFilename === undefined) {
144
- console.warn('Jitar bundle not found! Did you build the application first?');
145
- return html;
146
- }
147
- const jitarBundle = fs.readFileSync(path.join(paths.vite.assetOutput, jitarFilename), 'utf-8');
148
- 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}`;
149
111
  }
150
- return html.replace('<script', `<script type="module" crossorigin src="/${jitarBundleFilename}"></script>\n <script`);
112
+ return code;
151
113
  }
152
114
  };
153
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitar/plugin-vite",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "description": "Vite plugin for Jitar.",
5
5
  "author": "Masking Technology <info@masking.tech> (https://jitar.dev)",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "peerDependencies": {
34
34
  "jitar": "^0.11.0",
35
- "vite": ">=4.0.0 || >=5.0.0 || >=6.0.0 || >=7.0.0"
35
+ "vite": ">=7"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "vite": {