-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: auto-import machines correctly during dev (#19)
* refactor: machines importing * fix: use `imports.extend` to implement suffix (#18) * fix: remove unnecessary code * chore: add linting to `ci` workflow * chore: fix broken workflow * ci: run workflow on all push and PR events * ci: run push only on main branch * chore: remove machine Co-authored-by: Daniel Roe <daniel@roe.dev>
- Loading branch information
1 parent
be84828
commit 3e2d73b
Showing
7 changed files
with
574 additions
and
862 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,29 @@ | ||
import { useNuxt, addImports } from '@nuxt/kit' | ||
import { Import } from 'unimport' | ||
import { globby } from 'globby' | ||
import { useNuxt, addImportsDir } from '@nuxt/kit' | ||
import { resolve } from 'pathe' | ||
|
||
import type { CustomMachinesOptions } from '../types' | ||
|
||
export const setupCustomMachines = async (machinesOptions: CustomMachinesOptions) => { | ||
export const setupCustomMachines = (machinesOptions: CustomMachinesOptions) => { | ||
const nuxt = useNuxt() | ||
|
||
const nuxtSrcDir = nuxt.options.srcDir | ||
const { dir, importSuffix } = machinesOptions | ||
|
||
const resolvedDir = resolve(nuxtSrcDir, dir) | ||
const resolvedDir = resolve(nuxt.options.srcDir, dir) | ||
|
||
const scannedFiles = await scanMachines(resolvedDir) | ||
addImportsDir(resolvedDir) | ||
|
||
// Map with resolved paths as key, and values as import names | ||
const resolvedNames = new Map<string, string>() | ||
nuxt.hook('imports:extend', (imports) => { | ||
for (const i of imports) { | ||
const file = i.from | ||
if (!file.startsWith(resolvedDir)) { continue } | ||
|
||
for (const file of scannedFiles) { | ||
const fullFileName = file.split('/').at(-1) | ||
const bareName = fullFileName.split('.').at(0).toLowerCase() | ||
// Update import name with import suffix | ||
const fullFileName = file.split('/').at(-1) | ||
const bareName = fullFileName.split('.').at(0).toLowerCase() | ||
|
||
resolvedNames.set(file, bareName + importSuffix) | ||
} | ||
const newName = bareName + importSuffix | ||
|
||
const imports: Import[] = [] | ||
resolvedNames.forEach((importName, filePath) => { | ||
imports.push({ | ||
from: filePath, | ||
name: 'default', | ||
as: importName | ||
}) | ||
i.as = newName | ||
} | ||
}) | ||
|
||
addImports(imports) | ||
} | ||
|
||
const scanMachines = async (resolvedDir: string) => { | ||
// Sorting to make it consistent between runs | ||
return (await globby(resolvedDir)).sort() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export interface CustomMachinesOptions { | ||
dir?: string, | ||
importSuffix?: string, | ||
} | ||
dir?: string, | ||
importSuffix?: string, | ||
} | ||
|
||
export interface ModuleOptions { | ||
minimal: boolean; | ||
customMachines: CustomMachinesOptions | false; | ||
} | ||
minimal: boolean; | ||
customMachines: CustomMachinesOptions | false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3e2d73b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nuxt-xstate – ./
nuxt-xstate-git-main-lexpeartha.vercel.app
nuxt-xstate.lexpeartha.com
nuxt-xstate-lexpeartha.vercel.app
nuxt-xstate.vercel.app