From c6351b964e46197e8c540f6a718cb21f4f8e696f Mon Sep 17 00:00:00 2001 From: Roger Peters Date: Tue, 1 Jun 2021 11:17:15 -0400 Subject: [PATCH 1/5] fix: import sass file even if not listed in package.json close #2499 --- packages/playground/css/__tests__/css.spec.ts | 4 +++ packages/playground/css/index.html | 3 ++ packages/playground/css/pkg-dep/_index.scss | 3 ++ packages/playground/css/pkg-dep/package.json | 4 +++ packages/playground/css/sass.scss | 2 +- packages/vite/src/node/plugins/resolve.ts | 33 ++++++++++++++----- 6 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 packages/playground/css/pkg-dep/_index.scss create mode 100644 packages/playground/css/pkg-dep/package.json diff --git a/packages/playground/css/__tests__/css.spec.ts b/packages/playground/css/__tests__/css.spec.ts index 260ef7e431249d..1f5faac7f44461 100644 --- a/packages/playground/css/__tests__/css.spec.ts +++ b/packages/playground/css/__tests__/css.spec.ts @@ -183,6 +183,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') diff --git a/packages/playground/css/index.html b/packages/playground/css/index.html index 450cc4ef266de4..a6637396df888c 100644 --- a/packages/playground/css/index.html +++ b/packages/playground/css/index.html @@ -32,6 +32,9 @@

CSS

@import from SASS _partial: This should be orchid

Imported SASS string:


+  

+ @import dependency w/ no scss entrypoint: this should be lavender +

Less: This should be blue

