Skip to content

Commit

Permalink
fix: add working example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasan Farrokh committed Jul 1, 2021
1 parent d3e9c9f commit 4686737
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
7 changes: 5 additions & 2 deletions bin/vmd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const vmd = require('../dist/index')

console.log(vmd.default)
vmd.default.createDevServer().listen(3000)
const PORT = process.env.PORT || 3000

vmd.createDevServer().listen(PORT, () => {
console.log(`[ Vite Multi Device running on port: ${PORT} ]`)
})
2 changes: 1 addition & 1 deletion examples/react-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function App() {
<img src={logo} className="App-logo" alt="logo" />
<p>Hello Vite + React! + MultiDevice</p>
<p>
<em>current device -> { window.DEVICE.desktop ? 'desktop' : 'mobile' }</em>
<em>current device -&gt; { window.DEVICE.desktop ? 'desktop' : 'mobile' }</em>
</p>
<p>
<button onClick={() => setCount((count) => count + 1)}>
Expand Down
7 changes: 7 additions & 0 deletions examples/react-example/src/device.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {}

declare global {
interface Window {
DEVICE: { desktop: string; mobile: string }
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"homepage": "https://github.com/SasanFarrokh/vite-plugin-multi-device/tree/master/#readme",
"scripts": {
"dev": "tsup src/index.ts --external fsevents --watch",
"dev": "tsup src/index.ts --external fsevents,express,vite --watch",
"build": "tsup src/index.ts --external debug --external fsevents --dts",
"lint": "eslint --fix .",
"test": "jest",
Expand All @@ -38,5 +38,8 @@
},
"dependencies": {
"express": "^4.17.1"
},
"peerDependencies": {
"vite": "2.x.x"
}
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './multiDevicePlugin'
import multiDevice from './multiDevicePlugin'

export * from './server'

export default multiDevice
50 changes: 31 additions & 19 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ const DEVICE_MAP: DevicesMap = {
desktop: true,
}

export const deviceMiddleware = (devices: DevicesMap): RequestHandler => (req, res, next) => {
req.device = resolveDevice(req, devices);
export const deviceMiddleware = (devicesMap: DevicesMap): RequestHandler => (req, res, next) => {
req.device = resolveDevice(req, devicesMap);
next();
}

export function createDevServer (root = null, deviceMap = DEVICE_MAP): Application {
export function devMiddleware (root = null, deviceMap = DEVICE_MAP): RequestHandler[] {
const { createServer: createViteServer } = require('vite');

const app = express();

const vite = {} as Record<string, ViteDevServer>;

// const serverReady: Promise<unknown> =
Expand All @@ -36,22 +34,36 @@ export function createDevServer (root = null, deviceMap = DEVICE_MAP): Applicati
}
})();

app.use((req, res, next) => {
if (Object.keys(vite).length !== Object.keys(deviceMap).length) {
res.writeHead(503, { 'Retry-After': '5' });
return res.end(fs.readFileSync(path.resolve(__dirname, '../loading.html'), 'utf-8'));
}
next();
});
const template = fs.readFileSync('./index.html', 'utf-8');

return [
deviceMiddleware(deviceMap),
(req, res, next) => {
if (!req.device) {
return res.end('Device could not be guessed. check your configurations.')
}

app.use(deviceMiddleware(deviceMap))
if (!vite[req.device]) {
res.writeHead(503, { 'Retry-After': '5' });
return res.end(fs.readFileSync(path.resolve(__dirname, '../loading.html'), 'utf-8'));
}

app.use((req, res, next) => {
if (!req.device) {
return res.end('Device could not be guessed. check your configurationss.')
vite[req.device].middlewares.handle(req, res, next);
},
async (req, res) => {
const url = req.originalUrl;

// always read fresh template in dev
const html = await vite[req.device!].transformIndexHtml(url, template);

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
}
vite[req.device].middlewares.handle(req, res, next);
});
]
}

return app;
export function createDevServer(root = null, deviceMap = DEVICE_MAP): Application {
const app = express()
app.use(devMiddleware(root, deviceMap))
return app
}
4 changes: 4 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ declare global {
device?: string;
}
}

export interface Window {
DEVICE: { mobile: string; desktop: string; }
}
}

0 comments on commit 4686737

Please sign in to comment.