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 850a244
Show file tree
Hide file tree
Showing 257 changed files with 90 additions and 65 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/**
52 changes: 26 additions & 26 deletions .github/workflows/build_test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,12 @@ jobs:
- uses: actions/download-artifact@v2
with:
name: next-swc-binaries
path: packages/next/build/swc/dist
path: packages/next-swc/native

- uses: actions/download-artifact@v2
with:
name: wasm-binaries
path: packages/next/build/swc/crates/wasm
path: packages/next-swc/crates/wasm

- run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- run: ./scripts/publish-native.js $GITHUB_REF
Expand Down Expand Up @@ -592,7 +592,7 @@ jobs:
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/**') }}
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 @@ -601,7 +601,7 @@ jobs:
- name: Cache built files
uses: actions/cache@v2
with:
path: ./packages/next/build/swc/target
path: ./packages/next-swc/target
key: next-swc-cargo-cache-ubuntu-18.04--${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
next-swc-cargo-cache-ubuntu-18.04
Expand All @@ -615,7 +615,7 @@ 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/upload-artifact@v2.2.4
Expand Down Expand Up @@ -645,7 +645,7 @@ jobs:
with:
toolchain: nightly-2021-08-12
profile: minimal
- run: cd packages/next/build/swc && cargo test
- run: cd packages/next-swc && cargo test
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}

# Build binaries for publishing
Expand Down Expand Up @@ -724,7 +724,7 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.${{ matrix.name }}.node
key: next-swc-nightly-2021-08-12-${{ matrix.target }}-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-${{ matrix.target }}-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Cross build aarch64 setup
if: ${{ matrix.target == 'aarch64-apple-darwin' }}
Expand All @@ -741,7 +741,7 @@ jobs:
- name: Cache built files
uses: actions/cache@v2
with:
path: ./packages/next/build/swc/target
path: ./packages/next-swc/target
key: next-swc-cargo-cache-${{ matrix.os }}--${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
next-swc-cargo-cache-${{ matrix.os }}
Expand All @@ -751,7 +751,7 @@ jobs:
run: yarn build-native --release --target ${{ matrix.target }}
env:
MACOSX_DEPLOYMENT_TARGET: '10.13'
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2.2.4
Expand Down Expand Up @@ -807,13 +807,13 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.win32-ia32-msvc.node
key: next-swc-nightly-2021-08-12-win32-ia32-msvc-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-win32-ia32-msvc-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Build
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
shell: bash
run: yarn build-native --release --target i686-pc-windows-msvc
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -853,13 +853,13 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.win32-arm64-msvc.node
key: next-swc-nightly-2021-08-12-win32-arm64-msvc-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-win32-arm64-msvc-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Build
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
shell: bash
run: yarn build-native --release --target aarch64-pc-windows-msvc
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -902,7 +902,7 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.linux-x64-musl.node
key: next-swc-nightly-2021-08-12-linux-x64-musl-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-linux-x64-musl-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: 'Build'
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
Expand Down Expand Up @@ -961,12 +961,12 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.linux-arm64-gnu.node
key: next-swc-nightly-2021-08-12-linux-arm64-gnu-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-linux-arm64-gnu-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Cross build aarch64
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
run: yarn build-native --release --target aarch64-unknown-linux-gnu
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -1018,12 +1018,12 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.linux-arm64-musl.node
key: next-swc-nightly-2021-08-12-linux-arm64-musl-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-linux-arm64-musl-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Cross build aarch64
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
run: yarn build-native --release --target aarch64-unknown-linux-musl
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -1077,12 +1077,12 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.linux-arm-gnueabihf.node
key: next-swc-nightly-2021-08-12-linux-arm-gnueabihf-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-linux-arm-gnueabihf-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Cross build aarch64
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
run: yarn build-native --release --target armv7-unknown-linux-gnueabihf
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -1122,15 +1122,15 @@ jobs:
uses: actions/cache@v2
with:
path: packages/next/native/next-swc.android-arm64.node
key: next-swc-nightly-2021-08-12-android-arm64-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next/build/swc/**') }}
key: next-swc-nightly-2021-08-12-android-arm64-${{ hashFiles('.github/workflows/build_test_deploy.yml', 'packages/next-swc/**') }}

- name: Build
if: ${{ steps.binary-cache.outputs.cache-hit != 'true' }}
shell: bash
run: |
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android24-clang"
yarn build-native --release --target aarch64-linux-android
working-directory: packages/next
working-directory: packages/next-swc

- name: Upload artifact
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -1177,15 +1177,15 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Build
run: (wasm-pack build packages/next/build/swc/crates/wasm --release --scope=next --target ${{ matrix.target }})
run: (wasm-pack build packages/next-swc/crates/wasm --release --scope=next --target ${{ matrix.target }})

- name: Add target to folder name
run: mv packages/next/build/swc/crates/wasm/pkg packages/next/build/swc/crates/wasm/pkg-${{ matrix.target }}
run: mv packages/next-swc/crates/wasm/pkg packages/next-swc/crates/wasm/pkg-${{ matrix.target }}

- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: wasm-binaries
path: packages/next/build/swc/crates/wasm/pkg-*
path: packages/next-swc/crates/wasm/pkg-*

- run: ls packages/next/build/swc/crates/wasm
- run: ls packages/next-swc/crates/wasm
6 changes: 3 additions & 3 deletions .github/workflows/pull_request_stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
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/**') }}
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,7 +77,7 @@ 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/upload-artifact@v2.2.4
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('/')
}
Loading

0 comments on commit 850a244

Please sign in to comment.