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

Use loader aliases when using require invocations #634

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions addon/resolvers/classic/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals requirejs, require */
/* globals requirejs, require, loader */
Copy link
Member

Choose a reason for hiding this comment

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

I think we should import require from require, then we'd get the one we want automatically

Copy link
Author

Choose a reason for hiding this comment

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

Sorry @rwjblue I don't get it, can you please elaborate?

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe he wants to to import require from 'require'


import Ember from 'ember';
import { assert, deprecate, warn } from '@ember/debug';
Expand All @@ -13,7 +13,11 @@ if (typeof requirejs.entries === 'undefined') {

export class ModuleRegistry {
constructor(entries) {
this._entries = entries || requirejs.entries;
if(loader.__aliases && loader.__aliases.requirejs) {
this._entries = entries || window[loader.__aliases.requirejs].entries;
} else {
this._entries = entries || requirejs.entries;
}
}
moduleNames() {
return Object.keys(this._entries);
Expand Down Expand Up @@ -466,7 +470,13 @@ const Resolver = EmberObject.extend({
},

_extractDefaultExport(normalizedModuleName) {
let module = require(normalizedModuleName, null, null, true /* force sync */);

let module;
if(loader.__aliases && loader.__aliases.require) {
module = window[loader.__aliases.require](normalizedModuleName, null, null, true /* force sync */);
} else {
module = require(normalizedModuleName, null, null, true /* force sync */);
}
Comment on lines +474 to +479
Copy link

@jamesarosen jamesarosen Jun 2, 2023

Choose a reason for hiding this comment

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

This might be a little more resilient if you extracted a variable:

const req = loader.__aliases?.require ? window[loader.__aliases.require] : require
const module = req(normalizedModuleName, null, null, true /* force sync */);


if (module && module['default']) {
module = module['default'];
Expand Down