使用webpack

时间:2016-10-17 20:59:51

标签: javascript sass webpack material-design-lite sass-loader

我想在我的项目中使用MDL的布局组件。

因此,在我的app.css文件中,我正在导入此内容:

@import url('~material-design-lite/src/layout/_layout.scss');

在构建过程中,我收到以下错误:

ERROR in ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../variables in /Users/Mitch/Documents/project/web/node_modules/material-design-lite/src/layout
 @ ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss 3:10-66

ERROR in ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../mixins in /Users/Mitch/Documents/project/web/node_modules/material-design-lite/src/layout
 @ ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss 4:10-63

_layout.sccs从这些行开始

@import "../variables";
@import "../mixins";

任何想法我可能做错了什么?

webpack.config.js的相关部分:

const PATHS = {
    node_modules: path.resolve(__dirname, 'node_modules'),
    build: path.join(__dirname, 'public', 'build'),
    js: path.resolve(__dirname, 'public', 'js'),
    css: path.resolve(__dirname, 'public', 'css'),
};

...
resolve: {
    root: [
        path.resolve(__dirname),
        PATHS.node_modules,
        PATHS.js,
        PATHS.css
    ],
    ...
    extensions: ['', '.js', '.jsx', '.json', '.css', '.scss']
},
module: {
    loaders: [
        {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
        },
        {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract('style', 'css?modules!sass')
        },
        {
            test: /\.png$/,
            loader: 'url-loader?limit=100000'
        },
        {
            test: /\.jpg$/,
            loader: 'file-loader'
        },
        {
            test: /\.gif$/,
            loader: 'file-loader'
        },
        {
            test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&mimetype=application/font-woff'
        },
        {
            test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&mimetype=application/octet-stream'
        },
        {
            test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'file'
        },
        {
            test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url?limit=10000&mimetype=image/svg+xml'
        },
        {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
                presets: ['es2015']
            }
        }
    ]
}
...

相关依赖项:

...
"devDependencies": {
    "babel-core": "^6.14.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.14.0",
    "css-loader": "^0.25.0",
    "eslint": "^3.6.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "file-loader": "^0.9.0",
    "node-sass": "^3.10.1",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.7",
    "webpack": "^1.13.2",
    "webpack-validator": "^2.2.7"
}
...

更新

我尝试@import url('~material-design-lite/src/material-design-lite.scss');,因为这不是局部的,因为我将其修改为仅导入布局组件。

现在我收到以下错误:

ERROR in ./~/css-loader!./~/material-design-lite/src/material-design-lite.scss
Module build failed: Unknown word (19:1)

  17 | /* Material Design Lite */
  18 | 
> 19 | // Variables and mixins
     | ^
  20 | @import "variables";
  21 | @import "mixins";
  22 | 

 @ ./~/css-loader!./public/css/app.css 3:10-114

评论失败了。我不知道如何解决此问题,我尝试使用postcsspostcss-scss加载器和postcss-strip-inline-comments插件无效;旧错误只是被新错误所取代。

2 个答案:

答案 0 :(得分:2)

您示例中的实际问题是您正在导入这样的MDL

@import url('~material-design-lite/src/layout/_layout.scss');

而不是

@import '~material-design-lite/src/layout/_layout.scss';

第一个被sass-loader忽略(因为SASS没有url()导入)并且将由css-loader处理。但是,css-loader不会自动将.scss扩展为variables。这就是为什么你得到这些“未找到模块”的错误。

答案 1 :(得分:0)

我找到了解决方案/解决方法。

我做的是我在custom-mdl.scss目录中创建了css/文件,我只是在那里

@import '~material-design-lite/src/layout/_layout.scss';

并将我的入口点修改为

app : [ 'app.js', 'custom-mdl.scss', 'app.css' ]

而不仅仅是

app : [ 'app.js', 'app.css' ]

以上版本构建正常,并且捆绑中的custom-mdl内容之前的app.css输出符合预期。

请注意,我还从?modules配置中移除了ExtractTextPlugin,所以

ExtractTextPlugin.extract('style', 'css!sass')

而不是

ExtractTextPlugin.extract('style', 'css?modules!sass')

否则custom-mdl.scss文件的类名会出现乱码。