-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding vite vocab plugin package (#283)
- Loading branch information
1 parent
3a3ab6f
commit 3af37f0
Showing
30 changed files
with
791 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { defineConfig } from 'vite'; | ||
import vitePluginVocab from '@vocab/vite'; | ||
import { createVocabChunks } from '@vocab/vite/create-vocab-chunks'; | ||
import vocabConfig from './vocab.config.cjs'; | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
vitePluginVocab({ | ||
vocabConfig, | ||
}), | ||
], | ||
build: { | ||
rollupOptions: { | ||
output: { | ||
chunkFileNames: '[name].js', | ||
manualChunks: (id, ctx) => { | ||
const vocabChunk = createVocabChunks(id, ctx); | ||
if (vocabChunk) { | ||
return vocabChunk; | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"main": "dist/vocab-vite-create-language.cjs.js", | ||
"module": "dist/vocab-vite-create-language.esm.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"main": "dist/vocab-vite-create-vocab-chunks.cjs.js", | ||
"module": "dist/vocab-vite-create-vocab-chunks.esm.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@vocab/vite", | ||
"version": "0.0.1", | ||
"author": "SEEK", | ||
"license": "MIT", | ||
"private": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/seek-oss/vocab.git", | ||
"directory": "packages/vite" | ||
}, | ||
"main": "dist/vocab-vite.cjs.js", | ||
"module": "dist/vocab-vite.esm.js", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"module": "./dist/vocab-vite.esm.js", | ||
"default": "./dist/vocab-vite.cjs.js" | ||
}, | ||
"./create-language": { | ||
"module": "./create-language/dist/vocab-vite-create-language.esm.js", | ||
"default": "./create-language/dist/vocab-vite-create-language.cjs.js" | ||
}, | ||
"./create-vocab-chunks": { | ||
"module": "./create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.esm.js", | ||
"default": "./create-vocab-chunks/dist/vocab-vite-create-vocab-chunks.cjs.js" | ||
} | ||
}, | ||
"preconstruct": { | ||
"entrypoints": [ | ||
"index.ts", | ||
"create-language.ts", | ||
"create-vocab-chunks.ts" | ||
] | ||
}, | ||
"files": [ | ||
"dist", | ||
"create-language", | ||
"create-vocab-chunks" | ||
], | ||
"dependencies": { | ||
"@vocab/core": "workspace:^", | ||
"cjs-module-lexer": "^1.2.2", | ||
"debug": "^4.3.1", | ||
"es-module-lexer": "^1.0.0", | ||
"picocolors": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "^4.1.5", | ||
"vite": "^5.4.8" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^5.4.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const compiledVocabFileFilter = /\.vocab[\\/]index\.(?:ts|js|cjs|mjs)$/; | ||
export const virtualModuleId = 'virtual:vocab'; | ||
export const sourceQueryKey = '?source='; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { TranslationModule } from '@vocab/core'; | ||
import { getParsedICUMessages } from '@vocab/core/icu-handler'; | ||
|
||
export { createTranslationFile } from '@vocab/core/translation-file'; | ||
|
||
export const createLanguage = ( | ||
loadImport: () => Promise<any>, | ||
): TranslationModule<any> => { | ||
let promiseValue: Promise<any>; | ||
let resolvedValue: any; | ||
|
||
return { | ||
getValue: (locale) => { | ||
if (!resolvedValue) { | ||
return undefined; | ||
} | ||
return getParsedICUMessages(resolvedValue, locale); | ||
}, | ||
load: () => { | ||
if (!promiseValue) { | ||
promiseValue = loadImport(); | ||
promiseValue.then((value) => { | ||
resolvedValue = value.default; | ||
}); | ||
} | ||
return promiseValue; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Rollup } from 'vite'; | ||
import { trace as _trace } from './logger'; | ||
|
||
const trace = _trace.extend('create-vocab-chunks'); | ||
|
||
export const createVocabChunks = ( | ||
id: string, | ||
{ getModuleInfo }: Rollup.ManualChunkMeta, | ||
) => { | ||
const match = /(\w+)-virtual:vocab/.exec(id); | ||
if (!match) { | ||
return; | ||
} | ||
|
||
const language = match[1]; | ||
const dependentEntryPoints: string[] = []; | ||
|
||
const rootModuleInfo = getModuleInfo(id); | ||
|
||
if (!rootModuleInfo) { | ||
trace(`No module info found for ${id}`); | ||
} | ||
|
||
const idsToHandle = new Set<string>(getModuleInfo(id)?.dynamicImporters); | ||
|
||
for (const moduleId of idsToHandle) { | ||
const moduleInfo = getModuleInfo(moduleId); | ||
if (!moduleInfo) { | ||
trace(`No module info found for ${moduleId}`); | ||
continue; | ||
} | ||
|
||
const { isEntry, dynamicImporters, importers } = moduleInfo; | ||
|
||
if (isEntry || dynamicImporters.length > 0) { | ||
dependentEntryPoints.push(moduleId); | ||
} | ||
|
||
for (const importerId of importers) idsToHandle.add(importerId); | ||
} | ||
|
||
if (dependentEntryPoints.length > 0) { | ||
return `${language}-translations`; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Plugin as VitePlugin } from 'vite'; | ||
import type { UserConfig } from '@vocab/core'; | ||
|
||
import { transformVocabFile } from './transform-vocab-file'; | ||
import { virtualResourceLoader } from './virtual-resource-loader'; | ||
|
||
import { trace } from './logger'; | ||
|
||
import { compiledVocabFileFilter, virtualModuleId } from './consts'; | ||
|
||
export type VocabPluginOptions = { | ||
vocabConfig: UserConfig; | ||
}; | ||
|
||
export default function vitePluginVocab({ | ||
vocabConfig, | ||
}: VocabPluginOptions): VitePlugin { | ||
trace( | ||
`Creating Vocab plugin${ | ||
vocabConfig ? ` with config file ${vocabConfig}` : '' | ||
}`, | ||
); | ||
return { | ||
name: 'vite-plugin-vocab', | ||
resolveId(id) { | ||
if (id.includes(virtualModuleId)) { | ||
return `\0${id}`; | ||
} | ||
}, | ||
load(id) { | ||
if (id.includes(virtualModuleId)) { | ||
return virtualResourceLoader(id); | ||
} | ||
}, | ||
async transform(code, id) { | ||
if (compiledVocabFileFilter.test(id)) { | ||
const transformedCode = await transformVocabFile(code, id, vocabConfig); | ||
return { | ||
code: transformedCode, | ||
map: null, // provide source map if available | ||
}; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pc from 'picocolors'; | ||
import debug from 'debug'; | ||
|
||
export const trace = debug(`vocab:vite`); | ||
|
||
export const log = (...params: unknown[]) => { | ||
// eslint-disable-next-line no-console | ||
console.log(pc.yellow('Vocab'), ...params); | ||
}; |
Oops, something went wrong.