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 6261058
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
34 changes: 34 additions & 0 deletions examples/esm/.gitignore
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
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" />
</>
)
}
15 changes: 14 additions & 1 deletion packages/next/taskfile-swc.js
Expand Up @@ -14,7 +14,11 @@ module.exports = function (task) {
function* (
file,
serverOrClient,
{ stripExtension, keepImportAssertions = false } = {}
{
stripExtension,
keepImportAssertions = false,
interopClientDefaultExport = false,
} = {}
) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return
Expand Down Expand Up @@ -113,6 +117,15 @@ module.exports = function (task) {
if (output.map) {
const map = `${file.base}.map`

if (interopClientDefaultExport) {
output.code += `
if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
Object.assign(exports.default, exports);
module.exports = exports.default;
}
`
}

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

// add sourcemap to `files` array
Expand Down
2 changes: 1 addition & 1 deletion packages/next/taskfile.js
Expand Up @@ -1826,7 +1826,7 @@ export async function nextbuild(task, opts) {
export async function client(task, opts) {
await task
.source(opts.src || 'client/**/*.+(js|ts|tsx)')
.swc('client', { dev: opts.dev })
.swc('client', { dev: opts.dev, interopClientDefaultExport: true })
.target('dist/client')
notify('Compiled client files')
}
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 6261058

Please sign in to comment.