Compare commits
10 commits
440d7583ab
...
5baa976dbb
Author | SHA1 | Date | |
---|---|---|---|
|
5baa976dbb | ||
|
047dbdeb3b | ||
|
b44e53bb19 | ||
|
6bb34f2cbd | ||
|
7d7bea7625 | ||
|
6deaebf24e | ||
|
b4ab908ba2 | ||
|
15e91251ac | ||
|
0f6d40c631 | ||
|
9ad31d8d3f |
19 changed files with 14464 additions and 1623 deletions
|
@ -1,5 +0,0 @@
|
|||
example
|
||||
src
|
||||
package-lock.json
|
||||
tsconfig.json
|
||||
.prettierrc
|
47
__tests__/version.js
Normal file
47
__tests__/version.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
const puppeteer = require('puppeteer');
|
||||
const { setup, teardown } = require('jest-dev-server');
|
||||
|
||||
let browser;
|
||||
let page;
|
||||
|
||||
beforeAll(async () => {
|
||||
browser = await puppeteer.launch();
|
||||
page = await browser.newPage();
|
||||
|
||||
return setup([
|
||||
{
|
||||
command: 'npm run demo:dev',
|
||||
port: 3000,
|
||||
},
|
||||
{
|
||||
command: 'npm run demo:prod',
|
||||
port: 3001,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
browser.close();
|
||||
|
||||
await teardown();
|
||||
});
|
||||
|
||||
describe('import.meta.env contains PACKAGE_VERSION', () => {
|
||||
test('in development', async () => {
|
||||
await page.goto('http://localhost:3000/');
|
||||
|
||||
const element = await page.$('#PACKAGE_VERSION');
|
||||
const version = await element.evaluate((el) => el.textContent);
|
||||
|
||||
expect(version).toBe('0.0.0');
|
||||
});
|
||||
|
||||
test('in production', async () => {
|
||||
await page.goto('http://localhost:3001/');
|
||||
|
||||
const element = await page.$('#PACKAGE_VERSION');
|
||||
const version = await element.evaluate((el) => el.textContent);
|
||||
|
||||
expect(version).toBe('0.0.0');
|
||||
});
|
||||
});
|
0
example/.gitignore → demo/.gitignore
vendored
0
example/.gitignore → demo/.gitignore
vendored
1503
demo/package-lock.json
generated
Normal file
1503
demo/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"name": "example",
|
||||
"name": "demo",
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"serve": "vite preview"
|
||||
"serve": "vite preview --port 3001"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^17.0.0",
|
||||
|
@ -15,7 +15,7 @@
|
|||
"@types/react-dom": "^17.0.0",
|
||||
"@vitejs/plugin-react-refresh": "^1.1.0",
|
||||
"typescript": "^4.1.2",
|
||||
"vite": "^2.0.0-beta.69",
|
||||
"vite": "^2.2.0",
|
||||
"vite-plugin-package-version": "file:.."
|
||||
}
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
import React, { useState } from 'react'
|
||||
import logo from './logo.svg'
|
||||
import './App.css'
|
||||
import React, { useState } from 'react';
|
||||
import logo from './logo.svg';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
const [count, setCount] = useState(0)
|
||||
const [count, setCount] = useState(0);
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<div id="PACKAGE_VERSION">{import.meta.env.PACKAGE_VERSION}</div>
|
||||
<p>Hello Vite + React!</p>
|
||||
<p>
|
||||
<button onClick={() => setCount((count) => count + 1)}>
|
||||
|
@ -39,7 +40,7 @@ function App() {
|
|||
</p>
|
||||
</header>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export default App
|
||||
export default App;
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
|
@ -3,8 +3,6 @@ import ReactDOM from 'react-dom';
|
|||
import './index.css';
|
||||
import App from './App';
|
||||
|
||||
console.log(import.meta.env);
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
|
@ -5,4 +5,8 @@ import loadVersion from 'vite-plugin-package-version';
|
|||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [reactRefresh(), loadVersion()],
|
||||
server: {
|
||||
port: 3000,
|
||||
strictPort: true,
|
||||
},
|
||||
});
|
1411
example/package-lock.json
generated
1411
example/package-lock.json
generated
File diff suppressed because it is too large
Load diff
4
jest.config.js
Normal file
4
jest.config.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
// jest.config.js
|
||||
module.exports = {
|
||||
testTimeout: 10000,
|
||||
};
|
13030
package-lock.json
generated
13030
package-lock.json
generated
File diff suppressed because it is too large
Load diff
20
package.json
20
package.json
|
@ -2,7 +2,7 @@
|
|||
"name": "vite-plugin-package-version",
|
||||
"license": "MIT",
|
||||
"description": "vite plugin to load package version into env",
|
||||
"version": "0.0.2",
|
||||
"version": "1.0.2",
|
||||
"keywords": [
|
||||
"vite",
|
||||
"typescript",
|
||||
|
@ -16,17 +16,27 @@
|
|||
"url": "git+https://github.com/smnhgn/vite-plugin-package-version.git"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"build": "npm run clean && tsup src/index.ts --dts",
|
||||
"clean": "rimraf {dist,package-lock.json,node_modules} && npm i",
|
||||
"test": "jest",
|
||||
"dev": "tsup src/index.ts --dts --watch",
|
||||
"dev:client": "npm -C example run dev",
|
||||
"prepublishOnly": "npm run build"
|
||||
"build": "rimraf dist && tsup src/index.ts --dts",
|
||||
"demo:clean": "rimraf demo/{dist,package-lock.json,node_modules} && npm -C demo install",
|
||||
"demo:dev": "npm -C demo run dev",
|
||||
"demo:prod": "npm -C demo run build && npm -C demo run serve",
|
||||
"prepublishOnly": "npm run build && npm run test"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"vite": ">=2.0.0-beta.69"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^26.6.3",
|
||||
"jest-dev-server": "^5.0.0",
|
||||
"puppeteer": "^9.0.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"tsup": "^3.12.1",
|
||||
"typescript": "^4.1.5",
|
||||
"vite": "^2.0.0-beta.69"
|
||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -7,12 +7,10 @@ const createPlugin = (): Plugin => {
|
|||
name: 'vite-plugin-package-version',
|
||||
config: (_, env) => {
|
||||
if (env) {
|
||||
return {
|
||||
define: {
|
||||
['import.meta.env.PACKAGE_VERSION']:
|
||||
process.env.npm_package_version,
|
||||
},
|
||||
};
|
||||
const key = 'import.meta.env.PACKAGE_VERSION';
|
||||
const val = JSON.stringify(process.env.npm_package_version);
|
||||
|
||||
return { define: { [key]: val } };
|
||||
} else {
|
||||
envInjectionFailed = true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue