From fbc9658ca2be1db8f1794e60ceb91c0e597ca416 Mon Sep 17 00:00:00 2001 From: hmsk Date: Tue, 16 Apr 2024 00:46:58 -0700 Subject: [PATCH] extract out processing of options from main script --- src/index.ts | 39 ++++++---------------- src/pluginOptions.test.ts | 69 +++++++++++++++++++++++++++++++++++++-- src/pluginOptions.ts | 34 +++++++++++++++++-- 3 files changed, 108 insertions(+), 34 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1a12180c..4caacdbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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') @@ -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 - report?: string - pathToElm?: string - verbose?: boolean -} - -export const plugin = ( - opts: { - debug?: boolean - optimize?: boolean - nodeElmCompilerOptions?: NodeElmCompilerOptions - } = {}, -): Plugin => { +export const plugin = (userOptions: Parameters[0] = {}): Plugin => { + const options = parseOptions(userOptions) const compilableFiles: Map> = new Map() - const debug = opts.debug - const optimize = opts.optimize - const compilerOptionsOverwrite = opts.nodeElmCompilerOptions ?? {} return { name: 'vite-plugin-elm', @@ -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([]) } @@ -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)) @@ -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) { diff --git a/src/pluginOptions.test.ts b/src/pluginOptions.test.ts index 322b6d68..814cc6d6 100644 --- a/src/pluginOptions.test.ts +++ b/src/pluginOptions.test.ts @@ -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 + +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, + }) + }) }) diff --git a/src/pluginOptions.ts b/src/pluginOptions.ts index c062ac15..93260cc0 100644 --- a/src/pluginOptions.ts +++ b/src/pluginOptions.ts @@ -1,3 +1,33 @@ -export const parseOptions = () => { - return 'a' +interface NodeElmCompilerOptions { + cwd?: string + docs?: string + debug?: boolean + optimize?: boolean + processOpts?: Record + 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 ?? {}, + } }