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 7, 2022
1 parent 68e42c7 commit 3eb3bdf
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 2 deletions.
34 changes: 34 additions & 0 deletions examples/hello-world-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
23 changes: 23 additions & 0 deletions examples/hello-world-esm/README.md
@@ -0,0 +1,23 @@
# 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)).
14 changes: 14 additions & 0 deletions examples/hello-world-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/hello-world-esm/pages/about.js
@@ -0,0 +1,3 @@
export default function AboutPage() {
return <div>About us</div>
}
3 changes: 3 additions & 0 deletions examples/hello-world-esm/pages/day/index.js
@@ -0,0 +1,3 @@
export default function DayPage() {
return <div>Hello Day</div>
}
12 changes: 12 additions & 0 deletions examples/hello-world-esm/pages/index.js
@@ -0,0 +1,12 @@
import Link from 'next/link'

export default function IndexPage() {
return (
<div>
Hello World.{' '}
<Link href="/about">
<a>About</a>
</Link>
</div>
)
}
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

0 comments on commit 3eb3bdf

Please sign in to comment.