Skip to content

Commit

Permalink
Interpolate default exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Apr 6, 2022
1 parent 476a116 commit 3c547e3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
14 changes: 14 additions & 0 deletions examples/esm/package.json
@@ -0,0 +1,14 @@
{
"private": true,
"type": "module",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
3 changes: 3 additions & 0 deletions examples/esm/pages/index.js
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
13 changes: 13 additions & 0 deletions examples/esm/pages/modules.js
@@ -0,0 +1,13 @@
import Link from 'next/link'
import Image from 'next/image'

export default function Modules() {
return (
<>
<Link href="/">
<a id="link-to-home">link to home</a>
</Link>
<Image src="/static/image.png" width="100" height="100" />
</>
)
}
19 changes: 19 additions & 0 deletions packages/next/taskfile-swc.js
Expand Up @@ -113,6 +113,25 @@ module.exports = function (task) {
if (output.map) {
const map = `${file.base}.map`

if (isClient) {
output.code =
`var defaultExports = void 0;
Object.defineProperty(exports, 'default', {
set(value) {
defaultExports = value;
if (typeof value === 'function' || (typeof value === 'object' && value !== null)) {
module.exports = Object.assign(defaultExports, {
...module.exports,
});
}
},
get() {
return defaultExports;
}
});
` + output.code
}

output.code += Buffer.from(`\n//# sourceMappingURL=${map}`)

// add sourcemap to `files` array
Expand Down
14 changes: 10 additions & 4 deletions test/e2e/type-module-interop/index.test.ts
Expand Up @@ -19,10 +19,16 @@ describe('Type module interop', () => {
import Link from 'next/link'
import Image from 'next/image'
return <>
<Link href="/"><a id="link-to-home">link to home</a></Link>
<Image src="/static/image.png" width="100" height="100" />
</>
export default function Modules() {
return (
<>
<Link href="/">
<a id="link-to-home">link to home</a>
</Link>
<Image src="/static/image.png" width="100" height="100" />
</>
)
}
`,
},
dependencies: {},
Expand Down

0 comments on commit 3c547e3

Please sign in to comment.