Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Keep global require's toUrl and toAbsMid methods intact #47

Closed
wants to merge 3 commits into from

Conversation

maier49
Copy link
Contributor

@maier49 maier49 commented Jan 17, 2017

Type: bug

The following has been addressed in the PR:

  • There is a related issue
  • All code matches the style guide
  • Unit or Functional tests are included in the PR

Description:

Currently, if code is calling require.toUrl, that will trigger the expression require condition and that module will have require = function() { return '${path}'; } prepended to it. However, this function obviously doesn't have a toUrl property so the call will fail. Checking for usages of require.toUrl and not replacing require at all is problematic because webpack will still replace it with __webpack_require__ which doesn't have a toUrl function either.

It seems like there should probably be a better way to handle this. But this does work for todoMVC at least.

@maier49 maier49 requested a review from matt-gadd January 17, 2017 21:02
@codecov-io
Copy link

codecov-io commented Jan 17, 2017

Current coverage is 99.59% (diff: 100%)

No coverage report found for master at 6269ae6.

Powered by Codecov. Last update 6269ae6...343632a

Copy link
Member

@dylans dylans left a comment

Choose a reason for hiding this comment

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

I think this is probably fine (see my comment about consistency in references to global)... we may want to open a separate issue about that in meta?

@@ -83,7 +83,17 @@ export default class DojoLoadPlugin {
compilation.moduleTemplate.plugin('module', (source: any, module: any) => {
if (module.meta && module.meta.isPotentialLoad) {
const path = stripPath(basePath, module.userRequest);
const require = `var require = function () { return '${path}'; };`;
const require = `var require = (function () {
var globalScope = typeof window === 'undefined' ? global : window;
Copy link
Member

Choose a reason for hiding this comment

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

We do seem to have several ways in Dojo 2 of getting a reference to the global, whether it is https://github.com/dojo/shim/blob/79882ec5c4fa78eb91171e7ff42ace275aa68562/src/support/global.ts or https://github.com/dojo/loader/blob/master/src/loader.ts#L8 . Maybe we want to normalize this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Normalizing the usage definitely seems like a good idea. I've updated the usage here to match the loader example, since that was more or less equivalent to what this was already doing but more thorough.

@@ -62,7 +62,17 @@ describe('core-load', () => {
})[0];

assert.instanceOf(source, ConcatSource, 'A new `ConcatSource` is created.');
assert.strictEqual(source.source(), `var require = function () { return '/root/path/src/module'; };\n`,
assert.strictEqual(source.source(), `var require = (function () {
var globalScope = typeof window === 'undefined' ? global : window;
Copy link
Member

Choose a reason for hiding this comment

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

Same comment/question here about reference to global.

@dylans dylans added this to the 2017.01 milestone Jan 17, 2017
@mwistrand
Copy link

Where is globalObject.require being set? If an application is being built with webpack and therefore using its internal loader, there would be no require in the global namespace.

@maier49
Copy link
Contributor Author

maier49 commented Jan 18, 2017 via email

@matt-gadd
Copy link
Contributor

I don't think we should land this in its current form for the following reasons:

  • The current require code in cli-build gives context require support to webpack without an AMD loader giving parity in functionality. For toUrl and toAbsMid in this, that would not be the case which would be confusing in my opinion.

  • This boilerplate would be added to every point a contextual require is made (regardless of whether .toUrl or .toAbsMid is used) which seems like a overhead in bundle size.

  • You can actually use require.toUrl with a contextual require in AMD - which this doesn't support.

When we move to the version of intern that is loaderless, we won't have an AMD loader anyway, so it seems like in the mean time we might be better just changing the way we access an html page in todo-mvc to not use require.toUrl?

@maier49
Copy link
Contributor Author

maier49 commented Jan 18, 2017

@matt-gadd I agree with that reasoning.

It does bring up one other concern though. This code is currently triggering on usage of require.toUrl. If it actually was a contextual require it will still be replaced with a function without a toUrl method, meaning that using require.toUrl with a contextual require is not supported by the current version either. Unless I'm missing something in how that would work.

@maier49 maier49 closed this Jan 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants