yunusga / postcss-sort-media-queries

PostCSS plugin for sorting and combining CSS media queries with mobile-first / desktop-first methodologies.
https://postcss-sort-media-queries.github.io
MIT License
147 stars 7 forks source link

Cannot set property 'parent' of undefined #33

Closed in-in closed 3 years ago

in-in commented 3 years ago

I use Next.js and the plugin does not work with it properly.

After the next dev command I get an error:

error - ./components/footer/styles.module.css ((webpack)/css-loader/cjs.js??ref--5-oneOf-2-1!(webpack)/postcss-loader/cjs.js??ref--5-oneOf-2-2!./components/footer/styles.module.css)
TypeError: Cannot set property 'parent' of undefined
// postcss.config.js
module.exports = {
    plugins: {
        'autoprefixer': {},
        'postcss-flexbugs-fixes': {},
        'postcss-media-minmax': {},
        'postcss-nested': {},
        'postcss-sort-media-queries': {}
    }
}
// next.config.js
module.exports = {
    distDir: 'build',
    poweredByHeader: false,
    reactStrictMode: true
}
/* ./components/footer/styles.module.css */
.footer {
    grid-area: footer;
    background-color: green;

    @media (width >= 600px) {
        background-color: pink;
    }
}

package.json

    "dependencies": {
        "next": "10.1.3",
        "react": "17.0.2",
        "react-dom": "17.0.2"
    },
    "devDependencies": {
        "autoprefixer": "10.2.5",
        "postcss": "8.2.10",
        "postcss-flexbugs-fixes": "5.0.2",
        "postcss-media-minmax": "5.0.0",
        "postcss-nested": "5.0.5",
        "postcss-sort-media-queries": "3.8.9"
    }
yunusga commented 3 years ago

Hi @in-in. Can you show before after CSS?

in-in commented 3 years ago

I showed before CSS in the question (see the footer component above), but I do not have after CSS so the project can not build

yunusga commented 3 years ago

I create two test for this case, and all works fine

√ postcss media minmax -> nested (4 ms)
√ postcss nested -> media minmax (2 ms)
// mediaMinmaxBeforeNestedRun
postcss([autoprefixer, flexbugsFixes, mediaMinMax, nested, plugin(opts)]).process(input, { from: undefined })

// mediaMinmaxAfterNestedRun
postcss([autoprefixer, flexbugsFixes, nested, mediaMinMax, plugin(opts)]).process(input, { from: undefined })

Before

.footer {
    grid-area: footer;
    background-color: green;

    @media (width >= 600px) {
        background-color: pink;
    }

    .footer-content {
        grid-area: footer-content;

        @media (width >= 990px) {
            background-color: aliceblue;
        }
    }
}

After

.footer {
    grid-area: footer;
    background-color: green;
}

.footer .footer-content {
    grid-area: footer-content;
}

@media (min-width: 600px) {
    .footer {
        background-color: pink
    }
}
@media (min-width: 990px) {
    .footer .footer-content {
        background-color: aliceblue
    }
}
yunusga commented 3 years ago

@in-in This issue happen only with postcss-sort-media-queries?

yunusga commented 3 years ago

@in-in Try this order for plugins in postcss.config.js

plugins: {
    'postcss-nested': {},
    'postcss-media-minmax': {},
    'autoprefixer': {},
    'postcss-flexbugs-fixes': {},
    'postcss-sort-media-queries': {}
}
in-in commented 3 years ago

Nope, reordering didn't work for me If I comment out this line, my project builds fine 211071618664621

I created a test case for your investigation https://github.com/in-in/postcss-test-case

yunusga commented 3 years ago

@in-in without nested with sort - ok without sort with nested - ok

One more conflict with nested

yunusga commented 3 years ago

@in-in problem solved after update to postcss: 8.2.13 postcss-sort-media-queries: 3.9.10