vite-version/src/index.ts

33 lines
930 B
TypeScript
Raw Permalink Normal View History

2021-02-13 21:38:58 +11:00
import type { Plugin } from 'vite';
2023-05-08 20:07:47 +10:00
import {execSync} from "child_process"
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',
2023-05-08 20:07:47 +10:00
config: async (_, env) => {
if (env) {
2023-05-08 20:07:47 +10:00
return {
define: {
['import.meta.env.TPU_VERSION']: JSON.stringify(process.env.npm_package_version),
['import.meta.env.TPU_BUILD_DATE']: JSON.stringify(new Date().toISOString()),
['import.meta.env.TPU_COMMIT']: "deez"
}
};
} else {
envInjectionFailed = true;
}
},
configResolved(config) {
if (envInjectionFailed) {
config.logger.warn(
`[vite-plugin-package-version] import.meta.env.PACKAGE_VERSION was not injected due ` +
2023-05-08 20:07:47 +10:00
`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;