aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.35k stars 50 forks source link

Doesn't work with scss file #30

Open vimcaw opened 3 years ago

vimcaw commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

aleclarson commented 3 years ago

Must be related to https://github.com/vitejs/vite/issues/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 commented 2 years ago

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 commented 2 years ago

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

MetaMmodern commented 2 years ago

@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 commented 2 years ago

@aleclarson , hi, did you find any time to check that?

ghost commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

https://github.com/vitejs/vite/pull/10555

Thank you @MetaMmodern for the reproduction!

MetaMmodern commented 2 years ago

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

laudaikinhdi commented 1 year ago

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

emosheeep commented 12 months ago

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 }
            );
          }
        },
      },
    ]
  }
})
tomasztomys commented 2 months ago

When will it be merged?

tgrushka commented 1 month ago

OMG, 2 years on the vite PR, no action at all.

Why can't this be fixed in this plugin instead of waiting for vite to do something? Why doesn't this plugin just add normal aliases instead of logic that depends on vite's internal workings?

This is an example of why I can't stand NodeJS projects. Seems no one can work together and make things work. Defaults are not reasonable. Things never "just work." Heavy configuration instead of convention over configuration.

aleclarson commented 1 month ago

Hi @tgrushka, sorry to hear you're frustrated. To be fair, it's not common for people to want their tsconfig.json paths to be applied in SCSS files. It's more of a nicety, really. If you want to push this forward, I would suggest forking the unfinished Vite PR (https://github.com/vitejs/vite/pull/10555).

Why doesn't this plugin just add normal aliases instead of logic that depends on vite's internal workings?

See this comment.

Why can't this be fixed in this plugin instead of waiting for vite to do something?

Maybe it can, but that would require someone who cares to implement it. Adding more maintenance burden for a fringe use case is probably out of scope, though.

This is an example of why I can't stand NodeJS projects. Seems no one can work together and make things work. Defaults are not reasonable. Things never "just work." Heavy configuration instead of convention over configuration.

I sympathize with this frustration. Sadly, everyone expects free work from maintainers. There really needs to be a VC-funded, full-stack TypeScript framework that embodies the Rails/Laravel philosophy. There might already be one? I haven't looked, since I enjoy the freedom and power of piecing together different tools into my own quasi-framework.

Anywho, wishing you the best. I know the ecosystem can be annoying at times (for some people, all the time). 💙