Skip to content

Commit

Permalink
Add %PUBLIC_URL% support (#474)
Browse files Browse the repository at this point in the history
* Add %PUBLIC_URL% support

* Add %PUBLIC_URL% to documentation
  • Loading branch information
drwpow committed Jun 11, 2020
1 parent 3280de3 commit f268af6
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/05-configuration.md
Expand Up @@ -123,6 +123,8 @@ $ snowpack dev --no-bundle

#### Build Options

- **`baseUrl`** | `string` | Default: `/`
- In your HTML, replace all instances of `%PUBLIC_URL%` with this (inspired by the same [Create React App](https://create-react-app.dev/docs/using-the-public-folder/) concept). This is useful if your app will be deployed to 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
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
7 changes: 7 additions & 0 deletions src/commands/build.ts
Expand Up @@ -209,6 +209,13 @@ 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, 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
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
@@ -0,0 +1,2 @@
build
node_modules
18 changes: 18 additions & 0 deletions test/build/base-url/index.test.js
@@ -0,0 +1,18 @@
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];

expect(iconMatch).toBe('/static/favicon.ico');
expect(cssMatch).toBe('/static/index.css');
expect(jsMatch).toBe('/static/_dist_/index.js');
});
16 changes: 16 additions & 0 deletions test/build/base-url/package.json
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,3 @@
export default function doNothing() {
// I do nothing 🎉
}

0 comments on commit f268af6

Please sign in to comment.