inject npm_package_version into vite env
This commit is contained in:
parent
69d3ab74dd
commit
6d7be4f908
3 changed files with 32 additions and 8 deletions
6
example/package-lock.json
generated
6
example/package-lock.json
generated
|
@ -351,9 +351,9 @@
|
|||
}
|
||||
},
|
||||
"esbuild": {
|
||||
"version": "0.8.44",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.8.44.tgz",
|
||||
"integrity": "sha512-m9yyBZMgWuAB7e7tA2g9L4PovoLa5Xb73+Yg9uBBR2w3Fe4P9/nxqj/HLrw1k/rjdjF1eX1kNJRytboqOtRCCQ==",
|
||||
"version": "0.8.45",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.8.45.tgz",
|
||||
"integrity": "sha512-AhR+h/Kat9QMssi0rSJIei0yR+dJ0DQ5aDqnZy4VLu9kOBHdJh8vDSE2XzUlwnm4umvMFnfQTELZlsH7Zmvksw==",
|
||||
"dev": true
|
||||
},
|
||||
"escape-string-regexp": {
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import './index.css'
|
||||
import App from './App'
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
|
||||
console.log(import.meta.env);
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
)
|
||||
);
|
||||
|
|
22
src/index.ts
22
src/index.ts
|
@ -1,8 +1,30 @@
|
|||
import type { Plugin } from 'vite';
|
||||
|
||||
let envInjectionFailed = false;
|
||||
|
||||
const createPlugin = (): Plugin => {
|
||||
return {
|
||||
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).`
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue