This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 257
Dynamic public path #334
Merged
alexander-akait
merged 7 commits into
webpack-contrib:master
from
willhaben:dynamic_public_path
Aug 6, 2019
Merged
Dynamic public path #334
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa6f15b
feat: added option "prefixPublicPathWithWebpackPublicPath"
fabb e776945
feat: added unit tests
fabb 4f990d7
feat: introduced "postTransformPublicPath" option
fabb 584f7d9
feat: moved postTransformPublicPath tests to own file
fabb 64f2eb6
feat: added documentation for the postTransformPublicPath option
fabb 460127d
feat: added example for the postTransformPublicPath option
fabb 1bc97dc
feat: added some description for the postTransformPublicPath example
fabb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,6 +269,32 @@ module.exports = { | |
}; | ||
``` | ||
|
||
### `postTransformPublicPath` | ||
|
||
Type: `Function` | ||
Default: `undefined` | ||
|
||
Specifies a custom function to post-process the generated public path. This can be used to prepend or append dynamic global variables that are only available at runtime, like `__webpack_public_path__`. This would not be possible with just `publicPath`, since it stringifies the values. | ||
|
||
**webpack.config.js** | ||
|
||
```js | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(png|jpg|gif)$/, | ||
loader: 'file-loader', | ||
options: { | ||
publicPath: '/some/path/', | ||
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
``` | ||
|
||
### `context` | ||
|
||
Type: `String` | ||
|
@@ -577,6 +603,72 @@ Result: | |
path/to/file.png?e43b20c069c4a01867c31e98cbce33c9 | ||
``` | ||
|
||
--- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use heading here instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i added a little paragraph for explaining the use case. makes sense? |
||
|
||
**main.js** | ||
|
||
```js | ||
const namespace = process.env.NAMESPACE; | ||
const assetPrefixForNamespace = (namespace) => { | ||
switch (namespace) { | ||
case 'prod': | ||
return 'https://cache.myserver.net/web'; | ||
case 'uat': | ||
return 'https://cache-uat.myserver.net/web'; | ||
case 'st': | ||
return 'https://cache-st.myserver.net/web'; | ||
case 'dev': | ||
return 'https://cache-dev.myserver.net/web'; | ||
default: | ||
return ''; | ||
} | ||
}; | ||
__webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`; | ||
``` | ||
|
||
**file.js** | ||
|
||
```js | ||
import png from './image.png'; | ||
``` | ||
|
||
**webpack.config.js** | ||
|
||
```js | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(png|jpg|gif)$/, | ||
loader: 'file-loader', | ||
options: { | ||
context: '', | ||
emitFile: true, | ||
name: '[name].[hash].[ext]', | ||
publicPath: 'static/assets/', | ||
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, | ||
outputPath: 'static/assets/', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
``` | ||
|
||
Result when run with `NAMESPACE=prod` env variable: | ||
|
||
```bash | ||
# result | ||
https://cache.myserver.net/web/static/assets/image.somehash.png | ||
``` | ||
|
||
Result when run with `NAMESPACE=dev` env variable: | ||
|
||
```bash | ||
# result | ||
https://cache-dev.myserver.net/web/static/assets/image.somehash.png | ||
``` | ||
|
||
## Contributing | ||
|
||
Please take a moment to read our contributing guidelines if you haven't yet done so. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
test/__snapshots__/postTransformPublicPath-option.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`when applied with \`postTransformPublicPath\` option matches snapshot for appending to input parameter value 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"/test\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`postTransformPublicPath\` option matches snapshot for returned input parameter value without modification 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for appending to input parameter value 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"?test=test\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for prefixing with __webpack_public_path__ 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for prefixing with string 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = \\"path_prefix/\\" + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for returned input parameter value without modification 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for appending to input parameter value 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"?test=test\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for prefixing with __webpack_public_path__ 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; | ||
|
||
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for returned input parameter value without modification 1`] = ` | ||
Object { | ||
"assets": Array [ | ||
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png", | ||
], | ||
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import webpack from './helpers/compiler'; | ||
|
||
describe('when applied with `postTransformPublicPath` option', () => { | ||
it('matches snapshot for returned input parameter value without modification', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
postTransformPublicPath: (p) => p, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for appending to input parameter value', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
postTransformPublicPath: (p) => `${p} + "/test"`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('when applied with `publicPath` and `postTransformPublicPath` option', () => { | ||
describe('`{String}` value', () => { | ||
it('matches snapshot for returned input parameter value without modification', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath: 'public_path/', | ||
postTransformPublicPath: (p) => p, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for appending to input parameter value', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath: 'public_path/', | ||
postTransformPublicPath: (p) => `${p} + "?test=test"`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for prefixing with __webpack_public_path__', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath: 'public_path/', | ||
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('`{Function}` value', () => { | ||
it('matches snapshot for returned input parameter value without modification', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath(url) { | ||
return `public_path/${url}`; | ||
}, | ||
postTransformPublicPath: (p) => p, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for appending to input parameter value', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath(url) { | ||
return `public_path/${url}`; | ||
}, | ||
postTransformPublicPath: (p) => `${p} + "?test=test"`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for prefixing with string', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath: 'public_path/', | ||
postTransformPublicPath: (p) => `"path_prefix/" + ${p}`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
|
||
it('matches snapshot for prefixing with __webpack_public_path__', async () => { | ||
const config = { | ||
loader: { | ||
test: /(png|jpg|svg)/, | ||
options: { | ||
publicPath: 'public_path/', | ||
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`, | ||
}, | ||
}, | ||
}; | ||
|
||
const stats = await webpack('fixture.js', config); | ||
const [module] = stats.toJson().modules; | ||
const { assets, source } = module; | ||
|
||
expect({ assets, source }).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, i forgot, let's add Example use case for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i pushed an example, is this what you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i see, just think about additional section in
Example
in READMEThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ups, i forgot to push the commit i was talking about 😅
i meant this one: 460127d