Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for __dirname #8334

Closed
wants to merge 18 commits into from
Closed
13 changes: 13 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export default async function getBaseWebpackConfig(
mode: webpackMode,
name: isServer ? 'server' : 'client',
target: isServer ? 'node' : 'web',
cache: isServer && dev ? false : true,
Timer marked this conversation as resolved.
Show resolved Hide resolved
externals: !isServer
? undefined
: !isServerless
Expand Down Expand Up @@ -501,6 +502,18 @@ export default async function getBaseWebpackConfig(
include: [path.join(dir, 'data')],
use: 'next-data-loader',
},
isServer && {
test: /\.(tsx|ts|js|mjs|jsx|node)$/,
parser: { amd: false },
use: {
loader: '@zeit/webpack-asset-relocator-loader',
options: {
filterAssetBase: dir,
production: !dev,
debugLog: false,
Timer marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: [
Expand Down
1 change: 1 addition & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@babel/preset-typescript": "7.3.3",
"@babel/runtime": "7.4.5",
"@babel/runtime-corejs2": "7.4.5",
"@zeit/webpack-asset-relocator-loader": "0.6.2",
"amphtml-validator": "1.0.23",
"async-sema": "3.0.0",
"autodll-webpack-plugin": "0.4.2",
Expand Down
11 changes: 11 additions & 0 deletions test/integration/api-support/pages/api/dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'fs'
import path from 'path'

export default (req, res) => {
const fileContent = fs.readFileSync(
path.resolve(__dirname, '..', 'index.js'),
'utf8'
)

res.end(fileContent)
}
46 changes: 45 additions & 1 deletion test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
renderViaHTTP,
nextBuild,
nextStart,
File
File,
waitFor
} from 'next-test-utils'
import json from '../big.json'
import { createServer } from 'http'
Expand Down Expand Up @@ -248,6 +249,49 @@ function runTests (serverless = false) {
expect(res.status).toBe(404)
})

it('should work with __dirname', async () => {
const data = await fetchViaHTTP(appPort, '/api/dirname', null, {}).then(
res => res.ok && res.text()
)

expect(data).toContain('export default () => <div>API - support</div>')
})

it('should work with __dirname and re-compile', async () => {
const data = await fetchViaHTTP(appPort, '/api/dirname', null, {}).then(
res => res.ok && res.text()
)

expect(data).toContain('export default () => <div>API - support</div>')

const index = new File(join(appDir, 'pages/index.js'))

index.replace('API', 'IPA')

await waitFor(500)

const change = await fetchViaHTTP(appPort, '/api/dirname', null, {}).then(
res => res.ok && res.text()
)
expect(change).toContain('export default () => <div>IPA - support</div>')

index.restore()

const dirname = new File(join(appDir, 'pages/api/dirname.js'))

dirname.replace('index.js', 'user.js')

await waitFor(500)

const user = await fetchViaHTTP(appPort, '/api/dirname', null, {}).then(
res => res.ok && res.text()
)

expect(user).toContain('export default () => <div>User</div>')

dirname.restore()
})

it('should build api routes', async () => {
await nextBuild(appDir, [], { stdout: true })
if (serverless) {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,13 @@
dependencies:
"@babel/preset-typescript" "^7.0.0"

"@zeit/webpack-asset-relocator-loader@0.6.2":
version "0.6.2"
resolved "https://registry.yarnpkg.com/@zeit/webpack-asset-relocator-loader/-/webpack-asset-relocator-loader-0.6.2.tgz#b15353d8578562f299eddb94d4b771297acc0487"
integrity sha512-IUz2YvFAv03LHP4dhjHqG549ecZhdp7KU8B+vGfJRgcDzvlCmc9CA1YcYfJHVO96U8dU5z85RIysr2whYUXmgQ==
dependencies:
sourcemap-codec "^1.4.4"

JSONStream@^1.0.4, JSONStream@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -12640,6 +12647,11 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=

sourcemap-codec@^1.4.4:
version "1.4.6"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9"
integrity sha512-1ZooVLYFxC448piVLBbtOxFcXwnymH9oUF8nRd3CuYDVvkRBxRl6pB4Mtas5a4drtL+E8LDgFkQNcgIw6tc8Hg==

spawn-sync@^1.0.15:
version "1.0.15"
resolved "https://registry.yarnpkg.com/spawn-sync/-/spawn-sync-1.0.15.tgz#b00799557eb7fb0c8376c29d44e8a1ea67e57476"
Expand Down