Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PFX-811] - Tune Intl action #975

Merged
merged 10 commits into from
Nov 20, 2024
14 changes: 10 additions & 4 deletions packages/gasket-plugin-intl/lib/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const getPreferredLocale = require('./utils/get-preferred-locale');

const reqMap = new WeakMap();
Expand All @@ -19,11 +20,16 @@ async function getIntlLocale(gasket, req) {
}

/** @type {import('@gasket/core').ActionHandler<'getIntlManager'>} */
function getIntlManager(gasket) {
if (!gasket.config.intl.manager) {
throw new Error('IntlManager not configured (gasket.config.intl.manager)');
async function getIntlManager(gasket) {
if (!gasket.config.intl.managerFilename) {
throw new Error('IntlManager not configured (gasket.config.intl.managerFilename)');
}
return gasket.config.intl.manager;

if (!gasket.config.intl.experimentalImportAttributes) {
throw new Error('To use experimental import attributes you must configure `gasket.config.intl.experimentalImportAttributes`');
}

return (await import(path.join(gasket.config.root, gasket.config.intl.managerFilename))).default;
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions packages/gasket-plugin-intl/lib/build-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ module.exports = async function buildManifest(gasket, options = {}) {
const keyName = importName
.replace(/\.json$/, '')
.replace('./', '');
if (gasket.config.intl.experimentalImportAttributes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y'all should create a top level experimental/future flag which uses import asserts.

import as/from are executed at build time. createRequire is executed at runtime which makes it harder to run gasket in serverless environments

return { [keyName]: `%() => import('${importName}', { with: { type: 'json' } })%` };
}

return { [keyName]: `%() => import('${importName}')%` };
})
).reduce((a, c) => ({ ...a, ...c }), {});
Expand Down
3 changes: 2 additions & 1 deletion packages/gasket-plugin-intl/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface IntlConfig extends LocaleManifestConfig {
| string[];
nextRouting?: boolean;
manager?: IntlManager;
experimentalImportAttributes?: boolean;
}

declare module '@gasket/core' {
Expand All @@ -40,7 +41,7 @@ declare module '@gasket/core' {
* Provides access to the Intl manager instance to plugins.
* Especially useful for plugins that are still CJS.
*/
getIntlManager: () => IntlManager;
async getIntlManager: () => IntlManager;
}

export interface HookExecTypes {
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-plugin-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict --experimental-vm-modules' jest",
"test:watch": "npm run test -- --watch",
"test:coverage": "npm run test -- --coverage",
"posttest": "npm run lint && npm run typecheck",
Expand Down
28 changes: 18 additions & 10 deletions packages/gasket-plugin-intl/test/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-sync */
const actions = require('../lib/actions');

const mockIntlManager = {};

describe('actions', () => {
let req, mockGasket, mockLocale;

Expand All @@ -16,8 +14,9 @@ describe('actions', () => {
mockGasket = {
execWaterfall: jest.fn().mockImplementation((lifecycle, content) => content),
config: {
root: `${__dirname}/fixtures/gasket-root`,
intl: {
manager: mockIntlManager,
managerFilename: 'intl.js',
defaultLocale: 'en'
}
}
Expand All @@ -41,15 +40,24 @@ describe('actions', () => {
});

describe('getIntlManager', () => {
it('should return the configured manager', () => {
const result = actions.getIntlManager(mockGasket);
expect(result).toBe(mockIntlManager);
it('should return the configured manager', async () => {
mockGasket.config.intl.experimentalImportAttributes = true;
const result = await actions.getIntlManager(mockGasket);
expect(result).toEqual({ default: {} });
});

it('should throw if manager not configured', async () => {
delete mockGasket.config.intl.managerFilename;
await expect(() => actions.getIntlManager(mockGasket))
.rejects
.toThrow('IntlManager not configured (gasket.config.intl.managerFilename)');
});

it('should throw if manager not configured', () => {
delete mockGasket.config.intl.manager;
expect(() => actions.getIntlManager(mockGasket))
.toThrow('IntlManager not configured (gasket.config.intl.manager)');
it('should throw if experimental import attributes not configured', async () => {
delete mockGasket.config.intl.experimentalImportAttributes;
await expect(() => actions.getIntlManager(mockGasket))
.rejects
.toThrow('To use experimental import attributes you must configure `gasket.config.intl.experimentalImportAttributes`');
});
});
});
12 changes: 12 additions & 0 deletions packages/gasket-plugin-intl/test/build-manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ describe('buildManifest', function () {
);
});

it('allows for experimental import attributes if configured', async function () {
mockGasket.config.intl.experimentalImportAttributes = true;
await buildManifest(mockGasket);
const output = getOutput();
expect(output).toContain(
`'locales/en-US': () => import('./locales/en-US.json', { with: { type: 'json' } })`
);
expect(output).toContain(
`'locales/fr-FR': () => import('./locales/fr-FR.json', { with: { type: 'json' } })`
);
});

it('associates nested locale file keys to imports', async function () {
await buildManifest(mockGasket);
const output = getOutput();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
default: {}
};
Loading