Skip to content

Commit

Permalink
Extract next-swc Rust code into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
padmaia committed Nov 20, 2021
1 parent 9652bdd commit e08b92a
Show file tree
Hide file tree
Showing 260 changed files with 128 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ packages/create-next-app/templates/**
test/integration/eslint/**
test/development/basic/legacy-decorators/**/*
test-timings.json
packages/next/build/swc/crates/**
packages/next-swc/crates/**
bench/nested-deps/pages/**
bench/nested-deps/components/**
2 changes: 1 addition & 1 deletion .github/actions/next-stats-action/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ if (!allowedActions.has(actionInfo.actionName) && !actionInfo.isRelease) {
}
await fs.copy(
path.join(__dirname, '../native'),
path.join(dir, 'packages/next/native')
path.join(dir, 'packages/next-swc/native')
)

logger(`Linking packages in ${dir}`)
Expand Down
112 changes: 56 additions & 56 deletions .github/workflows/build_test_deploy.yml

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions .github/workflows/pull_request_stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ jobs:
uses: actions/cache@v2
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
path: packages/next/native/next-swc.linux-x64-gnu.node
key: dev-next-swc-nightly-2021-08-12-linux-x64-gnu-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
path: packages/next-swc/native/next-swc.linux-x64-gnu.node
key: dev-next-swc-nightly-2021-08-12-linux-x64-gnu-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

# We use restore-key to pick latest cache.
# We will not get exact match, but doc says
Expand All @@ -63,7 +63,7 @@ jobs:
- name: Cache built files
uses: actions/cache@v2
with:
path: ./packages/next/build/swc/target
path: ./packages/next-target
key: next-swc-cargo-cache-ubuntu-18.04--${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
next-swc-cargo-cache-ubuntu-18.04
Expand All @@ -77,13 +77,13 @@ jobs:
run: yarn build-native
env:
MACOSX_DEPLOYMENT_TARGET: '10.13'
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/[email protected]
with:
name: next-swc-dev-binary
path: packages/next/native/next-swc.linux-x64-gnu.node
path: packages/next-swc/native/next-swc.linux-x64-gnu.node

- name: Clear the cargo caches
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
Expand All @@ -107,9 +107,9 @@ jobs:
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
with:
name: next-swc-dev-binary
path: packages/next/native
path: packages/next-swc/native

- run: cp -r packages/next/native .github/actions/next-stats-action/native
- run: cp -r packages/next-swc/native .github/actions/next-stats-action/native
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}

- uses: ./.github/actions/next-stats-action
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ packages/react-dev-overlay/lib/**
**/__tmp__/**
lerna.json
.github/actions/next-stats-action/.work
packages/next/build/swc/crates/**/*
packages/next-swc/crates/**/*
packages/next-codemod/transforms/__testfixtures__/**/*
packages/next-codemod/transforms/__tests__/**/*
packages/next-codemod/**/*.js
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore_staged
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
**/.next/**
**/_next/**
**/dist/**
packages/next/build/swc/crates/**
packages/next-swc/crates/**
packages/next/compiled/**/*
packages/next/bundles/webpack/packages/*.runtime.js
lerna.json
Expand Down
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ yarn build
yarn prepublish
```

By default the latest canary of the next-swc binaries will be installed and used. If you are actively working on Rust code or you need to test out the most recent Rust code that hasn't been published as a canary yet you can [install Rust](https://www.rust-lang.org/tools/install) and run `yarn --cwd packages/next build-native`.
By default the latest canary of the next-swc binaries will be installed and used. If you are actively working on Rust code or you need to test out the most recent Rust code that hasn't been published as a canary yet you can [install Rust](https://www.rust-lang.org/tools/install) and run `yarn --cwd packages/next-swc build-native`.

If you need to clean the project for any reason, use `yarn clean`.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions packages/next-swc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@next/swc",
"version": "0.0.0",
"private": true,
"scripts": {
"build-native": "napi build --platform --cargo-name next_swc_napi native"
},
"napi": {
"name": "next-swc",
"triples": {
"defaults": true,
"additional": [
"i686-pc-windows-msvc",
"aarch64-unknown-linux-gnu",
"armv7-unknown-linux-gnueabihf",
"aarch64-apple-darwin",
"aarch64-linux-android",
"x86_64-unknown-freebsd",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl",
"aarch64-pc-windows-msvc"
]
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/next/build/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function loadBindings() {
for (const triple of triples) {
const localFilePath = path.join(
__dirname,
'../../../native',
'../../../../next-swc/native',
`next-swc.${triple.platformArchABI}.node`
)
if (fs.existsSync(localFilePath)) {
Expand Down
20 changes: 1 addition & 19 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
"prepublish": "npm run release && yarn types",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist",
"typescript": "tsc --noEmit --declaration",
"ncc-compiled": "ncc cache clean && taskr ncc",
"build-native": "napi build --platform --cargo-cwd build/swc --cargo-name next_swc_napi native"
"ncc-compiled": "ncc cache clean && taskr ncc"
},
"taskr": {
"requires": [
Expand Down Expand Up @@ -277,22 +276,5 @@
},
"engines": {
"node": ">=12.22.0"
},
"napi": {
"name": "next-swc",
"triples": {
"defaults": true,
"additional": [
"i686-pc-windows-msvc",
"aarch64-unknown-linux-gnu",
"armv7-unknown-linux-gnueabihf",
"aarch64-apple-darwin",
"aarch64-linux-android",
"x86_64-unknown-freebsd",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl",
"aarch64-pc-windows-msvc"
]
}
}
}
21 changes: 15 additions & 6 deletions packages/next/server/web/next-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface Options {
trailingSlash?: boolean
}

const REGEX_LOCALHOST_HOSTNAME =
/(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::1)/

export class NextURL extends URL {
private _basePath: string
private _locale?: {
Expand All @@ -33,11 +36,12 @@ export class NextURL extends URL {
private _options: Options
private _url: URL

constructor(url: string, options: Options = {}) {
super(formatRelative(url))
constructor(input: string, options: Options = {}) {
const url = createWHATWGURL(input)
super(url)
this._options = options
this._basePath = ''
this._url = formatRelative(url)
this._url = url
this.analyzeUrl()
}

Expand Down Expand Up @@ -163,7 +167,7 @@ export class NextURL extends URL {
}

set href(url: string) {
this._url = formatRelative(url)
this._url = createWHATWGURL(url)
this.analyzeUrl()
}

Expand Down Expand Up @@ -228,8 +232,13 @@ export class NextURL extends URL {
}
}

function formatRelative(url: string) {
return url.startsWith('/')
function createWHATWGURL(url: string) {
url = url.replace(REGEX_LOCALHOST_HOSTNAME, 'localhost')
return isRelativeURL(url)
? new URL(url.replace(/^\/+/, '/'), new URL('https://localhost'))
: new URL(url)
}

function isRelativeURL(url: string) {
return url.startsWith('/')
}
9 changes: 3 additions & 6 deletions scripts/publish-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ const cwd = process.cwd()
let gitref = process.argv.slice(2)[0]

// Copy binaries to package folders, update version, and publish
let nativePackagesDir = path.join(
cwd,
'packages/next/build/swc/crates/napi/npm'
)
let nativePackagesDir = path.join(cwd, 'packages/next-swc/crates/napi/npm')
let platforms = (await readdir(nativePackagesDir)).filter(
(name) => !name.startsWith('.')
)
Expand All @@ -27,7 +24,7 @@ const cwd = process.cwd()
try {
let binaryName = `next-swc.${platform}.node`
await copy(
path.join(cwd, 'packages/next/build/swc/dist', binaryName),
path.join(cwd, 'packages/next-swc/native', binaryName),
path.join(nativePackagesDir, platform, binaryName)
)
let pkg = JSON.parse(
Expand Down Expand Up @@ -62,7 +59,7 @@ const cwd = process.cwd()
}

// Update name/version of wasm packages and publish
let wasmDir = path.join(cwd, 'packages/next/build/swc/crates/wasm')
let wasmDir = path.join(cwd, 'packages/next-swc/crates/wasm')
for (let wasmTarget of ['web', 'nodejs']) {
let wasmPkg = JSON.parse(
await readFile(path.join(wasmDir, `pkg-${wasmTarget}/package.json`))
Expand Down
4 changes: 2 additions & 2 deletions test/integration/pnpm-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ async function usingPnpmCreateNextApp(appDir, fn) {
await runPnpm(tempAppDir, 'add', `next@${nextTarballPath}`)

await fs.copy(
path.join(__dirname, '../../../../packages/next/native'),
path.join(tempAppDir, 'node_modules/next/native')
path.join(__dirname, '../../../../packages/next-swc/native'),
path.join(tempAppDir, 'node_modules/next-swc/native')
)

await fn(tempAppDir)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/create-next-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function createNextInstall(dependencies) {
const swcPkgPath = path.join(origRepoDir, 'node_modules/@next', folder)
await fs.copy(
swcPkgPath,
path.join(origRepoDir, 'packages/next/native'),
path.join(origRepoDir, 'packages/next-swc/native'),
{
filter: (item) =>
item === swcPkgPath ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ it('parses and formats the default locale', () => {
expect(url.locale).toEqual('fr')
expect(url.toString()).toEqual('/root/fr/bar')
})

it('consider 127.0.0.1 and variations as localhost', () => {
const httpUrl = new NextURL('http://localhost:3000/hello')
expect(new NextURL('http://127.0.0.1:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://127.0.1.0:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://::1:3000/hello')).toStrictEqual(httpUrl)

const httpsUrl = new NextURL('https://localhost:3000/hello')
expect(new NextURL('https://127.0.0.1:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://127.0.1.0:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://::1:3000/hello')).toStrictEqual(httpsUrl)
})

0 comments on commit e08b92a

Please sign in to comment.