@grafana/create-plugin 6.5.0-canary.2320.20227367050.0 → 6.5.0-canary.2320.20231018478.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.
@@ -1,7 +1,10 @@
1
1
  import { coerce, gte } from 'semver';
2
2
  import { parseDocument, stringify } from 'yaml';
3
+ import * as recast from 'recast';
4
+ import * as typeScriptParser from 'recast/parsers/typescript.js';
3
5
  import { additionsDebug } from '../../../utils.js';
4
6
 
7
+ const { builders } = recast.types;
5
8
  function updateDockerCompose(context) {
6
9
  if (!context.doesFileExist("docker-compose.yaml")) {
7
10
  additionsDebug("docker-compose.yaml not found, skipping");
@@ -109,5 +112,159 @@ export default defineConfig({
109
112
  additionsDebug("Error creating i18next.config.ts:", error);
110
113
  }
111
114
  }
115
+ function addI18nextToExternalsArray(externalsArray) {
116
+ const hasI18next = externalsArray.elements.some((element) => {
117
+ if (element && (element.type === "Literal" || element.type === "StringLiteral") && typeof element.value === "string") {
118
+ return element.value === "i18next";
119
+ }
120
+ return false;
121
+ });
122
+ if (hasI18next) {
123
+ additionsDebug("'i18next' already in externals array");
124
+ return false;
125
+ }
126
+ let insertIndex = -1;
127
+ for (let i = 0; i < externalsArray.elements.length; i++) {
128
+ const element = externalsArray.elements[i];
129
+ if (element && (element.type === "Literal" || element.type === "StringLiteral") && element.value === "rxjs") {
130
+ insertIndex = i + 1;
131
+ break;
132
+ }
133
+ }
134
+ if (insertIndex === -1) {
135
+ for (let i = externalsArray.elements.length - 1; i >= 0; i--) {
136
+ const element = externalsArray.elements[i];
137
+ if (element && element.type !== "FunctionExpression" && element.type !== "ArrowFunctionExpression") {
138
+ insertIndex = i + 1;
139
+ break;
140
+ }
141
+ }
142
+ if (insertIndex === -1) {
143
+ insertIndex = externalsArray.elements.length;
144
+ }
145
+ }
146
+ externalsArray.elements.splice(insertIndex, 0, builders.literal("i18next"));
147
+ additionsDebug(`Added 'i18next' to externals array at position ${insertIndex}`);
148
+ return true;
149
+ }
150
+ function ensureI18nextExternal(context) {
151
+ try {
152
+ additionsDebug("Checking for externals configuration...");
153
+ const externalsPath = ".config/bundler/externals.ts";
154
+ if (context.doesFileExist(externalsPath)) {
155
+ additionsDebug(`Found ${externalsPath}, checking for i18next...`);
156
+ const externalsContent = context.getFile(externalsPath);
157
+ if (externalsContent) {
158
+ try {
159
+ const ast = recast.parse(externalsContent, {
160
+ parser: typeScriptParser
161
+ });
162
+ let hasChanges = false;
163
+ recast.visit(ast, {
164
+ visitVariableDeclarator(path) {
165
+ const { node } = path;
166
+ if (node.id.type === "Identifier" && node.id.name === "externals" && node.init && node.init.type === "ArrayExpression") {
167
+ additionsDebug("Found externals array in externals.ts");
168
+ if (addI18nextToExternalsArray(node.init)) {
169
+ hasChanges = true;
170
+ }
171
+ }
172
+ return this.traverse(path);
173
+ }
174
+ });
175
+ if (hasChanges) {
176
+ const output = recast.print(ast, {
177
+ tabWidth: 2,
178
+ trailingComma: true,
179
+ lineTerminator: "\n"
180
+ });
181
+ context.updateFile(externalsPath, output.code);
182
+ additionsDebug(`Updated ${externalsPath} with i18next external`);
183
+ }
184
+ return;
185
+ } catch (error) {
186
+ additionsDebug(`Error updating ${externalsPath}:`, error);
187
+ }
188
+ }
189
+ }
190
+ const webpackConfigPath = ".config/webpack/webpack.config.ts";
191
+ additionsDebug(`Checking for ${webpackConfigPath}...`);
192
+ if (context.doesFileExist(webpackConfigPath)) {
193
+ additionsDebug(`Found ${webpackConfigPath}, checking for inline externals...`);
194
+ const webpackContent = context.getFile(webpackConfigPath);
195
+ if (webpackContent) {
196
+ try {
197
+ const ast = recast.parse(webpackContent, {
198
+ parser: typeScriptParser
199
+ });
200
+ let hasChanges = false;
201
+ let foundExternals = false;
202
+ recast.visit(ast, {
203
+ visitObjectExpression(path) {
204
+ const { node } = path;
205
+ const properties = node.properties;
206
+ if (properties) {
207
+ for (const prop of properties) {
208
+ if (prop && (prop.type === "Property" || prop.type === "ObjectProperty")) {
209
+ const key = "key" in prop ? prop.key : null;
210
+ const value = "value" in prop ? prop.value : null;
211
+ if (key && key.type === "Identifier" && key.name === "externals" && value && value.type === "ArrayExpression") {
212
+ foundExternals = true;
213
+ additionsDebug("Found externals property in webpack.config.ts");
214
+ if (addI18nextToExternalsArray(value)) {
215
+ hasChanges = true;
216
+ }
217
+ }
218
+ }
219
+ }
220
+ }
221
+ return this.traverse(path);
222
+ },
223
+ visitProperty(path) {
224
+ const { node } = path;
225
+ if (node.key && node.key.type === "Identifier" && node.key.name === "externals" && node.value && node.value.type === "ArrayExpression") {
226
+ if (!foundExternals) {
227
+ foundExternals = true;
228
+ additionsDebug("Found externals property in webpack.config.ts (via visitProperty)");
229
+ }
230
+ if (addI18nextToExternalsArray(node.value)) {
231
+ hasChanges = true;
232
+ }
233
+ }
234
+ return this.traverse(path);
235
+ }
236
+ });
237
+ if (!foundExternals) {
238
+ additionsDebug("No externals property found in webpack.config.ts");
239
+ }
240
+ if (hasChanges) {
241
+ const output = recast.print(ast, {
242
+ tabWidth: 2,
243
+ trailingComma: true,
244
+ lineTerminator: "\n"
245
+ });
246
+ context.updateFile(webpackConfigPath, output.code);
247
+ additionsDebug(`Updated ${webpackConfigPath} with i18next external`);
248
+ } else if (foundExternals) {
249
+ additionsDebug("i18next already present in externals, no changes needed");
250
+ }
251
+ return;
252
+ } catch (error) {
253
+ additionsDebug(`Error updating ${webpackConfigPath}:`, error);
254
+ additionsDebug(`Error details: ${error instanceof Error ? error.message : String(error)}`);
255
+ }
256
+ } else {
257
+ additionsDebug(`File ${webpackConfigPath} exists but content is empty`);
258
+ }
259
+ } else {
260
+ additionsDebug(`File ${webpackConfigPath} does not exist`);
261
+ }
262
+ additionsDebug("No externals configuration found, skipping i18next external check");
263
+ } catch (error) {
264
+ additionsDebug(
265
+ `Unexpected error in ensureI18nextExternal: ${error instanceof Error ? error.message : String(error)}`
266
+ );
267
+ }
268
+ }
112
269
 
113
- export { createI18nextConfig, updateDockerCompose, updatePluginJson };
270
+ export { createI18nextConfig, ensureI18nextExternal, updateDockerCompose, updatePluginJson };
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot';
2
2
  import { additionsDebug } from '../../../utils.js';
3
- import { updateDockerCompose, updatePluginJson, createI18nextConfig } from './config-updates.js';
3
+ import { updateDockerCompose, updatePluginJson, createI18nextConfig, ensureI18nextExternal } from './config-updates.js';
4
4
  import { addI18nInitialization, createLoadResourcesFile } from './code-generation.js';
5
5
  import { addI18nDependency, addSemverDependency, updateEslintConfig, addI18nextCli } from './tooling.js';
6
6
  import { checkNeedsBackwardCompatibility, createLocaleFiles } from './utils.js';
@@ -43,6 +43,11 @@ function i18nAddition(context, options) {
43
43
  }
44
44
  addI18nextCli(context);
45
45
  createI18nextConfig(context);
46
+ try {
47
+ ensureI18nextExternal(context);
48
+ } catch (error) {
49
+ additionsDebug(`Error ensuring i18next external: ${error instanceof Error ? error.message : String(error)}`);
50
+ }
46
51
  return context;
47
52
  }
48
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/create-plugin",
3
- "version": "6.5.0-canary.2320.20227367050.0",
3
+ "version": "6.5.0-canary.2320.20231018478.0",
4
4
  "repository": {
5
5
  "directory": "packages/create-plugin",
6
6
  "url": "https://github.com/grafana/plugin-tools"
@@ -56,5 +56,5 @@
56
56
  "engines": {
57
57
  "node": ">=20"
58
58
  },
59
- "gitHead": "d76c5b7a65c1d44699de403e8692e51e129df9f0"
59
+ "gitHead": "2c9b13a41672a1b9e24a92b61a39533bcee266a6"
60
60
  }
@@ -1,9 +1,13 @@
1
1
  import { coerce, gte } from 'semver';
2
2
  import { parseDocument, stringify } from 'yaml';
3
+ import * as recast from 'recast';
4
+ import * as typeScriptParser from 'recast/parsers/typescript.js';
3
5
 
4
6
  import type { Context } from '../../../context.js';
5
7
  import { additionsDebug } from '../../../utils.js';
6
8
 
9
+ const { builders } = recast.types;
10
+
7
11
  export function updateDockerCompose(context: Context): void {
8
12
  if (!context.doesFileExist('docker-compose.yaml')) {
9
13
  additionsDebug('docker-compose.yaml not found, skipping');
@@ -137,3 +141,221 @@ export default defineConfig({
137
141
  additionsDebug('Error creating i18next.config.ts:', error);
138
142
  }
139
143
  }
144
+
145
+ /**
146
+ * Adds 'i18next' to an externals array if it's not already present
147
+ * @returns true if changes were made, false otherwise
148
+ */
149
+ function addI18nextToExternalsArray(externalsArray: recast.types.namedTypes.ArrayExpression): boolean {
150
+ // Check if 'i18next' is already in the array
151
+ const hasI18next = externalsArray.elements.some((element) => {
152
+ if (
153
+ element &&
154
+ (element.type === 'Literal' || element.type === 'StringLiteral') &&
155
+ typeof element.value === 'string'
156
+ ) {
157
+ return element.value === 'i18next';
158
+ }
159
+ return false;
160
+ });
161
+
162
+ if (hasI18next) {
163
+ additionsDebug("'i18next' already in externals array");
164
+ return false;
165
+ }
166
+
167
+ // Find the position after 'rxjs' to insert 'i18next'
168
+ let insertIndex = -1;
169
+ for (let i = 0; i < externalsArray.elements.length; i++) {
170
+ const element = externalsArray.elements[i];
171
+ if (element && (element.type === 'Literal' || element.type === 'StringLiteral') && element.value === 'rxjs') {
172
+ insertIndex = i + 1;
173
+ break;
174
+ }
175
+ }
176
+
177
+ // If 'rxjs' not found, append to the end (before the function at the end)
178
+ if (insertIndex === -1) {
179
+ // Find the last non-function element
180
+ for (let i = externalsArray.elements.length - 1; i >= 0; i--) {
181
+ const element = externalsArray.elements[i];
182
+ if (element && element.type !== 'FunctionExpression' && element.type !== 'ArrowFunctionExpression') {
183
+ insertIndex = i + 1;
184
+ break;
185
+ }
186
+ }
187
+ // If still not found, append at the end
188
+ if (insertIndex === -1) {
189
+ insertIndex = externalsArray.elements.length;
190
+ }
191
+ }
192
+
193
+ // Insert 'i18next' at the found position
194
+ externalsArray.elements.splice(insertIndex, 0, builders.literal('i18next'));
195
+ additionsDebug(`Added 'i18next' to externals array at position ${insertIndex}`);
196
+ return true;
197
+ }
198
+
199
+ export function ensureI18nextExternal(context: Context): void {
200
+ try {
201
+ additionsDebug('Checking for externals configuration...');
202
+
203
+ // Try new structure first: .config/bundler/externals.ts
204
+ const externalsPath = '.config/bundler/externals.ts';
205
+ if (context.doesFileExist(externalsPath)) {
206
+ additionsDebug(`Found ${externalsPath}, checking for i18next...`);
207
+ const externalsContent = context.getFile(externalsPath);
208
+ if (externalsContent) {
209
+ try {
210
+ const ast = recast.parse(externalsContent, {
211
+ parser: typeScriptParser,
212
+ });
213
+
214
+ let hasChanges = false;
215
+
216
+ // Find the externals array
217
+ recast.visit(ast, {
218
+ visitVariableDeclarator(path) {
219
+ const { node } = path;
220
+
221
+ if (
222
+ node.id.type === 'Identifier' &&
223
+ node.id.name === 'externals' &&
224
+ node.init &&
225
+ node.init.type === 'ArrayExpression'
226
+ ) {
227
+ additionsDebug('Found externals array in externals.ts');
228
+ if (addI18nextToExternalsArray(node.init)) {
229
+ hasChanges = true;
230
+ }
231
+ }
232
+
233
+ return this.traverse(path);
234
+ },
235
+ });
236
+
237
+ // Only update the file if we made changes
238
+ if (hasChanges) {
239
+ const output = recast.print(ast, {
240
+ tabWidth: 2,
241
+ trailingComma: true,
242
+ lineTerminator: '\n',
243
+ });
244
+ context.updateFile(externalsPath, output.code);
245
+ additionsDebug(`Updated ${externalsPath} with i18next external`);
246
+ }
247
+ return;
248
+ } catch (error) {
249
+ additionsDebug(`Error updating ${externalsPath}:`, error);
250
+ }
251
+ }
252
+ }
253
+
254
+ // Fall back to legacy structure: .config/webpack/webpack.config.ts with inline externals
255
+ const webpackConfigPath = '.config/webpack/webpack.config.ts';
256
+ additionsDebug(`Checking for ${webpackConfigPath}...`);
257
+ if (context.doesFileExist(webpackConfigPath)) {
258
+ additionsDebug(`Found ${webpackConfigPath}, checking for inline externals...`);
259
+ const webpackContent = context.getFile(webpackConfigPath);
260
+ if (webpackContent) {
261
+ try {
262
+ const ast = recast.parse(webpackContent, {
263
+ parser: typeScriptParser,
264
+ });
265
+
266
+ let hasChanges = false;
267
+ let foundExternals = false;
268
+
269
+ // Find the externals property in the Configuration object
270
+ // It can be in baseConfig or any variable with an object initializer
271
+ recast.visit(ast, {
272
+ visitObjectExpression(path) {
273
+ const { node } = path;
274
+ const properties = node.properties;
275
+
276
+ if (properties) {
277
+ for (const prop of properties) {
278
+ // Handle both Property and ObjectProperty types
279
+ if (prop && (prop.type === 'Property' || prop.type === 'ObjectProperty')) {
280
+ const key = 'key' in prop ? prop.key : null;
281
+ const value = 'value' in prop ? prop.value : null;
282
+
283
+ if (
284
+ key &&
285
+ key.type === 'Identifier' &&
286
+ key.name === 'externals' &&
287
+ value &&
288
+ value.type === 'ArrayExpression'
289
+ ) {
290
+ foundExternals = true;
291
+ additionsDebug('Found externals property in webpack.config.ts');
292
+ if (addI18nextToExternalsArray(value)) {
293
+ hasChanges = true;
294
+ }
295
+ // Don't break, continue to check all object expressions
296
+ }
297
+ }
298
+ }
299
+ }
300
+
301
+ return this.traverse(path);
302
+ },
303
+ visitProperty(path) {
304
+ const { node } = path;
305
+
306
+ // Also check properties directly (fallback)
307
+ if (
308
+ node.key &&
309
+ node.key.type === 'Identifier' &&
310
+ node.key.name === 'externals' &&
311
+ node.value &&
312
+ node.value.type === 'ArrayExpression'
313
+ ) {
314
+ if (!foundExternals) {
315
+ foundExternals = true;
316
+ additionsDebug('Found externals property in webpack.config.ts (via visitProperty)');
317
+ }
318
+ if (addI18nextToExternalsArray(node.value)) {
319
+ hasChanges = true;
320
+ }
321
+ }
322
+
323
+ return this.traverse(path);
324
+ },
325
+ });
326
+
327
+ if (!foundExternals) {
328
+ additionsDebug('No externals property found in webpack.config.ts');
329
+ }
330
+
331
+ // Only update the file if we made changes
332
+ if (hasChanges) {
333
+ const output = recast.print(ast, {
334
+ tabWidth: 2,
335
+ trailingComma: true,
336
+ lineTerminator: '\n',
337
+ });
338
+ context.updateFile(webpackConfigPath, output.code);
339
+ additionsDebug(`Updated ${webpackConfigPath} with i18next external`);
340
+ } else if (foundExternals) {
341
+ additionsDebug('i18next already present in externals, no changes needed');
342
+ }
343
+ return;
344
+ } catch (error) {
345
+ additionsDebug(`Error updating ${webpackConfigPath}:`, error);
346
+ additionsDebug(`Error details: ${error instanceof Error ? error.message : String(error)}`);
347
+ }
348
+ } else {
349
+ additionsDebug(`File ${webpackConfigPath} exists but content is empty`);
350
+ }
351
+ } else {
352
+ additionsDebug(`File ${webpackConfigPath} does not exist`);
353
+ }
354
+
355
+ additionsDebug('No externals configuration found, skipping i18next external check');
356
+ } catch (error) {
357
+ additionsDebug(
358
+ `Unexpected error in ensureI18nextExternal: ${error instanceof Error ? error.message : String(error)}`
359
+ );
360
+ }
361
+ }
@@ -2,7 +2,7 @@ import * as v from 'valibot';
2
2
 
3
3
  import type { Context } from '../../../context.js';
4
4
  import { additionsDebug } from '../../../utils.js';
5
- import { updateDockerCompose, updatePluginJson, createI18nextConfig } from './config-updates.js';
5
+ import { updateDockerCompose, updatePluginJson, createI18nextConfig, ensureI18nextExternal } from './config-updates.js';
6
6
  import { addI18nInitialization, createLoadResourcesFile } from './code-generation.js';
7
7
  import { updateEslintConfig, addI18nDependency, addSemverDependency, addI18nextCli } from './tooling.js';
8
8
  import { checkNeedsBackwardCompatibility, createLocaleFiles } from './utils.js';
@@ -77,5 +77,12 @@ export default function i18nAddition(context: Context, options: I18nOptions): Co
77
77
  // 10. Create i18next.config.ts
78
78
  createI18nextConfig(context);
79
79
 
80
+ // 11. Ensure i18next is in externals array
81
+ try {
82
+ ensureI18nextExternal(context);
83
+ } catch (error) {
84
+ additionsDebug(`Error ensuring i18next external: ${error instanceof Error ? error.message : String(error)}`);
85
+ }
86
+
80
87
  return context;
81
88
  }