diff --git a/packages/playground/css/pkg-dep/_index.scss b/packages/playground/css/pkg-dep/_index.scss new file mode 100644 index 00000000000000..cf8514edadfcf0 --- /dev/null +++ b/packages/playground/css/pkg-dep/_index.scss @@ -0,0 +1,3 @@ +.sass-dep { + color: lavender; +} diff --git a/packages/playground/css/pkg-dep/package.json b/packages/playground/css/pkg-dep/package.json new file mode 100644 index 00000000000000..74083b11d10de7 --- /dev/null +++ b/packages/playground/css/pkg-dep/package.json @@ -0,0 +1,4 @@ +{ + "name": "dep", + "version": "1.0.0" +} diff --git a/packages/playground/css/sass.scss b/packages/playground/css/sass.scss index 2fc31fd40274c5..f6afe24feef9c6 100644 --- a/packages/playground/css/sass.scss +++ b/packages/playground/css/sass.scss @@ -2,7 +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 */ diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 65dea3c50ee2d6..7c5fde404fefbe 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -59,6 +59,7 @@ export interface InternalResolveOptions extends ResolveOptions { asSrc?: boolean tryIndex?: boolean tryPrefix?: string + skipPackage?: boolean preferRelative?: boolean isRequire?: boolean } @@ -283,7 +284,8 @@ function tryFsResolve( options, false, targetWeb, - options.tryPrefix + options.tryPrefix, + options.skipPackage )) ) { return res @@ -297,7 +299,8 @@ function tryFsResolve( options, false, targetWeb, - options.tryPrefix + options.tryPrefix, + options.skipPackage )) ) { return res @@ -311,7 +314,8 @@ function tryFsResolve( options, tryIndex, targetWeb, - options.tryPrefix + options.tryPrefix, + options.skipPackage )) ) { return res @@ -324,7 +328,8 @@ function tryResolveFile( options: InternalResolveOptions, tryIndex: boolean, targetWeb: boolean, - tryPrefix?: string + tryPrefix?: string, + skipPkg?: boolean ): string | undefined { let isReadable = false try { @@ -338,11 +343,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 (!skipPkg) { + 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 @@ -601,6 +608,14 @@ export function resolvePackageEntry( entryPoint = entryPoint || data.main || 'index.js' + // make sure we don't get scripts when looking for sass + if (options.mainFields && options.mainFields[0] == 'sass' && options.extensions) { + if (!options.extensions.includes(path.extname(entryPoint))) { + entryPoint = ''; + options.skipPackage = true; + } + } + // resolve object browser field in package.json const { browser: browserField } = data if (targetWeb && isObject(browserField)) { From 4e81206c1327cf0ca1e05fc61caa981634d8eda4 Mon Sep 17 00:00:00 2001 From: roger6106 Date: Tue, 1 Jun 2021 14:08:56 -0400 Subject: [PATCH 2/5] fix: simplify if statements Co-authored-by: Shinigami --- packages/vite/src/node/plugins/resolve.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 7c5fde404fefbe..d7c291b282e8f1 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -609,11 +609,12 @@ export function resolvePackageEntry( entryPoint = entryPoint || data.main || 'index.js' // make sure we don't get scripts when looking for sass - if (options.mainFields && options.mainFields[0] == 'sass' && options.extensions) { - if (!options.extensions.includes(path.extname(entryPoint))) { + if ( + options.mainFields?.[0] === 'sass' && + !(options.extensions ?? []).includes(path.extname(entryPoint)) + ) { entryPoint = ''; options.skipPackage = true; - } } // resolve object browser field in package.json From 86afee156cc7ae59937e1e56957039435c9df46b Mon Sep 17 00:00:00 2001 From: Roger Peters Date: Tue, 1 Jun 2021 14:10:40 -0400 Subject: [PATCH 3/5] fix: add optional chaining --- packages/vite/src/node/plugins/resolve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index d7c291b282e8f1..0c5f19198ef433 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -611,7 +611,7 @@ export function resolvePackageEntry( // make sure we don't get scripts when looking for sass if ( options.mainFields?.[0] === 'sass' && - !(options.extensions ?? []).includes(path.extname(entryPoint)) + !options.extensions?.includes(path.extname(entryPoint)) ) { entryPoint = ''; options.skipPackage = true; From 65d807b54c4ee4262313ba9ec199aa30c3b4a089 Mon Sep 17 00:00:00 2001 From: Roger Peters Date: Wed, 2 Jun 2021 12:49:17 -0400 Subject: [PATCH 4/5] fix: fix test --- packages/playground/css/pkg-dep/index.js | 0 packages/playground/css/pkg-dep/package.json | 3 ++- packages/playground/css/sass.scss | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 packages/playground/css/pkg-dep/index.js diff --git a/packages/playground/css/pkg-dep/index.js b/packages/playground/css/pkg-dep/index.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/playground/css/pkg-dep/package.json b/packages/playground/css/pkg-dep/package.json index 74083b11d10de7..e31c3b33e1e0da 100644 --- a/packages/playground/css/pkg-dep/package.json +++ b/packages/playground/css/pkg-dep/package.json @@ -1,4 +1,5 @@ { "name": "dep", - "version": "1.0.0" + "version": "1.0.0", + "main": "index.js" } diff --git a/packages/playground/css/sass.scss b/packages/playground/css/sass.scss index f6afe24feef9c6..3c7095418e01e6 100644 --- a/packages/playground/css/sass.scss +++ b/packages/playground/css/sass.scss @@ -2,7 +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 +@import '@/pkg-dep'; // package w/out sass field .sass { /* injected via vite.config.js */ From 56a82a4d79db4627e823e327e699c6c615847db5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 2 Aug 2021 12:32:58 +0800 Subject: [PATCH 5/5] chore: rename --- packages/vite/src/node/plugins/resolve.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index fcec519af8fa6a..c4c25abffbe000 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -59,7 +59,7 @@ export interface InternalResolveOptions extends ResolveOptions { asSrc?: boolean tryIndex?: boolean tryPrefix?: string - skipPackage?: boolean + skipPackageJson?: boolean preferRelative?: boolean isRequire?: boolean } @@ -288,7 +288,7 @@ function tryFsResolve( false, targetWeb, options.tryPrefix, - options.skipPackage + options.skipPackageJson )) ) { return res @@ -303,7 +303,7 @@ function tryFsResolve( false, targetWeb, options.tryPrefix, - options.skipPackage + options.skipPackageJson )) ) { return res @@ -318,7 +318,7 @@ function tryFsResolve( tryIndex, targetWeb, options.tryPrefix, - options.skipPackage + options.skipPackageJson )) ) { return res @@ -332,7 +332,7 @@ function tryResolveFile( tryIndex: boolean, targetWeb: boolean, tryPrefix?: string, - skipPkg?: boolean + skipPackageJson?: boolean ): string | undefined { let isReadable = false try { @@ -346,7 +346,7 @@ function tryResolveFile( if (!fs.statSync(file).isDirectory()) { return normalizePath(ensureVolumeInPath(file)) + postfix } else if (tryIndex) { - if (!skipPkg) { + if (!skipPackageJson) { const pkgPath = file + '/package.json' if (fs.existsSync(pkgPath)) { // path points to a node package @@ -617,8 +617,8 @@ export function resolvePackageEntry( options.mainFields?.[0] === 'sass' && !options.extensions?.includes(path.extname(entryPoint)) ) { - entryPoint = ''; - options.skipPackage = true; + entryPoint = '' + options.skipPackageJson = true } // resolve object browser field in package.json