-
Notifications
You must be signed in to change notification settings - Fork 109
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
Dynamically import CSS #261
Comments
I think #205 supports your use case, but you need to bring your own webpack config for it. let app = new EmberApp(defaults, {
autoImport: {
webpack: {
// In here you need to configure something like webpack's style-loader
}
}
}); |
That PR suggests that the included CSS files will end up in `vendor.js` which I *don’t* want. I want them to be separate files, same as the JS chunk files created. And then, perhaps, have an easy way of importing them from the frontend (that’s not mandatory - the current way is almost OK).
|
Whether the code ends up in vendor vs another bundle is determined by whether anybody is dynamically importing it. Try adding style-loader and do |
@ef4 - thanks for the support. So here is what I did: ...
module: {
rules: [
{
test: /\.css$/i,
use: [
{ loader: 'style-loader', options: { injectType: 'linkTag' } },
'css-loader',
],
},
],
},
... This in the The problem is that If I remove So this kind of works for me, but perhaps you could take a look at the other use case (which I also prefer) - the Thanks again! |
I think that falls squarely into picking a different webpack config. If there's an option to style-loader, or a replacement for style-loader, that does what you want, that would be the solution. (All that said, I do want to add a default standardized CSS configuration for ember-auto-import, so that it follows the same behavior that will be in embroider. But that would be a future feature that we don't have right now.) |
@ef4 - I'm not sure I understand what you mean. My point was - I made it work "fine" with In any case, I managed to do what I wanted (or almost at least) so this issue can be closed if you want, or you could leave it open until you decide that the other part with the link tag is OK too. Thank you! |
Oh, I see. So you already tried that config but we break it somehow. Understood now. |
Reading this thread I could reduce my build size by importing css dynamic, but now I'm facing some strange behaviour, @ef4 and @boris-petrov if you could help me with this, would be awesome... This is my load css in a component.js: *loadScript() {
yield import('flatpickr/dist/flatpickr.min.css');
this.script = yield import('flatpickr').then(m => m.default);
} ...
"css-loader": "3.6.0",
"ember-auto-import": "1.5.3",
"style-loader": "1.2.1",
... My autoImport config: autoImport: {
alias: {
flatpickr: 'flatpickr/dist/flatpickr.min',
},
webpack: {
module: {
// to make this work, i must for some reason I don't understand, duplicate this rule.
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
]
},
}
} With just one rule I got this error:
Is this a webpack, or css-loader/style-loader or ember-auto-import issue? Thanks in advance ;) |
@brunoocasali - that's the webpack config we use: {
test: /\.css$/i,
use: [
{
loader: 'style-loader',
},
'css-loader',
],
}, Works fine on our side. I guess it should for you too. |
Unfortunately @boris-petrov this not work too, I've got the same error I've reported above :/ |
@brunoocasali why not use the addon for flatpickr? https://github.com/shipshapecode/ember-flatpickr |
In my current knowledge @rwwagner90 any addon I add to my ember app, will be bundled and carried all over the place even if the user does not use it in every route it load, am I right? (If I misunderstood please show me the light!) The I really wanted to give the users only what they need only when they want it! |
I had success with https://webpack.js.org/plugins/mini-css-extract-plugin/ |
To me this thread is a bit fragmented, so I wanted to give a summary for potential future readers about how I made it work. Needed node packages (ember-auto-import @1.11.3):
ember-cli-build.js:
Where you want to use it (component.js or wherever):
There's nothing to do with the promise of The alias is not needed I guess, could also be import via Edit: As ef4 pointed out, for version 2.x of ember-auto-import no need to have the webpack part, so the ember-cli-build file would look something like that: ember-cli-build.js:
|
I would also point out that ember-auto-import 2.0 (which is in beta release right now) includes style-loader and css-loader by default, so in that case you would remove the whole |
We don't currently offer an option to extend the style-loader config, but that would be a reasonable thing to add if you want to make a PR. |
I think this is all resolved between ember-auto-import 2.0 and #408. |
I'm trying to achieve the same functionality as dynamically-importing JavaScript (in order to have a smaller bundle size). That is, I have a CSS file that I want to load on-demand. What I do right now is:
ember-cli-build.js:
app code:
Not the best code as you see. I mostly hate the part in
ember-cli-build
. I guessember-auto-import
can help mostly with it. I read this answer - point 2 seems like something similar to what I need but it doesn't work - I think the browser tries to parse the result file as JavaScript. This PR also doesn't do what I want.Any suggestions as to what I could do or what should be developed in
ember-auto-import
in order to support that?The text was updated successfully, but these errors were encountered: