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": {
|
"esbuild": {
|
||||||
"version": "0.8.44",
|
"version": "0.8.45",
|
||||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.8.44.tgz",
|
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.8.45.tgz",
|
||||||
"integrity": "sha512-m9yyBZMgWuAB7e7tA2g9L4PovoLa5Xb73+Yg9uBBR2w3Fe4P9/nxqj/HLrw1k/rjdjF1eX1kNJRytboqOtRCCQ==",
|
"integrity": "sha512-AhR+h/Kat9QMssi0rSJIei0yR+dJ0DQ5aDqnZy4VLu9kOBHdJh8vDSE2XzUlwnm4umvMFnfQTELZlsH7Zmvksw==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"escape-string-regexp": {
|
"escape-string-regexp": {
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import React from 'react'
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom';
|
||||||
import './index.css'
|
import './index.css';
|
||||||
import App from './App'
|
import App from './App';
|
||||||
|
|
||||||
|
console.log(import.meta.env);
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App />
|
<App />
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
)
|
);
|
||||||
|
|
22
src/index.ts
22
src/index.ts
|
@ -1,8 +1,30 @@
|
||||||
import type { Plugin } from 'vite';
|
import type { Plugin } from 'vite';
|
||||||
|
|
||||||
|
let envInjectionFailed = false;
|
||||||
|
|
||||||
const createPlugin = (): Plugin => {
|
const createPlugin = (): Plugin => {
|
||||||
return {
|
return {
|
||||||
name: 'vite-plugin-package-version',
|
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