diff --git a/src/plugin/devHelper/index.js b/src/plugin/devHelper/index.js index 5bace111..b4fb3c6f 100644 --- a/src/plugin/devHelper/index.js +++ b/src/plugin/devHelper/index.js @@ -16,6 +16,15 @@ export default (o, c, d) => { } return oldParse.bind(this)(cfg) } + const oldLocale = d.locale + d.locale = function (preset, object, isLocal) { + if (typeof object === 'undefined' && typeof preset === 'string') { + if (!d.Ls[preset]) { + console.warn(`Guessing you may want to use locale ${preset}, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs`) + } + } + return oldLocale(preset, object, isLocal) + } } } diff --git a/test/plugin/devHelper.test.js b/test/plugin/devHelper.test.js index c0c69357..c69190a1 100644 --- a/test/plugin/devHelper.test.js +++ b/test/plugin/devHelper.test.js @@ -32,3 +32,9 @@ it('Warning Enable customParseFormat plugin while passing the second format para dayjs('2020', 'YYYY') expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format') }) + +it('Warning: Setting locale before loading locale', () => { + const consoleSpy = jest.spyOn(console, 'warn') + dayjs.locale('zh-cn') + expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs') +})