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

Serverless asset compatibility (e.g. Firebase) #7758

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"eslint": "5.16.0",
"eslint-plugin-typescript": "0.14.0",
"express": "4.17.0",
"firebase": "6.2.1",
"fs-extra": "7.0.1",
"get-port": "5.0.0",
"isomorphic-unfetch": "3.0.0",
Expand Down
24 changes: 23 additions & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ export default async function getBaseWebpackConfig(
},
}

const isProductionServerless = !dev && isServer && target === 'serverless'

const devtool = dev ? 'cheap-module-source-map' : false

let webpackConfig: webpack.Configuration = {
Expand Down Expand Up @@ -341,7 +343,7 @@ export default async function getBaseWebpackConfig(
(chunk.name === CLIENT_STATIC_FILES_RUNTIME_MAIN ||
chunk.name === CLIENT_STATIC_FILES_RUNTIME_WEBPACK)
) {
return chunk.name.replace(/\.js$/, '-[contenthash].js')
return chunk.name.replace(/\.js$/, '-[hash].js')
}
return '[name]'
},
Expand All @@ -366,10 +368,30 @@ export default async function getBaseWebpackConfig(
...nodePathList, // Support for NODE_PATH environment variable
],
},
...(isProductionServerless
? {
// `@zeit/webpack-asset-relocator-loader` will relocated all assets
// so we can't let webpack mock this to `/` & `/index.js`
node: { __dirname: false, __filename: false },
}
: undefined),
// @ts-ignore this is filtered
module: {
strictExportPresence: true,
rules: [
isProductionServerless && {
test: /\.(m?js|node)$/,
parser: { amd: false },
use: {
loader: '@zeit/webpack-asset-relocator-loader',
options: {
outputAssetBase: 'assets',
existingAssetNames: [],
wrapperCompatibility: false,
production: true,
},
},
},
(selectivePageBuilding || config.experimental.terserLoader) &&
!isServer && {
test: /\.(js|mjs|jsx)$/,
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
9 changes: 9 additions & 0 deletions test/integration/firebase/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
target: 'serverless',
onDemandEntries: {
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
autoExport: true
}
}
19 changes: 19 additions & 0 deletions test/integration/firebase/pages/about/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import firebase from 'firebase/app'
import 'firebase/firestore'

if (!firebase.apps.length) {
firebase.initializeApp({ projectId: 'noop' })
}

const store = firebase.firestore()

const Comp = ({ results }) => {
return <div>Hello Firebase: {results}</div>
}

Comp.getInitialProps = async () => {
const query = await store.collection('users').get()
return { results: query.size }
}

export default Comp
28 changes: 28 additions & 0 deletions test/integration/firebase/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP
} from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir, [])
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/about/history')
expect(html).toMatch(/Hello Firebase: <!-- -->0/)
})
})
4 changes: 3 additions & 1 deletion test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export function buildTS (args = [], cwd, env = {}) {
instance.stderr.on('data', handleData)

instance.on('exit', code => {
if (code) { return reject(new Error('exited with code: ' + code + '\n' + output)) }
if (code) {
return reject(new Error('exited with code: ' + code + '\n' + output))
}
resolve()
})
})
Expand Down
Loading