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
sass import with webpack 5 #87
Comments
It looks like you misunderstand. The "path/to/nodo_modules" part has to be replaced with the absolute or the relative (from your stylesheet) path to your project's node_modules folder. Anyway, I believe making package.json's When you want to use // modules.rules[]
{
// ...
use: [
// ...
{
loader: "sass-loader",
options: {
// ...
sassOptions: {
includePaths: [
path.resolve(__dirname, './node_modules'),
],
},
},
},
// ...
],
//...
},
// ... I actually have never used WebPack in actual development. So, it would be appreciated if someone corrects me if I said something wrong. |
I am actually not sure if that's true. I have also tried the above solution, however, the described error persists. At least with Webpack 5 exports specified in the module's package.json are explicitly respected (see here) and will result in an error as described above. I was however able to access the css-sources after manually "./css/": "./dist/css/.css" to the package.json (as initially suggested). Furthermore, see this quite related issue, where I also got the first link from. |
@Matu2083, I'm not saying Webpack doesn't allow users to load sass/css files using the exports field's paths. What I meant is, instead of relying on Webpack's hack, setting up sass through webpack.config would be more preferable because it allows you to use the same technique when you use gulp, rollup, or even Sass CLI, and I'd like to encourage people to do so. The example in my comment is for using It looks like the method to import you used is the one meant for importing .css file. (this may be a good example.) The issue with the location of The reason why the error was gone by changing the location is because all the variable declarations in datepicker-bs4.scss have the For the above reasons, Now, let's think about the validity of having the sass folder's path in the exports field. To me, it looks like the issues you had and your response to them, which I think inappropriate, were caused by mixing up the functionality of Sass's So, in conclusion, unless the use of package.json within Sass becomes a (at least de facto) standard, I will not add the sass folder's path to the exports field. |
For your reference, here's the code I was able to create the dist file (dist/main.js) successfully without modifying the library's source during the test. Structure:
src/index.js: import 'bootstrap';
import {Datepicker} from 'vanillajs-datepicker';
import './app.scss';
const el = document.querySelector('.date');
var datepicker = new Datepicker(el, {
buttonClass: 'btn',
clearBtn: true,
todayBtn: true,
}); src/app.scss: @import "bootstrap/scss/bootstrap";
$dp-dropdown-z: 9999;
@import "vanillajs-datepicker/sass/datepicker-bs4";
$body-color: red;
body {
color: $body-color;
} index.html: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<div class="container mt-3">
<div class="form-group"><input type="text" class="form-control date"></div>
</div>
<script type="text/javascript" src="./dist/main.js"></script>
</body>
</html> package.json: {
"dependencies": {
"bootstrap": "^4.6.1",
"css-loader": "^6.5.0",
"sass": "^1.43.4",
"sass-loader": "^12.3.0",
"style-loader": "^3.3.1",
"vanillajs-datepicker": "^1.1.4",
"webpack-cli": "^4.9.1"
},
"scripts": {
"build": "webpack-cli"
}
} webpack.config.js: const path = require('path');
module.exports = {
mode: 'development',
entry: './src',
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
"style-loader",
"css-loader",
{
loader: "sass-loader",
options: {
implementation: require("sass"),
sassOptions: {
includePaths: [path.resolve(__dirname, './node_modules')],
}
},
},
],
},
],
},
}; |
@mymth: Thank you for your detailed response. I can confirm that your above setup works. After giving it some thought I agree with your points referring to the sass folder. I think an open question is whether you want to make the dist/css available for export. |
Hi, thanks for this detailed explanation
It is not a de-facto standard, webpack supports some extra conditions described at As a non-javascript developer, I do not understand the side effects of having something like
Since |
@tagliala yes! It seems to be the right way to go. But from my experience, this approach simply DOESN'T WORK. The documentation mentions |
I don't know. I've tested it and it works with the webpack configuration provided by You can give a try to this branch: https://github.com/tagliala/vanillajs-datepicker/tree/bugfix/export-sass |
You're right @tagliala . It worked. I wonder what might be the problem on my side. Do you think there's anything else I need to put in my |
Sorry, I didn't get this, could you please clarify? |
@tagliala let's forget about it. I figured out my problem. Had to tweak the |
@tindl88 does this branch work for your use case? https://github.com/tagliala/vanillajs-datepicker/tree/bugfix/export-sass |
Thanks @tagliala, but I still see that error with that branch. package.json |
I have the same problem, but @mymth code isn't applicable in my case since I'm using vue-cli (instead of webpack). For now, I removed the |
@ziorufus It looks like vue-cli uses webpack internally. So, adding the same sass-loader options to your vue.config.js like below seems to work. const path = require('path');
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
css: {
loaderOptions: {
sass: {
sassOptions: {
includePaths: [
path.resolve(__dirname, './node_modules'),
],
},
},
},
},
}); Just in case you are unaware of this: the above is to import datepicker's scss file inside stylesheet like this: <style lang="scss">
@import 'vanillajs-datepicker/sass/datepicker';
</style> not like this: <script setup>
import from 'vanillajs-datepicker/sass/datepicker.scss';
</script> |
@mymth thank you very much, it works perfectly! |
I had the exact same problem. I can't import in my Laravel Project like this way in my app.scss:
Finally I found if I use the full path work as expected
I hope helps |
I'm using webpack 5 and bootstrap and tried to import the sass files as documented.
import 'path/to/node_modules/vanillajs-datepicker/sass/datepicker';
import 'path/to/node_modules/vanillajs-datepicker/sass/datepicker-bs4';
When I build the project, I get these errors:
ERROR in ./src/main.js
Module not found: Error: Package path ./sass/datepicker is not exported from package \node_modules\vanillajs-datepicker (see exports field in \node_modules\vanillajs-datepicker\package.json)
ERROR in ./src/main.js
Module not found: Error: Package path ./sass/datepicker-bs4 is not exported from package \node_modules\vanillajs-datepicker (see exports field in \node_modules\vanillajs-datepicker\package.json)
Found a workaround to adjust the exports in the package.json file from the datepicker:
"exports": {
".": "./js/main.js",
"./Datepicker": "./js/Datepicker.js",
"./DateRangePicker": "./js/DateRangePicker.js",
"./locales/": "./js/i18n/locales/.js",
"./locales/": "./js/i18n/locales/",
"./sass/": "./sass/.scss",
},
Can you please check this ?
The text was updated successfully, but these errors were encountered: