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

feat(options): add moduleFilename option #381

Merged

Conversation

kevinchappell
Copy link
Contributor

@kevinchappell kevinchappell commented Apr 19, 2019

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

The filename option currently takes a string the enables devs to customize the filename of the generated css file. This works well and the customized filename is created in the same output directory as the js file it is extracted from, however there are cases when the generated files may need to be in different directories. This change enables modifying the filenameTemplate using chunk data.

Use Case:

webpack.config.js - entry

  entry: {
    'dist/main': 'src/index.js',
    'demo/assets/js/demo': 'src/demo/index.js',
  }

The above config will create dist/main.js and demo/assets/js/demo.js.

Use:

webpack.config.js - plugins

  plugins: [
    new MiniCssExtractPlugin({
      moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.min.css`,
    })
  ]

We can change the output directory, chunkhash length and more by using a function that gets called during compilation.mainTemplate.hooks.renderManifest.

Breaking Changes

None

Additional Info

src/index.js Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Apr 20, 2019

Codecov Report

Merging #381 into master will increase coverage by 0.25%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #381      +/-   ##
==========================================
+ Coverage   64.93%   65.19%   +0.25%     
==========================================
  Files           4        4              
  Lines         405      408       +3     
  Branches       82       83       +1     
==========================================
+ Hits          263      266       +3     
  Misses        118      118              
  Partials       24       24
Impacted Files Coverage Δ
src/index.js 87.96% <100%> (+0.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0a0355...d215aee. Read the comment docs.

@alexander-akait
Copy link
Member

Can you rebase, thanks for PR, good work!

@kevinchappell
Copy link
Contributor Author

Can you rebase, thanks for PR, good work!

done

src/index.js Outdated Show resolved Hide resolved
@kevinchappell kevinchappell changed the title feat(options): add filenameTemplate option feat(options): add moduleFilename option Apr 24, 2019
@ghostflow
Copy link

when will this pr be merged?
need this feature~

@alexander-akait alexander-akait merged commit 13e9cbf into webpack-contrib:master May 10, 2019
@ifeltsweet
Copy link

ifeltsweet commented May 16, 2019

This doesn't work on chunks that are split out with SplitChunksPlugin – only works for entries. But our team found that chunkFilename can actually be a function, so this works:

new MiniCssExtractPlugin({
    chunkFilename: ({ chunk }) => {
        if (chunk.entryModule) {
            // extracted from entry
            return '[name].[contenthash].css';
        } else {
            // extracted from split chunks
            return 'common/css/[name].[contenthash].css'
        }
    },
})

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

Successfully merging this pull request may close these issues.

None yet

4 participants