Skip to content

Feat/filename transform #321

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

Closed

Conversation

kevinchappell
Copy link
Contributor

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 by providing chunkhash, contenthash, name, and id as arguments when a function is used for the filename option instead of a string.

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({
      filename: ({ 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

Most likely none

Additional Info

Improved some existing tests. Now testing filename and path generation.

@jsf-clabot
Copy link

jsf-clabot commented Dec 6, 2018

CLA assistant check
All committers have signed the CLA.

@alexander-akait
Copy link
Member

What is use case? Why it is need if you have output as Function in webpack?

@pgoforth
Copy link

pgoforth commented Dec 6, 2018

What is use case? Why it is need if you have output as Function in webpack?

Exporting your CSS to a different subfolder based on the chunk.

Copy link

@pgoforth pgoforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wait for this PR as the other one was stale

@alexander-akait
Copy link
Member

@pgoforth
Copy link

pgoforth commented Dec 6, 2018

@pgoforth
Why don't use https://github.com/webpack/webpack/blob/master/schemas/WebpackOptions.json#L857?

@evilebottnawi
That handles the JS files nicely, and supports having a function as a callback. The MiniCSSExtractPlugin, however, currently does not support providing a function as a callback, therefore the CSS will be extracted and placed into the same directory as your JS file. Most times this is acceptable, however, if you have a desired folder structure like the following:

dist
│   
└───app1
│   │
│   └─  app1.js
│   └─  app1.css
│   
└───app2
│   │
│   └─  app2.js
│   └─  app2.css
│
└───cmn
│   └───css
│   │   │
│   │   └─  vendor.css
│   │
│   └───js
│   │   │
│   │   └─  vendor.js

...it is easy to get the vendor.js file to go into cmn/js, but the current MiniCSSExtractPlugin would generate the vendor.css file into the cmn/js directory. Not always desirable. If you set MiniCSSExtractPlugin:filename to be cmn/css you would get:

dist
│   
└───app1
│   │
│   └─  app1.js
│   
└───app2
│   │
│   └─  app2.js
│
└───cmn
│   └───css
│   │   │
│   │   └─  app1.css
│   │   └─  app2.css
│   │   └─  vendor.css
│   │
│   └───js
│   │   │
│   │   └─  vendor.js

...also, not desirable.

@TimothyBJacobs
Copy link

We need this functionality as well.

@kevinchappell
Copy link
Contributor Author

kevinchappell commented Dec 12, 2018

What is use case? Why it is need if you have output as Function in webpack?

I tried this way but was unable to generate the files into their separate directories.

@kevinchappell
Copy link
Contributor Author

Can someone have a look at why dependency_cache is failing? It does not look related to the pull request. I had a look at the logs and it seems ok until del-cli dist is called from npm run clean. This output is from the logs:

> [email protected] clean /home/circleci/project
> del-cli dist

sh: /home/circleci/project/node_modules/.bin/del-cli: Permission denied
npm info lifecycle [email protected]~clean: Failed to exec clean script
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! [email protected] clean: `del-cli dist`
npm ERR! Exit status 126
npm ERR! 
npm ERR! Failed at the [email protected] clean script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/circleci/.npm/_logs/2018-12-09T06_44_33_877Z-debug.log
npm info lifecycle [email protected]~prebuild: Failed to exec prebuild script
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! [email protected] prebuild: `npm run clean`
npm ERR! Exit status 126
npm ERR! 
npm ERR! Failed at the [email protected] prebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@domonique-ncino
Copy link

Can I help in anyway to get this pushed faster? I need this functionality badly.

@alexander-akait
Copy link
Member

Need rebase and fix CI problem

@kevinchappell kevinchappell force-pushed the feat/filename-transform branch from ceb72c8 to 3a03fc4 Compare December 19, 2018 12:29
@alexander-akait
Copy link
Member

@kevinchappell need fix CI problem

update readme to reflect webpack's output.filename
use DEFAULT_FILENAME for options.chunkFilename when options.filename is a function
chunkFilename should handle string and function options.filename
@domonique-ncino
Copy link

I'm not familiar with webpack plugin development. If I can get pushed in the right direction to learn so this can get pushed would be great. This is a major need.

@kevinchappell
Copy link
Contributor Author

Are anymore changes needed on this?

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.

7 participants