Search code examples
javascriptsasswebpackmaterial-design-litesass-loader

Load specific component from MDL using webpack


I want to just use the layout component of MDL into my project.

So, in my app.css file, I am importing this:

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

and during the building process I am getting the following error:

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 starts with these lines

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

Any ideas what I may be doing wrong?

Relevant parts of my 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']
            }
        }
    ]
}
...

Relevant dependencies :

...
"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"
}
...

Update

I tried to @import url('~material-design-lite/src/material-design-lite.scss');, since this is not a partial, after I modified it to only import the layout component.

Now I am getting the following errors:

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

It fails on the comments. I have no idea how to fix this, I tried using the postcss and postcss-scss loaders and postcss-strip-inline-comments plugin to no avail; the old errors were simply replaced by new errors.


Solution

  • The actual problem in your example was that you were importing MDL like this

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

    instead of

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

    The first is ignored by the sass-loader (because SASS does not have url() imports) and will be processed by the css-loader. The css-loader, however, will not automatically expand .scss to variables, for instance. That's why you were getting these "Module not found" errors.