Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use chunkhash only for some output files in webpack

I am building an application that is being loaded as a plugin in other applications. Think grammarly or intercom.
So, I will have a javascript file (the loader) that customers need to reference in their application.
This file in turn adds an iFrame and loads my index.html which references my main bundle.

Now, I want to have the loader to not have a chunkhash in its filename but the bundle should have the chunkhash.

Possible? If so, how?

My webpack config (ejected from create-react-app-typescript):

entry: {
    app: [require.resolve('./polyfills'), paths.appIndexJs],
    loader: "./src/integration/loader.ts"
},
output: {
    // The build folder.
    path: paths.appBuild,
    // Generated JS file names (with nested folders).
    // There will be one main bundle, and one file per asynchronous chunk.
    // We don't currently advertise code splitting but Webpack supports it.
    filename: '[name].[chunkhash:8].js',
    chunkFilename: '[name].[chunkhash:8].chunk.js',
    // We inferred the "public path" (such as / or /my-project) from homepage.
    publicPath: publicPath,
    // Point sourcemap entries to original disk location (format as URL on Windows)
    devtoolModuleFilenameTemplate: info =>
        path
            .relative(paths.appSrc, info.absoluteResourcePath)
            .replace(/\\/g, '/'),
},
like image 668
Daniel Hilgarth Avatar asked Nov 16 '25 16:11

Daniel Hilgarth


1 Answers

Why not use a function for filename.

...
output: {
    ...
    filename: (chunkData) => {
        return chunkData.chunk.name === 'loader'
            ? '[name].js'
            : '[name].[chunkhash:8].js';
    },
}

See https://webpack.js.org/configuration/output/#output-filename

like image 188
Aravindan Ve Avatar answered Nov 19 '25 08:11

Aravindan Ve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!