Skip to content

Commit

Permalink
fix: namespace regression, close #25
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 25, 2021
1 parent 5b25e90 commit 4439274
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
],
"scripts": {
"dev": "npm run build -- --watch",
"example:dev": "npm -C example run dev",
"example:build": "npm -C example run build",
"example:dev": "npm -C example/basic run dev",
"example:build": "npm -C example/basic run build",
"build": "tsup src/index.ts --dts --format cjs,esm",
"prepublishOnly": "npm run build",
"release": "npx git-ensure -a && npx bumpp --commit --tag --push"
Expand Down
55 changes: 46 additions & 9 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative, resolve } from 'path'
import { relative } from 'path'
import Debug from 'debug'
import chokidar from 'chokidar'
import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
Expand All @@ -24,7 +24,7 @@ export class Context {
options: Options,
public readonly viteConfig: ResolvedConfig,
) {
this.options = resolveOptions(options)
this.options = resolveOptions(options, viteConfig)
const { globs, dirs } = this.options

if (viteConfig.command === 'serve') {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Required<Options>,
libraries: UILibraryOptions[]
extensions: string[]
dirs: string[]
resolvedDirs: string[]
globs: string[]
}

Expand Down
9 changes: 5 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, parse } from 'path'
import { join, parse, resolve } from 'path'
import minimatch from 'minimatch'
import { ResolvedConfig } from 'vite'
import { ComponentInfo, ResolvedOptions, Options } from './types'
Expand Down Expand Up @@ -75,7 +75,7 @@ export function stringifyComponentImport({ name, path, importName }: ComponentIn
return `import ${name} from '${path}'`
}

export function resolveOptions(options: Options): ResolvedOptions {
export function resolveOptions(options: Options, viteConfig: ResolvedConfig): ResolvedOptions {
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
resolved.libraries = toArray(resolved.libraries).map(i => typeof i === 'string' ? { name: i } : i)
resolved.customComponentResolvers = toArray(resolved.customComponentResolvers)
Expand All @@ -87,6 +87,7 @@ export function resolveOptions(options: Options): ResolvedOptions {
: `{${resolved.extensions.join(',')}}`

resolved.dirs = toArray(resolved.dirs)
resolved.resolvedDirs = resolved.dirs.map(i => resolve(viteConfig.root, i))

resolved.globs = resolved.dirs.map(i =>
resolved.deep
Expand All @@ -101,14 +102,14 @@ export function resolveOptions(options: Options): ResolvedOptions {
}

export function getNameFromFilePath(filePath: string, options: ResolvedOptions): string {
const { dirs, directoryAsNamespace, globalNamespaces } = options
const { resolvedDirs, directoryAsNamespace, globalNamespaces } = options

const parsedFilePath = parse(filePath)

let strippedPath = ''

// remove include directories from filepath
for (const dir of dirs) {
for (const dir of resolvedDirs) {
if (parsedFilePath.dir.startsWith(dir)) {
strippedPath = parsedFilePath.dir.slice(dir.length)
break
Expand Down

0 comments on commit 4439274

Please sign in to comment.