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
11 changes: 11 additions & 0 deletions packages/next/build/webpack-config.ts
Expand Up @@ -495,6 +495,17 @@ 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,
},
},
},
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: [
Expand Down
1 change: 1 addition & 0 deletions packages/next/package.json
Expand Up @@ -69,6 +69,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-retry": "1.2.3",
"async-sema": "3.0.0",
Expand Down
11 changes: 11 additions & 0 deletions test/integration/api-support/pages/api/read-server.js
@@ -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)
}
11 changes: 11 additions & 0 deletions test/integration/api-support/pages/api/read-serverless.js
@@ -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)
}
10 changes: 10 additions & 0 deletions test/integration/api-support/pages/api/save.js
@@ -0,0 +1,10 @@
import fs from 'fs'
import path from 'path'

export default ({ query }, res) => {
const { target } = query

fs.writeFileSync(path.resolve(__dirname, `${target}.js`), target)

res.end(target)
}
61 changes: 59 additions & 2 deletions test/integration/api-support/test/index.test.js
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 All @@ -21,6 +22,8 @@ let server
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2

function runTests (serverless = false) {
const apiRead = `/api/read-server${serverless ? 'less' : ''}`

it('should render page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/API - support/)
Expand Down Expand Up @@ -278,6 +281,60 @@ function runTests (serverless = false) {
expect(res.status).toBe(404)
})

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

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

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

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

const index = new File(join(appDir, `pages${apiRead}.js`))

index.replace('req', '_')

await waitFor(500)

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

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

await waitFor(500)

const user = await fetchViaHTTP(appPort, apiRead, null, {}).then(
res => res.ok && res.text()
)

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

index.restore()
})

// it('should save file with __dirname', async () => {
// const target = `server${serverless ? 'less' : ''}`
// const data = await fetchViaHTTP(
// appPort,
// `/api/save?target=${target}`,
// null,
// {}
// ).then(res => res.ok && res.text())
// const filePath = join(appDir, `pages/api/${target}.js`)
// const file = readFileSync(filePath, 'utf8')
// expect(data).toBe(target)
// expect(file).toBe(target)
// unlinkSync(filePath)
// })

it('should build api routes', async () => {
await nextBuild(appDir, [], { stdout: true })
if (serverless) {
Expand Down Expand Up @@ -323,7 +380,7 @@ function runTests (serverless = false) {
{}
).then(res => res.ok && res.json())

expect(data).toEqual({ post: 'post-1', id: '1' })
expect(data).toEqual({ post: 'post-1', cid: '1' })
})

it('should compile only server code in development', async () => {
Expand Down
Expand Up @@ -40,15 +40,15 @@ describe('Empty Project', () => {

it('It should not show empty object warning for page without `getInitialProps`', async () => {
output = ''
await renderViaHTTP(appPort, '/static')
await renderViaHTTP(appPort, '/static-page')
await waitFor(100)
expect(output).not.toMatch(
/returned an empty object from `getInitialProps`/
)
})

it('should show empty object warning during client transition', async () => {
const browser = await webdriver(appPort, '/static')
const browser = await webdriver(appPort, '/static-page')
await browser.eval(`(function() {
window.gotWarn = false
const origWarn = console.warn
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -2613,6 +2613,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 @@ -12719,6 +12726,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