-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] babel-loader cache consider browserslistrc
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/xarc-webpack/src/util/generate-babel-loader-cache-id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |