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

Bump os-locale (add support for ESM dependencies) #5415

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('jest').Config} */
module.exports = {
coveragePathIgnorePatterns: [
'<rootDir>/node_modules/',
Expand Down Expand Up @@ -27,7 +28,7 @@ module.exports = {
'^.+\\.js$': 'babel-jest',
'^.+\\.txt$': '<rootDir>/tests/jest-raw-loader.js',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
transformIgnorePatterns: ['<rootDir>/node_modules/(?!os-locale)'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Standard config for ESM packages

testEnvironment: 'node',
verbose: false,
};
120 changes: 17 additions & 103 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"image-size": "1.1.1",
"jed": "1.1.1",
"json-merge-patch": "1.0.2",
"os-locale": "5.0.0",
"os-locale": "6.0.2",
"pino": "8.20.0",
"semver": "7.6.3",
"source-map-support": "0.5.21",
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import upath from 'upath';
import Jed from 'jed';
import semver from 'semver';
import { oneLine } from 'common-tags';
import osLocale from 'os-locale';
import { osLocaleSync } from 'os-locale';

import log from 'logger';
import { PACKAGE_TYPES, LOCAL_PROTOCOLS } from 'const';
Expand Down Expand Up @@ -136,7 +136,7 @@ export function getVariable(context, name) {
}

export function getLocale() {
return osLocale.sync();
return osLocaleSync();
}

export function getI18Data(locale) {
Expand Down
8 changes: 7 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ const webpack = require('webpack');
// eslint-disable-next-line import/no-extraneous-dependencies
const nodeExternals = require('webpack-node-externals');

// ESM packages must be bundled because webpack will otherwise turn `require()` into `import()`
// and make the import silently asynchronous. In most cases this breaks the import.
const dependenciesToBundle = [
'os-locale', // Used exclusively in sync functions
];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As expected, there's "no way" to require a ESM package synchronously. This is not a webpack/config issue. The only way for this to work is to turn addons-linter into an type=module package.

So this is the alternative: do not keep os-locale as an external dependency, so that node does not try to require it at runtime.


module.exports = {
// Set the webpack4 mode 'none' for compatibility with the behavior of the
// webpack3 bundling step.
mode: 'none',
entry: {
'addons-linter': './src/main.js',
},
target: 'node',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Webpack defaults to browserlist, if set. We have it already and it's set to node 16. This enables ESM support

output: {
filename: '[name].js',
libraryTarget: 'commonjs2',
Expand All @@ -38,6 +43,7 @@ module.exports = {
externals: [
nodeExternals({
modulesFromFile: true,
allowlist: dependenciesToBundle,
}),
],
plugins: [
Expand Down