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

onImport argument includes top-level (importing) file #234

Closed
jmm opened this issue Oct 21, 2016 · 3 comments
Closed

onImport argument includes top-level (importing) file #234

jmm opened this issue Oct 21, 2016 · 3 comments

Comments

@jmm
Copy link

jmm commented Oct 21, 2016

Hello,

With postcss@5.2.5 and postcss-import@8.1.2 I'm getting the top level file that's passed to postcss in the argument to onImport, which is not what I expect. It's documented as:

array of imported files

Which I interpet as: the files referenced by @import rules.

So say I have 2 files like this:

top-level.css

@import "./imported.css";

imported.css

/* */

In onImport I get an array containing the paths for both files.

Is that intentional? If so, can you please document it. And if that's the case, can you commit to it always being the first element in the array?

Thanks!

@RyanZim
Copy link
Collaborator

RyanZim commented Oct 24, 2016

@MoOx Related: postcss-cli needs to know which file is the parent of the import.

postcss-cli formerly relied on the (undocumented) fact that inside onImport, this.from was the parent. This is no longer so. postcss/postcss-cli#44

Either the parent must be guaranteed to be the first element, or onImport should receive a second argument parent. If we go with the first option, the current implementation would have to be changed since Object.keys() technically isn't guaranteed to be deterministic.

@RyanZim
Copy link
Collaborator

RyanZim commented Nov 3, 2016

X-ref: #233

@RyanZim
Copy link
Collaborator

RyanZim commented Nov 10, 2016

@jmm We added a dependency message for each import in #241. This was released as https://github.com/postcss/postcss-import/releases/tag/8.2.0.

You can access the messages by filtering result.messages:

var postcss = require('postcss');
postcss([ require('postcss-import') ])
    .process(css, { from: 'src/app.css', to: 'app.css' })
    .then(function (result) {

        // Get list of imports:
        var imports = result.messages.filter(message => message.type === 'dependency')

        // Write File...
    });

imports would be an array of objects, each with this form:

{
  type: 'dependency',
  file: 'ImportedFile.css',
  parent: 'FileContainingTheImport.css',
}

In the future, I might deprecate or remove onImport. For now, I think I'll just leave things as they are and recommend using the dependency message instead.

@RyanZim RyanZim closed this as completed Nov 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants