When using optimization.splitchunks library is not available in import
See original GitHub issueBug report ?
Not sure if its a bug, but I am stuck here and want help from the community…
What is the current behavior? I am creating a library to be used in react web and react-native. Since library size is too heavy, am trying to split it into chunks and trying splitChunks but library doesn’t seem to be available for import.
Importing a simple module from this library isn’t available when splitChunks is enabled. However when not using splitChunks, it works like charm.
Here is my webpack.config
module.exports = {
entry:
{
'index': './src/index.ts',
'utils': './src/index.utils.ts',
},
output: {
path: path.join(__dirname, "dist"),
filename: "[name].ts",
library: ["frontend-business", "[name]"],
libraryTarget: "commonjs2"
},
devtool: 'inline-source-map',
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules|build|dist)/,
use: {
loader: 'babel-loader',
}
},
{
test: /\.ts?$/,
include: path.resolve(__dirname, 'src'),
use: 'ts-loader',
exclude: /(node_modules|build|dist)/,
},
]
},
externals: {
'react': 'commonjs react', // this line is just to use the React dependency of our parent-platform-project instead of using our own React.
'@types/react': 'commonjs @types/react'
},
optimization: {
splitChunks: {
chunks: 'all',
}
}
};
What is the expected behavior? With splitChunks it should have work as without it because I dont see any extra work associated with splitChunks to make it work.
Other relevant information: webpack version: 4.41.2 Node.js version: 10.16.3 Operating System: Darwin
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:17 (7 by maintainers)
Top Results From Across the Web
How to config webpack - libraryTarget + splitChunks to import ...
If I don't use splitChunks all works fine ( import { Tools } from 'libs/dist/. ... runtime' but nope... both imports not working...
Read more >SplitChunksPlugin - webpack
optimization.splitChunks. This configuration object represents the default behavior of the SplitChunksPlugin . webpack.config.js
Read more >webpack/webpack - Gitter
So I'm running into a problem where as soon as I enable any kind of optimization.splitChunks settings my module now is importing in...
Read more >Webpack: An in-depth introduction to SplitChunksPlugin
optimization : { // Instruct webpack not to obfuscate the resulting code minimize: false, splitChunks: { minSize: 0, chunks: 'all', minChunks: 4, ...
Read more >The 100% correct way to split your chunks with Webpack
Working out the best way to serve up files to your users can be a tricky ... With Webpack 4 and the import()...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

no one was able to fix this… only dynamic imports works when code splitting are enabled.
Hi, @sokra!
It is sometimes useful (especially in reactlandia) to have a library with multiple entrypoints, with each entrypoint re-exporting a common module (think of a singleton). We need all entrypoints to
requirethe shared module. It is trivial to achieve with rollup, see this demo: https://github.com/dkamyshov/rollup-split-library-reproduction/tree/master/distWith webpack, it is somewhat achievable with
libraryTarget: 'module', though it uses some weird dynamic requires and I’m not sure whether webpack would consume such a library. Those dynamic requires look like this: