-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(config): go back to initial approach
- Loading branch information
Showing
8 changed files
with
74 additions
and
45 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const get = require('lodash/get'); | ||
|
||
/** | ||
* Custom JS module loader. | ||
* This loader is used to load Custom Application config files that are not JSON files, | ||
* for example any JS or TS files. | ||
* To load the file, we need to make sure that we use our Babel preset to allow parsing | ||
* the file with the supported features. TypeScript files are also loaded via the preset. | ||
* Futhermore, we need to load the config file as a module, meaning that the exported | ||
* Custom Application config can potentially contain JS functions and these should be preserved. | ||
* To do that, we use `@babel/register` and ensure the Babel preset is used. | ||
*/ | ||
function loadJsModule(filePath) { | ||
// Skip registering Babel for tests, as Babel is alrady configured. | ||
if (process.env.NODE_ENV !== 'test') { | ||
require('@babel/register')({ | ||
babelrc: false, | ||
extensions: ['.js', '.cjs', '.mjs', '.ts'], | ||
presets: ['@commercetools-frontend/babel-preset-mc-app'], | ||
}); | ||
} | ||
|
||
// Require the module. It's expected that the module exports the application config. | ||
const moduleExport = require(filePath); | ||
|
||
// In case we are loading an ES module, we need to pick the `default` export. | ||
return get(moduleExport, 'default', moduleExport); | ||
} | ||
|
||
module.exports = loadJsModule; |
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
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
9 changes: 8 additions & 1 deletion
9
packages/application-config/test/fixtures/app-cjs/constants.js
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 |
---|---|---|
@@ -1 +1,8 @@ | ||
exports.entryPointUriPath = 'test'; | ||
const { | ||
entryPointUriPathToPermissionKeys, | ||
} = require('@commercetools-frontend/application-shell/ssr'); | ||
|
||
const entryPointUriPath = 'test'; | ||
|
||
exports.entryPointUriPath = entryPointUriPath; | ||
exports.PERMISSIONS = entryPointUriPathToPermissionKeys(entryPointUriPath); |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
const { | ||
entryPointUriPathToPermissionKeys, | ||
} = require('@commercetools-frontend/application-shell/ssr'); | ||
|
||
const entryPointUriPath = 'test'; | ||
const PERMISSIONS = entryPointUriPathToPermissionKeys(entryPointUriPath); | ||
|
||
module.exports = { entryPointUriPath }; | ||
module.exports = { entryPointUriPath, PERMISSIONS }; |
3 changes: 3 additions & 0 deletions
3
packages/application-config/test/fixtures/app-mjs/constants.js
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
import { entryPointUriPathToPermissionKeys } from '@commercetools-frontend/application-shell/ssr'; | ||
|
||
export const entryPointUriPath = 'test'; | ||
export const PERMISSIONS = entryPointUriPathToPermissionKeys(entryPointUriPath); |
3 changes: 3 additions & 0 deletions
3
packages/application-config/test/fixtures/app-ts/constants.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 |
---|---|---|
@@ -1 +1,4 @@ | ||
import { entryPointUriPathToPermissionKeys } from '@commercetools-frontend/application-shell/ssr'; | ||
|
||
export const entryPointUriPath = 'test'; | ||
export const PERMISSIONS = entryPointUriPathToPermissionKeys(entryPointUriPath); |
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