This is special restriction added by developers of create-react-app. It is implemented in ModuleScopePlugin
to ensure files reside in src/
. That plugin ensures that relative imports from app's source directory don't reach outside of it.
There is no official way to disable this feature except using eject
and modify webpack config.
But, most features and its updates are hidden into the internals of create-react-app system. If you make eject
you will have no more new features and its update. So if you are not ready to manage and configure application included to configure webpack and so on - do not do eject
operation.
Play by the existing rules - move assets to src or use based on public
folder url without import.
However instead of eject
there are much unofficial solutions, based on
rewire which allows you to programmatically modify the webpack config without eject
. But removing the ModuleScopePlugin
plugin is not good - this loses some protection and does not adds some features available in src
. ModuleScopePlugin
is designed to support multiple folders.
The better way is to add fully working additional directories similar to src
also protected by ModuleScopePlugin
. This can be done using react-app-rewire-alias
Anyway do not import from public
folder - that will be duplicated in the build
folder and will be available by two different url (and with different ways to load), which ultimately worsen the package download size.
Importing from the src
folder is preferable and has advantages. Everything will be packed by webpack to the bundle with chunks optimal size and for best loading efficiency.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…