Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with scss file #30

Open
vimcaw opened this issue Jun 12, 2021 · 13 comments
Open

Doesn't work with scss file #30

vimcaw opened this issue Jun 12, 2021 · 13 comments
Labels
upstream bug Bug caused by a dependency

Comments

@vimcaw
Copy link

vimcaw commented Jun 12, 2021

config:

import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';

// https://vitejs.dev/config/
export default defineConfig(configEnv => ({
  plugins: [
    tsconfigPaths({ extensions: ['ts', 'tsx', 'scss', 'css'] }),
    ...
  ],
  ...
}));

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "types": ["vite/client"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "src"
  },
  "include": ["./src"]
}

I set baseUrl to src dir, and import some scss by @use 'style/color'(there is a file in /src/style/color.scss )

Everything works fine when I manually configure the alias, but when I use vite-tsconfig-paths it throws an error:

image

2:08:14 PM [vite] Internal server error: Can't find stylesheet to import.
  ╷
1 │ @use 'style/color';
  │ ^^^^^^^^^^^^^^^^^^
  ╵
  src/style/global.scss 1:1  root stylesheet
  Plugin: vite:css
  File: /Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/src/style/global.scss
  Error: Can't find stylesheet to import.
    ╷
  1 │ @use 'style/color';
    │ ^^^^^^^^^^^^^^^^^^
    ╵
    src/style/global.scss 1:1  root stylesheet
      at Object._newRenderError (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:13174:19)
      at Object._wrapException (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:13000:16)
      at _render_closure1.call$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:81147:21)
      at _RootZone.runBinary$3$3 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:27268:18)
      at _FutureListener.handleError$1 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25818:19)
      at _Future__propagateToListeners_handleError.call$0 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:26116:49)
      at Object._Future__propagateToListeners (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:4539:77)
      at _Future._completeError$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25948:9)
      at _AsyncAwaitCompleter.completeError$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25602:12)
      at Object._asyncRethrow (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:4338:17)
2:08:14 PM [vite] Internal server error: Can't find stylesheet to import.
  ╷
1 │ @use 'style/color';
  │ ^^^^^^^^^^^^^^^^^^
  ╵
  src/style/override/formElementCommon.scss 1:1  @use
  src/style/override/index.scss 1:1              root stylesheet
  Plugin: vite:css
  File: /Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/src/style/override/formElementCommon.scss
  Error: Can't find stylesheet to import.
    ╷
  1 │ @use 'style/color';
    │ ^^^^^^^^^^^^^^^^^^
    ╵
    src/style/override/formElementCommon.scss 1:1  @use
    src/style/override/index.scss 1:1              root stylesheet
      at Object._newRenderError (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:13174:19)
      at Object._wrapException (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:13000:16)
      at _render_closure1.call$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:81147:21)
      at _RootZone.runBinary$3$3 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:27268:18)
      at _FutureListener.handleError$1 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25818:19)
      at _Future__propagateToListeners_handleError.call$0 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:26116:49)
      at Object._Future__propagateToListeners (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:4539:77)
      at _Future._completeError$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25948:9)
      at _AsyncAwaitCompleter.completeError$2 (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:25602:12)
      at Object._asyncRethrow (/Users/vimcaw/ProjectsAtWork/deep-loving-cloud-admin/node_modules/sass/sass.dart.js:4338:17)


@aleclarson
Copy link
Owner

loose: true should do the trick, and you can omit the extensions option (which is only used for resolving an import specifier without an explicit extension).

@vimcaw
Copy link
Author

vimcaw commented Jun 12, 2021

Unfortunately, after setting loose: true and remove extensions, it still doesn't work and throw the same error.

@aleclarson
Copy link
Owner

aleclarson commented Jun 12, 2021

Must be related to vitejs/vite#2683, so not an issue with this plugin.

edit: Oh I missed this part of that issue:

And this doesn't apply to SCSS.

I wonder, is the resolveId hook even called for @use statements of SCSS files?

@MetaMmodern
Copy link

Hi, @aleclarson . Maybe reopen this issue? I'm getting similar errors on my side. Trying to jump to vite from webpack.
image
image
image

@aleclarson aleclarson reopened this Dec 18, 2021
@aleclarson
Copy link
Owner

If someone can reproduce this in the ./demo folder and link it here, that would be great

@MetaMmodern
Copy link

MetaMmodern commented Dec 18, 2021

@aleclarson hi, yes, I've done a demo, it's here: https://github.com/MetaMmodern/vite-tsconfig-paths/tree/scss-test. Should I make a PR?

Btw, if I try to resolve with using just vite it kinda works, in a way that it does not throw errors, but my app looks like it hasn't downloaded styles..
Update2: the problem there was that my styles were in :global block. Seems webpack required it. I removed that top-level block and it started working with manual aliases, this plugin still does not work.

@MetaMmodern

This comment was marked as spam.

@AlanBlanchetNeovision
Copy link

Any updates ? I have this config

export default defineConfig({
 plugins: [react(), tsconfigPaths()],
 server: {
   port: 3000
 }
})

And I get this error :

[vite] Internal server error: [sass] Can't find stylesheet to import.
  ╷
1 │ @import "scss/env.scss";
  │         ^^^^^^^^^^^^^^^
  ╵
  src/components/App/index.module.scss 1:9  root stylesheet
  Plugin: vite:css

I found a workaround by manually adding paths to be resolved by vite.

export default defineConfig({
  plugins: [react(), tsconfigPaths()],
  server: {
    port: 3000
  },
  resolve: {
    alias: {
      "scss": path.resolve(__dirname, "src/scss")
    }
  }
})

Thanks

@aleclarson
Copy link
Owner

This is an upstream bug in Vite.

The resolver used by sass doesn't include custom Vite plugins.

https://github.com/vitejs/vite/blob/f148c18460337fb798dbcc510a790dc50fb479f8/packages/vite/src/node/plugins/css.ts#L708-L714

@aleclarson aleclarson added the upstream bug Bug caused by a dependency label Oct 20, 2022
@aleclarson
Copy link
Owner

aleclarson commented Oct 20, 2022

Once this PR is merged, I have confirmed this will be fixed. (But you have to use loose: true)

vitejs/vite#10555

Thank you @MetaMmodern for the reproduction!

@MetaMmodern
Copy link

@aleclarson omg, it's been 10 month, thank you))

@laudaikinhdi
Copy link

laudaikinhdi commented Feb 15, 2023

@aleclarson I added loose: true but not working in SCSS
image
image

@emosheeep
Copy link

emosheeep commented Nov 28, 2023

I was involved in trouble too…But for now, I’ve used a hack way to achieve what I want

const resolvePlugin = tsconfigPaths({
  // to support resolve alias in scss files(any other non-script files)
  loose: true, 
})

export default defineConfig({
  resolve: {
    alias: [
      {
        find: /(.*)/,
        replacement: '$1',
        async customResolver(source, importer, options) {
          // HACK: resolve ts alias in style files.
          if (importer && /\.scss/.test(importer)) {
            // @ts-ignore
            return await resolvePlugin.resolveId.call(
              this,
              source,
              importer,
              { skipSelf: true }
            );
          }
        },
      },
    ]
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream bug Bug caused by a dependency
Projects
None yet
Development

No branches or pull requests

6 participants