Skip to content
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

Merged
merged 1 commit into from
Sep 11, 2018
Merged

add variableName option #11

merged 1 commit into from
Sep 11, 2018

Conversation

ywmail
Copy link
Contributor

@ywmail ywmail commented Sep 6, 2018

add option variableName for another choice.

@agoldis agoldis self-assigned this Sep 6, 2018
@agoldis
Copy link
Owner

agoldis commented Sep 7, 2018

@ywmail great work on PR! I will review and merge during the upcoming weekend.

Copy link
Owner

@agoldis agoldis left a 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!

  1. See inline comments
  2. 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) {",
"",
Copy link
Owner

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?

Copy link
Contributor Author

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.")`,
        " }",

Copy link
Contributor Author

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?

Copy link
Owner

@agoldis agoldis Sep 11, 2018

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

Copy link
Contributor Author

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. :)

` 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};})();`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$PIX?

Copy link
Contributor Author

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.

@ywmail
Copy link
Contributor Author

ywmail commented Sep 10, 2018

Thanks for the review and your great work, it's really a great plugin. 💯
I'll update the PR later, it's my first PR on github. :) 🔢

@ywmail ywmail force-pushed the master branch 2 times, most recently from 7622164 to 7da7e0a Compare September 11, 2018 11:30
@ywmail
Copy link
Contributor Author

ywmail commented Sep 11, 2018

@agoldis
Update the PR: add some tests to confirm that variableName works as expected and doesn't break the original function. :)
A great thanks for reviewing the code. 💯

@agoldis agoldis merged commit a5e1463 into agoldis:master Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants