Skip to content

Commit

Permalink
feat: Include two versions of the timeZone plugin with limited data
Browse files Browse the repository at this point in the history
* 1900-2050
* 2012-2022
  • Loading branch information
prantlf committed Nov 17, 2018
1 parent 241488d commit 022a45a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:m
```js
// Load dayjs, plugins and language packs.
import dayjs from 'dayjs-ext'
// import "timeZone-1900-2050" or "timeZone-2012-2022" to save your package size
import timeZonePlugin from 'dayjs-ext/plugin/timeZone'
import customParseFormat from 'dayjs-ext/plugin/customParseFormat'
import localizableFormat from 'dayjs-ext/plugin/localizableFormat'
Expand Down Expand Up @@ -99,7 +100,10 @@ If used in the browser, the following scripts would be needed:
<script arc="https://unpkg.com/timezone-support/dist/index.umd.js"></script>
<script arc="https://unpkg.com/fast-plural-rules/dist/index.umd.js"></script>
<script arc="https://unpkg.com/dayjs-ext/dayjs.min.js"></script>
<-- include "timeZone-1900-2050" or "timeZone-2012-2022" to save your package size -->
<script arc="https://unpkg.com/dayjs-ext/plugin/timeZone.js"></script>
<script arc="https://unpkg.com/dayjs-ext/plugin/customParseFormat.js"></script>
<script arc="https://unpkg.com/dayjs-ext/plugin/localizableFormat.js"></script>
<script arc="https://unpkg.com/dayjs-ext/plugin/relativeTime.js"></script>
```

Expand Down
18 changes: 18 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const configFactory = require('./rollup.config')
const { promisify } = util

const promisifyReadDir = promisify(fs.readdir)
const promisifyReadFile = promisify(fs.readFile)
const promisifyWriteFile = promisify(fs.writeFile)

const formatName = n => n.replace(/\.js/, '').replace('-', '_')

Expand All @@ -15,6 +17,20 @@ async function build(option) {
await bundle.write(option.output)
}

async function addLimitedTimeZonePluginVersions() {
const originalFile = path.join(__dirname, '../plugin/timeZone.js')
const originalContent = await promisifyReadFile(originalFile, { encoding: 'utf-8' })
const limitedVersions = ['1900-2050', '2012-2022']
for (let i = 0; i < limitedVersions.length; ++i) { // eslint-disable-line no-plusplus
const limitedVersion = limitedVersions[i]
const limitedFile = path.join(__dirname, `../plugin/timeZone-${limitedVersion}.js`)
const limitedContent = originalContent.replace('require("timezone-support")',
`require("timezone-support/dist/index-${limitedVersion}")`)
// eslint-disable-next-line no-await-in-loop
await promisifyWriteFile(limitedFile, limitedContent)
}
}

(async () => {
try {
const locales = await promisifyReadDir(path.join(__dirname, '../src/locale'))
Expand All @@ -39,6 +55,8 @@ async function build(option) {
input: './src/index.js',
fileName: './dayjs.min.js'
}))

addLimitedTimeZonePluginVersions()
} catch (e) {
console.error(e) // eslint-disable-line no-console
}
Expand Down
3 changes: 3 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ If the plugin [LocalizableFormat](#localizableformat) is installed, format token
- TimeZone extends `dayjs()` and `dayjs().format` APIs to support the most important usage scenatrios - parsing from a specific time zone and formatting in other time zone.

```javascript
// import "timeZone-1900-2050" or "timeZone-2012-2022" to save your package size
import timeZone from 'dayjs-ext/plugin/timeZone'

dayjs.extend(timeZone)
Expand Down Expand Up @@ -282,7 +283,9 @@ This plugin has a dependency on the [`timezone-support`](https://www.npmjs.com/p

```html
<script arc="https://unpkg.com/dayjs-ext/dayjs.min.js"></script>
<-- include "timeZone-1900-2050" or "timeZone-2012-2022" to save your package size -->
<script arc="https://unpkg.com/dayjs-ext/plugin/timeZone.js"></script>
<-- include "index-1900-2050.umd.js" or "index-2012-2022.umd.js" to save your package size -->
<script arc="https://unpkg.com/timezone-support/dist/index.umd.js"></script>
```

Expand Down

0 comments on commit 022a45a

Please sign in to comment.