@vocab/vite 0.1.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/LICENSE +21 -0
- package/README.md +827 -0
- package/create-language/dist/vocab-vite-create-language.cjs.d.ts +2 -0
- package/create-language/dist/vocab-vite-create-language.cjs.dev.js +34 -0
- package/create-language/dist/vocab-vite-create-language.cjs.js +7 -0
- package/create-language/dist/vocab-vite-create-language.cjs.prod.js +34 -0
- package/create-language/dist/vocab-vite-create-language.esm.js +26 -0
- package/create-language/package.json +4 -0
- package/create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.d.ts +2 -0
- package/create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.dev.js +54 -0
- package/create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.js +7 -0
- package/create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.prod.js +54 -0
- package/create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.esm.js +50 -0
- package/create-vocab-chunks/package.json +4 -0
- package/dist/declarations/src/create-language.d.ts +3 -0
- package/dist/declarations/src/create-vocab-chunks.d.ts +5 -0
- package/dist/declarations/src/index.d.ts +6 -0
- package/dist/logger-0d92b2c5.cjs.prod.js +12 -0
- package/dist/logger-3e3dccee.cjs.dev.js +12 -0
- package/dist/logger-e3a4b043.esm.js +6 -0
- package/dist/vocab-vite.cjs.d.ts +3 -0
- package/dist/vocab-vite.cjs.dev.js +141 -0
- package/dist/vocab-vite.cjs.js +7 -0
- package/dist/vocab-vite.cjs.prod.js +141 -0
- package/dist/vocab-vite.esm.js +116 -0
- package/package.json +54 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/create-language.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidm9jYWItdml0ZS1jcmVhdGUtbGFuZ3VhZ2UuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9kaXN0L2RlY2xhcmF0aW9ucy9zcmMvY3JlYXRlLWxhbmd1YWdlLmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var icuHandler = require('@vocab/core/icu-handler');
|
|
6
|
+
var translationFile = require('@vocab/core/translation-file');
|
|
7
|
+
|
|
8
|
+
const createLanguage = loadImport => {
|
|
9
|
+
let promiseValue;
|
|
10
|
+
let resolvedValue;
|
|
11
|
+
return {
|
|
12
|
+
getValue: locale => {
|
|
13
|
+
if (!resolvedValue) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
return icuHandler.getParsedICUMessages(resolvedValue, locale);
|
|
17
|
+
},
|
|
18
|
+
load: () => {
|
|
19
|
+
if (!promiseValue) {
|
|
20
|
+
promiseValue = loadImport();
|
|
21
|
+
promiseValue.then(value => {
|
|
22
|
+
resolvedValue = value.default;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return promiseValue;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(exports, 'createTranslationFile', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return translationFile.createTranslationFile; }
|
|
33
|
+
});
|
|
34
|
+
exports.createLanguage = createLanguage;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var icuHandler = require('@vocab/core/icu-handler');
|
|
6
|
+
var translationFile = require('@vocab/core/translation-file');
|
|
7
|
+
|
|
8
|
+
const createLanguage = loadImport => {
|
|
9
|
+
let promiseValue;
|
|
10
|
+
let resolvedValue;
|
|
11
|
+
return {
|
|
12
|
+
getValue: locale => {
|
|
13
|
+
if (!resolvedValue) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
return icuHandler.getParsedICUMessages(resolvedValue, locale);
|
|
17
|
+
},
|
|
18
|
+
load: () => {
|
|
19
|
+
if (!promiseValue) {
|
|
20
|
+
promiseValue = loadImport();
|
|
21
|
+
promiseValue.then(value => {
|
|
22
|
+
resolvedValue = value.default;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return promiseValue;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
Object.defineProperty(exports, 'createTranslationFile', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return translationFile.createTranslationFile; }
|
|
33
|
+
});
|
|
34
|
+
exports.createLanguage = createLanguage;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getParsedICUMessages } from '@vocab/core/icu-handler';
|
|
2
|
+
export { createTranslationFile } from '@vocab/core/translation-file';
|
|
3
|
+
|
|
4
|
+
const createLanguage = loadImport => {
|
|
5
|
+
let promiseValue;
|
|
6
|
+
let resolvedValue;
|
|
7
|
+
return {
|
|
8
|
+
getValue: locale => {
|
|
9
|
+
if (!resolvedValue) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return getParsedICUMessages(resolvedValue, locale);
|
|
13
|
+
},
|
|
14
|
+
load: () => {
|
|
15
|
+
if (!promiseValue) {
|
|
16
|
+
promiseValue = loadImport();
|
|
17
|
+
promiseValue.then(value => {
|
|
18
|
+
resolvedValue = value.default;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return promiseValue;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { createLanguage };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/create-vocab-chunks.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidm9jYWItdml0ZS1jcmVhdGUtdm9jYWItY2h1bmtzLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vZGlzdC9kZWNsYXJhdGlvbnMvc3JjL2NyZWF0ZS12b2NhYi1jaHVua3MuZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var logger = require('../../dist/logger-3e3dccee.cjs.dev.js');
|
|
6
|
+
require('picocolors');
|
|
7
|
+
require('debug');
|
|
8
|
+
|
|
9
|
+
function getChunkName(lang) {
|
|
10
|
+
return `${lang}-translations`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const trace = logger.trace.extend('create-vocab-chunks');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets vocab virtual module details and creates chunks for each language
|
|
17
|
+
*/
|
|
18
|
+
const createVocabChunks = (id, {
|
|
19
|
+
getModuleInfo
|
|
20
|
+
}) => {
|
|
21
|
+
const match = /virtual:vocab-(\w+)/.exec(id);
|
|
22
|
+
if (!match) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const language = match[1];
|
|
26
|
+
const dependentEntryPoints = [];
|
|
27
|
+
const rootModuleInfo = getModuleInfo(id);
|
|
28
|
+
if (!rootModuleInfo) {
|
|
29
|
+
trace(`No module info found for ${id}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const idsToHandle = new Set(rootModuleInfo.dynamicImporters);
|
|
33
|
+
for (const moduleId of idsToHandle) {
|
|
34
|
+
const moduleInfo = getModuleInfo(moduleId);
|
|
35
|
+
if (!moduleInfo) {
|
|
36
|
+
trace(`No module info found for ${moduleId}`);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const {
|
|
40
|
+
isEntry,
|
|
41
|
+
dynamicImporters,
|
|
42
|
+
importers
|
|
43
|
+
} = moduleInfo;
|
|
44
|
+
if (isEntry || dynamicImporters.length > 0) {
|
|
45
|
+
dependentEntryPoints.push(moduleId);
|
|
46
|
+
}
|
|
47
|
+
for (const importerId of importers) idsToHandle.add(importerId);
|
|
48
|
+
}
|
|
49
|
+
if (dependentEntryPoints.length > 0) {
|
|
50
|
+
return getChunkName(language);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
exports.createVocabChunks = createVocabChunks;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var logger = require('../../dist/logger-0d92b2c5.cjs.prod.js');
|
|
6
|
+
require('picocolors');
|
|
7
|
+
require('debug');
|
|
8
|
+
|
|
9
|
+
function getChunkName(lang) {
|
|
10
|
+
return `${lang}-translations`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const trace = logger.trace.extend('create-vocab-chunks');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets vocab virtual module details and creates chunks for each language
|
|
17
|
+
*/
|
|
18
|
+
const createVocabChunks = (id, {
|
|
19
|
+
getModuleInfo
|
|
20
|
+
}) => {
|
|
21
|
+
const match = /virtual:vocab-(\w+)/.exec(id);
|
|
22
|
+
if (!match) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const language = match[1];
|
|
26
|
+
const dependentEntryPoints = [];
|
|
27
|
+
const rootModuleInfo = getModuleInfo(id);
|
|
28
|
+
if (!rootModuleInfo) {
|
|
29
|
+
trace(`No module info found for ${id}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const idsToHandle = new Set(rootModuleInfo.dynamicImporters);
|
|
33
|
+
for (const moduleId of idsToHandle) {
|
|
34
|
+
const moduleInfo = getModuleInfo(moduleId);
|
|
35
|
+
if (!moduleInfo) {
|
|
36
|
+
trace(`No module info found for ${moduleId}`);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const {
|
|
40
|
+
isEntry,
|
|
41
|
+
dynamicImporters,
|
|
42
|
+
importers
|
|
43
|
+
} = moduleInfo;
|
|
44
|
+
if (isEntry || dynamicImporters.length > 0) {
|
|
45
|
+
dependentEntryPoints.push(moduleId);
|
|
46
|
+
}
|
|
47
|
+
for (const importerId of importers) idsToHandle.add(importerId);
|
|
48
|
+
}
|
|
49
|
+
if (dependentEntryPoints.length > 0) {
|
|
50
|
+
return getChunkName(language);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
exports.createVocabChunks = createVocabChunks;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { t as trace$1 } from '../../dist/logger-e3a4b043.esm.js';
|
|
2
|
+
import 'picocolors';
|
|
3
|
+
import 'debug';
|
|
4
|
+
|
|
5
|
+
function getChunkName(lang) {
|
|
6
|
+
return `${lang}-translations`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const trace = trace$1.extend('create-vocab-chunks');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Gets vocab virtual module details and creates chunks for each language
|
|
13
|
+
*/
|
|
14
|
+
const createVocabChunks = (id, {
|
|
15
|
+
getModuleInfo
|
|
16
|
+
}) => {
|
|
17
|
+
const match = /virtual:vocab-(\w+)/.exec(id);
|
|
18
|
+
if (!match) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const language = match[1];
|
|
22
|
+
const dependentEntryPoints = [];
|
|
23
|
+
const rootModuleInfo = getModuleInfo(id);
|
|
24
|
+
if (!rootModuleInfo) {
|
|
25
|
+
trace(`No module info found for ${id}`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const idsToHandle = new Set(rootModuleInfo.dynamicImporters);
|
|
29
|
+
for (const moduleId of idsToHandle) {
|
|
30
|
+
const moduleInfo = getModuleInfo(moduleId);
|
|
31
|
+
if (!moduleInfo) {
|
|
32
|
+
trace(`No module info found for ${moduleId}`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const {
|
|
36
|
+
isEntry,
|
|
37
|
+
dynamicImporters,
|
|
38
|
+
importers
|
|
39
|
+
} = moduleInfo;
|
|
40
|
+
if (isEntry || dynamicImporters.length > 0) {
|
|
41
|
+
dependentEntryPoints.push(moduleId);
|
|
42
|
+
}
|
|
43
|
+
for (const importerId of importers) idsToHandle.add(importerId);
|
|
44
|
+
}
|
|
45
|
+
if (dependentEntryPoints.length > 0) {
|
|
46
|
+
return getChunkName(language);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { createVocabChunks };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('picocolors');
|
|
4
|
+
var debug = require('debug');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var debug__default = /*#__PURE__*/_interopDefault(debug);
|
|
9
|
+
|
|
10
|
+
const trace = debug__default["default"](`vocab:vite`);
|
|
11
|
+
|
|
12
|
+
exports.trace = trace;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('picocolors');
|
|
4
|
+
var debug = require('debug');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
7
|
+
|
|
8
|
+
var debug__default = /*#__PURE__*/_interopDefault(debug);
|
|
9
|
+
|
|
10
|
+
const trace = debug__default["default"](`vocab:vite`);
|
|
11
|
+
|
|
12
|
+
exports.trace = trace;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
export { default } from "./declarations/src/index.js";
|
|
3
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidm9jYWItdml0ZS5janMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9pbmRleC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@vocab/core');
|
|
6
|
+
var esModuleLexer = require('es-module-lexer');
|
|
7
|
+
var cjsModuleLexer = require('cjs-module-lexer');
|
|
8
|
+
var logger = require('./logger-3e3dccee.cjs.dev.js');
|
|
9
|
+
require('picocolors');
|
|
10
|
+
require('debug');
|
|
11
|
+
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var esModuleLexer__namespace = /*#__PURE__*/_interopNamespace(esModuleLexer);
|
|
31
|
+
var cjsModuleLexer__namespace = /*#__PURE__*/_interopNamespace(cjsModuleLexer);
|
|
32
|
+
|
|
33
|
+
const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
|
|
34
|
+
const virtualModuleId = 'virtual:vocab';
|
|
35
|
+
const sourceQueryKey = '?source=';
|
|
36
|
+
|
|
37
|
+
const trace = logger.trace.extend('transform');
|
|
38
|
+
function findExportNames(source, mode) {
|
|
39
|
+
if (mode === 'esm') {
|
|
40
|
+
const [, exports] = esModuleLexer__namespace.parse(source);
|
|
41
|
+
return exports;
|
|
42
|
+
}
|
|
43
|
+
const {
|
|
44
|
+
exports
|
|
45
|
+
} = cjsModuleLexer__namespace.parse(source);
|
|
46
|
+
return exports;
|
|
47
|
+
}
|
|
48
|
+
const transformVocabFile = async (code, id, config) => {
|
|
49
|
+
trace('Transforming vocab file', id);
|
|
50
|
+
let result = code;
|
|
51
|
+
const devJsonFilePath = core.getDevLanguageFileFromTsFile(id);
|
|
52
|
+
const loadedTranslation = core.loadTranslation({
|
|
53
|
+
filePath: devJsonFilePath,
|
|
54
|
+
fallbacks: 'all'
|
|
55
|
+
}, config);
|
|
56
|
+
const renderLanguageLoader = renderLanguageLoaderAsync(loadedTranslation);
|
|
57
|
+
const translations = /* ts */`
|
|
58
|
+
const translations = createTranslationFile({
|
|
59
|
+
${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
|
|
60
|
+
});
|
|
61
|
+
`;
|
|
62
|
+
await esModuleLexer__namespace.init;
|
|
63
|
+
const esmExports = findExportNames(code, 'esm');
|
|
64
|
+
if (esmExports.length > 0) {
|
|
65
|
+
const exportName = esmExports[0];
|
|
66
|
+
trace(`Found ESM export '${exportName.n}' in ${id}`);
|
|
67
|
+
result = /* ts */`
|
|
68
|
+
import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
|
|
69
|
+
${translations}
|
|
70
|
+
export { translations as ${exportName.n} };
|
|
71
|
+
`;
|
|
72
|
+
} else {
|
|
73
|
+
// init needs to be called and waited upon
|
|
74
|
+
await cjsModuleLexer__namespace.init();
|
|
75
|
+
const exportName = findExportNames(code, 'cjs')[0];
|
|
76
|
+
trace(`Found CJS export '${exportName}' in ${id}`);
|
|
77
|
+
result = /* ts */`
|
|
78
|
+
import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
|
|
79
|
+
${translations}
|
|
80
|
+
exports.${exportName} = translations;
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
trace('Created translation file', result);
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
const renderLanguageLoaderAsync = loadedTranslation => lang => {
|
|
87
|
+
const identifier = JSON.stringify(createIdentifier(lang, loadedTranslation));
|
|
88
|
+
return /* ts */`createLanguage(() => import(${identifier}))`.trim();
|
|
89
|
+
};
|
|
90
|
+
const createIdentifier = (lang, loadedTranslation) => {
|
|
91
|
+
var _loadedTranslation$la;
|
|
92
|
+
const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
93
|
+
const langJson = {};
|
|
94
|
+
for (const key of loadedTranslation.keys) {
|
|
95
|
+
langJson[key] = languageTranslations[key].message;
|
|
96
|
+
}
|
|
97
|
+
const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
|
|
98
|
+
const encodedResource = `${sourceQueryKey}${base64}`;
|
|
99
|
+
return `${virtualModuleId}-${lang}.json${encodedResource}`;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const virtualResourceLoader = path => Buffer.from(path.split(sourceQueryKey)[1], 'base64').toString('utf-8');
|
|
103
|
+
|
|
104
|
+
function vitePluginVocab({
|
|
105
|
+
vocabConfig
|
|
106
|
+
}) {
|
|
107
|
+
let isSSR = false;
|
|
108
|
+
logger.trace(`Creating Vocab plugin${vocabConfig ? ` with config file ${vocabConfig}` : ''}`);
|
|
109
|
+
return {
|
|
110
|
+
name: 'vite-plugin-vocab',
|
|
111
|
+
apply: 'build',
|
|
112
|
+
configResolved(config) {
|
|
113
|
+
// Check if the build is for SSR. This plugin should not run for SSR builds.
|
|
114
|
+
isSSR = Boolean(config.build.ssr);
|
|
115
|
+
},
|
|
116
|
+
resolveId(id) {
|
|
117
|
+
if (isSSR) return null;
|
|
118
|
+
if (id.includes(virtualModuleId)) {
|
|
119
|
+
return `\0${id}`;
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
load(id) {
|
|
123
|
+
if (isSSR) return null;
|
|
124
|
+
if (id.includes(`\0${virtualModuleId}`)) {
|
|
125
|
+
return virtualResourceLoader(id);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
async transform(code, id) {
|
|
129
|
+
if (isSSR) return null;
|
|
130
|
+
if (compiledVocabFileFilter.test(id)) {
|
|
131
|
+
const transformedCode = await transformVocabFile(code, id, vocabConfig);
|
|
132
|
+
return {
|
|
133
|
+
code: transformedCode,
|
|
134
|
+
map: null // provide source map if available
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
exports["default"] = vitePluginVocab;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@vocab/core');
|
|
6
|
+
var esModuleLexer = require('es-module-lexer');
|
|
7
|
+
var cjsModuleLexer = require('cjs-module-lexer');
|
|
8
|
+
var logger = require('./logger-0d92b2c5.cjs.prod.js');
|
|
9
|
+
require('picocolors');
|
|
10
|
+
require('debug');
|
|
11
|
+
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var esModuleLexer__namespace = /*#__PURE__*/_interopNamespace(esModuleLexer);
|
|
31
|
+
var cjsModuleLexer__namespace = /*#__PURE__*/_interopNamespace(cjsModuleLexer);
|
|
32
|
+
|
|
33
|
+
const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
|
|
34
|
+
const virtualModuleId = 'virtual:vocab';
|
|
35
|
+
const sourceQueryKey = '?source=';
|
|
36
|
+
|
|
37
|
+
const trace = logger.trace.extend('transform');
|
|
38
|
+
function findExportNames(source, mode) {
|
|
39
|
+
if (mode === 'esm') {
|
|
40
|
+
const [, exports] = esModuleLexer__namespace.parse(source);
|
|
41
|
+
return exports;
|
|
42
|
+
}
|
|
43
|
+
const {
|
|
44
|
+
exports
|
|
45
|
+
} = cjsModuleLexer__namespace.parse(source);
|
|
46
|
+
return exports;
|
|
47
|
+
}
|
|
48
|
+
const transformVocabFile = async (code, id, config) => {
|
|
49
|
+
trace('Transforming vocab file', id);
|
|
50
|
+
let result = code;
|
|
51
|
+
const devJsonFilePath = core.getDevLanguageFileFromTsFile(id);
|
|
52
|
+
const loadedTranslation = core.loadTranslation({
|
|
53
|
+
filePath: devJsonFilePath,
|
|
54
|
+
fallbacks: 'all'
|
|
55
|
+
}, config);
|
|
56
|
+
const renderLanguageLoader = renderLanguageLoaderAsync(loadedTranslation);
|
|
57
|
+
const translations = /* ts */`
|
|
58
|
+
const translations = createTranslationFile({
|
|
59
|
+
${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
|
|
60
|
+
});
|
|
61
|
+
`;
|
|
62
|
+
await esModuleLexer__namespace.init;
|
|
63
|
+
const esmExports = findExportNames(code, 'esm');
|
|
64
|
+
if (esmExports.length > 0) {
|
|
65
|
+
const exportName = esmExports[0];
|
|
66
|
+
trace(`Found ESM export '${exportName.n}' in ${id}`);
|
|
67
|
+
result = /* ts */`
|
|
68
|
+
import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
|
|
69
|
+
${translations}
|
|
70
|
+
export { translations as ${exportName.n} };
|
|
71
|
+
`;
|
|
72
|
+
} else {
|
|
73
|
+
// init needs to be called and waited upon
|
|
74
|
+
await cjsModuleLexer__namespace.init();
|
|
75
|
+
const exportName = findExportNames(code, 'cjs')[0];
|
|
76
|
+
trace(`Found CJS export '${exportName}' in ${id}`);
|
|
77
|
+
result = /* ts */`
|
|
78
|
+
import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
|
|
79
|
+
${translations}
|
|
80
|
+
exports.${exportName} = translations;
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
trace('Created translation file', result);
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
const renderLanguageLoaderAsync = loadedTranslation => lang => {
|
|
87
|
+
const identifier = JSON.stringify(createIdentifier(lang, loadedTranslation));
|
|
88
|
+
return /* ts */`createLanguage(() => import(${identifier}))`.trim();
|
|
89
|
+
};
|
|
90
|
+
const createIdentifier = (lang, loadedTranslation) => {
|
|
91
|
+
var _loadedTranslation$la;
|
|
92
|
+
const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
93
|
+
const langJson = {};
|
|
94
|
+
for (const key of loadedTranslation.keys) {
|
|
95
|
+
langJson[key] = languageTranslations[key].message;
|
|
96
|
+
}
|
|
97
|
+
const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
|
|
98
|
+
const encodedResource = `${sourceQueryKey}${base64}`;
|
|
99
|
+
return `${virtualModuleId}-${lang}.json${encodedResource}`;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const virtualResourceLoader = path => Buffer.from(path.split(sourceQueryKey)[1], 'base64').toString('utf-8');
|
|
103
|
+
|
|
104
|
+
function vitePluginVocab({
|
|
105
|
+
vocabConfig
|
|
106
|
+
}) {
|
|
107
|
+
let isSSR = false;
|
|
108
|
+
logger.trace(`Creating Vocab plugin${vocabConfig ? ` with config file ${vocabConfig}` : ''}`);
|
|
109
|
+
return {
|
|
110
|
+
name: 'vite-plugin-vocab',
|
|
111
|
+
apply: 'build',
|
|
112
|
+
configResolved(config) {
|
|
113
|
+
// Check if the build is for SSR. This plugin should not run for SSR builds.
|
|
114
|
+
isSSR = Boolean(config.build.ssr);
|
|
115
|
+
},
|
|
116
|
+
resolveId(id) {
|
|
117
|
+
if (isSSR) return null;
|
|
118
|
+
if (id.includes(virtualModuleId)) {
|
|
119
|
+
return `\0${id}`;
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
load(id) {
|
|
123
|
+
if (isSSR) return null;
|
|
124
|
+
if (id.includes(`\0${virtualModuleId}`)) {
|
|
125
|
+
return virtualResourceLoader(id);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
async transform(code, id) {
|
|
129
|
+
if (isSSR) return null;
|
|
130
|
+
if (compiledVocabFileFilter.test(id)) {
|
|
131
|
+
const transformedCode = await transformVocabFile(code, id, vocabConfig);
|
|
132
|
+
return {
|
|
133
|
+
code: transformedCode,
|
|
134
|
+
map: null // provide source map if available
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
exports["default"] = vitePluginVocab;
|