Skip to content

Commit 8a1673b

Browse files
feat: remove unsed locales (#57)
* feat: remove unsed locales * fix: import from 'esm' * fix: imports * fix: import locales * style: fix escape * chore: update lock file * fix: import locales
1 parent 885ea2c commit 8a1673b

File tree

3 files changed

+3201
-3226
lines changed

3 files changed

+3201
-3226
lines changed

lib/module.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,10 @@ module.exports = function (moduleOptions) {
1616
...moduleOptions
1717
}
1818

19-
if (options.defaultLocale) {
20-
options.defaultLocale = options.defaultLocale.replace(/[_-]/, '')
21-
}
22-
23-
if (options.fallbackLocale) {
24-
options.fallbackLocale = options.fallbackLocale.replace(/[_-]/, '')
25-
}
26-
2719
if (!Array.isArray(options.locales)) {
2820
options.locales = [options.locales]
2921
}
3022

31-
options.locales = options.locales.map(l => l.replace(/[_-]/, ''))
32-
3323
if (options.defaultLocale && !options.locales.includes(options.defaultLocale)) {
3424
options.locales.push(options.defaultLocale)
3525
}
@@ -43,6 +33,15 @@ module.exports = function (moduleOptions) {
4333
fileName: 'date-fns.js',
4434
options
4535
})
36+
37+
this.extendBuild((config) => {
38+
const { ContextReplacementPlugin } = require('webpack')
39+
40+
config.plugins.push(new ContextReplacementPlugin(
41+
/date-fns[/\\]/,
42+
new RegExp(`[/\\\\](${options.locales.join('|')})[/\\\\]index.js$`)
43+
))
44+
})
4645
}
4746

4847
module.exports.meta = require('../package.json')

lib/plugin.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
<% if (options.methods) { %>
2-
import { <%= options.methods.join(', ') %> } from 'date-fns'
3-
<% } else { %>
4-
import * as dateFns from 'date-fns'
5-
<% } %>
6-
7-
<% if (options.locales) { %>
8-
import { <%= options.locales.join(', ') %> } from 'date-fns/locale'
9-
<% } %>
10-
11-
export default (ctx, inject) => {
12-
<% if (options.methods) { %>
1+
<% if (options.methods && options.methods.length) { %>import { <%= options.methods.join(', ') %> } from 'date-fns'
2+
<% } else { %>import * as dateFns from 'date-fns'<% } %>
3+
4+
const locales = []
5+
6+
const importLocale = async (locale) => {
7+
try {
8+
locales[locale] = await import(`date-fns/locale/${locale}/index.js`)
9+
} catch (e) {
10+
//
11+
}
12+
}
13+
14+
export default async (ctx, inject) => {
15+
<% if (options.methods && options.methods.length) { %>
1316
const dateFns = { <%= options.methods.join(', ') %> }
1417
<% } %>
1518
const dateObj = {...dateFns}
1619

20+
<% if (options.locales && options.locales.length) { %>
21+
<%= options.locales.map(l => `await importLocale('${l}')`).join('\n') %>
22+
<% } %>
23+
1724
function parsedDate(date) {
1825
if (dateFns.parseISO) {
1926
return typeof date !== 'string' ? date : dateFns.parseISO(date)
@@ -77,10 +84,6 @@ export default (ctx, inject) => {
7784
}
7885

7986
function mergeOptions(options) {
80-
const locales = []
81-
82-
<%= options.locales.map(l => `locales['${l}'] = ${l}` ).join('\n') %>
83-
8487
<% if (options.defaultLocale) { %>
8588
options = { locale: '<%= options.defaultLocale %>', ...options }
8689
<% } %>
@@ -90,13 +93,11 @@ function mergeOptions(options) {
9093
<% } %>
9194

9295
if (options && typeof options.locale === 'string') {
93-
const locale = options.locale.replace(/[_-]/, '')
94-
const fallbackLocale = options.fallbackLocale ? options.fallbackLocale.replace(/[_-]/, '') : null
95-
if (locales[locale]) {
96-
options.locale = locales[locale]
97-
} else if (fallbackLocale && locales[fallbackLocale]) {
96+
if (locales[options.locale]) {
97+
options.locale = locales[options.locale]
98+
} else if (options.fallbackLocale && locales[options.fallbackLocale]) {
9899
console.warn(`[date-fns] locale '${options.locale}' not found, using fallback locale '${options.fallbackLocale}'`)
99-
options.locale = locales[fallbackLocale]
100+
options.locale = locales[options.fallbackLocale]
100101
} else {
101102
console.warn(`[date-fns] locale '${options.locale}' not found.`)
102103
}

0 commit comments

Comments
 (0)