- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试弄清楚如何配置 webpack 以便将变量文件导入组件目录中的每个 scss 文件。据我所知,这应该这样做:
// imports variables.scss into every module component
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
'style-loader',
'css-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
resources: ['./src/styles/modules/_variables.scss'],
},
},
],
},
在我的组件的 sass (news.scss) 中,我有(没有 import 语句):
text {
color: $greyish;
}
在我的组件中:
import css from './news.scss'
...
<p className={css.text}>test</p>
现在,这行不通了
{
"status": 1,
"file": "/Users/Bartosz/Projects/matchday-frontend/src/components/News/news.scss",
"line": 7,
"column": 10,
"message": "Undefined variable: \"$greyish\".",
"formatted": "Error: Undefined variable: \"$greyish\".\n on line 7 of src/components/News/news.scss\n>> color: $greyish;\n\n ---------^\n"
}
我的整个 webpack 文件是在 create-react-app 中用 npm run eject 完成的,它看起来像这样:
'use strict';
const autoprefixer = require('autoprefixer');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
const getClientEnvironment = require('./env');
const paths = require('./paths');
// Webpack uses `publicPath` to determine where the app is being served from.
// In development, we always serve from the root. This makes config easier.
const publicPath = '/';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
const publicUrl = '';
// Get environment variables to inject into our app.
const env = getClientEnvironment(publicUrl);
// This is the development configuration.
// It is focused on developer experience and fast rebuilds.
// The production configuration is different and lives in a separate file.
module.exports = {
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
devtool: 'cheap-module-source-map',
// These are the "entry points" to our application.
// This means they will be the "root" imports that are included in JS bundle.
// The first two entry points enable "hot" CSS and auto-refreshes for JS.
entry: [
// We ship a few polyfills by default:
require.resolve('./polyfills'),
// Include an alternative client for WebpackDevServer. A client's job is to
// connect to WebpackDevServer by a socket and get notified about changes.
// When you save a file, the client will either apply hot updates (in case
// of CSS changes), or refresh the page (in case of JS changes). When you
// make a syntax error, this client will display a syntax error overlay.
// Note: instead of the default WebpackDevServer client, we use a custom one
// to bring better experience for Create React App users. You can replace
// the line below with these two lines if you prefer the stock client:
// require.resolve('webpack-dev-server/client') + '?/',
// require.resolve('webpack/hot/dev-server'),
require.resolve('react-dev-utils/webpackHotDevClient'),
// Finally, this is your app's code:
paths.appIndexJs,
// We include the app code last so that if there is a runtime error during
// initialization, it doesn't blow up the WebpackDevServer client, and
// changing JS code would still trigger a refresh.
],
output: {
// Add /* filename */ comments to generated require()s in the output.
pathinfo: true,
// This does not produce a real file. It's just the virtual path that is
// served by WebpackDevServer in development. This is the JS bundle
// containing code from all our entry points, and the Webpack runtime.
filename: 'static/js/bundle.js',
// There are also additional JS chunk files if you use code splitting.
chunkFilename: 'static/js/[name].chunk.js',
// This is the URL that app is served from. We use "/" in development.
publicPath: publicPath,
// Point sourcemap entries to original disk location (format as URL on Windows)
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
},
resolve: {
// This allows you to set a fallback for where Webpack should look for modules.
// We placed these paths second because we want `node_modules` to "win"
// if there are any conflicts. This matches Node resolution mechanism.
// https://github.com/facebookincubator/create-react-app/issues/253
modules: ['node_modules', paths.appNodeModules].concat(
// It is guaranteed to exist because we tweak it in `env.js`
process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebookincubator/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
},
plugins: [
// Prevents users from importing files from outside of src/ (or node_modules/).
// This often causes confusion because we only process files within src/ with babel.
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
// please link the files into your node_modules/ and let module-resolution kick in.
// Make sure your source files are compiled, as they will not be processed in any way.
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
],
},
module: {
strictExportPresence: true,
rules: [
// TODO: Disable require.ensure as it's not a standard language feature.
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
// { parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|jsx|mjs)$/,
enforce: 'pre',
use: [
{
options: {
formatter: eslintFormatter,
eslintPath: require.resolve('eslint'),
},
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
// Process JS with Babel.
{
test: /\.(js|jsx|mjs)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
},
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use a plugin to extract that CSS to a file, but
// in development "style" loader enables hot editing of CSS.
{
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
// imports variables.scss into every module component
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
'style-loader',
'css-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
resources: ['./src/styles/modules/_variables.scss'],
},
},
],
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader doesn't use a "test" so it will catch all modules
// that fall through the other loaders.
{
// Exclude `js` files to keep "css" loader working as it injects
// its runtime that would otherwise processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/, /\.sass$/, /\.scss$/],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
// ** STOP ** Are you adding a new loader?
// Make sure to add the new loader(s) before the "file" loader.
],
},
plugins: [
// Makes some environment variables available in index.html.
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
// In development, this will be an empty string.
new InterpolateHtmlPlugin(env.raw),
// Generates an `index.html` file with the <script> injected.
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
}),
// Add module names to factory functions so they appear in browser profiler.
new webpack.NamedModulesPlugin(),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
new webpack.DefinePlugin(env.stringified),
// This is necessary to emit hot updates (currently CSS only):
new webpack.HotModuleReplacementPlugin(),
// Watcher doesn't work well if you mistype casing in a path so we use
// a plugin that prints an error when you attempt to do this.
// See https://github.com/facebookincubator/create-react-app/issues/240
new CaseSensitivePathsPlugin(),
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebookincubator/create-react-app/issues/186
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
],
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty',
},
// Turn off performance hints during development because we don't do any
// splitting or minification in interest of speed. These warnings become
// cumbersome.
performance: {
hints: false,
},
};
最佳答案
尝试使用选项 prependData
而不是 resources
。引用:https://webpack.js.org/loaders/sass-loader/#prependdata
关于reactjs - webpack 配置加载 sass 变量无需 import 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161871/
我刚刚通过更改 import * as CodeMirror 修复了一个错误简单明了import CodeMirror . 我复制了this code . (从 TypeScript 移植) impo
我调试(在 PyCharm 中)一个脚本。我在断点处停止,然后转到调试控制台窗口,然后从那里调用导入行,如下所示: import my_util1 from my_utils 然后我调用 my_uti
谁能给我解释一下 import 语句是如何工作的? 例如,我在 myapp/app/models 包中有一个类型 User: package models type User struct {
我想导入 Control.App进入一个引用 PrimIO.PrimIO 的模块通过不合格的名称 PrimIO在很多地方。当然,问题在于 Control.App还导出一个名为 PrimIO 的定义.我
我应该使用 from foo import bar 或者 import foo.bar as bar 当导入模块 还有无需/希望更改名称 (bar)? 有什么不同吗?有关系吗? 最佳答案 假设 bar
我正在 Windows 上使用 Theano 进行深度学习实验的第一步,我很惊讶仅仅加载库需要多少时间。 这是小测试程序: from time import time t0 = time() impo
在 TypeScript 中,如何在不创建任何别名的情况下从文件“导入 *”? 例如我有一个包含顶级导出函数的文件“utils”,我想导入所有这些函数而不为每个函数重新创建别名。 像这样: impor
我应该使用 from foo import bar 或 import foo.bar as bar 当导入模块并且不需要/希望更改名称(bar)? 有什么不同吗?有关系吗? 最佳答案 假设bar是fo
这个问题在这里已经有了答案: Use 'import module' or 'from module import'? (23 个回答) 关闭8年前。 我想知道代码片段之间是否有任何区别 from u
我试过了 from urllib import request mine = request.Request() 和 import urllib.request mine = urllib.reque
所以,我有一个关于 Python 导入的小谜团。我确信出于某种原因事情应该是这样的,因为 Guido 很少出错。但是,为什么会这样呢? $ cat myModule.py #!/usr/bin/pyt
我们正在将 Rails 3.2 应用程序升级到 Rails 4.0。 我们有一个 assets/stylesheets/application/index.css.sass加载一些其他 sass 文件
我正在开发一个相当小的 Typescript 代码库,该代码库已经足够大,可以拆分到多个文件中。这是一个二十一点游戏。我目前有一堆代码,看起来像: var player = new Player();
是否可以以当模块为 use 时的方式编写模块? d 没有显式导入所有子例程都被导入,当它是 use d 显式导入只有这些显式导入的子程序可用? #!/usr/bin/env perl6 use v6;
这个问题在这里已经有了答案: how to watch changes in whole directory/folder containing many sass files (9 个回答) 5年前
我真的很难让它在 xcode 4 中工作。 我有一个项目将在许多应用程序(网络)中重用,因此我创建一个工作区并添加我的两个项目。到目前为止,一切都很好....这就是失败的地方.. #import "J
经典提取器和新提取器之间的主要区别是什么,哪个最好用? 最佳答案 经典提取器使用原始工作流程,与爬虫和连接器相同。 新的提取器更加精简,通常看起来和感觉都更好,并且经典提取器中的许多小错误已在新提取器
在处理 google webfont import mixin 时,我注意到无法动态构建 @import URL。 .gFontImport (@name, @weights, @subsets) {
我正在关注Django 1.8 tutorial 。在我的项目中mysite ,有一个源文件夹polls 。文件夹中有views.py模块其中 index函数已定义。还有一个urls.py文件: fr
我想使用名为 warp 的第三方库编译一个简单的 Rust 程序: [package] name = "hello-world-warp" version = "0.1.0" [dependencie
我是一名优秀的程序员,十分优秀!