Skip to content

Commit

Permalink
fix: resolve css @import relative imports without leading dot
Browse files Browse the repository at this point in the history
fix #1737
  • Loading branch information
yyx990803 committed Jan 27, 2021
1 parent 3dcf7f8 commit 78eb32c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/config.ts
Expand Up @@ -162,6 +162,7 @@ export type ResolvedConfig = Readonly<
asSrc?: boolean
tryIndex?: boolean | string
extensions?: string[]
relativeFirst?: boolean
}) => ResolveFn
}
>
Expand Down Expand Up @@ -322,6 +323,7 @@ export async function resolveConfig(
isProduction,
isBuild: command === 'build',
asSrc: options?.asSrc || true,
relativeFirst: options?.relativeFirst || false,
tryIndex: options?.tryIndex || true,
extensions: options?.extensions
})
Expand Down
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -346,7 +346,8 @@ function createCSSResolvers(config: ResolvedConfig): CSSResolvers {
cssResolve ||
(cssResolve = config.createResolver({
extensions: ['.css'],
tryIndex: false
tryIndex: false,
relativeFirst: true
}))
)
},
Expand All @@ -356,7 +357,8 @@ function createCSSResolvers(config: ResolvedConfig): CSSResolvers {
sassResolve ||
(sassResolve = config.createResolver({
extensions: ['.scss', '.sass', '.css'],
tryIndex: '_index'
tryIndex: '_index',
relativeFirst: true
}))
)
},
Expand All @@ -365,7 +367,9 @@ function createCSSResolvers(config: ResolvedConfig): CSSResolvers {
return (
lessResolve ||
(lessResolve = config.createResolver({
extensions: ['.less', '.css']
extensions: ['.less', '.css'],
tryIndex: false,
relativeFirst: true
}))
)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -63,6 +63,7 @@ interface ResolveOptions {
*/
asSrc: boolean
tryIndex?: boolean | string
relativeFirst?: boolean
extensions?: string[]
dedupe?: string[]
}
Expand All @@ -74,6 +75,7 @@ export function resolvePlugin({
asSrc,
dedupe,
tryIndex = true,
relativeFirst = false,
extensions = SUPPORTED_EXTS
}: ResolveOptions): Plugin {
let server: ViteDevServer | undefined
Expand Down Expand Up @@ -122,7 +124,7 @@ export function resolvePlugin({
}

// relative
if (id.startsWith('.')) {
if (id.startsWith('.') || (relativeFirst && /^\w/.test(id))) {
const basedir = importer ? path.dirname(importer) : process.cwd()
let fsPath = path.resolve(basedir, id)
// handle browser field mapping for relative imports
Expand Down

0 comments on commit 78eb32c

Please sign in to comment.