question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When using optimization.splitchunks library is not available in import

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
madruga8commented, Jun 19, 2022

no one was able to fix this… only dynamic imports works when code splitting are enabled.

1reaction
dkamyshovcommented, Sep 7, 2020

Hi, @sokra!

That’s pretty unusual for a library.

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 require the shared module. It is trivial to achieve with rollup, see this demo: https://github.com/dkamyshov/rollup-split-library-reproduction/tree/master/dist

With 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:

/******/ 	(() => {
/******/ 		var next = __webpack_require__.x;
/******/ 		__webpack_require__.x = () => {
/******/ 			__webpack_require__.e("src_a_js-src_b_js");
/******/ 			return next();
/******/ 		};
/******/ 	})();
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found