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

Fix dayjs/esm type definitions #1581

Closed
wants to merge 3 commits into from
Closed
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
24 changes: 2 additions & 22 deletions build/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const { ncp } = require('ncp')

const { promisify } = util

const typeFileExt = '.d.ts'
const localeDir = path.join(process.env.PWD, 'esm/locale')
const pluginDir = path.join(process.env.PWD, 'esm/plugin')
const localeTypePath = path.join(process.env.PWD, 'esm/locale', `index${typeFileExt}`);
const localeDir = path.join(__dirname, '../esm/locale');

(async () => {
try {
Expand All @@ -20,24 +17,7 @@ const localeTypePath = path.join(process.env.PWD, 'esm/locale', `index${typeFile
await promisify(fs.writeFile)(filePath, result, 'utf8')
})

await promisify(ncp)('./types/', './esm')

const readLocaleFile = await promisify(fs.readFile)(localeTypePath, 'utf8')
const localResult = readLocaleFile.replace("'dayjs", "'dayjs/esm")
await promisify(fs.writeFile)(localeTypePath, localResult, 'utf8')

const readPluginDir = await promisify(fs.readdir)(pluginDir)
readPluginDir.forEach(async (p) => {
if (p.includes(typeFileExt)) {
const pluginName = p.replace(typeFileExt, '')
const filePath = path.join(pluginDir, p)
const targetPath = path.join(pluginDir, pluginName, `index${typeFileExt}`)
const readFile = await promisify(fs.readFile)(filePath, 'utf8')
const result = readFile.replace(/'dayjs'/g, "'dayjs/esm'")
await promisify(fs.writeFile)(targetPath, result, 'utf8')
await promisify(fs.unlink)(filePath)
}
})
await promisify(ncp)('./types/esm/', './esm')
} catch (e) {
console.error(e) // eslint-disable-line no-console
}
Expand Down
4 changes: 3 additions & 1 deletion build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const promisifyWriteFile = promisify(fs.writeFile)
const localeNameRegex = /\/\/ (.*) \[/
const formatName = n => n.replace(/\.js/, '').replace('-', '_')

const filterEsmTypes = source => !(fs.lstatSync(source).isDirectory() && source.endsWith(`${path.sep}esm`))

const localePath = path.join(__dirname, '../src/locale')

async function build(option) {
Expand Down Expand Up @@ -62,7 +64,7 @@ async function listLocaleJson(localeArr) {
fileName: './dayjs.min.js'
}))

await promisify(ncp)('./types/', './')
await promisify(ncp)('./types/', './', { filter: filterEsmTypes })

// list locales
await listLocaleJson(locales)
Expand Down
25 changes: 25 additions & 0 deletions types/esm/constant.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export var SECONDS_A_MINUTE: number
export var SECONDS_A_HOUR: number
export var SECONDS_A_DAY: number
export var SECONDS_A_WEEK: number
export var MILLISECONDS_A_SECOND: number
export var MILLISECONDS_A_MINUTE: number
export var MILLISECONDS_A_HOUR: number
export var MILLISECONDS_A_DAY: number
export var MILLISECONDS_A_WEEK: number

export var MS: string
export var S: string
export var MIN: string
export var H: string
export var D: string
export var W: string
export var M: string
export var Q: string
export var Y: string
export var DATE: string
export var FORMAT_DEFAULT: string
export var INVALID_DATE_STRING: string

export var REGEX_PARSE: RegExp
export var REGEX_FORMAT: RegExp
Loading