Skip to content

Commit

Permalink
fix: import sass file even if not listed in package.json (#3627)
Browse files Browse the repository at this point in the history
  • Loading branch information
roger6106 authored Aug 2, 2021
1 parent a5d0e8f commit a5b2b4f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ test('@import dependency w/ stylus entry', async () => {
expect(await getColor('.css-dep-stylus')).toBe('red')
})

test('@import dependency w/out package scss', async () => {
expect(await getColor('.sass-dep')).toBe('lavender')
})

test('async chunk', async () => {
const el = await page.$('.async')
expect(await getColor(el)).toBe('teal')
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h1>CSS</h1>
<p class="sass-partial">@import from SASS _partial: This should be orchid</p>
<p>Imported SASS string:</p>
<pre class="imported-sass"></pre>
<p class="sass-dep">
@import dependency w/ no scss entrypoint: this should be lavender
</p>

<p class="less">Less: This should be blue</p>
<p class="less-at-import">
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/pkg-dep/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sass-dep {
color: lavender;
}
Empty file.
5 changes: 5 additions & 0 deletions packages/playground/css/pkg-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "dep",
"version": "1.0.0",
"main": "index.js"
}
1 change: 1 addition & 0 deletions packages/playground/css/sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@import '@/nested/partial'; // sass convention: omitting leading _ for partials
@import 'css-dep'; // package w/ sass entry points
@import 'virtual-dep'; // virtual file added through importer
@import '@/pkg-dep'; // package w/out sass field

.sass {
/* injected via vite.config.js */
Expand Down
34 changes: 25 additions & 9 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface InternalResolveOptions extends ResolveOptions {
asSrc?: boolean
tryIndex?: boolean
tryPrefix?: string
skipPackageJson?: boolean
preferRelative?: boolean
isRequire?: boolean
}
Expand Down Expand Up @@ -286,7 +287,8 @@ function tryFsResolve(
options,
false,
targetWeb,
options.tryPrefix
options.tryPrefix,
options.skipPackageJson
))
) {
return res
Expand All @@ -300,7 +302,8 @@ function tryFsResolve(
options,
false,
targetWeb,
options.tryPrefix
options.tryPrefix,
options.skipPackageJson
))
) {
return res
Expand All @@ -314,7 +317,8 @@ function tryFsResolve(
options,
tryIndex,
targetWeb,
options.tryPrefix
options.tryPrefix,
options.skipPackageJson
))
) {
return res
Expand All @@ -327,7 +331,8 @@ function tryResolveFile(
options: InternalResolveOptions,
tryIndex: boolean,
targetWeb: boolean,
tryPrefix?: string
tryPrefix?: string,
skipPackageJson?: boolean
): string | undefined {
let isReadable = false
try {
Expand All @@ -341,11 +346,13 @@ function tryResolveFile(
if (!fs.statSync(file).isDirectory()) {
return normalizePath(ensureVolumeInPath(file)) + postfix
} else if (tryIndex) {
const pkgPath = file + '/package.json'
if (fs.existsSync(pkgPath)) {
// path points to a node package
const pkg = loadPackageData(pkgPath)
return resolvePackageEntry(file, pkg, options, targetWeb)
if (!skipPackageJson) {
const pkgPath = file + '/package.json'
if (fs.existsSync(pkgPath)) {
// path points to a node package
const pkg = loadPackageData(pkgPath)
return resolvePackageEntry(file, pkg, options, targetWeb)
}
}
const index = tryFsResolve(file + '/index', options)
if (index) return index + postfix
Expand Down Expand Up @@ -605,6 +612,15 @@ export function resolvePackageEntry(

entryPoint = entryPoint || data.main || 'index.js'

// make sure we don't get scripts when looking for sass
if (
options.mainFields?.[0] === 'sass' &&
!options.extensions?.includes(path.extname(entryPoint))
) {
entryPoint = ''
options.skipPackageJson = true
}

// resolve object browser field in package.json
const { browser: browserField } = data
if (targetWeb && isObject(browserField)) {
Expand Down

0 comments on commit a5b2b4f

Please sign in to comment.