-
Notifications
You must be signed in to change notification settings - Fork 27k
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
CJS -> ESM interoperability for imports inside external modules loaded from CDN #32213
Comments
It appears that since Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultHead = defaultHead;
...
var _default = Head;
exports.default = _default; ...when importing {
__esModule: true,
default: function Head(param),
defaultHead: function defaultHead(param),
} Moving forward, Vercel will either have to... A.) Generate an ESM file version for all its exports and support outside ES modules (won't solve 3rd party dependencies) B.) Or transpile all JavaScript assets over URL through Webpack's babel/swc plugins (as of now, it just appears to copy them over to C.) Or build an internal utility function that interpolates all import statements by checking for a import React from "react";
import Head from "next/head";
// this could be internalized and automatically
// handle "default" import statements
const interopDefault = (obj) => {
return obj && obj.__esModule ? obj.default : obj;
}
const Globals = ()=>{
return(/*#__PURE__*/ interopDefault(React.createElement("div", null, /*#__PURE__*/ interopDefault(React.createElement(interopDefault(Head), null, /*#__PURE__*/ interopDefault(React.createElement("link", {
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap"
})))))));
};
export default Globals; D.) Or export all of its assets as named exports and expect the developer to use the named export instead (won't solve 3rd party dependencies)... Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultHead = defaultHead;
exports.Head = Head;
...
var _default = Head;
exports.default = _default; import React from "react";
import { Head } from "next/head";
const Globals = ()=>{
return(/*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(Head, null, /*#__PURE__*/ React.createElement("link", {
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap"
}))));
};
export default Globals; E.) Or expect the developer to use the import React from "react";
import Head from "next/head";
const Globals = ()=>{
return(/*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(Head.default, null, /*#__PURE__*/ React.createElement("link", {
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap"
}))));
};
export default Globals; Unfortunately, this issue will become more widespread as a developer adds more 3rd party dependencies via URL, since |
@mattcarlotta yes, you were correct. import React from "react";
import mod from "next/head";
const Head = mod.default
const Globals = ()=>{
return(/*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(Head, null, /*#__PURE__*/ React.createElement("link", {
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap"
}))));
};
export default Globals; And this renders the component without any issues. But in that case, it was a manual workaround to force the project to load only |
Head
from next/head
inside a http
module breaks the rendering
And i am not sure if nextjs controls the |
I believe styled-components.cjs.js ...
return e && "object" == typeof e && "default" in e ? e.default : e
... styled-components.js ...
var r="default" in e ? e.default : e
... next/dist/shared/lib/head.js (doesn't interpolate // returns/builds the obj instead of returning just the default property
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
// returns/builds the obj instead of returning just the default property
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
} On a separate note it appears that imports have some node module resolution. The reason I say some is because you can’t use |
If the package is dealing the |
This appears to have more complexity than I thought. In short, the Your codesandbox example above also appears to be rendered clientside only (which is why you’re not running into the same issues) |
Looks like this opens door for new discussion, like does |
Please verify that your issue can be recreated with Why was this issue marked with the
|
This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
12.0.7
What version of Node.js are you using?
12.22.6 and above
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
If a CDN esm module uses
Head
fromnext/head
breaksnext dev
Here is a esm module using
next/head
https://jscdn.teleporthq.io/new-project-68d4/globals.js@0888a7f951f31cc0e33ee614e97d5b6e398ed19c
And it is imported inside
https://jscdn.teleporthq.io/new-project-68d4/card.js@edb67533c00984de863a24b4433f30bab9eae03e
Now, if we try to import and use the component. It breaks the render.
Expected Behavior
Component to load the module and render it without breaking
To Reproduce
Here is a online repl which does the same to reproduce the bug
https://replit.com/@JayaKrishnaNamb/nextjs-http#pages/index.js
GitHub repo to reproduce in local
https://github.com/teleporthq/nextjs-http
The text was updated successfully, but these errors were encountered: