Skip to content

Modular logistics and supply chain operating system (LSOS)

fix(console): bound Tailwind scans and Intl locale bundles

Ronald A. Richardson224b28c54df9
diff --git a/console/config/intl-polyfill-context.js b/console/config/intl-polyfill-context.js
new file mode 100644
index 00000000..ca680657
--- /dev/null
+++ b/console/config/intl-polyfill-context.js
@@ -0,0 +1,10 @@
+'use strict';
+
+const { ContextReplacementPlugin } = require('webpack');
+
+module.exports = function intlPolyfillContext() {
+ // load-intl-polyfills normalizes every translation locale to its two- or
+ // three-letter base language before importing data. Keep all base languages
+ // available for extensions without bundling unreachable regional variants.
+ return new ContextReplacementPlugin(/@formatjs[/\\]intl-(?:numberformat|pluralrules|datetimeformat|relativetimeformat)[/\\]locale-data$/, false, /^\.\/[a-z]{2,3}\.js$/i);
+};
diff --git a/console/config/tailwind-content.js b/console/config/tailwind-content.js
new file mode 100644
index 00000000..a872282d
--- /dev/null
+++ b/console/config/tailwind-content.js
@@ -0,0 +1,44 @@
+/* eslint-env node */
+'use strict';
+
+const fs = require('fs');
+const path = require('path');
+
+function entries(directory) {
+ try {
+ return fs.readdirSync(directory);
+ } catch (error) {
+ if (error.code === 'ENOENT' || error.code === 'ENOTDIR') {
+ return [];
+ }
+ throw error;
+ }
+}
+
+// Resolve only the consumer's direct packages. Recursive node_modules globs follow
+// pnpm links into dependency graphs and linked packages' development worktrees.
+// Keep this helper local: published ember-ui versions may not provide it yet.
+module.exports = function addonContent(root = process.cwd()) {
+ const modules = path.resolve(root, 'node_modules');
+ const packages = entries(modules).flatMap((name) => (name.startsWith('@') ? entries(path.join(modules, name)).map((child) => `${name}/${child}`) : [name]));
+ const roots = new Set();
+
+ for (const name of packages) {
+ if (!name.startsWith('@fleetbase/') && !name.endsWith('-engine')) {
+ continue;
+ }
+ const addon = path.join(modules, name, 'addon');
+ if (fs.existsSync(addon)) {
+ roots.add(fs.realpathSync(addon));
+ }
+ }
+
+ // Keep globs within each source tree so newly created templates are watched.
+ return [...roots].sort().map(
+ (directory) =>
+ `${directory
+ .split(path.sep)
+ .join('/')
+ .replace(/[!*?{}()[\]]/g, '\\$&')}/**/*.{hbs,js}`
+ );
+};
diff --git a/console/ember-cli-build.js b/console/ember-cli-build.js
index 163fa922..96492cc1 100644
--- a/console/ember-cli-build.js
+++ b/console/ember-cli-build.js
@@ -5,6 +5,7 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const Funnel = require('broccoli-funnel');
const writeFile = require('broccoli-file-creator');
const toBoolean = require('./config/utils/to-boolean');
+const intlPolyfillContext = require('./config/intl-polyfill-context');
module.exports = function (defaults) {
const app = new EmberApp(defaults, {
@@ -24,6 +25,12 @@ module.exports = function (defaults) {
silent: true,
},
+ autoImport: {
+ webpack: {
+ plugins: [intlPolyfillContext()],
+ },
+ },
+
'ember-simple-auth': {
useSessionSetupMethod: true,
},
diff --git a/console/tailwind.config.js b/console/tailwind.config.js
index e0b2f0d2..52655a8c 100644
--- a/console/tailwind.config.js
+++ b/console/tailwind.config.js
@@ -1,16 +1,10 @@
+/* eslint-env node */
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: ['class', '[data-theme="dark"]'],
content: {
relative: true,
- files: [
- './app/**/*.{hbs,js}',
- './node_modules/.pnpm/@fleetbase+*/**/addon/**/*.{hbs,js}',
- './node_modules/@fleetbase+*/addon/**/*.{hbs,js}',
- './node_modules/@fleetbase/ember-ui/addon/templates/**/*.{hbs,js}',
- './node_modules/@fleetbase/ember-ui/addon/components/**/*.{hbs,js}',
- './node_modules/**/*-engine/addon/**/*.{hbs,js}',
- ],
+ files: ['./app/**/*.{hbs,js}', ...require('./config/tailwind-content')(__dirname)],
},
safelist: [
{
diff --git a/console/tests/node/intl-polyfill-context-test.cjs b/console/tests/node/intl-polyfill-context-test.cjs
new file mode 100644
index 00000000..8a7bf3a7
--- /dev/null
+++ b/console/tests/node/intl-polyfill-context-test.cjs
@@ -0,0 +1,64 @@
+'use strict';
+
+const assert = require('node:assert/strict');
+const fs = require('node:fs/promises');
+const os = require('node:os');
+const path = require('node:path');
+const { test } = require('node:test');
+const webpack = require('webpack');
+const intlPolyfillContext = require('../../config/intl-polyfill-context');
+
+test('FormatJS contexts retain extension base languages and omit unreachable regional data', async () => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'fleetbase-intl-context-'));
+ const packages = ['numberformat', 'pluralrules', 'datetimeformat', 'relativetimeformat'].map((name) => `@formatjs/intl-${name}`);
+ const imports = packages.map((name) => `import(\`${name}/locale-data/\${lang}.js\`)`);
+ const output = path.join(root, 'dist');
+ let compiler;
+
+ try {
+ for (const name of [...packages, 'unrelated']) {
+ const directory = path.join(root, 'node_modules', name, 'locale-data');
+ await fs.mkdir(path.join(directory, 'nested'), { recursive: true });
+ for (const language of ['en', 'fil', 'pt-BR', 'nested/en']) {
+ await fs.writeFile(path.join(directory, `${language}.js`), `module.exports = ${JSON.stringify(language)};`);
+ }
+ }
+
+ await fs.writeFile(
+ path.join(root, 'entry.js'),
+ `exports.load = lang => Promise.all([${imports.join(',')}]);\nexports.unrelated = lang => import(\`unrelated/locale-data/\${lang}.js\`);`
+ );
+ compiler = webpack({
+ context: root,
+ mode: 'development',
+ target: 'node',
+ devtool: false,
+ entry: './entry.js',
+ output: { path: output, filename: 'entry.cjs', library: { type: 'commonjs2' } },
+ plugins: [intlPolyfillContext()],
+ });
+ const stats = await new Promise((resolve, reject) => compiler.run((error, result) => (error ? reject(error) : resolve(result))));
+ assert.equal(stats.hasErrors(), false, stats.toString('errors-only'));
+
+ const canonicalRoot = await fs.realpath(root);
+ const modules = [...stats.compilation.modules].map((module) => module.resource);
+ for (const name of packages) {
+ const directory = path.join(canonicalRoot, 'node_modules', name, 'locale-data');
+ assert.ok(modules.includes(path.join(directory, 'en.js')));
+ assert.ok(modules.includes(path.join(directory, 'fil.js')), 'New extension languages remain available');
+ assert.ok(!modules.includes(path.join(directory, 'pt-BR.js')));
+ assert.ok(!modules.includes(path.join(directory, 'nested/en.js')));
+ }
+ assert.ok(modules.includes(path.join(canonicalRoot, 'node_modules/unrelated/locale-data/pt-BR.js')));
+
+ const built = require(path.join(output, 'entry.cjs'));
+ assert.deepEqual(
+ (await built.load('fil')).map((module) => module.default),
+ ['fil', 'fil', 'fil', 'fil']
+ );
+ assert.equal((await built.unrelated('pt-BR')).default, 'pt-BR');
+ } finally {
+ if (compiler) await new Promise((resolve, reject) => compiler.close((error) => (error ? reject(error) : resolve())));
+ await fs.rm(root, { recursive: true, force: true });
+ }
+});