Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cacheIdentifier option as a function? #946

Open
YoonjiJang opened this issue Jul 14, 2022 · 1 comment
Open

cacheIdentifier option as a function? #946

YoonjiJang opened this issue Jul 14, 2022 · 1 comment

Comments

@YoonjiJang
Copy link

YoonjiJang commented Jul 14, 2022

BEFORE YOU SUBMIT please read the following:

I'm submitting a feature request

Webpack Version:
5.x

Babel Core Version:
7.18.x

Babel Loader Version:
8.2.x

Please tell us about your environment:
OSX 13.x

Current behavior:
cacheIdentifier is an option that defaults to stringified json with @babel/core version, babel-loader version, and "options" object.

https://github.com/babel/babel-loader/blob/v8.2.5/src/index.js#L189-L193

Expected/desired behavior:
Want to be able to set it to a function whose argument includes values used in the default value.

// webpack.config.js

{
  loader: 'babel-loader',
  options: {
    cacheIdentifier({  options, babelCoreVersion, babelLoaderVersion }) {
      // returns string
      return JSON.stringify({ options, babelCoreVersion, babelLoaderVersion, someOtherKey: 'cache-busting value' });
    },
  },
},

Maybe something like this could work.

// babel-loader/src/index.js

const cacheIdentifierOptions = {
  options,
  "@babel/core": transform.version,
  "@babel/loader": version,
};

let {
  // ...
  cacheIdentifier = JSON.stringify(cacheIdentifierOptions),
  // ...
} = loaderOptions;

if (cacheIdentifier === 'function') {
  cacheIdentifier = cacheIdentifier(cacheIdentifierOptions);
} 
  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with a gist/jsbin of your webpack configuration.

  • What is the expected behavior?

  • What is the motivation / use case for changing the behavior?
    The default value makes sense for most use cases and includes info that depends on the file being transpiled, such as filename, but I can't access it :/

@JLHwung
Copy link
Contributor

JLHwung commented Jul 14, 2022

As a workaround, you can create cacheIdentifier in your webpack config and pass it down to the babel-loader.

const babelConfig = fs.readFileSync("path/to/babel/Config");
const { version: babelVersion } = require("@babel/core");
const { version: babelLoaderVersion } = require("@babel/loader");

{
  loader: 'babel-loader',
  options: {
    cacheIdentifier: [babelConfig, babelVersion, babelLoaderVersion, other_variables].join('\0');
  }
}

It should be even faster than the defaults because the config is not serialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants