Skip to content

Commit

Permalink
Add %PUBLIC_URL% support
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jun 11, 2020
1 parent 18a213b commit a75ea0d
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/05-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ $ snowpack dev --no-bundle

#### Build Options

- **`baseUrl`** | `string` | Default: `/`
- Prefix all URLs with this string at build time (useful if your app lives in a subdirectory)
- **`metaDir`** | `string` | Default: `__snowpack__`
- By default, Snowpack outputs Snowpack-related metadata such as [HMR](#hot-module-replacement) and [ENV](#environment-variables) info to a folder called `__snowpack__`. You can rename that folder with this option (e.g.: `metaDir: 'static/snowpack'`).

Expand Down
3 changes: 3 additions & 0 deletions src/commands/build-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export function wrapHtmlResponse({
hasHmr?: boolean;
buildOptions: SnowpackConfig['buildOptions'];
}) {
// replace %PUBLIC_URL% in HTML files (along with surrounding slashes, if any)
code = code.replace(/\/?%PUBLIC_URL%\/?/g, '/');

if (hasHmr) {
code += `<script type="module" src="/${buildOptions.metaDir}/hmr.js"></script>`;
}
Expand Down
10 changes: 10 additions & 0 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ export async function command(commandOptions: CommandOptions) {
}
const outPath = f.replace(dirDisk, dirDest);
mkdirp.sync(path.dirname(outPath));

// replace %PUBLIC_URL% in HTML files
if (path.extname(f) === '.html') {
let code = await fs.readFile(f, 'utf8');
code = code.replace(
/%PUBLIC_URL%\/?/g,
path.normalize(`/${config.buildOptions.baseUrl}`),
);
return fs.writeFile(outPath, code, 'utf8');
}
return fs.copyFile(f, outPath);
}),
);
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface SnowpackConfig {
};
};
buildOptions: {
baseUrl: string;
metaDir: string;
};
proxy: Proxy[];
Expand Down Expand Up @@ -156,6 +157,7 @@ const DEFAULT_CONFIG: Partial<SnowpackConfig> = {
bundle: undefined,
},
buildOptions: {
baseUrl: '/',
metaDir: '__snowpack__',
},
};
Expand Down Expand Up @@ -224,6 +226,7 @@ const configSchema = {
buildOptions: {
type: ['object'],
properties: {
baseUrl: {type: 'string'},
metaDir: {type: 'string'},
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/build/base-url/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
node_modules
19 changes: 19 additions & 0 deletions test/build/base-url/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs');
const path = require('path');
const execa = require('execa');

it('buildOptions.baseUrl', () => {
// %PUBLIC_URL% gets replaced with this setting
execa.sync('npm', ['run', 'TEST'], {cwd: __dirname});

const outputHTML = fs.readFileSync(path.resolve(__dirname, 'build', 'index.html'), 'utf8');

const iconMatch = outputHTML.match(/<link rel="icon" href="([^"]+)/)[1];
const cssMatch = outputHTML.match(/<link rel="stylesheet" type="text\/css" href="([^"]+)/)[1];
const jsMatch = outputHTML.match(/<script type="module" src="([^"]+)/)[1];

// TODO: when Windows backslash bug is fixed, simplify this and only test for forward slashes
expect(['/static/favicon.ico', '\\static\\favicon.ico'].includes(iconMatch)).toBe(true);
expect(['/static/index.css', '\\static\\index.css'].includes(cssMatch)).toBe(true);
expect(['/static/_dist_/index.js', '\\static\\_dist_\\index.js'].includes(jsMatch)).toBe(true);
});
16 changes: 16 additions & 0 deletions test/build/base-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"scripts": {
"start": "node ../../../pkg/dist-node/index.bin.js dev",
"TEST": "node ../../../pkg/dist-node/index.bin.js build"
},
"snowpack": {
"scripts": {
"mount:public": "mount public --to /",
"mount:src": "mount src --to /_dist_"
},
"buildOptions": {
"baseUrl": "/static/"
}
},
"dependencies": {}
}
Binary file added test/build/base-url/public/favicon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions test/build/base-url/public/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#img {
display: block;
margin: auto;
height: 128px;
width: 128px;
padding: 2rem;
}

#canvas {
display: block;
margin: 2rem auto;
width: 540px;
height: 540px;
}
24 changes: 24 additions & 0 deletions test/build/base-url/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Web site created using create-snowpack-app" />
<link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/index.css" />
<title>Snowpack App</title>
</head>
<body>
<script type="module" src="%PUBLIC_URL%/_dist_/index.js"></script>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
3 changes: 3 additions & 0 deletions test/build/base-url/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function doNothing() {
// I do nothing 🎉
}

0 comments on commit a75ea0d

Please sign in to comment.