diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts
index 011ef401a7dd..a17b3f7e3a4b 100644
--- a/packages/next/build/webpack-config.ts
+++ b/packages/next/build/webpack-config.ts
@@ -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: [
diff --git a/packages/next/package.json b/packages/next/package.json
index d8c91eca35cd..da37eb22a1b6 100644
--- a/packages/next/package.json
+++ b/packages/next/package.json
@@ -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",
diff --git a/test/integration/api-support/pages/api/blog/[post]/comment/[id].js b/test/integration/api-support/pages/api/blog/[post]/comment/[cid].js
similarity index 100%
rename from test/integration/api-support/pages/api/blog/[post]/comment/[id].js
rename to test/integration/api-support/pages/api/blog/[post]/comment/[cid].js
diff --git a/test/integration/api-support/pages/api/read-server.js b/test/integration/api-support/pages/api/read-server.js
new file mode 100644
index 000000000000..eb5d62459f1d
--- /dev/null
+++ b/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)
+}
diff --git a/test/integration/api-support/pages/api/read-serverless.js b/test/integration/api-support/pages/api/read-serverless.js
new file mode 100644
index 000000000000..eb5d62459f1d
--- /dev/null
+++ b/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)
+}
diff --git a/test/integration/api-support/pages/api/save.js b/test/integration/api-support/pages/api/save.js
new file mode 100644
index 000000000000..a88c7ffb5308
--- /dev/null
+++ b/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)
+}
diff --git a/test/integration/api-support/test/index.test.js b/test/integration/api-support/test/index.test.js
index 2969c4a9c275..aa563142f5e2 100644
--- a/test/integration/api-support/test/index.test.js
+++ b/test/integration/api-support/test/index.test.js
@@ -10,7 +10,8 @@ import {
renderViaHTTP,
nextBuild,
nextStart,
- File
+ File,
+ waitFor
} from 'next-test-utils'
import json from '../big.json'
import { createServer } from 'http'
@@ -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/)
@@ -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 () =>
API - support
')
+ })
+
+ 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 () => API - support
')
+
+ 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 () => API - support
')
+
+ 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 () => User
')
+
+ 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) {
@@ -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 () => {
diff --git a/test/integration/empty-object-getInitialProps/pages/static.js b/test/integration/empty-object-getInitialProps/pages/static-page.js
similarity index 100%
rename from test/integration/empty-object-getInitialProps/pages/static.js
rename to test/integration/empty-object-getInitialProps/pages/static-page.js
diff --git a/test/integration/empty-object-getInitialProps/test/index.test.js b/test/integration/empty-object-getInitialProps/test/index.test.js
index 193696fb89bc..1d83d70710f1 100644
--- a/test/integration/empty-object-getInitialProps/test/index.test.js
+++ b/test/integration/empty-object-getInitialProps/test/index.test.js
@@ -40,7 +40,7 @@ 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`/
@@ -48,7 +48,7 @@ describe('Empty Project', () => {
})
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
diff --git a/yarn.lock b/yarn.lock
index fd68fa51ec0c..d9e550a9ce1d 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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"
@@ -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"