-
-
Notifications
You must be signed in to change notification settings - Fork 609
Custom module export support #645
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
Comments
It should be possible, but I need to triage the use case for custom CSS exports first before consider adding it to css-meta-loader import { getOptions } from 'loader-utils';
export default function loader (css, map) {
// Loader Options
const options = getOptions(this) || {};
// Make the Loader Async
const cb = this.async();
// Some transform you need to get the metadata e.g via postcss && plugins
... (?)
// Dummy
const exports = `export const meta = 'Hello';`
const result = [
`// CSS Exports\n${exports}\n`
`export default `\${css}\``,
].join('\n')
cb(null, result, map);
return null
} import css, { meta } from './file.css' What kind of stats (metadata) you intend to include and where does it come from/how is it generated ? |
That's how I thought it'd work. Nothing else gets through. I'm not actually sure if the css-loader itself respects exports made by previous loaders on the same source. I have two use-cases currently, but I could think of more:
Instead, this, or something similar would be desirable: |
Yep correct, this will change in import * as styles from './file.css'
// CSS Modules
// from 'var locals = { container: '3467i2fgz'}' (CommonJS)
// to 'export const container = '3467i2fgz' ' (ESM Named Export)
console.log(style.container) // '3467i2fgz' So in theory there could be other (custom) named ESM exports, unrelated to CSS Modules ( import * as styles from './file.css'
console.log(styles.meta) // metadata...
Currently I'm working on the long outstanding ESM upgrade of the CSS related loaders and a few things will likely change there, notably that CSS Modules will be removed from the
Maybe using plugin.js import postcss from 'postcss';
export default postcss.plugin('name', (options) => (css, result) => {
// your plugin code here...
result.messages.push({
plugin: 'name', meta: { ...data }
})
}); // The postcss-loader handles result.messages (see comment below)
postcss([ plugin() ]).process(css, options)
.then(({ css, map, messages }) => {
console.log(messages) /* e.g [ { plugin: 'name', meta: { ...data } ] */
}); The |
Lots of interesting thoughts! loader-exportsI like where things are going with this. But it's important we avoid potential name-collisions.
Where css.container is a hash (thanks to css loader being in module:true mode) and This way the following would be possible for my container query loader: A thought:
Basically, all loaders should work in both scenarios, which means styles-loader cannot be a mandatory loader at the end of the chain. Is there any way I could help out in this effort? PS: I'm all for removing css modules from the css-loader, we'll just need to make sure then that these postcss messages can be / are exported. postcss messagesI actually tried the |
To further demonstrate what I'm aiming for, I've made a prototype: Hopefully it might kickstart further discussions? |
@evilebottnawi , maybe? 😅 |
@ZeeCoder Sry for the delay I didn't start to ignore this 😛, but I need to finish the exporting for CSS Modules (all related plugins with their |
Sure, thanks for the update @michael-ciniawsky 👍 |
0ee635d#diff-1fdf421c05c1140f6d71444ea2b27638R116
|
I'll check it out as soon as I have some time, thanks for all the hard work. 🎉 👍 |
Pending |
I assume these were released? 😅 |
I'm not too sure how to test this @michael-ciniawsky 👆 That's what I get with the postcss-loader added without any plugins. |
I'm getting the same error using |
@michael-ciniawsky any idea? 🤔 |
@michael-ciniawsky I'm still lost with this one, was there a release? |
I'm not sure if I'm opening this issue at the right place, but I'm not certain where else could I address this issue.
My goal
I wanted to make a custom loader, that would allow me to import some statistics from a css file, like so:
import stats from 'MyComponent.css';
This setup would also involve style-loader and css-loader so that my css is injected in the browser.
The issue
It seems like due to the tight coupling of the style-loader and css-loader in module: true mode, I can't define custom exports like I wanted to.
I thought even the following would be possible:
import { loader1Data, loader2Data } from 'MyComponent.css';
Where those imports are whatever the respective chained loaders decided to expose.
Question
Would the above be possible somehow?
To me it seems like this would need a rewrite and some standard that loaders working on CSS would need to follow in order to contribute to a module that's finally exported at the end.
I'm really interested in any thought, or advice on who I need to approach with this problem.
Also, this is the original discussion on the webpack/dev Gitter:
https://gitter.im/webpack/webpack/dev?at=5a2c9ce93a80a84b5bdac9ef
The text was updated successfully, but these errors were encountered: