Skip to content

Commit

Permalink
test: add test coverage for resolveViteBase (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Jan 12, 2022
1 parent 8337ec9 commit b4da17c
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 36 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/js-lint.yml → .github/workflows/js.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: JS lint
name: JS Build & Test

on: [push, pull_request]

jobs:
build:
name: JS Lint
name: Vite Plugin Ruby

strategy:
matrix:
Expand All @@ -25,7 +25,11 @@ jobs:
with:
version: 5.13.6
run_install: |
- cwd: vite-plugin-ruby
- recursive: false
- name: Lint
run: pnpm lint
- name: Build
run: pnpm -C vite-plugin-ruby build

- name: Test
run: pnpm -C vite-plugin-ruby test
7 changes: 4 additions & 3 deletions vite-plugin-ruby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"dev": "npm run build -- --watch",
"example:dev": "npm -C example run dev",
"example:build": "npm -C example run build",
"build": "rimraf -rf dist && tsup src/index.ts",
"build": "tsup src/index.ts",
"prepublishOnly": "npm run build",
"test": "vitest",
"postpublish": "PACKAGE_VERSION=$(cat package.json | grep \\\"version\\\" | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag vite-plugin-ruby@$PACKAGE_VERSION && git push --tags"
},
"dependencies": {
Expand All @@ -50,11 +51,11 @@
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/node": "^14.18.5",
"rimraf": "^3.0.2",
"rollup": "^2.63.0",
"standard-version": "^9.3.2",
"tsup": "^5.11.10",
"typescript": "^4.5.4",
"vite": "^2.7.10"
"vite": "^2.7.10",
"vitest": "^0.0.141"
}
}
125 changes: 105 additions & 20 deletions vite-plugin-ruby/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vite-plugin-ruby/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- example/
- example/
- tests/
24 changes: 16 additions & 8 deletions vite-plugin-ruby/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { booleanOption, loadJsonConfig, configOptionFromEnv, slash } from './uti
import { Config, ResolvedConfig, UnifiedConfig, MultiEnvConfig, Entrypoints } from './types'

// Internal: Default configuration that is also read from Ruby.
const defaultConfig: ResolvedConfig = loadJsonConfig(resolve(__dirname, '../default.vite.json'))
export const defaultConfig: ResolvedConfig = loadJsonConfig(resolve(__dirname, '../default.vite.json'))

// Internal: Returns the files defined in the entrypoints directory that should
// be processed by rollup.
Expand Down Expand Up @@ -98,13 +98,21 @@ function coerceConfigurationValues (config: ResolvedConfig, projectRoot: string,
const buildOutputDir = join(config.publicDir, config.publicOutputDir)
const outDir = relative(root, buildOutputDir) // Vite expects it to be relative

// Add the asset host to enable usage of a CDN.
const assetHost = config.assetHost || ''
const assetHostWithProtocol = assetHost && !assetHost.startsWith('http') ? `//${assetHost}` : assetHost
const host = assetHostWithProtocol || config.base || ''
const suffix = config.publicOutputDir ? `${slash(config.publicOutputDir)}/` : ''
const base = `${host}/${suffix}`

const base = resolveViteBase(config)
const entrypoints = resolveEntrypointFiles(projectRoot, root, config)
return { ...config, root, outDir, base, entrypoints }
}

// Internal: Configures Vite's base according to the asset host and publicOutputDir.
export function resolveViteBase ({ assetHost, base, publicOutputDir }: ResolvedConfig) {
if (assetHost && !assetHost.startsWith('http')) assetHost = `//${assetHost}`

return [
ensureTrailingSlash(assetHost || base || '/'),
publicOutputDir ? ensureTrailingSlash(slash(publicOutputDir)) : '',
].join('')
}

function ensureTrailingSlash (path: string) {
return path.endsWith('/') ? path : `${path}/`
}
32 changes: 32 additions & 0 deletions vite-plugin-ruby/tests/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, test, expect } from 'vitest'
import { defaultConfig, resolveViteBase } from '@plugin/config'
import type { ResolvedConfig } from '@plugin/types'

const expectBaseFor = (config: ResolvedConfig) =>
expect(resolveViteBase({ ...defaultConfig, ...config }))

describe('resolveViteBase', () => {
test('default usage', () => {
expectBaseFor({}).toEqual('/vite/')
expectBaseFor({ publicOutputDir: '' }).toEqual('/')
expectBaseFor({ publicOutputDir: 'assets/frontend' }).toEqual('/assets/frontend/')
})

test('windows compatibility', () => {
expectBaseFor({ publicOutputDir: 'assets\\frontend' }).toEqual('/assets/frontend/')
})

test('custom base', () => {
expectBaseFor({ base: '/sub' }).toEqual('/sub/vite/')
expectBaseFor({ base: '/sub/' }).toEqual('/sub/vite/')
expectBaseFor({ base: '/sub', publicOutputDir: '' }).toEqual('/sub/')
expectBaseFor({ base: '/sub/', publicOutputDir: '' }).toEqual('/sub/')
})

test('supports asset host', () => {
expectBaseFor({ assetHost: 'assets-cdn.com' }).toEqual('//assets-cdn.com/vite/')
expectBaseFor({ assetHost: 'https://assets-cdn.com' }).toEqual('https://assets-cdn.com/vite/')
expectBaseFor({ assetHost: 'assets-cdn.com', publicOutputDir: '' }).toEqual('//assets-cdn.com/')
expectBaseFor({ assetHost: 'http://assets-cdn.com', publicOutputDir: '' }).toEqual('http://assets-cdn.com/')
})
})
7 changes: 7 additions & 0 deletions vite-plugin-ruby/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vite-plugin-ruby-tests",
"private": true,
"devDependencies": {
"vite-plugin-ruby": "workspace:*"
}
}
Loading

0 comments on commit b4da17c

Please sign in to comment.