Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/servicePlugin/runCommand.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import serverUrl from '../utils/serverUrl';
import proxyPaths from '../utils/proxyPaths';
import logSuccessLunch from '../utils/logSuccessLaunch';
import server from '../server';

Expand Down Expand Up @@ -29,6 +30,7 @@ export default ({

if (shouldServeApp && !isInProduction) {
serverUrl.writeToFile(localUrl);
proxyPaths.writeToFile(routes);
}

logSuccessLunch({
Expand Down
2 changes: 2 additions & 0 deletions src/servicePlugin/serveCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk';
import nodemon from 'nodemon';
import { commandOptionsDefaults } from '../config';
import serverUrl from '../utils/serverUrl';
import proxyPaths from '../utils/proxyPaths';

export default ({
srvPath,
Expand Down Expand Up @@ -35,6 +36,7 @@ export default ({
nodemon.on('quit', () => {
resolve();
serverUrl.deleteFile();
proxyPaths.deleteFile();
process.exit();
});
});
Expand Down
22 changes: 20 additions & 2 deletions src/servicePlugin/webpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import serverUrl from '../utils/serverUrl';
import proxyPaths from '../utils/proxyPaths';

import { done, warn } from '@vue/cli-shared-utils';

export default ({ devServer }) => {
if (!serverUrl.isSet()) {
if (!serverUrl.isSet() && !proxyPaths.isSet()) {
warn('You have to launch the express server before ' +
'if you want to use relative path in your code!', 'Express server');
} else {
devServer.proxy(serverUrl.getFromFile());
const url = serverUrl.getFromFile();
const routes = proxyPaths.getFromFile();

let proxyConfig = {
'/socket.io': {
target: url,
ws: true,
},
};

for (const route of routes) {
proxyConfig[route.path] = {
target: url,
};
}

devServer.proxy(proxyConfig);
done('Fallback will be done on your server. You can use relative calls to it in your code.', 'Express server');
}
};
21 changes: 21 additions & 0 deletions src/utils/proxyPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';
import path from 'path';

const tmpPath = path.resolve(__dirname, '.tmpProxyPaths');

export default {
isSet () {
return fs.existsSync(tmpPath);
},
getFromFile () {
return JSON.parse(fs.readFileSync(tmpPath, 'utf8'));
},
writeToFile (routes) {
fs.writeFileSync(tmpPath, JSON.stringify(routes));
},
deleteFile () {
if (this.isSet()) {
fs.unlinkSync(tmpPath);
}
},
};