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

Cannot import CSS from node_modules using "~" pattern #382

Closed
ashubham opened this issue Jun 10, 2020 · 12 comments
Closed

Cannot import CSS from node_modules using "~" pattern #382

ashubham opened this issue Jun 10, 2020 · 12 comments

Comments

@ashubham
Copy link
Contributor

ashubham commented Jun 10, 2020

Describe the bug

@import '~npm_package/path/file.scss';

// styles here

Throws error:

Error: Can't find stylesheet to import.

This seems related to #291 . But the problem here is when a ~ is prefixed.

System Info

  • required vite version: 0.20.7
  • required Operating System: OSX
  • required Node version: 12.11.0

Logs (Optional if provided reproduction)

Error: Can't find stylesheet to import.
  ╷
1 │ @import '~@bootstrap/styles/_base.scss';
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  ../js/ts-packages/radiant-react/widgets/alert/muted/muted-alert.scss 1:9  root stylesheet
    at Object._newRenderError (/Users/ashish.shubham/thoughtspot/blink-v2/node_modules/sass/sass.dart.js:14477:19)
    at Object._wrapException (/Users/ashish.shubham/thoughtspot/blink-v2/node_modules/sass/sass.dart.js:14324:16)
    at StaticClosure._renderSync (/Users/ashish.shubham/thoughtspot/blink-v2/node_modules/sass/sass.dart.js:14299:18)
    at Object.Primitives_applyFunction (/Users/ashish.shubham/thoughtspot/blink-v2/node_modules/sass/sass.dart
@m4rvr
Copy link
Contributor

m4rvr commented Jun 11, 2020

What should ~ or @ refer to? I don't think this works without a defined alias.

@ashubham
Copy link
Contributor Author

@MarvinRudolph Updated the description to make it clearer, basically ~ resolves with node_module resolution algorithm to find the nearest package.

@DJ-991
Copy link

DJ-991 commented Jun 11, 2020 via email

@yyx990803
Copy link
Member

@AXWELL911 please unwatch this repo, don’t spam everyone with unsubscribe emails.

@yyx990803
Copy link
Member

~ is a convention alias configured in vue-cli. There’s no such thing in Vite.

@zzetao
Copy link
Contributor

zzetao commented Jul 18, 2020

Hi, You can use @modules/npm_package_name.

@jameshulse
Copy link

I found this issue as I made the same mistake. In vite it is simply @import 'npm_package/path/file.scss' without any extra prefix like ~.

@vesper8
Copy link

vesper8 commented Jan 28, 2021

Is there a way to set it up so that it continues to work with vue-cli but also works with vite?

I tried adding an alias in vite.config.js but no luck so far

@cypressious
Copy link

cypressious commented Feb 13, 2021

It would be nice to have a way to configure this in vite to make migration from vue-cli projects easier.

In the meantime, an ugly workaround is to define aliases for each library

resolve: {
    alias: {
        '~bulma': 'bulma',
        '~buefy': 'buefy',
    }
}

@yee94
Copy link

yee94 commented Apr 18, 2021

Add this config to vite.config.js

   resolve: {
      alias: [
        {
          find: /^~.+/,
          replacement: (val) => {
            return val.replace(/^~/, "");
          },
        },
      ],
    },

@tomocrafter
Copy link

tomocrafter commented Apr 19, 2021

@xiaobebe alias's replacement takes string rather than function. therefore it occurs type error.
and also tried replacement: "" but doesn't make @import "~something" work properly like webpack.

@he8us
Copy link

he8us commented Apr 25, 2021

I use the following config for an Electron Project I use. It seems to work perfectly:

  • @/components/Loading resolve to my src/renderer/components/Loading
  • ~bootstrap resolves to node_modules/bootstrap
  • ~@blueprintjs to node_modules/@blueprintjs`
  resolve: {
    alias: [
      {
        find: /~(.+)/,
        replacement: join(process.cwd(), 'node_modules/$1'),
      },

      {
        find: /@\//,
        replacement: join(process.cwd(), './src/renderer') + '/',
      },
    ],
  },

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests