SassNinja / postcss-extract-media-query

PostCSS plugin to extract all media query from CSS and emit as separate files.
MIT License
130 stars 20 forks source link

Rules get applied every time a new file is processed #31

Open hamsterkacke opened 4 years ago

hamsterkacke commented 4 years ago

Hey!

I have a simple gulp process:

`gulp.task('css', () => {

const plugins = [
    extractMediaQuery({
        output: {
            path: dirs.dest + '/components',
        }
    })
];
return gulp.src('./dist/components/*.css')
    .pipe(postcss(plugins))
    .pipe(gulp.dest('./dist/components'));

});`

The CSS is already process from sass to css in a step before. So i have various css files in my dist/components folder. No i want to run extractMediaQuery on it. This works.

BUT there is a major drawback:

The first file, that has extractable stuff in it, gets extracted fine. But on the next file, that has some media queries, the rules from the first extraction are applied again. And so on and so on.

So my first media css file is 318byte. The second one then too, even if the main css file doesn't have media queries. Next one is content + 318byte and so on and so on,

Not sure what i am doing wrong. Why is postcss applying the same stuff over and over again till the last file?

Here are as an example the first process file and the last process file.

@media screen and (min-width:768px){.badge .rating{padding:14px 0}} @media screen and (min-width:768px){.badge .text .image img{width:23px}} @media screen and (min-width:768px){.badge .text .rating-in-words .big{font-size:18px}} @media screen and (min-width:768px){.badge .text .rating-in-words .small{font-size:12px}}

@media screen and (min-width:768px){.badge .rating{padding:14px 0}} @media screen and (min-width:768px){.badge .text .image img{width:23px}} @media screen and (min-width:768px){.badge .text .rating-in-words .big{font-size:18px}} @media screen and (min-width:768px){.badge .text .rating-in-words .small{font-size:12px}} ......redacted by me.... @media screen and (min-width:768px){.toc{position:-webkit-sticky;position:sticky;top:88px}}

As you can see, the first four lines are reapplied.

Any help on that?

gravyraveydavey commented 3 years ago

Also having this issue. When using Gulp to process several CSS files (after all other processing), the media query extraction is compounding into the subsequent files - like a buffer isn't being cleared after each file has finished being processed.

@Hamsterkacke my work around was to make a series gulp task (rather than async) for processing the files entirely separately.

// after all SCSS compilation and other processing has happened

// task for splitting media queries out on a specific file passed as an argument
const splitMediaQueries = ( file ) => {

    const extractMediaQueryOptions = {
        queries: project.mediaQueryExtract.queries,
        output: {
            path: output.files + project.styles.dist + "/split/",
        },
        extractAll: false,
        stats:  true,
    }

    return gulp
        .src( output.files + project.styles.dist + "purged/" + file )
        .pipe( postcss( [ extractMediaQuery( extractMediaQueryOptions ) ] ) )
        .pipe( gulp.dest( output.files + project.styles.dist + "split/" ) );
};

// wrapper for primary stylesheet
const splitStyles = () => {
    return splitMediaQueries( 'style.css' );
}
// wrapper for critical, inline stylesheet
const splitHeadInline = () => {
    return splitMediaQueries( 'head-inline.css' );
}

// series task to process the two stylesheets sequentially 
const sequenceSplitStyles = gulp.series( splitStyles, splitHeadInline );

// the series for all style processing 
const sequenceStyles = gulp.series( styles, purgeStyles, sequenceSplitStyles, htdocs, purgeRejected );

// export the whole chain as a task
exports.styles = sequenceStyles;

Slower processing, but gets the extraction working!

SassNinja commented 2 years ago

Hey folks,

sorry for the late response

I've tried to understand your problem - however I'm afraid I'm unable to reproduce what you described. Have you seen my example for gulp?

I modified it to process more than one CSS file and the result remains the same, meaning it's working as expected without mixing up different files. This is how my files looked:

// src/one.css
.one { display: none; }

@media screen and (min-width: 1024px) {
  .one { display: block; }
}

// src/two.css
.two { display: none; }

@media screen and (min-width: 1024px) {
  .two { display: block; }
}

Afterwards it emits four files to the dist folder with the expected content. If I change my test files into SCSS and add gulp-sass to the pipeline, it doesn't change the result.

Therefore I'm afraid I cannot do anything as it's looking good for me. Could you provide full gulp example?

Neibaf commented 2 years ago

Hello @SassNinja,

I think i have the same issue.

If you look at your file two-1024px.css, it has this rules

@media screen and (min-width: 1024px) {
  .one { display: block; }
  .two { display: block; }
}

But it should be

@media screen and (min-width: 1024px) {
  .two { display: block; }
}