2017-06-08 17:41:22 +00:00
|
|
|
import { join } from 'path'
|
|
|
|
import { readdirSync, existsSync } from 'fs'
|
|
|
|
|
Process available chunk names properly in dev mode (#4604)
Fixes #4603.
Tests explain it the best:
```js
describe('development mode (no chunkhash)', () => {
it('should strip the extension from the filename', () => {
const filename = 'foo_bar_0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the extension even if there\'s a hyphen in the name', () => {
const filename = 'foo-bar-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
})
})
describe('production mode (with chunkhash)', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
})
})
```
2018-06-14 09:04:03 +00:00
|
|
|
export function getChunkNameFromFilename (filename, dev) {
|
|
|
|
if (dev) {
|
2018-06-14 17:47:49 +00:00
|
|
|
return filename.replace(/\.[^.]*$/, '')
|
Process available chunk names properly in dev mode (#4604)
Fixes #4603.
Tests explain it the best:
```js
describe('development mode (no chunkhash)', () => {
it('should strip the extension from the filename', () => {
const filename = 'foo_bar_0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the extension even if there\'s a hyphen in the name', () => {
const filename = 'foo-bar-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
})
})
describe('production mode (with chunkhash)', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
})
})
```
2018-06-14 09:04:03 +00:00
|
|
|
}
|
2018-06-09 11:46:27 +00:00
|
|
|
return filename.replace(/-[^-]*$/, '')
|
|
|
|
}
|
|
|
|
|
Process available chunk names properly in dev mode (#4604)
Fixes #4603.
Tests explain it the best:
```js
describe('development mode (no chunkhash)', () => {
it('should strip the extension from the filename', () => {
const filename = 'foo_bar_0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the extension even if there\'s a hyphen in the name', () => {
const filename = 'foo-bar-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
})
})
describe('production mode (with chunkhash)', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
})
})
```
2018-06-14 09:04:03 +00:00
|
|
|
export function getAvailableChunks (distDir, dev) {
|
2018-06-04 13:45:39 +00:00
|
|
|
const chunksDir = join(distDir, 'chunks')
|
2017-06-08 17:41:22 +00:00
|
|
|
if (!existsSync(chunksDir)) return {}
|
|
|
|
|
|
|
|
const chunksMap = {}
|
|
|
|
const chunkFiles = readdirSync(chunksDir)
|
|
|
|
|
|
|
|
chunkFiles.forEach(filename => {
|
|
|
|
if (/\.js$/.test(filename)) {
|
Process available chunk names properly in dev mode (#4604)
Fixes #4603.
Tests explain it the best:
```js
describe('development mode (no chunkhash)', () => {
it('should strip the extension from the filename', () => {
const filename = 'foo_bar_0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the extension even if there\'s a hyphen in the name', () => {
const filename = 'foo-bar-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, true)).toBe('foo-bar-0123456789abcdef')
})
})
describe('production mode (with chunkhash)', () => {
it('should strip the hash from the filename', () => {
const filename = 'foo_bar_0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo_bar_0123456789abcdef')
})
it('should only strip the part after the last hyphen in the filename', () => {
const filename = 'foo-bar-0123456789abcdef-0123456789abcdef.js'
expect(getChunkNameFromFilename(filename, false)).toBe('foo-bar-0123456789abcdef')
})
})
```
2018-06-14 09:04:03 +00:00
|
|
|
const chunkName = getChunkNameFromFilename(filename, dev)
|
2017-12-27 18:59:17 +00:00
|
|
|
chunksMap[chunkName] = filename
|
2017-06-08 17:41:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return chunksMap
|
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
const internalPrefixes = [
|
|
|
|
/^\/_next\//,
|
|
|
|
/^\/static\//
|
|
|
|
]
|
|
|
|
|
|
|
|
export function isInternalUrl (url) {
|
|
|
|
for (const prefix of internalPrefixes) {
|
|
|
|
if (prefix.test(url)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addCorsSupport (req, res) {
|
|
|
|
if (!req.headers.origin) {
|
|
|
|
return { preflight: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
2018-06-07 18:18:29 +00:00
|
|
|
// Based on https://github.com/primus/access-control/blob/4cf1bc0e54b086c91e6aa44fb14966fa5ef7549c/index.js#L158
|
2018-06-13 20:06:36 +00:00
|
|
|
if (req.headers['access-control-request-headers']) {
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers'])
|
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
res.writeHead(200)
|
|
|
|
res.end()
|
|
|
|
return { preflight: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
return { preflight: false }
|
|
|
|
}
|