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

Use relative links for serving internal assets #261

Merged
merged 3 commits into from Apr 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
<!-- Add changelog entries for new changes under this section -->

* **Improvements**
* Use relative links for serving internal assets ([#261](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/261), fixes [#254](https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/254))
* Properly escape embedded JS/JSON ([#262](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/262))

* **Bug Fix**
Expand Down
9 changes: 8 additions & 1 deletion src/viewer.js
Expand Up @@ -14,6 +14,7 @@ const Logger = require('./Logger');
const analyzer = require('./analyzer');

const projectRoot = path.resolve(__dirname, '..');
const assetsRoot = path.join(projectRoot, 'public');

module.exports = {
startServer,
Expand Down Expand Up @@ -169,7 +170,13 @@ async function generateReport(bundleStats, opts) {
}

function getAssetContent(filename) {
return fs.readFileSync(`${projectRoot}/public/${filename}`, 'utf8');
const assetPath = path.join(assetsRoot, filename);

if (!assetPath.startsWith(assetsRoot)) {
throw new Error(`"${filename}" is outside of the assets root`);
}

return fs.readFileSync(assetPath, 'utf8');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion views/script.ejs
Expand Up @@ -4,5 +4,5 @@
<%- escapeScript(assetContent(filename)) %>
</script>
<% } else { %>
<script src="/<%= filename %>"></script>
<script src="<%= filename %>"></script>
<% } %>