Unverified Commit 01109734 authored by LongYinan's avatar LongYinan Committed by GitHub

Interpolate default exports (#35933)

* Add failing test for next/link next/image with "type": "module"

* Interpolate default exports
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 1ed38dd2
# 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
# ESM Hello World example
This example shows the most basic idea behind Next.js, and it's running on native [esm](https://nodejs.org/api/esm.html) mode. We have 2 pages: `pages/index.js` and `pages/about.js`. The former responds to `/` requests and the latter to `/about`. Using `next/link` you can add hyperlinks between them with universal routing capabilities. The `day` directory shows that you can have subdirectories.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example-esm) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/hello-world-esm)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/hello-world-esm&project-name=hello-world-esm&repository-name=hello-world-esm)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example hello-world-esm hello-world-esm-app
# or
yarn create next-app --example hello-world-esm hello-world-esm-app
# or
pnpm create next-app -- --example hello-world-esm hello-world-esm-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
{
"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"
}
}
export default function AboutPage() {
return <div>About us</div>
}
export default function DayPage() {
return <div>Hello Day</div>
}
import Link from 'next/link'
export default function IndexPage() {
return (
<div>
Hello World.{' '}
<Link href="/about">
<a>About</a>
</Link>
</div>
)
}
...@@ -14,7 +14,11 @@ module.exports = function (task) { ...@@ -14,7 +14,11 @@ module.exports = function (task) {
function* ( function* (
file, file,
serverOrClient, serverOrClient,
{ stripExtension, keepImportAssertions = false } = {} {
stripExtension,
keepImportAssertions = false,
interopClientDefaultExport = false,
} = {}
) { ) {
// Don't compile .d.ts // Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return if (file.base.endsWith('.d.ts')) return
...@@ -113,6 +117,15 @@ module.exports = function (task) { ...@@ -113,6 +117,15 @@ module.exports = function (task) {
if (output.map) { if (output.map) {
const map = `${file.base}.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}`) output.code += Buffer.from(`\n//# sourceMappingURL=${map}`)
// add sourcemap to `files` array // add sourcemap to `files` array
......
...@@ -1826,7 +1826,7 @@ export async function nextbuild(task, opts) { ...@@ -1826,7 +1826,7 @@ export async function nextbuild(task, opts) {
export async function client(task, opts) { export async function client(task, opts) {
await task await task
.source(opts.src || 'client/**/*.+(js|ts|tsx)') .source(opts.src || 'client/**/*.+(js|ts|tsx)')
.swc('client', { dev: opts.dev }) .swc('client', { dev: opts.dev, interopClientDefaultExport: true })
.target('dist/client') .target('dist/client')
notify('Compiled client files') notify('Compiled client files')
} }
......
...@@ -2,6 +2,7 @@ import { createNext } from 'e2e-utils' ...@@ -2,6 +2,7 @@ import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { hasRedbox, renderViaHTTP } from 'next-test-utils' import { hasRedbox, renderViaHTTP } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
describe('Type module interop', () => { describe('Type module interop', () => {
let next: NextInstance let next: NextInstance
...@@ -14,6 +15,21 @@ describe('Type module interop', () => { ...@@ -14,6 +15,21 @@ describe('Type module interop', () => {
return <p>hello world</p> return <p>hello world</p>
} }
`, `,
'pages/modules.jsx': `
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" />
</>
)
}
`,
}, },
dependencies: {}, dependencies: {},
}) })
...@@ -39,4 +55,16 @@ describe('Type module interop', () => { ...@@ -39,4 +55,16 @@ describe('Type module interop', () => {
expect(await hasRedbox(browser)).toBe(false) expect(await hasRedbox(browser)).toBe(false)
await browser.close() await browser.close()
}) })
it('should render server-side with modules', async () => {
const html = await renderViaHTTP(next.url, '/modules')
const $ = cheerio.load(html)
expect($('#link-to-home').text()).toBe('link to home')
})
it('should render client-side with modules', async () => {
const browser = await webdriver(next.url, '/modules')
expect(await hasRedbox(browser)).toBe(false)
await browser.close()
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment