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",
|
"version": "0.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"serve": "vite preview"
|
"serve": "vite preview --port 3001"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^17.0.0",
|
"react": "^17.0.0",
|
||||||
|
@ -15,7 +15,7 @@
|
||||||
"@types/react-dom": "^17.0.0",
|
"@types/react-dom": "^17.0.0",
|
||||||
"@vitejs/plugin-react-refresh": "^1.1.0",
|
"@vitejs/plugin-react-refresh": "^1.1.0",
|
||||||
"typescript": "^4.1.2",
|
"typescript": "^4.1.2",
|
||||||
"vite": "^2.0.0-beta.69",
|
"vite": "^2.2.0",
|
||||||
"vite-plugin-package-version": "file:.."
|
"vite-plugin-package-version": "file:.."
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,15 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react';
|
||||||
import logo from './logo.svg'
|
import logo from './logo.svg';
|
||||||
import './App.css'
|
import './App.css';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [count, setCount] = useState(0)
|
const [count, setCount] = useState(0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<header className="App-header">
|
<header className="App-header">
|
||||||
<img src={logo} className="App-logo" alt="logo" />
|
<img src={logo} className="App-logo" alt="logo" />
|
||||||
|
<div id="PACKAGE_VERSION">{import.meta.env.PACKAGE_VERSION}</div>
|
||||||
<p>Hello Vite + React!</p>
|
<p>Hello Vite + React!</p>
|
||||||
<p>
|
<p>
|
||||||
<button onClick={() => setCount((count) => count + 1)}>
|
<button onClick={() => setCount((count) => count + 1)}>
|
||||||
|
@ -39,7 +40,7 @@ function App() {
|
||||||
</p>
|
</p>
|
||||||
</header>
|
</header>
|
||||||
</div>
|
</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 './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 />
|
|
@ -5,4 +5,8 @@ import loadVersion from 'vite-plugin-package-version';
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [reactRefresh(), loadVersion()],
|
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,
|
||||||
|
};
|
13062
package-lock.json
generated
13062
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",
|
"name": "vite-plugin-package-version",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "vite plugin to load package version into env",
|
"description": "vite plugin to load package version into env",
|
||||||
"version": "0.0.2",
|
"version": "1.0.2",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"vite",
|
"vite",
|
||||||
"typescript",
|
"typescript",
|
||||||
|
@ -16,17 +16,27 @@
|
||||||
"url": "git+https://github.com/smnhgn/vite-plugin-package-version.git"
|
"url": "git+https://github.com/smnhgn/vite-plugin-package-version.git"
|
||||||
},
|
},
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"clean": "rm -rf dist",
|
"clean": "rimraf {dist,package-lock.json,node_modules} && npm i",
|
||||||
"build": "npm run clean && tsup src/index.ts --dts",
|
"test": "jest",
|
||||||
"dev": "tsup src/index.ts --dts --watch",
|
"dev": "tsup src/index.ts --dts --watch",
|
||||||
"dev:client": "npm -C example run dev",
|
"build": "rimraf dist && tsup src/index.ts --dts",
|
||||||
"prepublishOnly": "npm run build"
|
"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": {
|
"peerDependencies": {
|
||||||
"vite": ">=2.0.0-beta.69"
|
"vite": ">=2.0.0-beta.69"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"jest": "^26.6.3",
|
||||||
|
"jest-dev-server": "^5.0.0",
|
||||||
|
"puppeteer": "^9.0.0",
|
||||||
|
"rimraf": "^3.0.2",
|
||||||
"tsup": "^3.12.1",
|
"tsup": "^3.12.1",
|
||||||
"typescript": "^4.1.5",
|
"typescript": "^4.1.5",
|
||||||
"vite": "^2.0.0-beta.69"
|
"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',
|
name: 'vite-plugin-package-version',
|
||||||
config: (_, env) => {
|
config: (_, env) => {
|
||||||
if (env) {
|
if (env) {
|
||||||
return {
|
const key = 'import.meta.env.PACKAGE_VERSION';
|
||||||
define: {
|
const val = JSON.stringify(process.env.npm_package_version);
|
||||||
['import.meta.env.PACKAGE_VERSION']:
|
|
||||||
process.env.npm_package_version,
|
return { define: { [key]: val } };
|
||||||
},
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
envInjectionFailed = true;
|
envInjectionFailed = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue