Skip to content

Commit

Permalink
[fix] babel-loader cache consider browserslistrc
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Feb 6, 2021
1 parent 5cc0bb2 commit 00a24be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/xarc-webpack/src/partials/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Path from "path";
const identity = require("lodash/identity");
const assign = require("lodash/assign");
const babelLoader = require.resolve("babel-loader");
import { generateBabelLoaderCacheId } from "../util/generate-babel-loader-cache-id";

import { loadXarcOptions } from "../util/load-xarc-options";

Expand Down Expand Up @@ -39,6 +40,7 @@ module.exports = function(options) {

const test = xarcOptions.babel.enableTypeScript ? /\.[tj]sx?$/ : /\.jsx?$/;

const cacheIdentifier = generateBabelLoaderCacheId(xarcOptions.cwd);
const babelLoaderConfig = {
_name: "babel",
test,
Expand All @@ -47,7 +49,10 @@ module.exports = function(options) {
{
loader: babelLoader,
options: Object.assign(
{ cacheDirectory: Path.resolve(xarcOptions.cwd, ".etmp/babel-loader") },
{
cacheIdentifier,
cacheDirectory: Path.resolve(xarcOptions.cwd, ".etmp/babel-loader")
},
options.babel
)
}
Expand Down
51 changes: 51 additions & 0 deletions packages/xarc-webpack/src/util/generate-babel-loader-cache-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as Fs from "fs";
import * as Path from "path";

/**
* Generate a cache identifier for babel-loader
*
* @param cwd - app dir
* @returns cache identifier
*/
export function generateBabelLoaderCacheId(cwd: string): string {
/*
* cacheIdentifier: Default is a string composed by the @babel/core's version,
* the babel-loader's version, the contents of .babelrc file if it exists,
* and the value of the environment variable BABEL_ENV with a fallback to
* the NODE_ENV environment variable. This can be set to a custom value to
* force cache busting if the identifier changes.
*/
// @babel/core's version
// babel-loader's version
const pkgVersions = ["@babel/core", "babel-loader"].map(pkg => {
return require(`${pkg}/package.json`).version; // eslint-disable-line
});

let babelConfig;
let browsersListRc;
const dir = cwd || process.env.XARC_CWD || process.cwd();

try {
// content of babel.config.js
babelConfig = Fs.readFileSync(Path.join(dir, "babel.config.js"), "utf-8");
} catch {
//
}
// .browserslistrc content
try {
browsersListRc = Fs.readFileSync(Path.join(dir, ".browserslistrc"), "utf-8");
} catch {
//
}

return pkgVersions
.concat(
babelConfig,
process.env.BABEL_ENV,
process.env.NODE_NV,
browsersListRc,
process.env.BROWSERSLIST_ENV
)
.filter(x => x)
.join("\n");
}

0 comments on commit 00a24be

Please sign in to comment.