Closed Danzo7 closed 3 years ago
I'm running into this too.
Some cursory investigation suggests this only occurs when the import path looks absolute. e.g. @import 'partial'
fails but @import './partial'
works. This is reasonable for CSS-style imports which are URLs, however Sass overloads the @import
method for local imports and @import 'partial'
can refer to a relative file i.e. it is functionally equal to @import './partial'
.
I think the fix might involve editing scan-imports.ts' parseFileForInstallTargets to add a special-case for handling imports in sass/scss files instead of treating them the same as css files.
As a work around, I think consumers can use dart-sass's @use
notation instead of @import
notation which will not trigger this import discovery process. (This isn't helpful for me right now as I'm working on a legacy codebase that is still using libsass).
An alternative workaround is to ensure you always include a ./
when targeting relative files when using an @import
. i.e. replace @import 'partial'
with @import './partial'
and to avoid usage of custom import paths (i.e. importing anything that doesn't start a relative path)
I'm running into this too.
Some cursory investigation suggests this only occurs when the import path looks absolute. e.g.
@import 'partial'
fails but@import './partial'
works. This is reasonable for CSS-style imports which are URLs, however Sass overloads the@import
method for local imports and@import 'partial'
can refer to a relative file i.e. it is functionally equal to@import './partial'
.I think the fix might involve editing scan-imports.ts' parseFileForInstallTargets to add a special-case for handling imports in sass/scss files instead of treating them the same as css files.
As a work around, I think consumers can use dart-sass's
@use
notation instead of@import
notation which will not trigger this import discovery process. (This isn't helpful for me right now as I'm working on a legacy codebase that is still using libsass).An alternative workaround is to ensure you always include a
./
when targeting relative files when using an@import
. i.e. replace@import 'partial'
with@import './partial'
and to avoid usage of custom import paths (i.e. importing anything that doesn't start a relative path)
I didn't know that using @import
is not even recommended anymore by the sass team
The Sass team discourages the continued use of the @import rule. Sass will gradually phase it out over the next few years, and eventually remove it from the language entirely. Prefer the @use rule instead. (Note that only Dart Sass currently supports
@use
. Users of other implementations must use the@import
rule instead.) I will considered that in the future project thank you.
just to explain why using relative path may not solve this issue in my case:
using a relative path @import './partial'
instead of @import 'partial'
will work but only when your partial.scss
is within the same path as your importer.sass
but if your project is a three-architecture with a great depth you will struggle using relative paths, that why dart-sass introduce --load-path
for CLI and includePaths
for JS API which add the ability of importing from any path that is included in this property without having to use the relative path which means for a file in ./src/someOtherPath/../_partial.scss
its can only be imported in this way @import 'partial'
.
includePaths
or relative paths, so for the first example I just do @import '@someAlias/partial'
. and replace that with the correct relative path on buildI still think its a bug in the snowpack that can accrue anytime in the future and has to be fix
I just hit this as well, but in a slightly different scenario. I'm using the loadPath
config for sass
, which results in you being able to write absolute imports that consequently seem to be picked up incorrectly as explained by @BPScott.
E.g.:
[
'@snowpack/plugin-sass',
{
compilerOptions: {
loadPath: ['node_modules'],
},
},
],
and then I have imports in my scss files like:
@import "somepackage/styles/theme";
which trigger this when I run snowpack in dev
mode:
[snowpack] ! building dependencies...
[snowpack] Package "somepackage/styles/theme" not found. Have you installed it?
However, it works as intended when running build
. Switching to a @use
does indeed also solve the problem, and is preferred in basically all (?) of the cases anyway.
I just hit this as well, but in a slightly different scenario. I'm using the
loadPath
config forsass
, which results in you being able to write absolute imports that consequently seem to be picked up incorrectly as explained by @BPScott.E.g.:
[ '@snowpack/plugin-sass', { compilerOptions: { loadPath: ['node_modules'], }, }, ],
and then I have imports in my scss files like:
@import "somepackage/styles/theme";
which trigger this when I run snowpack in
dev
mode:[snowpack] ! building dependencies... [snowpack] Package "somepackage/styles/theme" not found. Have you installed it?
However, it works as intended when running
build
. Switching to a@use
does indeed also solve the problem, and is preferred in basically all (?) of the cases anyway.
it was a wrong written regular expression and this PR will fix hopefully: https://github.com/snowpackjs/snowpack/pull/2817
OK did some investigation, and I did find that in instances where extension was omitted, we were falling back to .js
when a better guess at the extension could be inferred (for example, if we’re in a .scss
file, let’s assume that @import "component"
refers to ./component.scss
rather than ./component.js
).
I think #2817 can help in some ways—it improves import RegExs—but perhaps we can prevent Sass from even being parsed as JS in the first place.
Should this be fixed in 3.4.0
? I'm still seeing the same issue with Snowpack attempting to find JS modules for, e.g.
@import "overlay-buttons";
in my .scss
files. ([snowpack] Package "overlay-buttons" not found. Have you installed it?
).
Using relative paths:
@import "./overlay-buttons";
works just fine.
I'm using v3.8.8
and I'm still experiencing this issue with snowpack build --watch
. I had to change all my import
s to use a relative path because Snowpack kept asking me to install the missing packages.
Am I the only one experiencing this issue or is it still active?
Here's my config:
// Snowpack Configuration File
// See all supported options: https://www.snowpack.dev/reference/configuration
/** @type {import("snowpack").SnowpackUserConfig } */
module.exports = {
root: 'resources/cth/',
plugins: [
[
'@snowpack/plugin-sass',
{
compilerOptions: {
// Temporary fix
quiet: true,
},
},
],
'@snowpack/plugin-postcss',
],
devOptions: {},
packageOptions: {
polyfillNode: true,
},
buildOptions: {
out: 'public/application/themes/cth/snowpack/',
},
optimize: {
// bundle: true,
minify: true,
target: 'es2017',
treeshake: true,
// entrypoints: ['public/application/single_pages/**/*.php', 'public/application/themes/**/*.php'],
},
};
Error when import SASS partials : only when using
snowpack dev
snowpack think
partial.scss
is a module for some reason when read an .SCSS file that contain@import 'partial.scss';
full stack: