简体   繁体   English

我正在尝试将 SASS 变量导入到我的 javascript 文件中

[英]I am trying to import SASS variables into my javascript file

I an attempt to get my .js file to see SASS variables, I've followed this article .我试图让我的.js文件查看 SASS 变量,我已经关注了这篇文章 This question features an error I get from attempting this method.这个问题的特点是我尝试这种方法时遇到的错误。 But even if you have had success importing SASS variables in javascript in another way, please do share your success with me!但是,即使您以另一种方式成功地在 javascript 中导入 SASS 变量,也请与我分享您的成功!

I will show you the code I've written and the error I get.我会告诉你我写的代码和我得到的错误。 First the code:先上代码:

My webpack.config.js file:我的webpack.config.js文件:

module.exports = {

    entry: `./sass.js`, 
    output: {
      path: `${__dirname}`, 
      filename: 'bundle.js'
    },
    mode: 'development',

    // ...
    module: {
        rules: [
        {
        test: /\.scss$/,
        use: ["style-loader", "css-loader", "sass-loader"]
        },
        // ...
        ]
    }  
}

My .scss file:我的.scss文件:


$kiwi: red;

:export {
    kiwi: $kiwi,
}

My .js file:我的.js文件:

import variables from './webpack_sass.scss';

My .html file (which I've checked does correctly see bundle.js ):我的.html文件(我检查过的文件正确地看到了bundle.js ):

<head>
  <script type="module" src="./bundle.js"></script>
</head>

I have installed webpack & webpack-cli .我已经安装了webpackwebpack-cli Here's a picture of my package.json file:这是我的package.json文件的图片:

在此处输入图片说明

The error I get is:我得到的错误是:

在此处输入图片说明


:export syntax is a sugar that css-loader adds, all you need to do is to enable the css-modules feature. :export语法是css-loader添加的糖,您需要做的就是启用css-modules功能。

You have 2 choices:您有 2 个选择:

  1. add .module.scss suffix to your file (since css-module is enabled by default for it).module.scss后缀添加到您的文件中(因为默认情况下启用了 css-module)
  2. enable css-modules to all .scss imports, by specifying通过指定为所有.scss导入启用 css-modules
// webpack.config.js

module.exports = {
  entry: `./sass.js`,
  output: {
    path: `${__dirname}`,
    filename: 'bundle.js',
  },
  mode: 'development',

  // ...
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true, // <- this enables css-modules for all imports
            },
          },
          'sass-loader',
        ],
      },
      // ...
    ],
  },
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图将CORS正确地实现到我的JavaScript代码,并且工作了一段时间,我相信项目中缺少一些文件吗? - I am trying to implement CORS to my JavaScript code correctly, and it worked for a while, I believe I am missing some file from the project? 我正在尝试在 javascript [codeigniter post 方法] 中传递 3 个变量 - i am trying to pass 3 variables in javascript [codeigniter post method] 我正在使用Jquery将动态php数据加载到文件中,但是无法将数据存储到Javascript变量中 - I am using Jquery to load dynamic php data into my file but cannot store the data into Javascript variables 为什么在尝试将转译的 js 文件导入我的应用程序时会收到 $ isotafunction 和 window.renderDashboardisnotafunction 的错误? - Why am I getting errors of $ isnotafunction and window.renderDashboardisnotafunction when trying to import a transpiled js file into my application? 有没有办法将变量从 javascript 导入到 sass,反之亦然? - Is there a way to import variables from javascript to sass or vice versa? 我正在尝试使用javascript从json文件中提取中位数 - I am trying to pluck the median from a json file using javascript 我正在尝试使用 javascript new fileReader() 读取文件 - I am trying to read the file using javascript new fileReader() 我试图在plist版本中读取.strings文件。 我有 <key> 和 <string> 。 我不确定如何在我的JavaScript代码中阅读这些输入。 - I am trying to read a .strings file in plist version. I have <key> and <string>. I am not sure how to read these inputs in my javascript code. 尝试创建一个单独的文件,我可以在包含“new”的 JavaScript 中导入 - Trying to create a separate file I can import in JavaScript with "new" involved 我正在尝试将一些 PHP 变量和 Javascript 变量传递到 Javascript window.location.href 中的 url - I am trying to pass some PHP variables and Javascript variable into url in Javascript window.location.href
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM