You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inline loader not migrateable to webpack 5 when used in require.context #12900
changed the title [-]inline loader not migrateable when used in require.context[/-][+]inline loader not migrateable to webpack 5 when used in require.context[/+]on Mar 15, 2021
@eps1lon You need do it only for rules where test can have intersections (or where you don't specify test), maybe we should privatize resourceQuery and resourceFragment to improve DX
maybe we should privatize resourceQuery and resourceFragment
What about this? So you don't need exclude resourceQuery from other rules
I'm not following. How would I then be able to migrate from inline-loaders? Based on file extension is not possible since I need some files both as regular JS and their raw source.
How would I then be able to migrate from inline-loaders?
Using resourceQuery,
Based on file extension is not possible since I need some files both as regular JS and their raw source.
resourceQuery: /^(?!\?raw$).*/,
I provide solution above.
Do you want to avoid using resourceQuery: /^(?!\?raw$).*/,? If yes, above I suggested that we could prioritize resourceQuery over test, so you don't need resourceQuery: /^(?!\?raw$).*/, in your config
That does not work for me. I do not have control over all the rules since I'm using nextjs. I could technically but the order of the rules isn't some public API. And even then the point about future rules still apply: I need to manually verify no rules conflicts whenever I update nextjs.
I have a distinct but similar case where I feel it's complicated to migrate to the asset modules system.
Docusaurus has a modular architecture with a plugin system.
A Docusaurus image plugin should be able to "override" the default image asset module rule.
It looks to me that before, the order mattered, and we just had to add the custom image loader at the beginning of the list rule to override the existing rule. So we have plugins that do:
It seems that now, adding this rule at the beginning does not prevent the default asset loaders in Docusaurus core to be used, unless we use oneOf.
I don't feel like using oneOf is very good in this case because it would break our existing plugin ecosystem that relies on append/prepend in rules, and expose more implementation details of the core Webpack config to plugins that want to customize it. Playing with resourceQuery and notResourceQuery is also not very convenient in our case, as plugin authors would have to potentially add notResourceQuery clauses.
For Docusaurus, using oneOf could work, as we don't have a large plugin ecosystem it might affect only a few users, but I guess it could be more problematic for larger frameworks like Next.js or Gatsby.
Apart that, I like this new asset modules system, more convenient that using inline loader syntax with a messy querystring
btw. there is another way, which would work without touching any other part of the config:
{test: /\.txt$/,type: "asset/source",},
import source from "file.txt!=!./file.js";
This treats content of file.js as it was written in file.txt.
Not sure if this works with require.context, but you could try it with require.context("code.txt!=!./code", false, /\.js$/), but this might look a bit weird anyway.
Otherwise you could also go with:
import sources from "./all-code.js!";
and generate code like that in the all-code.jsloader:
Activity
[-]inline loader not migrateable when used in require.context[/-][+]inline loader not migrateable to webpack 5 when used in require.context[/+]alexander-akait commentedon Mar 15, 2021
Intersect rules, please use:
otherwise
testis preferredeps1lon commentedon Mar 15, 2021
That's not what the documentation is saying though? Now I have to change any existing (and future) rule that might process assets loaded as source.
alexander-akait commentedon Mar 15, 2021
@eps1lon You need do it only for rules where
testcan have intersections (or where you don't specifytest), maybe we should privatizeresourceQueryandresourceFragmentto improve DX/cc @sokra
eps1lon commentedon Mar 15, 2021
That's not adressing my problem though. I have to make sure anybody who adds rules considers this.
How does you comment adress the problem where I don't have access to the rule objects e.g. when extending a nextjs webpack configuration?
alexander-akait commentedon Mar 15, 2021
What about this? So you don't need exclude
resourceQueryfrom other ruleseps1lon commentedon Mar 15, 2021
I'm not following. How would I then be able to migrate from inline-loaders? Based on file extension is not possible since I need some files both as regular JS and their raw source.
alexander-akait commentedon Mar 15, 2021
Sorry, I don't understand you.
Using
resourceQuery,resourceQuery: /^(?!\?raw$).*/,I provide solution above.
Do you want to avoid using
resourceQuery: /^(?!\?raw$).*/,? If yes, above I suggested that we could prioritizeresourceQueryovertest, so you don't needresourceQuery: /^(?!\?raw$).*/,in your configeps1lon commentedon Mar 15, 2021
But you said you want to "privatize ressourceQuery". I interpreted "privatize" as "not being part of the public API" i.e. not usable.
Could you summarize how I would write the initial set of rules without changing the rule that applies the babel-loader?
alexander-akait commentedon Mar 15, 2021
@eps1lon sorry, typo, I mean
prioritizeRight now only using
resourceQuery: /^(?!\?raw$).*/,, we need think more about itsokra commentedon Mar 15, 2021
That's pretty un-readable. You could use
resourceQuery: { not: /raw/ }.But even better use
oneOfrules:write a helper function if this is too much nesting for you...
eps1lon commentedon Mar 16, 2021
I just copied the docs 😅: https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax
That does not work for me. I do not have control over all the rules since I'm using nextjs. I could technically but the order of the rules isn't some public API. And even then the point about future rules still apply: I need to manually verify no rules conflicts whenever I update nextjs.
alexander-akait commentedon Mar 16, 2021
@sokra using
oneOfis not very convenient in many cases, also some developers can't have access towebpack.config.js5 remaining items
sokra commentedon Mar 16, 2021
Yeah that why I only mentioned the other solutions first.
But he is already using
require.contextas webpack-specific syntax and there is not official replacement for that and there probably won't be...If you want a solution without any webpack magic you can go with:
WIP attempt to use webpack 5 asset modules
slorber commentedon Apr 30, 2021
I have a distinct but similar case where I feel it's complicated to migrate to the asset modules system.
Docusaurus has a modular architecture with a plugin system.
A Docusaurus image plugin should be able to "override" the default image asset module rule.
It looks to me that before, the order mattered, and we just had to add the custom image loader at the beginning of the list rule to override the existing rule. So we have plugins that do:
It seems that now, adding this rule at the beginning does not prevent the default asset loaders in Docusaurus core to be used, unless we use oneOf.
I don't feel like using oneOf is very good in this case because it would break our existing plugin ecosystem that relies on append/prepend in rules, and expose more implementation details of the core Webpack config to plugins that want to customize it. Playing with resourceQuery and notResourceQuery is also not very convenient in our case, as plugin authors would have to potentially add notResourceQuery clauses.
For Docusaurus, using oneOf could work, as we don't have a large plugin ecosystem it might affect only a few users, but I guess it could be more problematic for larger frameworks like Next.js or Gatsby.
Apart that, I like this new asset modules system, more convenient that using inline loader syntax with a messy querystring
Edit: here's a migration PR attempt: meta/docusaurus#4708
webpack-bot commentedon Oct 16, 2021
This issue had no activity for at least three months.
It's subject to automatic issue closing if there is no activity in the next 15 days.
sokra commentedon Feb 20, 2022
Don't use
.default?Airkro commentedon Apr 21, 2022
Is possible to support
typeasimport.meta.webpackContextparameter?vankop commentedon Apr 21, 2022
there is no
typeinimport.meta.webpackContext. we don't plan to support more options forrequire.context.vankop commentedon Apr 21, 2022
there is an answer on original issue. please create separate issues.
ITsvetkoFF commentedon Mar 22, 2023
For those who are struggling with
for a webpack config that you are not controlling completely (like in Storybook)
there is another approach that I (in a desperate moment) was able to find in code search over github: source
where this happens:
MichaelAllenWarner commentedon Aug 7, 2024
This is extremely ugly but worked for me. Thank you!