-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
add variableName option #11
Conversation
@ywmail great work on PR! I will review and merge during the upcoming weekend. |
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.
@ywmail
Thanks again for submitting the PR! Great work here!
- See inline comments
- How about adding tests that confirm that the introduced feature works?
E.g.:
it("replaces with a value of variableName", async () => {
global.publicPath = "customPathFromVariable";
appendChildTrap = ({ src }) =>
assert.strictEqual(src.split("/")[0], "customPathFromVariable");
await compileWithWebpackVersion(
webpackVersion,
"variableName_pluginConf"
);
global.publicPath = undefined;
delete global["publicPath"];
}),
it("uses default public path when variableName is undefined", async () => {
appendChildTrap = ({ src }) =>
assert.strictEqual(src.split("/")[0], "originalPublicPath");
await compileWithWebpackVersion(
webpackVersion,
"variableName_pluginConf"
);
}),
src/plugin.js
Outdated
@@ -70,6 +75,9 @@ class WebpackRequireFrom { | |||
"Object.defineProperty(" + mainTemplate.requireFn + ', "p", {', | |||
" get: function () {", | |||
getterBody, | |||
" },", | |||
" set: function (value) {", | |||
"", |
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.
Curious why did you add a setter?
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.
Because webpack allow users to change the __webpack_public_path__
in their code for a dynamic outputPath, if their is no setter in property p
, the application will fail with a error message:
Cannot set property p of function...
See:
https://webpack.js.org/guides/public-path/#on-the-fly
so i think it's better to add a setter to warn the user in the setter function like:
" set: function (value) {",
`console.warn("You should not change __webpack_public_path__ directly, use ${PLUGIN_NAME} to config public path.")`,
" }",
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.
Do you think it's necessary?
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.
@ywmail
Good catch, I wonder what would be the most expected behaviour for that case.
I would expect to get the value of __webpack_public_path__
if no webpack-require-from
overrides exist, and if there's an override, return whatever plugin defines.
What do you think?
I will open a separate issue to gently handle this scenario meanwhile
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.
@agoldis
Yes, I also think it's better to handle this scenario in separate issue gently, a warning in setter function is just temporary in my case. I will remove this part from the PR. :)
src/codeBuilders.js
Outdated
` if (typeof ${variableName} !== "string") {`, | ||
` throw new Error("${PLUGIN_NAME}: '${variableName}' is not a string or not available at runtime. See https://github.com/agoldis/webpack-require-from#troubleshooting");`, | ||
" }", | ||
` return (function(){if($PIX) return ${variableName};})();`, |
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.
$PIX
?
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.
$PIX
is the global object in my project, I committed this by mistake, will remove it.
Thanks for the review and your great work, it's really a great plugin. 💯 |
7622164
to
7da7e0a
Compare
@agoldis |
add option
variableName
for another choice.