Skip to content

Commit

Permalink
feat: add new salesforce site scoped module for default language (@W-…
Browse files Browse the repository at this point in the history
…12984619) (#181)

Co-authored-by: Fatema Saifee <fatema.saifee@fatemas-ltmrjdw.internal.salesforce.com>
  • Loading branch information
nolanlawson and Fatema Saifee authored May 12, 2023
1 parent 69fdc0c commit 7f510f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,28 @@ describe('@salesforce/site/activeLanguages import', () => {
'Invalid import from @salesforce/site/activeLanguages'
);
});

describe('@salesforce/site/defaultLanguage import', () => {
test(
'does default transformation',
`
import defaultLanguage from '@salesforce/site/defaultLanguage';
`,
`
import defaultLanguage from '@salesforce/site/defaultLanguage';
`
);

test(
'allows non-@salesforce/site/defaultLanguages named imports',
`
import { otherNamed } from './something-valid';
import defaultLanguage from '@salesforce/site/defaultLanguage';
`,
`
import { otherNamed } from './something-valid';
import defaultLanguage from '@salesforce/site/defaultLanguage';
`
);

});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ const siteActiveLanguagesTemplate = babelTemplate(`
}
`);

const siteDefaultLanguagesTemplate = babelTemplate(`
let RESOURCE_NAME;
try {
RESOURCE_NAME = require(IMPORT_SOURCE).default;
} catch (e) {
RESOURCE_NAME = { code: 'en-US', label: 'English (US)' };
}
`);

function siteDefaultLanguagesScopedImportTransform(t, path) {
const { importSource, resourceNames } = getImportInfo(path);
const defaultImport = resourceNames[0];

path.replaceWithMultiple(
siteDefaultLanguagesTemplate({
RESOURCE_NAME: t.identifier(defaultImport),
IMPORT_SOURCE: t.stringLiteral(importSource),
})
);
}

function siteActiveLanguagesScopedImportTransform(t, path) {
const { importSource, resourceNames } = getImportInfo(path);
const defaultImport = resourceNames[0];
Expand Down Expand Up @@ -48,6 +69,9 @@ module.exports = function ({ types: t }) {
function siteScopedImportTransform(t, path, importId) {
importId = importId.substring(SITE_ID_IMPORT_IDENTIFIER.length);
switch (importId) {
case 'defaultLanguages':
siteDefaultLanguagesScopedImportTransform(t, path);
break;
case 'activeLanguages':
siteActiveLanguagesScopedImportTransform(t, path);
break;
Expand Down

0 comments on commit 7f510f7

Please sign in to comment.