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

32 lines
750 B
TypeScript
Raw 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) {
return {
define: {
['import.meta.env.PACKAGE_VERSION']:
process.env.npm_package_version,
},
};
} 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;