简体   繁体   English

为什么 webpack 别名不起作用?

[英]Why webpack alias doesn't work?

I use webpack and postcss-import in my project.我在我的项目中使用 webpack 和 postcss-import 。 I write components and some components can be nested to another so it's complicated to write path for them like import '../../../component-a';我编写组件,并且某些组件可以嵌套到另一个组件,因此为它们编写路径很复杂,例如import '../../../component-a'; and so on.等等。 I want to create alias for to solve this problem.我想创建别名来解决这个问题。 So in my webpack.config.js I wrote:所以在我的 webpack.config.js 中我写道:

    resolve: {
        alias: {
            '@blocks': path.resolve(__dirname, './source/blocks'),
            '@styles': path.resolve(__dirname, './source/styles')

my webpack config located in root.我的 webpack 配置位于根目录中。 So, the problem is when I write in css something like @import '@styles/vars.css';所以,问题是当我在 css 中写类似@import '@styles/vars.css'; – it doesn't works. – 它不起作用。 I get en error 'undefined variable bla bla bla...'.我收到错误“未定义的变量 bla bla bla ...”。 But when I import blocks inside of js files and write import Logo from '@blocks/logo';但是当我在 js 文件中导入块并import Logo from '@blocks/logo'; – it works just fine. - 它工作得很好。 Also I wanna notice that @import '@styles/vars.css';我还想注意@import '@styles/vars.css'; – works with stylus. – 使用触控笔。 So maybe I miss something about postcss-import plugin.所以也许我错过了一些关于 postcss-import 插件的东西。 How to make it works?如何使它起作用?

UPD postcss config part: UPD postcss 配置部分:

``` ```

var webpack = require("webpack");

function postcssModules () {
    return [
        require('postcss-nested')(),
        require('postcss-import')({
            addDependencyTo: webpack
        }),
        require('postcss-simple-vars'),
        require('postcss-cssnext')({
            warnForDuplicates: false
        }),
        require('lost')
    ]
};

module.exports = postcssModules;

``` ```

May be this can help (I answered there): 可能这可以帮助(我在那里回答):

https://github.com/postcss/postcss-import/issues/190#issuecomment-298078092 https://github.com/postcss/postcss-import/issues/187#issuecomment-298078080 https://github.com/postcss/postcss-import/issues/190#issuecomment-298078092 https://github.com/postcss/postcss-import/issues/187#issuecomment-298078080

If you want to reuse webpack's alias, replace 如果要重用webpack的别名,请替换

const resolver = ResolverFactory.createResolver({
  alias: {
    '@blabla': path.resolve(__dirname, 'src')
  },
  extensions: ['.css'],
  modules: ['src', 'node_modules'],
  useSyncFileSystemCalls: true,
  fileSystem
});

with

const webpackBaseConfig = require('./webpack.config.base').default;

const resolver = ResolverFactory.createResolver(
Object.assign({},
    webpackBaseConfig.resolve, {
    modules: ['src', 'node_modules'],
    useSyncFileSystemCalls: true,
    fileSystem
  })
);

For now, there is a library postcss-import-resolver to manage aliases in module.css files.目前,有一个库postcss-import-resolver来管理 module.css 文件中的别名。

in webpack.config.js :webpack.config.js

import path from 'path';
import resolver from 'postcss-import-resolver';

...
{
  loader: 'postcss-loader',
  options: {
    plugins: {
      'postcss-import': {
        resolve: resolver({
          alias: {
            '@common': path.resolve('./src/common/styles')
          },
        })
      }
    }
}

and in css file:css文件中:

@import '@common/variables';

div {
    background-color: var(--common-var); // variable from './src/common/styles/variables.css'
}

Also, make sure that postcss-import is one of the first plugins.另外,请确保postcss-import是第一个插件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM