Compare commits

...

10 commits

Author SHA1 Message Date
Simon Haugen
5baa976dbb 1.0.2 2021-05-05 20:54:49 +02:00
Simon Haugen
047dbdeb3b fix: removed postinstall script 2021-05-05 20:53:34 +02:00
Simon Haugen
b44e53bb19 1.0.1 2021-04-22 22:55:14 +02:00
Simon Haugen
6bb34f2cbd added tests 2021-04-22 22:52:14 +02:00
Simon Haugen
7d7bea7625 1.0.0 2021-04-22 18:53:22 +02:00
Simon Haugen
6deaebf24e 0.0.5 2021-04-22 18:10:42 +02:00
Simon Haugen
b4ab908ba2 fix: parse error on define 2021-04-22 18:09:45 +02:00
Simon Haugen
15e91251ac 0.0.4 2021-02-13 14:35:03 +01:00
Simon Haugen
0f6d40c631 use "files" to only ship necessary files 2021-02-13 14:34:52 +01:00
Simon Haugen
9ad31d8d3f 0.0.3 2021-02-13 14:29:21 +01:00
19 changed files with 14464 additions and 1623 deletions

View file

@ -1,5 +0,0 @@
example
src
package-lock.json
tsconfig.json
.prettierrc

47
__tests__/version.js Normal file
View 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');
});
});

1503
demo/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -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:.."
}
}

View 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;

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -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 />

View file

@ -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

File diff suppressed because it is too large Load diff

4
jest.config.js Normal file
View file

@ -0,0 +1,4 @@
// jest.config.js
module.exports = {
testTimeout: 10000,
};

13062
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -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"

View file

@ -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;
}