Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import resolved path #187

Closed
NXTaar opened this issue Mar 9, 2016 · 19 comments
Closed

Cannot import resolved path #187

NXTaar opened this issue Mar 9, 2016 · 19 comments

Comments

@NXTaar
Copy link

NXTaar commented Mar 9, 2016

i have postcss-import configured and here is my resolved part in webpack.config.js.

resolve: {
        root: path.resolve(__dirname),
        alias: {
            components: path.resolve(__dirname+'/src/components'),
            layouts: path.resolve(__dirname+'/src/layouts'),
            styles: path.resolve(__dirname+'/src/styles')
            //vendor: path.resolve(__dirname+'/frontend/vendor'),
            //modules: path.resolve(__dirname+'/frontend/modules')
        },
        extensions: ['', '.js', '.png']
    }

When i try to import resolved path in some .css files like this

@import "styles/colors.scss"; // also tried @import "~styles/colors.scss";

.inputField {
    input {
        height: 36px;
        border: 1px solid $gray;
    }
}

I'm getting this error

ERROR in ./~/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./~/postcss-loader!./src/components/input/styles/touchbank.scss
Module build failed: Error: ENOENT: no such file or directory, open 'C:\Projects\TouchBank\src\components\input\styles\styles\colors.scss'
    at Error (native)
 @ ./src/components/input/styles/touchbank.scss 4:14-214 13:2-17:4 14:20-220

looks like it doesn't know about webpack's resolve path, and tries import the file, as it's placed near the current css file

@TrySound
Copy link
Member

TrySound commented Mar 9, 2016

@NXTaar I'm not sure that webpack can affect somehow on postcss-import.

@MoOx
Copy link
Contributor

MoOx commented Mar 9, 2016

postcss have nothing to do with webpack. Just checkout postcss-import options in the readme. You will find what you look for.

@MoOx MoOx closed this as completed Mar 9, 2016
@NXTaar
Copy link
Author

NXTaar commented Mar 10, 2016

The problem stays
here is my dir strcuture

root/
|
|-- src/              
|   |-- components/
        |--input/
           |--styles.scss
|   |-- layouts/
|   |-- styles/
        |--colors.scss

my post css option

module.exports = function (webpack) {
    return [
        autoprefixer,
        precss,
        postcssImport({
            addDependencyTo: webpack,
            path: [ path.resolve(__dirname + "/src/styles") ]
        }),
        reset({
            reset: {
            margin: 0,
            padding: 0,
            borderRadius: 0
        }}),
        initial
    ]
};

It still looking in the input directory locally

ERROR in ./~/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./~/postcss-loader!./src/components/input/styles/touchbank.scss
Module build failed: Error: ENOENT: no such file or directory, open 'C:\Projects\TouchBank\src\components\input\styles\styles\colors.scss'
    at Error (native)
 @ ./src/components/input/styles/touchbank.scss 4:14-214 13:2-17:4 14:20-220

also tried @import 'colors.scss', @import 'styles/colors.scss' - same result

@MoOx
Copy link
Contributor

MoOx commented Mar 10, 2016

@import './colors.scss' ?

@NXTaar
Copy link
Author

NXTaar commented Mar 10, 2016

still the same

ERROR in ./~/css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!./~/postcss-loader!./src/components/input/styles.scss
Module build failed: Error: ENOENT: no such file or directory, open 'C:\Projects\TouchBank\src\components\input\colors.scss'
    at Error (native)
 @ ./src/components/input/styles.scss 4:14-205 13:2-17:4 14:20-211

Maybe i was setting the path parameter in postcss-inport options wrong?

@MoOx
Copy link
Contributor

MoOx commented Mar 10, 2016

why do you use path.resolve with an absolute path?

@MoOx
Copy link
Contributor

MoOx commented Mar 10, 2016

Try adding a slash at the end of the path

@MoOx
Copy link
Contributor

MoOx commented Mar 10, 2016

also sidenote: using autoprefixer as the first plugin is bad as anything imported or generated by precss will not be prefixed

@NXTaar
Copy link
Author

NXTaar commented Mar 10, 2016

why do you use path.resolve with an absolute path?`

Because i'm on windows, using path.resolve makes slash handling more stable between different OS (on Mac and Linux there is no need of path.resolve)

using autoprefixer

thanks, moved it to the end

Try adding a slash at the end of the path

Done. still nothing. How exactly should i import the colors.scss file?

@TrySound
Copy link
Member

@NXTaar Try to change root option.

@tpai
Copy link

tpai commented Mar 16, 2016

@NXTaar Your precss module must under postcss-import.

It works for me.

module.exports = {
    postcss: function(webpack) {
        return {
            defaults: [postcssImport, precss, autoprefixer],
            cleaner: [
                postcssImport({
                    addDependencyTo: webpack,
                    path: [ path.resolve(__dirname + "/src") ]
                }),
                precss,
                autoprefixer({browsers:["> 5%", "ie >= 8", "Firefox < 20"]})
            ]
        }
    }
}

@MoOx
Copy link
Contributor

MoOx commented Mar 16, 2016

Don't use autoprefixer before postcss-import, inline source won't be prefixed.

@tpai
Copy link

tpai commented Mar 16, 2016

@MoOx Thanks, should I put autoprefixer below precss?

@MoOx
Copy link
Contributor

MoOx commented Mar 16, 2016

Definitely. autoprefixer should be called after all plugins that might generate rules. Btw @ai should display a note in bold in the README like I am doing for this plugin (postcss-import).

@ai
Copy link
Member

ai commented Mar 16, 2016

@MoOx maybe you should add check that import is on first place? You have all API for it.

@MoOx
Copy link
Contributor

MoOx commented Mar 16, 2016

Maybe someday. But people might want to lint first, so not sure...

@Tux1
Copy link

Tux1 commented Sep 24, 2016

There is no way to use webpack aliases within postcss-import ?

@longlho
Copy link

longlho commented Sep 24, 2016

@Tux1 postcss is separate from webpack so I don't think it's possible

@pgmanutd
Copy link

pgmanutd commented Apr 28, 2017

May be enhanced-resolve can help.

Your postcss.config.js can be like this:

'use strict';

const path = require('path');
const ResolverFactory = require('enhanced-resolve/lib/ResolverFactory');
const NodeJsInputFileSystem = require('enhanced-resolve/lib/NodeJsInputFileSystem');
const CachedInputFileSystem = require('enhanced-resolve/lib/CachedInputFileSystem');

const CACHED_DURATION = 60000;
const fileSystem = new CachedInputFileSystem(new NodeJsInputFileSystem(), CACHED_DURATION);

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

module.exports = {
  plugins: {
    'postcss-import': {
      resolve(id, basedir) {
        return resolver.resolveSync({}, basedir, id);
      }
    }
  },
};

And use it like this:

@import '@blabla/your/path/to/file.css';

@postcss postcss locked and limited conversation to collaborators Apr 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants