Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 460127d

Browse files
committed
feat: added example for the postTransformPublicPath option
1 parent 64f2eb6 commit 460127d

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,72 @@ Result:
603603
path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
604604
```
605605

606+
---
607+
608+
**main.js**
609+
610+
```js
611+
const namespace = process.env.NAMESPACE;
612+
const assetPrefixForNamespace = (namespace) => {
613+
switch (namespace) {
614+
case 'prod':
615+
return 'https://cache.myserver.net/web';
616+
case 'uat':
617+
return 'https://cache-uat.myserver.net/web';
618+
case 'st':
619+
return 'https://cache-st.myserver.net/web';
620+
case 'dev':
621+
return 'https://cache-dev.myserver.net/web';
622+
default:
623+
return '';
624+
}
625+
};
626+
__webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;
627+
```
628+
629+
**file.js**
630+
631+
```js
632+
import png from './image.png';
633+
```
634+
635+
**webpack.config.js**
636+
637+
```js
638+
module.exports = {
639+
module: {
640+
rules: [
641+
{
642+
test: /\.(png|jpg|gif)$/,
643+
loader: 'file-loader',
644+
options: {
645+
context: '',
646+
emitFile: true,
647+
name: '[name].[hash].[ext]',
648+
publicPath: 'static/assets/',
649+
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
650+
outputPath: 'static/assets/',
651+
},
652+
},
653+
],
654+
},
655+
};
656+
```
657+
658+
Result when run with `NAMESPACE=prod` env variable:
659+
660+
```bash
661+
# result
662+
https://cache.myserver.net/web/static/assets/image.somehash.png
663+
```
664+
665+
Result when run with `NAMESPACE=dev` env variable:
666+
667+
```bash
668+
# result
669+
https://cache-dev.myserver.net/web/static/assets/image.somehash.png
670+
```
671+
606672
## Contributing
607673

608674
Please take a moment to read our contributing guidelines if you haven't yet done so.

0 commit comments

Comments
 (0)