vite-plugin-package-version/src/index.ts

33 lines
876 B
TypeScript
Raw Permalink Normal View History

2021-02-13 21:38:58 +11:00
import type { Plugin } from 'vite';
2021-02-13 20:24:24 +11:00
let envInjectionFailed = false;
2021-02-13 21:38:58 +11:00
const createPlugin = (): Plugin => {
2021-02-13 20:24:24 +11:00
return {
2021-02-13 20:32:50 +11:00
name: 'vite-plugin-package-version',
config: (_, env) => {
if (env) {
2021-04-23 02:09:45 +10:00
const key = 'import.meta.env.PACKAGE_VERSION';
const val = JSON.stringify(process.env.npm_package_version);
2022-01-30 19:02:48 +11:00
const key2 = 'import.meta.env.BUILD_DATE';
2022-01-30 19:01:50 +11:00
const val2 = JSON.stringify(new Date());
2022-01-30 18:58:05 +11:00
return { define: { [key]: val, [key2]: val2 } };
} else {
envInjectionFailed = true;
}
},
configResolved(config) {
if (envInjectionFailed) {
config.logger.warn(
`[vite-plugin-package-version] import.meta.env.PACKAGE_VERSION was not injected due ` +
`to incompatible vite version (requires vite@^2.0.0-beta.69).`
);
}
},
2021-02-13 20:24:24 +11:00
};
};
2021-02-13 21:38:58 +11:00
export default createPlugin;