Skip to content

Commit

Permalink
Merge pull request #15394 from mshima/skip_ci-react_hash
Browse files Browse the repository at this point in the history
Fix index reproducibility in react framework.
  • Loading branch information
pascalgrimaud committed Jun 19, 2021
2 parents af37b33 + 32ebfc0 commit 91c50c7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions generators/client/templates/react/jest.conf.js.ejs
Expand Up @@ -25,6 +25,9 @@ module.exports = {
],
setupFiles: ['<rootDir>/<%= CLIENT_MAIN_SRC_DIR %>app/setup-tests.ts'],
globals: {
<%_ if (enableTranslation) { _%>
I18N_HASH: 'generated_hash',
<%_ } _%>
'ts-jest': {
tsconfig: './tsconfig.test.json',
compiler: 'typescript',
Expand Down
1 change: 1 addition & 0 deletions generators/client/templates/react/package.json
Expand Up @@ -54,6 +54,7 @@
"eslint-plugin-react": "7.24.0",
"eslint-webpack-plugin": "2.5.4",
"file-loader": "6.2.0",
"folder-hash": "4.0.1",
"fork-ts-checker-webpack-plugin": "6.2.10",
"html-webpack-plugin": "5.3.1",
"identity-obj-proxy": "3.0.0",
Expand Down
1 change: 1 addition & 0 deletions generators/client/templates/react/package.json.ejs
Expand Up @@ -122,6 +122,7 @@
"lint-staged": "<%= dependabotPackageJson.devDependencies['lint-staged'] %>",
<%_ } _%>
<%_ if (enableTranslation) { _%>
"folder-hash": "<%= dependabotPackageJson.devDependencies['folder-hash'] %>",
"merge-jsons-webpack-plugin": "<%= dependabotPackageJson.devDependencies['merge-jsons-webpack-plugin'] %>",
<%_ } _%>
"mini-css-extract-plugin": "<%= dependabotPackageJson.devDependencies['mini-css-extract-plugin'] %>",
Expand Down
Expand Up @@ -31,7 +31,7 @@ export type LocaleState = Readonly<typeof initialState>;

export const setLocale: (locale: string) => AppThunk = locale => async dispatch => {
if (!Object.keys(TranslatorContext.context.translations).includes(locale)) {
const response = await axios.get(`i18n/${locale}.json?buildTimestamp=${process.env.BUILD_TIMESTAMP}`, { baseURL: '' });
const response = await axios.get(`i18n/${locale}.json?_=${I18N_HASH}`, { baseURL: '' });
TranslatorContext.registerTranslations(locale, response.data);
}
dispatch(updateLocale(locale));
Expand Down
Expand Up @@ -16,7 +16,10 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_ if (enableTranslation) { _%>
declare const I18N_HASH: string;

<%_ } _%>
declare module '*.json' {
const value: any;
export default value;
Expand Down
19 changes: 15 additions & 4 deletions generators/client/templates/react/webpack/webpack.common.js.ejs
Expand Up @@ -24,6 +24,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
<%_ if (enableTranslation) { _%>
const { hashElement } = require('folder-hash');
const MergeJsonWebpackPlugin = require('merge-jsons-webpack-plugin');
<%_ } if (buildToolUnknown) { _%>

Expand Down Expand Up @@ -58,7 +59,16 @@ const getTsLoaderRule = env => {
return rules;
};

module.exports = options => merge(
module.exports = async options => {
<%_ if (enableTranslation) { _%>
const languagesHash = await hashElement(path.resolve(__dirname, '../<%= MAIN_SRC_DIR %>i18n'), {
algo: 'md5',
encoding: 'hex',
files: { include: ['*.json'] },
});

<%_ } _%>
return merge(
{
cache: {
// 1. Set cache type to filesystem
Expand Down Expand Up @@ -125,11 +135,11 @@ module.exports = options => merge(
},
plugins: [
new webpack.DefinePlugin({
<%_ if (enableTranslation) { _%>
I18N_HASH: JSON.stringify(languagesHash.hash),
<%_ } _%>
'process.env': {
NODE_ENV: `'${options.env}'`,
<%_ if (enableTranslation) { _%>
BUILD_TIMESTAMP: `'${new Date().getTime()}'`,
<%_ } _%>
<%_ if (buildToolUnknown) { _%>
VERSION: `'${packageJson.version}'`,
<%_ } else { _%>
Expand Down Expand Up @@ -186,3 +196,4 @@ module.exports = options => merge(
}
// jhipster-needle-add-webpack-config - JHipster will add custom config
);
};
Expand Up @@ -29,7 +29,7 @@ const commonConfig = require('./webpack.common.js');

const ENV = 'development';

module.exports = (options) => webpackMerge(commonConfig({ env: ENV }), {
module.exports = async (options) => webpackMerge(await commonConfig({ env: ENV }), {
devtool: 'cheap-module-source-map', // https://reactjs.org/docs/cross-origin-errors.html
mode: ENV,
entry: [
Expand Down
Expand Up @@ -29,7 +29,7 @@ const commonConfig = require('./webpack.common.js');

const ENV = 'production';

module.exports = webpackMerge(commonConfig({ env: ENV }), {
module.exports = async () => webpackMerge(await commonConfig({ env: ENV }), {
// devtool: 'source-map', // Enable source maps. Please note that this will slow down the build
mode: ENV,
entry: {
Expand Down

0 comments on commit 91c50c7

Please sign in to comment.