@intlayer/webpack 1.0.1 → 1.2.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.
Files changed (117) hide show
  1. package/dist/cjs/{scripts/startServer.cjs → getEntries.cjs} +19 -14
  2. package/dist/cjs/getEntries.cjs.map +1 -0
  3. package/dist/cjs/getEntries.d.ts +5 -0
  4. package/dist/cjs/index.cjs +0 -7
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/index.d.ts +0 -2
  7. package/dist/cjs/utils.cjs +26 -17
  8. package/dist/cjs/utils.cjs.map +1 -1
  9. package/dist/cjs/utils.d.ts +5 -2
  10. package/dist/cjs/webpack-plugin.cjs +4 -134
  11. package/dist/cjs/webpack-plugin.cjs.map +1 -1
  12. package/dist/cjs/webpack-plugin.d.ts +1 -11
  13. package/dist/cjs/webpack.config.cjs +28 -25
  14. package/dist/cjs/webpack.config.cjs.map +1 -1
  15. package/dist/esm/getEntries.d.mts +5 -0
  16. package/dist/esm/getEntries.mjs +17 -0
  17. package/dist/esm/getEntries.mjs.map +1 -0
  18. package/dist/esm/index.d.mts +0 -2
  19. package/dist/esm/index.mjs +0 -4
  20. package/dist/esm/index.mjs.map +1 -1
  21. package/dist/esm/utils.d.mts +5 -2
  22. package/dist/esm/utils.mjs +13 -15
  23. package/dist/esm/utils.mjs.map +1 -1
  24. package/dist/esm/webpack-plugin.d.mts +1 -11
  25. package/dist/esm/webpack-plugin.mjs +4 -134
  26. package/dist/esm/webpack-plugin.mjs.map +1 -1
  27. package/dist/esm/webpack.config.mjs +29 -26
  28. package/dist/esm/webpack.config.mjs.map +1 -1
  29. package/package.json +11 -15
  30. package/src/getEntries.ts +19 -0
  31. package/src/index.ts +2 -12
  32. package/src/utils.ts +19 -19
  33. package/src/webpack-plugin.ts +5 -180
  34. package/src/webpack.config.ts +32 -28
  35. package/dist/cjs/scripts/bundle.cjs +0 -65
  36. package/dist/cjs/scripts/bundle.cjs.map +0 -1
  37. package/dist/cjs/scripts/bundle.d.ts +0 -7
  38. package/dist/cjs/scripts/index.cjs +0 -25
  39. package/dist/cjs/scripts/index.cjs.map +0 -1
  40. package/dist/cjs/scripts/index.d.ts +0 -3
  41. package/dist/cjs/scripts/startServer.cjs.map +0 -1
  42. package/dist/cjs/scripts/startServer.d.ts +0 -5
  43. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs +0 -76
  44. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.cjs.map +0 -1
  45. package/dist/cjs/transpiler/dictionary_to_main/createDictionaryList.d.ts +0 -6
  46. package/dist/cjs/transpiler/dictionary_to_main/index.cjs +0 -23
  47. package/dist/cjs/transpiler/dictionary_to_main/index.cjs.map +0 -1
  48. package/dist/cjs/transpiler/dictionary_to_main/index.d.ts +0 -1
  49. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs +0 -87
  50. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.cjs.map +0 -1
  51. package/dist/cjs/transpiler/dictionary_to_type/createModuleAugmentation.d.ts +0 -7
  52. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs +0 -110
  53. package/dist/cjs/transpiler/dictionary_to_type/createType.cjs.map +0 -1
  54. package/dist/cjs/transpiler/dictionary_to_type/createType.d.ts +0 -42
  55. package/dist/cjs/transpiler/dictionary_to_type/index.cjs +0 -25
  56. package/dist/cjs/transpiler/dictionary_to_type/index.cjs.map +0 -1
  57. package/dist/cjs/transpiler/dictionary_to_type/index.d.ts +0 -3
  58. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs +0 -45
  59. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.cjs.map +0 -1
  60. package/dist/cjs/transpiler/intlater_module_to_dictionary/extractNestedJSON.d.ts +0 -44
  61. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs +0 -27
  62. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.cjs.map +0 -1
  63. package/dist/cjs/transpiler/intlater_module_to_dictionary/index.d.ts +0 -4
  64. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs +0 -67
  65. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.cjs.map +0 -1
  66. package/dist/cjs/transpiler/intlater_module_to_dictionary/processModule.d.ts +0 -8
  67. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileBundledCode.cjs +0 -96
  68. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileBundledCode.cjs.map +0 -1
  69. package/dist/cjs/transpiler/intlater_module_to_dictionary/transpileBundledCode.d.ts +0 -6
  70. package/dist/esm/scripts/bundle.d.mts +0 -7
  71. package/dist/esm/scripts/bundle.mjs +0 -40
  72. package/dist/esm/scripts/bundle.mjs.map +0 -1
  73. package/dist/esm/scripts/index.d.mts +0 -3
  74. package/dist/esm/scripts/index.mjs +0 -3
  75. package/dist/esm/scripts/index.mjs.map +0 -1
  76. package/dist/esm/scripts/startServer.d.mts +0 -5
  77. package/dist/esm/scripts/startServer.mjs +0 -12
  78. package/dist/esm/scripts/startServer.mjs.map +0 -1
  79. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.d.mts +0 -6
  80. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs +0 -52
  81. package/dist/esm/transpiler/dictionary_to_main/createDictionaryList.mjs.map +0 -1
  82. package/dist/esm/transpiler/dictionary_to_main/index.d.mts +0 -1
  83. package/dist/esm/transpiler/dictionary_to_main/index.mjs +0 -2
  84. package/dist/esm/transpiler/dictionary_to_main/index.mjs.map +0 -1
  85. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.d.mts +0 -7
  86. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs +0 -62
  87. package/dist/esm/transpiler/dictionary_to_type/createModuleAugmentation.mjs.map +0 -1
  88. package/dist/esm/transpiler/dictionary_to_type/createType.d.mts +0 -42
  89. package/dist/esm/transpiler/dictionary_to_type/createType.mjs +0 -86
  90. package/dist/esm/transpiler/dictionary_to_type/createType.mjs.map +0 -1
  91. package/dist/esm/transpiler/dictionary_to_type/index.d.mts +0 -3
  92. package/dist/esm/transpiler/dictionary_to_type/index.mjs +0 -3
  93. package/dist/esm/transpiler/dictionary_to_type/index.mjs.map +0 -1
  94. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.d.mts +0 -44
  95. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs +0 -21
  96. package/dist/esm/transpiler/intlater_module_to_dictionary/extractNestedJSON.mjs.map +0 -1
  97. package/dist/esm/transpiler/intlater_module_to_dictionary/index.d.mts +0 -4
  98. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs +0 -4
  99. package/dist/esm/transpiler/intlater_module_to_dictionary/index.mjs.map +0 -1
  100. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.d.mts +0 -8
  101. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs +0 -43
  102. package/dist/esm/transpiler/intlater_module_to_dictionary/processModule.mjs.map +0 -1
  103. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.d.mts +0 -6
  104. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.mjs +0 -72
  105. package/dist/esm/transpiler/intlater_module_to_dictionary/transpileBundledCode.mjs.map +0 -1
  106. package/src/scripts/bundle.ts +0 -44
  107. package/src/scripts/index.ts +0 -2
  108. package/src/scripts/startServer.ts +0 -14
  109. package/src/transpiler/dictionary_to_main/createDictionaryList.ts +0 -65
  110. package/src/transpiler/dictionary_to_main/index.ts +0 -1
  111. package/src/transpiler/dictionary_to_type/createModuleAugmentation.ts +0 -95
  112. package/src/transpiler/dictionary_to_type/createType.ts +0 -140
  113. package/src/transpiler/dictionary_to_type/index.ts +0 -2
  114. package/src/transpiler/intlater_module_to_dictionary/extractNestedJSON.ts +0 -60
  115. package/src/transpiler/intlater_module_to_dictionary/index.ts +0 -3
  116. package/src/transpiler/intlater_module_to_dictionary/processModule.ts +0 -69
  117. package/src/transpiler/intlater_module_to_dictionary/transpileBundledCode.ts +0 -107
@@ -1,140 +0,0 @@
1
- import { existsSync, mkdirSync, writeFileSync } from 'fs';
2
- import { resolve } from 'path';
3
- import { getConfiguration } from '@intlayer/config';
4
- import {
5
- NodeType,
6
- type Content,
7
- type ContentModule,
8
- type TypedNode,
9
- } from '@intlayer/core';
10
- import { getTypeName } from './createModuleAugmentation';
11
-
12
- const { content, internationalization } = getConfiguration();
13
- const { typesDir } = content;
14
-
15
- /**
16
- *
17
- * This function generates a TypeScript type definition from a JSON object
18
- *
19
- * Example:
20
- *
21
- * const input = {
22
- * id: '1',
23
- * name: 'John Doe',
24
- * address: {
25
- * id: '2',
26
- * street: '123 Main St',
27
- * city: 'Springfield',
28
- * }
29
- * };
30
- *
31
- * const result = generateTypeScriptType(input, 'RootObject');
32
- * console.log(result);
33
- *
34
- * Output:
35
- *
36
- * type RootObject = {
37
- * id: '1',
38
- * name: string,
39
- * address: {
40
- * id: '2',
41
- * street: string,
42
- * city: string,
43
- * },
44
- * };
45
- *
46
- */
47
- export const generateTypeScriptType = (obj: ContentModule): string => {
48
- let typeDefinition = ``;
49
-
50
- const typeName = getTypeName(obj.id);
51
-
52
- typeDefinition += `export type ${typeName} = {\n`;
53
- typeDefinition += generateTypeScriptTypeContent(obj);
54
- typeDefinition += '};\n\n';
55
-
56
- return typeDefinition;
57
- };
58
-
59
- export const generateTypeScriptTypeContent = (obj: Content): string => {
60
- let typeDefinition = ``;
61
-
62
- for (const [key, value] of Object.entries(obj)) {
63
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
- const nodeType: NodeType | undefined = (value as TypedNode).nodeType;
65
-
66
- if (
67
- // Check if the value is a typed node
68
- typeof value === 'object' &&
69
- nodeType === NodeType.Translation
70
- ) {
71
- const tsType =
72
- typeof value[internationalization.defaultLocale as keyof typeof value];
73
- typeDefinition += ` ${key}: ${tsType},\n`;
74
- } else if (
75
- // Check if the value is a typed node
76
- typeof value === 'object' &&
77
- nodeType === NodeType.Enumeration
78
- ) {
79
- const tsType =
80
- typeof value[internationalization.defaultLocale as keyof typeof value];
81
-
82
- typeDefinition += ` ${key}: (quantity: number) => ${tsType},\n`;
83
- } else if (
84
- // Check if the value is a nested object
85
- typeof value === 'object' &&
86
- !Array.isArray(value)
87
- ) {
88
- // Nested object, recurse
89
- const nestedType = generateTypeScriptTypeContent(value as Content);
90
- typeDefinition += ` ${key}: {${nestedType}},\n`;
91
- } else if (
92
- // Check if the value is an array
93
- Array.isArray(value)
94
- ) {
95
- // Array handling (simplified, assumes non-empty arrays with uniform type)
96
- const arrayType = typeof value[0];
97
- typeDefinition += ` ${key}: ${arrayType}[],\n`;
98
- } else if (
99
- // Check if the value is an 'id'
100
- typeof value === 'string' &&
101
- key === 'id'
102
- ) {
103
- // Special handling for 'id' field
104
- const tsType = `"${value}"`;
105
- typeDefinition += ` ${key}: ${tsType},\n`;
106
- } else {
107
- // Primitive type
108
- const tsType = typeof value;
109
- typeDefinition += ` ${key}: ${tsType},\n`;
110
- }
111
- }
112
-
113
- return typeDefinition;
114
- };
115
-
116
- /**
117
- * This function generates a TypeScript type definition from a JSON object
118
- */
119
- export const createTypes = (dictionariesPaths: string[]): string[] => {
120
- const resultTypesPaths: string[] = [];
121
-
122
- // Create type folders if they don't exist
123
- if (!existsSync(typesDir)) {
124
- mkdirSync(typesDir, { recursive: true });
125
- }
126
-
127
- for (const dictionaryPath of dictionariesPaths) {
128
- const contentModule: ContentModule = require(dictionaryPath);
129
- const dictionaryName: string = contentModule.id;
130
- const typeDefinition: string = generateTypeScriptType(contentModule);
131
-
132
- const outputPath = resolve(typesDir, `${dictionaryName}.d.ts`);
133
-
134
- writeFileSync(outputPath, typeDefinition);
135
-
136
- resultTypesPaths.push(outputPath);
137
- }
138
-
139
- return resultTypesPaths;
140
- };
@@ -1,2 +0,0 @@
1
- export * from './createModuleAugmentation';
2
- export * from './createType';
@@ -1,60 +0,0 @@
1
- import type { Content, ContentModule } from '@intlayer/core';
2
-
3
- /**
4
- *
5
- * This function extracts all nested objects with an 'id' field from the input object and returns them as an array
6
- *
7
- * Example:
8
- *
9
- * const input = {
10
- * id: '1',
11
- * name: 'John Doe',
12
- * address: {
13
- * id: '2',
14
- * street: '123 Main St',
15
- * city: 'Springfield',
16
- * state: 'IL'
17
- * },
18
- * };
19
- * const result = extractObjectsWithId(input);
20
- * console.log(result);
21
- *
22
- * Output:
23
- *
24
- * [{
25
- * id: '1',
26
- * name: 'John Doe',
27
- * address: {
28
- * id: '2',
29
- * street: '123 Main St',
30
- * city: 'Springfield',
31
- * state: 'IL'
32
- * }
33
- * },
34
- * {
35
- * id: '2',
36
- * street: '123 Main St',
37
- * city: 'Springfield',
38
- * state: 'IL'
39
- * }]
40
- *
41
- */
42
- export const extractObjectsWithId = (input: ContentModule): ContentModule[] => {
43
- // Function to recursively search and extract nested objects with an 'id'
44
- const search = (obj: Content, results: ContentModule[]): void => {
45
- if (obj && typeof obj === 'object') {
46
- if (Object.prototype.hasOwnProperty.call(obj, 'id')) {
47
- results.push(obj as ContentModule);
48
- }
49
- for (const key of Object.keys(obj)) {
50
- if (typeof obj[key] === 'object') {
51
- search(obj[key] as Content, results);
52
- }
53
- }
54
- }
55
- };
56
-
57
- const results: ContentModule[] = [];
58
- search(input, results);
59
- return results;
60
- };
@@ -1,3 +0,0 @@
1
- export * from './extractNestedJSON';
2
- export * from './processModule';
3
- export * from './transpileBundledCode';
@@ -1,69 +0,0 @@
1
- import { resolve } from 'path';
2
- import type {
3
- Content,
4
- ContentModule,
5
- FlatContent,
6
- FlatContentValue,
7
- } from '@intlayer/core';
8
-
9
- /**
10
- * Function to load the module file in a sandboxed environment
11
- */
12
- const loadModule = async (modulePath: string): Promise<ContentModule> => {
13
- // @TODO: Sandbox the module to prevent malicious code execution
14
-
15
- return (await import(modulePath)).default;
16
- };
17
-
18
- /**
19
- * Function to replace function and async function fields with their results in the object
20
- */
21
- const processFunctionResults = async (entry: Content): Promise<FlatContent> => {
22
- if (entry && typeof entry === 'object') {
23
- const promises: Promise<void>[] = [];
24
- const result: FlatContent = {};
25
-
26
- for (const key of Object.keys(entry)) {
27
- const field = entry?.[key];
28
-
29
- if (typeof field === 'object') {
30
- result[key] = (await processFunctionResults(
31
- field as Content
32
- )) as FlatContentValue;
33
- }
34
-
35
- if (typeof field === 'function') {
36
- // Wait for the function to resolve if it's an async function
37
- const promise = (async () => {
38
- // Execute the function and await the result if it's a Promise
39
- const value = await field();
40
- result[key] = value as FlatContentValue;
41
- })();
42
- promises.push(promise);
43
- } else {
44
- result[key] = field as FlatContentValue;
45
- }
46
- }
47
-
48
- // Wait for all async operations to complete
49
- await Promise.all(promises);
50
-
51
- return result;
52
- }
53
-
54
- return entry;
55
- };
56
-
57
- /**
58
- * Function to load, process the module and return the Intlayer ContentModule from the module file
59
- */
60
- export const processModule = async (file: string) => {
61
- try {
62
- const functionPath = resolve(file);
63
- const entry = await loadModule(functionPath);
64
-
65
- return (await processFunctionResults(entry)) as ContentModule;
66
- } catch (error) {
67
- console.error('Error processing module:', error);
68
- }
69
- };
@@ -1,107 +0,0 @@
1
- import { existsSync } from 'fs';
2
- import { mkdir, writeFile } from 'fs/promises';
3
- import { resolve } from 'path';
4
- import { getConfiguration } from '@intlayer/config';
5
- import type { ContentModule } from '@intlayer/core';
6
- import { extractObjectsWithId } from './extractNestedJSON';
7
- import { processModule } from './processModule';
8
-
9
- const { content } = getConfiguration();
10
- const { dictionariesDir, bundleFileExtension } = content;
11
-
12
- const loadBundledModule = async (bundledEntryPath: string) => {
13
- const entryFilePath = resolve(bundledEntryPath);
14
-
15
- const isEntryFilePathExist = existsSync(entryFilePath);
16
-
17
- if (!isEntryFilePathExist) {
18
- console.error('Entry file not found. ' + entryFilePath);
19
- return;
20
- }
21
-
22
- const isEntryPathValid = entryFilePath.endsWith(bundleFileExtension);
23
-
24
- if (!isEntryPathValid) {
25
- return;
26
- }
27
-
28
- try {
29
- // Remove the module from the cache
30
- delete require.cache[entryFilePath];
31
-
32
- // Require the module anew
33
- const entry = await import(entryFilePath);
34
-
35
- let result: ContentModule | undefined;
36
-
37
- if (entry.default) {
38
- // JS or TS file
39
- result = await processModule(entryFilePath);
40
- } else {
41
- // JSON file
42
- result = entry;
43
- }
44
-
45
- return result;
46
- } catch (err) {
47
- console.error(`Error transpiling ${entryFilePath}:`, err);
48
- }
49
- };
50
-
51
- const buildDictionary = async (dictionaries: ContentModule[]) => {
52
- const resultDictionariesPaths: string[] = [];
53
-
54
- for await (const content of dictionaries) {
55
- const contentString = JSON.stringify(content);
56
-
57
- const id = content.id;
58
- const outputFileName = `${id}.json`;
59
- const resultFilePath = resolve(dictionariesDir, outputFileName);
60
-
61
- // Create the json file
62
- await writeFile(resultFilePath, contentString, 'utf8').catch((err) => {
63
- console.error(`Error creating ${outputFileName}:`, err);
64
- });
65
-
66
- resultDictionariesPaths.push(resultFilePath);
67
- }
68
-
69
- return resultDictionariesPaths;
70
- };
71
-
72
- /**
73
- * This function transpile the bundled code to to make dictionaries as JSON files
74
- */
75
- export const transpileBundledCode = async (
76
- bundledEntriesPaths: string[] | string
77
- ) => {
78
- const resultDictionariesPaths: string[] = [];
79
-
80
- if (typeof bundledEntriesPaths === 'string') {
81
- bundledEntriesPaths = [bundledEntriesPaths];
82
- }
83
-
84
- const filteredBundledEntriesPaths: string[] = bundledEntriesPaths.filter(
85
- // Filter js files;
86
- (bundledEntryPath) => bundledEntryPath.endsWith(bundleFileExtension)
87
- );
88
-
89
- // Create the dictionaries folder if it doesn't exist
90
- await mkdir(resolve(dictionariesDir), { recursive: true });
91
-
92
- for await (const bundledEntryPath of filteredBundledEntriesPaths) {
93
- const result = await loadBundledModule(bundledEntryPath);
94
-
95
- if (!result) {
96
- continue;
97
- }
98
-
99
- const nestedContent: ContentModule[] = extractObjectsWithId(result);
100
-
101
- const dictionariesPaths: string[] = await buildDictionary(nestedContent);
102
-
103
- resultDictionariesPaths.push(...dictionariesPaths);
104
- }
105
-
106
- return resultDictionariesPaths;
107
- };