@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.
@@ -0,0 +1,116 @@
1
+ import { getDevLanguageFileFromTsFile, loadTranslation } from '@vocab/core';
2
+ import * as esModuleLexer from 'es-module-lexer';
3
+ import * as cjsModuleLexer from 'cjs-module-lexer';
4
+ import { t as trace$1 } from './logger-e3a4b043.esm.js';
5
+ import 'picocolors';
6
+ import 'debug';
7
+
8
+ const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/;
9
+ const virtualModuleId = 'virtual:vocab';
10
+ const sourceQueryKey = '?source=';
11
+
12
+ const trace = trace$1.extend('transform');
13
+ function findExportNames(source, mode) {
14
+ if (mode === 'esm') {
15
+ const [, exports] = esModuleLexer.parse(source);
16
+ return exports;
17
+ }
18
+ const {
19
+ exports
20
+ } = cjsModuleLexer.parse(source);
21
+ return exports;
22
+ }
23
+ const transformVocabFile = async (code, id, config) => {
24
+ trace('Transforming vocab file', id);
25
+ let result = code;
26
+ const devJsonFilePath = getDevLanguageFileFromTsFile(id);
27
+ const loadedTranslation = loadTranslation({
28
+ filePath: devJsonFilePath,
29
+ fallbacks: 'all'
30
+ }, config);
31
+ const renderLanguageLoader = renderLanguageLoaderAsync(loadedTranslation);
32
+ const translations = /* ts */`
33
+ const translations = createTranslationFile({
34
+ ${Object.keys(loadedTranslation.languages).map(lang => `${JSON.stringify(lang)}: ${renderLanguageLoader(lang)}`).join(',\n')}
35
+ });
36
+ `;
37
+ await esModuleLexer.init;
38
+ const esmExports = findExportNames(code, 'esm');
39
+ if (esmExports.length > 0) {
40
+ const exportName = esmExports[0];
41
+ trace(`Found ESM export '${exportName.n}' in ${id}`);
42
+ result = /* ts */`
43
+ import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
44
+ ${translations}
45
+ export { translations as ${exportName.n} };
46
+ `;
47
+ } else {
48
+ // init needs to be called and waited upon
49
+ await cjsModuleLexer.init();
50
+ const exportName = findExportNames(code, 'cjs')[0];
51
+ trace(`Found CJS export '${exportName}' in ${id}`);
52
+ result = /* ts */`
53
+ import { createLanguage, createTranslationFile } from '@vocab/vite/create-language';
54
+ ${translations}
55
+ exports.${exportName} = translations;
56
+ `;
57
+ }
58
+ trace('Created translation file', result);
59
+ return result;
60
+ };
61
+ const renderLanguageLoaderAsync = loadedTranslation => lang => {
62
+ const identifier = JSON.stringify(createIdentifier(lang, loadedTranslation));
63
+ return /* ts */`createLanguage(() => import(${identifier}))`.trim();
64
+ };
65
+ const createIdentifier = (lang, loadedTranslation) => {
66
+ var _loadedTranslation$la;
67
+ const languageTranslations = (_loadedTranslation$la = loadedTranslation.languages[lang]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
68
+ const langJson = {};
69
+ for (const key of loadedTranslation.keys) {
70
+ langJson[key] = languageTranslations[key].message;
71
+ }
72
+ const base64 = Buffer.from(JSON.stringify(langJson), 'utf-8').toString('base64');
73
+ const encodedResource = `${sourceQueryKey}${base64}`;
74
+ return `${virtualModuleId}-${lang}.json${encodedResource}`;
75
+ };
76
+
77
+ const virtualResourceLoader = path => Buffer.from(path.split(sourceQueryKey)[1], 'base64').toString('utf-8');
78
+
79
+ function vitePluginVocab({
80
+ vocabConfig
81
+ }) {
82
+ let isSSR = false;
83
+ trace$1(`Creating Vocab plugin${vocabConfig ? ` with config file ${vocabConfig}` : ''}`);
84
+ return {
85
+ name: 'vite-plugin-vocab',
86
+ apply: 'build',
87
+ configResolved(config) {
88
+ // Check if the build is for SSR. This plugin should not run for SSR builds.
89
+ isSSR = Boolean(config.build.ssr);
90
+ },
91
+ resolveId(id) {
92
+ if (isSSR) return null;
93
+ if (id.includes(virtualModuleId)) {
94
+ return `\0${id}`;
95
+ }
96
+ },
97
+ load(id) {
98
+ if (isSSR) return null;
99
+ if (id.includes(`\0${virtualModuleId}`)) {
100
+ return virtualResourceLoader(id);
101
+ }
102
+ },
103
+ async transform(code, id) {
104
+ if (isSSR) return null;
105
+ if (compiledVocabFileFilter.test(id)) {
106
+ const transformedCode = await transformVocabFile(code, id, vocabConfig);
107
+ return {
108
+ code: transformedCode,
109
+ map: null // provide source map if available
110
+ };
111
+ }
112
+ }
113
+ };
114
+ }
115
+
116
+ export { vitePluginVocab as default };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@vocab/vite",
3
+ "version": "0.1.1",
4
+ "author": "SEEK",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/seek-oss/vocab.git",
9
+ "directory": "packages/vite"
10
+ },
11
+ "main": "dist/vocab-vite.cjs.js",
12
+ "module": "dist/vocab-vite.esm.js",
13
+ "exports": {
14
+ "./package.json": "./package.json",
15
+ ".": {
16
+ "module": "./dist/vocab-vite.esm.js",
17
+ "default": "./dist/vocab-vite.cjs.js"
18
+ },
19
+ "./create-language": {
20
+ "module": "./create-language/dist/vocab-vite-create-language.esm.js",
21
+ "default": "./create-language/dist/vocab-vite-create-language.cjs.js"
22
+ },
23
+ "./create-vocab-chunks": {
24
+ "module": "./create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.esm.js",
25
+ "default": "./create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.js"
26
+ }
27
+ },
28
+ "preconstruct": {
29
+ "entrypoints": [
30
+ "index.ts",
31
+ "create-language.ts",
32
+ "create-vocab-chunks.ts"
33
+ ]
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "create-language",
38
+ "create-vocab-chunks"
39
+ ],
40
+ "dependencies": {
41
+ "cjs-module-lexer": "^1.2.2",
42
+ "debug": "^4.3.1",
43
+ "es-module-lexer": "^1.0.0",
44
+ "picocolors": "^1.0.0",
45
+ "@vocab/core": "^1.6.3"
46
+ },
47
+ "devDependencies": {
48
+ "@types/debug": "^4.1.5",
49
+ "vite": "^5.4.8"
50
+ },
51
+ "peerDependencies": {
52
+ "vite": "^5.4.8"
53
+ }
54
+ }