Skip to content

Commit

Permalink
extract out processing of options from main script
Browse files Browse the repository at this point in the history
  • Loading branch information
hmsk committed Apr 16, 2024
1 parent d3b9874 commit fbc9658
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 34 deletions.
39 changes: 10 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { findUp } from 'find-up'
import { injectAssets } from './assetsInjector.js'
import { injectHMR } from './hmrInjector.js'
import { acquireLock } from './mutex.js'
import { parseOptions } from './pluginOptions.js'
/* eslint-enable @typescript-eslint/ban-ts-comment */

const trimDebugMessage = (code: string): string => code.replace(/(console\.warn\('Compiled in DEBUG mode)/, '// $1')
Expand All @@ -32,28 +33,9 @@ const findClosestElmJson = async (pathname: string) => {
return elmJson ? dirname(elmJson) : undefined
}

type NodeElmCompilerOptions = {
cwd?: string
docs?: string
debug?: boolean
optimize?: boolean
processOpts?: Record<string, string>
report?: string
pathToElm?: string
verbose?: boolean
}

export const plugin = (
opts: {
debug?: boolean
optimize?: boolean
nodeElmCompilerOptions?: NodeElmCompilerOptions
} = {},
): Plugin => {
export const plugin = (userOptions: Parameters<typeof parseOptions>[0] = {}): Plugin => {
const options = parseOptions(userOptions)
const compilableFiles: Map<string, Set<string>> = new Map()
const debug = opts.debug
const optimize = opts.optimize
const compilerOptionsOverwrite = opts.nodeElmCompilerOptions ?? {}

return {
name: 'vite-plugin-elm',
Expand Down Expand Up @@ -93,8 +75,8 @@ export const plugin = (
if (moduleId === id) break
importer = moduleId
}
const resolveAcoompany = async (accompany: string) => (await this.resolve(accompany, importer))?.id ?? ''
return Promise.all(withParams.map(resolveAcoompany))
const resolveAccompany = async (accompany: string) => (await this.resolve(accompany, importer))?.id ?? ''
return Promise.all(withParams.map(resolveAccompany))
} else {
return Promise.resolve([])
}
Expand All @@ -110,14 +92,13 @@ export const plugin = (

const releaseLock = await acquireLock()
try {
const isBuild = process.env.NODE_ENV === 'production'
const compiled: string = await compiler.compileToString(targets, {
output: '.js',
optimize: typeof optimize === 'boolean' ? optimize : !debug && isBuild,
verbose: isBuild,
debug: debug ?? !isBuild,
optimize: options.optimize,
verbose: options.isBuild,
debug: options.debug,
cwd: await findClosestElmJson(pathname),
...compilerOptionsOverwrite,
...options.nodeElmCompilerOptionsOverwrite,
})

const esm = injectAssets(toESModule(compiled))
Expand All @@ -128,7 +109,7 @@ export const plugin = (
}

return {
code: isBuild ? esm : trimDebugMessage(injectHMR(esm, dependencies.map(viteProjectPath))),
code: options.isBuild ? esm : trimDebugMessage(injectHMR(esm, dependencies.map(viteProjectPath))),
map: null,
}
} catch (e) {
Expand Down
69 changes: 66 additions & 3 deletions src/pluginOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
import { expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { parseOptions } from './pluginOptions.js'

it('returns "a"', () => {
expect(parseOptions()).toBe('a')
type Result = ReturnType<typeof parseOptions>

const defaultDevOptions: Result = {
isBuild: false,
debug: true,
optimize: false,
nodeElmCompilerOptionsOverwrite: {},
}

describe('in dev build', () => {
it('returns default in dev', () => {
expect(parseOptions({})).toEqual(defaultDevOptions)
})

it('overwrites debug and optimize as it given', () => {
expect(parseOptions({ debug: false, optimize: true })).toEqual({
...defaultDevOptions,
debug: false,
optimize: true,
})
})

it('passes thru options for node-elm-compiler', () => {
expect(
parseOptions({
nodeElmCompilerOptions: { pathToElm: 'wherever' },
}),
).toEqual({
...defaultDevOptions,
nodeElmCompilerOptionsOverwrite: { pathToElm: 'wherever' },
})
})
})

describe('in production build', () => {
beforeEach(() => {
vi.stubEnv('NODE_ENV', 'production')
})

it('returns default for prod build', () => {
expect(parseOptions({})).toEqual({
...defaultDevOptions,
isBuild: true,
debug: false,
optimize: true,
})
})

it('overwrites debug and optimize per just only debug', () => {
expect(parseOptions({ debug: true })).toEqual({
...defaultDevOptions,
isBuild: true,
debug: true,
optimize: false,
})
})

it('overwrites debug and optimize', () => {
expect(parseOptions({ debug: true, optimize: false })).toEqual({
...defaultDevOptions,
isBuild: true,
debug: true,
optimize: false,
})
})
})
34 changes: 32 additions & 2 deletions src/pluginOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
export const parseOptions = () => {
return 'a'
interface NodeElmCompilerOptions {
cwd?: string
docs?: string
debug?: boolean
optimize?: boolean
processOpts?: Record<string, string>
report?: string
pathToElm?: string
verbose?: boolean
}

interface InputOptions {
debug?: boolean
optimize?: boolean
nodeElmCompilerOptions?: NodeElmCompilerOptions
}

interface ParsedOptions {
isBuild: boolean
debug: boolean
optimize: boolean
nodeElmCompilerOptionsOverwrite: NodeElmCompilerOptions
}

export const parseOptions = (inputOptions: InputOptions): ParsedOptions => {
const isBuild = process.env.NODE_ENV === 'production'
return {
isBuild,
debug: inputOptions.debug ?? !isBuild,
optimize: typeof inputOptions.optimize === 'boolean' ? inputOptions.optimize : !inputOptions.debug && isBuild,
nodeElmCompilerOptionsOverwrite: inputOptions.nodeElmCompilerOptions ?? {},
}
}

0 comments on commit fbc9658

Please sign in to comment.