Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent build error when assets/ does not exist #13

Merged
merged 1 commit into from
Jan 31, 2022
Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Instead of building your frontend on startup,
you can use a config template like the one above and populate it using `envsubst`:

```Dockerfile
CMD ["/bin/sh", "-c", "envsubst < ./dist/assets/env-config.template.js > ./dist/assets/env-config.js && exec nginx -g 'daemon off;'"]
CMD ["/bin/sh", "-c", "envsubst < ./dist/env-config.template.js > ./dist/env-config.js && exec nginx -g 'daemon off;'"]
```

`@geprog/vite-plugin-env-config` generates the required template from a list of variable names and provides the already populated file via the dev-server during development.
6 changes: 3 additions & 3 deletions src/envConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
const envConfigContent = createEnvConfigContent(userOptions.variables || [], false);

server.middlewares.use((req, res, next) => {
if (req.url === '/assets/env-config.js') {
if (req.url === '/env-config.js') {
// standard headers
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache');
Expand All @@ -55,12 +55,12 @@ export function envConfig(userOptions: Partial<EnvConfigOptions> = {}): Plugin {
closeBundle() {
const templateContent = createEnvConfigContent(userOptions.variables || [], true);

const TEMPLATE_PATH = path.join(root, 'dist', 'assets', 'env-config.template.js');
const TEMPLATE_PATH = path.join(root, 'dist', 'env-config.template.js');
fs.writeFileSync(TEMPLATE_PATH, templateContent, 'utf8');
},

transformIndexHtml(html) {
return html.replace('</head>', '<script src="/assets/env-config.js"></script></head>');
return html.replace('</head>', '<script src="/env-config.js"></script></head>');
},
};
}