Skip to content

Commit

Permalink
feat: support for multiple assets folders
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Apr 3, 2024
1 parent dc4cabf commit 8f1b1f2
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ sade2
.option('--sw', 'Path to a script to be loaded in a service worker.')
.option(
'--assets',
'Folder with assets to be served by the http server. (default process.cwd())'
'One or more folders with assets to be served by the http server. (default process.cwd() will always be included)'
)
.option('--cwd', 'Current directory.', defaultOptions.cwd)
.option(
Expand Down Expand Up @@ -217,6 +217,7 @@ sade2
*
* @type {import('./src/types.js').RunnerOptions}
*/

const options = merge(config ? config.config : {}, {
input: input ? [input, ...opts._] : undefined,
testFiles: [],
Expand Down
1 change: 1 addition & 0 deletions mocks/assets/a/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "a": 1 }
1 change: 1 addition & 0 deletions mocks/assets/b/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
27 changes: 27 additions & 0 deletions mocks/assets/test.a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-undef */
// eslint-disable-next-line strict
const { is } = require('uvu/assert')

describe('assets', () => {
it('can fetch a and package.json', async () => {
// assets from cwd is available
is(
await fetch(new URL('package.json', import.meta.url)).then(
(res) => res.status
),
200
)

// assets from a
is(
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
200
)

// assets from b
is(
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
404
)
})
})
27 changes: 27 additions & 0 deletions mocks/assets/test.ab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-undef */
// eslint-disable-next-line strict
const { is } = require('uvu/assert')

describe('assets', () => {
it('can fetch a, b and package.json', async () => {
// assets from cwd is available
is(
await fetch(new URL('package.json', import.meta.url)).then(
(res) => res.status
),
200
)

// assets from a
is(
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
200
)

// assets from b
is(
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
200
)
})
})
2 changes: 1 addition & 1 deletion src/node/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const merge = mergeOptions.bind({ ignoreUndefined: true })
*/
const defaultOptions = {
cwd: process.cwd(),
assets: '',
assets: undefined,
browser: 'chromium',
debug: false,
mode: 'main', // worker
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface RunnerOptions {
mode: 'main' | 'worker' | 'node'
incognito: boolean
extension: boolean
assets: string
assets?: string[]
before?: string
sw?: string
cov: boolean
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface CliOptions {
watch?: boolean
before?: string
sw?: string
assets: string
assets?: string[]
cwd: string
extensions: string
config?: string
Expand Down
46 changes: 28 additions & 18 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const merge = mergeOptions.bind({
*/
export const defaultOptions = {
cwd: process.cwd(),
assets: '',
assets: undefined,
browser: 'chromium',
debug: false,
mode: 'main', // worker
Expand Down Expand Up @@ -592,13 +592,21 @@ function getPort(port = 3000, host = '127.0.0.1') {
*
* @param {string} dir - Runner directory
* @param {string} cwd - Current working directory
* @param {string} assets - Assets directory
* @param {string[]| undefined} assets - Assets directories
* @returns {Promise<{ url: string; server: import('http').Server }>}
*/
export async function createPolka(dir, cwd, assets) {
const host = '127.0.0.1'
const port = await getPort(0, host)
const url = `http://${host}:${port}/`
if (typeof assets === 'string') {
assets = [assets]
} else if (assets === undefined || assets === null) {
assets = []
}
if (!assets.includes(cwd)) {
assets.push(cwd)
}
return new Promise((resolve, reject) => {
const { server } = polka()
.use(
Expand All @@ -618,22 +626,24 @@ export async function createPolka(dir, cwd, assets) {
)
.use(
// @ts-ignore
sirv(path.join(cwd, assets), {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
/** @type {string} */ pathname
) => {
// workaround for https://github.com/lukeed/sirv/issues/158 - we
// can't unset the `Content-Encoding` header because sirv sets it
// after this function is invoked and will only set it if it's not
// already set, so we need to set it to a garbage value that will be
// ignored by browsers
if (pathname.endsWith('.gz')) {
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
}
},
})
...assets.map((dir) =>
sirv(dir, {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
/** @type {string} */ pathname
) => {
// workaround for https://github.com/lukeed/sirv/issues/158 - we
// can't unset the `Content-Encoding` header because sirv sets it
// after this function is invoked and will only set it if it's not
// already set, so we need to set it to a garbage value that will be
// ignored by browsers
if (pathname.endsWith('.gz')) {
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
}
},
})
)
)
.listen(port, host, (/** @type {Error} */ err) => {
if (err) {
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,36 @@ describe('uvu', () => {
})
})

describe('assets', () => {
it('1 asset', async () => {
const proc = await execa('./cli.js', [
'mocks/assets/test.a.js',
'--runner',
'mocha',
'--assets',
'./mocks/assets/a',
])

is(proc.exitCode, 0, 'exit code')
ok(proc.stdout.includes('passing'), 'process stdout')
})

it('2 assets', async () => {
const proc = await execa('./cli.js', [
'mocks/assets/test.ab.js',
'--runner',
'mocha',
'--assets',
'./mocks/assets/a',
'--assets',
'./mocks/assets/b',
])

is(proc.exitCode, 0, 'exit code')
ok(proc.stdout.includes('passing'), 'process stdout')
})
})

describe.skip('benchmark', function () {
it('benchmark', async () => {
const proc = await execa('./cli.js', [
Expand Down

0 comments on commit 8f1b1f2

Please sign in to comment.