Skip to content

Commit 8e90c3f

Browse files
author
Marcus Pousette
committed
feat: support for multiple assets folders
1 parent dc4cabf commit 8e90c3f

File tree

10 files changed

+120
-23
lines changed

10 files changed

+120
-23
lines changed

cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ sade2
180180
.option('--sw', 'Path to a script to be loaded in a service worker.')
181181
.option(
182182
'--assets',
183-
'Folder with assets to be served by the http server. (default process.cwd())'
183+
'One or more folders with assets to be served by the http server. (default process.cwd() will always be included)'
184184
)
185185
.option('--cwd', 'Current directory.', defaultOptions.cwd)
186186
.option(
@@ -217,6 +217,7 @@ sade2
217217
*
218218
* @type {import('./src/types.js').RunnerOptions}
219219
*/
220+
220221
const options = merge(config ? config.config : {}, {
221222
input: input ? [input, ...opts._] : undefined,
222223
testFiles: [],

mocks/assets/a/a.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": 1 }

mocks/assets/b/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

mocks/assets/test.a.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-undef */
2+
// eslint-disable-next-line strict
3+
const { is } = require('uvu/assert')
4+
5+
describe('assets', () => {
6+
it('can fetch a and package.json', async () => {
7+
// assets from cwd is available
8+
is(
9+
await fetch(new URL('package.json', import.meta.url)).then(
10+
(res) => res.status
11+
),
12+
200
13+
)
14+
15+
// assets from a
16+
is(
17+
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
18+
200
19+
)
20+
21+
// assets from b
22+
is(
23+
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
24+
404
25+
)
26+
})
27+
})

mocks/assets/test.ab.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-undef */
2+
// eslint-disable-next-line strict
3+
const { is } = require('uvu/assert')
4+
5+
describe('assets', () => {
6+
it('can fetch a, b and package.json', async () => {
7+
// assets from cwd is available
8+
is(
9+
await fetch(new URL('package.json', import.meta.url)).then(
10+
(res) => res.status
11+
),
12+
200
13+
)
14+
15+
// assets from a
16+
is(
17+
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
18+
200
19+
)
20+
21+
// assets from b
22+
is(
23+
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
24+
200
25+
)
26+
})
27+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"clean": "rm -rf pnpm-lock.yaml .coverage .nyc_output dist node_modules .tmp",
7171
"prepare": "tsc && copyfiles 'src/**/*.d.ts' dist",
7272
"test": "mocha test.js --bail --timeout 50000",
73-
"lint": "eslint . && prettier --check '*.{js,ts,yml,json}' --ignore-path .gitignore && tsc"
73+
"lint": "eslint . && prettier '*.{js,ts,yml,json}' --ignore-path .gitignore && tsc"
7474
},
7575
"dependencies": {
7676
"acorn-loose": "^8.3.0",

src/node/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const merge = mergeOptions.bind({ ignoreUndefined: true })
2828
*/
2929
const defaultOptions = {
3030
cwd: process.cwd(),
31-
assets: '',
31+
assets: undefined,
3232
browser: 'chromium',
3333
debug: false,
3434
mode: 'main', // worker

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface RunnerOptions {
1616
mode: 'main' | 'worker' | 'node'
1717
incognito: boolean
1818
extension: boolean
19-
assets: string
19+
assets?: string[]
2020
before?: string
2121
sw?: string
2222
cov: boolean
@@ -56,7 +56,7 @@ export interface CliOptions {
5656
watch?: boolean
5757
before?: string
5858
sw?: string
59-
assets: string
59+
assets?: string[]
6060
cwd: string
6161
extensions: string
6262
config?: string

src/utils/index.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const merge = mergeOptions.bind({
3131
*/
3232
export const defaultOptions = {
3333
cwd: process.cwd(),
34-
assets: '',
34+
assets: undefined,
3535
browser: 'chromium',
3636
debug: false,
3737
mode: 'main', // worker
@@ -592,13 +592,21 @@ function getPort(port = 3000, host = '127.0.0.1') {
592592
*
593593
* @param {string} dir - Runner directory
594594
* @param {string} cwd - Current working directory
595-
* @param {string} assets - Assets directory
595+
* @param {string[]| undefined} assets - Assets directories
596596
* @returns {Promise<{ url: string; server: import('http').Server }>}
597597
*/
598598
export async function createPolka(dir, cwd, assets) {
599599
const host = '127.0.0.1'
600600
const port = await getPort(0, host)
601601
const url = `http://${host}:${port}/`
602+
if (typeof assets === 'string') {
603+
assets = [assets]
604+
} else if (assets === undefined || assets === null) {
605+
assets = []
606+
}
607+
if (!assets.includes(cwd)) {
608+
assets.push(cwd)
609+
}
602610
return new Promise((resolve, reject) => {
603611
const { server } = polka()
604612
.use(
@@ -618,22 +626,24 @@ export async function createPolka(dir, cwd, assets) {
618626
)
619627
.use(
620628
// @ts-ignore
621-
sirv(path.join(cwd, assets), {
622-
dev: true,
623-
setHeaders: (
624-
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
625-
/** @type {string} */ pathname
626-
) => {
627-
// workaround for https://github.com/lukeed/sirv/issues/158 - we
628-
// can't unset the `Content-Encoding` header because sirv sets it
629-
// after this function is invoked and will only set it if it's not
630-
// already set, so we need to set it to a garbage value that will be
631-
// ignored by browsers
632-
if (pathname.endsWith('.gz')) {
633-
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
634-
}
635-
},
636-
})
629+
...assets.map((dir) =>
630+
sirv(dir, {
631+
dev: true,
632+
setHeaders: (
633+
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
634+
/** @type {string} */ pathname
635+
) => {
636+
// workaround for https://github.com/lukeed/sirv/issues/158 - we
637+
// can't unset the `Content-Encoding` header because sirv sets it
638+
// after this function is invoked and will only set it if it's not
639+
// already set, so we need to set it to a garbage value that will be
640+
// ignored by browsers
641+
if (pathname.endsWith('.gz')) {
642+
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
643+
}
644+
},
645+
})
646+
)
637647
)
638648
.listen(port, host, (/** @type {Error} */ err) => {
639649
if (err) {

test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,36 @@ describe('uvu', () => {
310310
})
311311
})
312312

313+
describe('assets', () => {
314+
it('1 asset', async () => {
315+
const proc = await execa('./cli.js', [
316+
'mocks/assets/test.a.js',
317+
'--runner',
318+
'mocha',
319+
'--assets',
320+
'./mocks/assets/a',
321+
])
322+
323+
is(proc.exitCode, 0, 'exit code')
324+
ok(proc.stdout.includes('passing'), 'process stdout')
325+
})
326+
327+
it('2 assets', async () => {
328+
const proc = await execa('./cli.js', [
329+
'mocks/assets/test.ab.js',
330+
'--runner',
331+
'mocha',
332+
'--assets',
333+
'./mocks/assets/a',
334+
'--assets',
335+
'./mocks/assets/b',
336+
])
337+
338+
is(proc.exitCode, 0, 'exit code')
339+
ok(proc.stdout.includes('passing'), 'process stdout')
340+
})
341+
})
342+
313343
describe.skip('benchmark', function () {
314344
it('benchmark', async () => {
315345
const proc = await execa('./cli.js', [

0 commit comments

Comments
 (0)