├── renovate.json ├── .eslintrc.js ├── .eslintignore ├── commitlint.config.js ├── .gitignore ├── test ├── fixture │ ├── basic │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── default-timezone │ │ ├── pages │ │ │ └── index.vue │ │ └── nuxt.config.js │ ├── timezone │ │ ├── pages │ │ │ └── index.vue │ │ └── nuxt.config.js │ ├── plugin-disabled │ │ ├── pages │ │ │ └── index.vue │ │ └── nuxt.config.js │ ├── with-array │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ ├── with-object │ │ ├── nuxt.config.js │ │ └── pages │ │ │ └── index.vue │ └── plugins │ │ ├── nuxt.config.js │ │ └── pages │ │ └── index.vue ├── timezone.test.js ├── default-timezone.test.js ├── plugin-disabled.test.js ├── plugins.test.js ├── basic.test.js ├── with-array.test.js └── with-object.test.js ├── jest.config.js ├── husky.config.js ├── .editorconfig ├── lib ├── plugin.js └── module.js ├── types └── index.d.ts ├── LICENSE ├── .github └── workflows │ └── ci.yml ├── package.json ├── CHANGELOG.md └── README.md /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: [ 4 | '@nuxtjs' 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | lib/plugin.js 9 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | -------------------------------------------------------------------------------- /test/fixture/basic/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixture/default-timezone/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ $moment('2014-06-01T12:00:00Z').format('ha z') }} 4 | 5 | 6 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | collectCoverageFrom: ['lib/**/*.js', '!lib/plugin.js'], 4 | testEnvironment: 'node' 5 | } 6 | -------------------------------------------------------------------------------- /test/fixture/timezone/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ $moment('2014-06-01T12:00:00Z').tz('America/Los_Angeles').format('ha z') }} 4 | 5 | 6 | -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', 4 | 'pre-commit': 'yarn lint', 5 | 'pre-push': 'yarn lint' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixture/plugin-disabled/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | Works! 4 | 5 | 6 | 7 | 12 | -------------------------------------------------------------------------------- /test/fixture/plugin-disabled/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | moment: { 7 | plugin: false 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/with-array/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | [require('../../../'), ['es', 'de']] 5 | ], 6 | moment: { 7 | defaultLocale: 'de' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/default-timezone/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | moment: { 7 | defaultTimezone: 'America/Los_Angeles' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixture/with-object/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | moment: { 7 | locales: ['es', 'de'], 8 | defaultLocale: 'de' 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixture/timezone/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | moment: { 7 | timezone: { 8 | matchZones: 'America/Los_Angeles' 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /test/fixture/plugins/nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rootDir: __dirname, 3 | buildModules: [ 4 | { handler: require('../../../') } 5 | ], 6 | moment: { 7 | plugins: [ 8 | 'moment-strftime', 9 | 'moment-fquarter' 10 | ] 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /test/fixture/basic/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ $moment('19951225', 'YYYYMMDD').format('YYYY') }} 5 | 6 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /test/fixture/plugins/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ $moment('19951225', 'YYYYMMDD').fquarter() }} 5 | 6 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /test/fixture/with-array/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ $moment('19951225', 'YYYYMMDD').format('YYYY') }} 5 | 6 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /test/fixture/with-object/pages/index.vue: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ $moment('19951225', 'YYYYMMDD').format('YYYY') }} 5 | 6 | 7 | 8 | 17 | -------------------------------------------------------------------------------- /test/timezone.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('timezone', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'timezone')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('render', async () => { 15 | const html = await get('/') 16 | expect(html).toContain('5am PDT') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/default-timezone.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('default-timezone', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'default-timezone')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('render', async () => { 15 | const html = await get('/') 16 | expect(html).toContain('5am PDT') 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /lib/plugin.js: -------------------------------------------------------------------------------- 1 | import moment from 'moment' 2 | 3 | <%= options.locales.map(l => `import 'moment/locale/${l}'`).join('\n') %> 4 | 5 | <%= options.plugins.map(p => `import '${p}'`).join('\n') %> 6 | 7 | <% if(options.defaultLocale) { %>moment.locale('<%= options.defaultLocale %>')<% } %> 8 | 9 | <% if(options.defaultTimezone) { %>moment.tz.setDefault('<%= options.defaultTimezone %>')<% } %> 10 | 11 | export default (ctx, inject) => { 12 | ctx.$moment = moment 13 | inject('moment', moment) 14 | } 15 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | import moment from 'moment' 2 | import Vue from 'vue' 3 | 4 | declare module '@nuxt/vue-app' { 5 | interface Context { 6 | $moment: typeof moment 7 | } 8 | } 9 | 10 | // Nuxt 2.9+ 11 | declare module '@nuxt/types' { 12 | interface Context { 13 | $moment: typeof moment 14 | } 15 | } 16 | 17 | declare module 'vue/types/vue' { 18 | interface Vue { 19 | $moment: typeof moment 20 | } 21 | } 22 | 23 | declare module 'vuex/types/index' { 24 | interface Store { 25 | $moment: typeof moment 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /test/plugin-disabled.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('plugin disabled', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'plugin-disabled')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('moment should be undefined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$moment).toBeUndefined() 17 | }) 18 | 19 | test('render', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('Works!') 22 | }) 23 | }) 24 | -------------------------------------------------------------------------------- /test/plugins.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('plugins', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'plugins')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('render moment-strftime', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.document.querySelector('p').textContent).toBe('12/25/1995') 17 | }) 18 | 19 | test('render moment-fquarter', async () => { 20 | const html = await get('/') 21 | expect(html).toContain('Q3 1995/96') 22 | }) 23 | }) 24 | -------------------------------------------------------------------------------- /test/basic.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('basic', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'basic')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('moment should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$moment).toBeDefined() 17 | expect(window.$nuxt.$moment.locales()).toEqual(['en']) 18 | }) 19 | 20 | test('render month', async () => { 21 | const window = await nuxt.renderAndGetWindow(url('/')) 22 | expect(window.document.querySelector('p').textContent).toBe('December') 23 | }) 24 | 25 | test('render year', async () => { 26 | const html = await get('/') 27 | expect(html).toContain('1995') 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /test/with-array.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('with array', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'with-array')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('moment should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$moment).toBeDefined() 17 | expect(window.$nuxt.$moment.locales()).toEqual(['en', 'es', 'de']) 18 | }) 19 | 20 | test('render month', async () => { 21 | const window = await nuxt.renderAndGetWindow(url('/')) 22 | expect(window.document.querySelector('p').textContent).toBe('Dezember') 23 | }) 24 | 25 | test('render year', async () => { 26 | const html = await get('/') 27 | expect(html).toContain('1995') 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /test/with-object.test.js: -------------------------------------------------------------------------------- 1 | const { setup, loadConfig, get, url } = require('@nuxtjs/module-test-utils') 2 | 3 | describe('with object', () => { 4 | let nuxt 5 | 6 | beforeAll(async () => { 7 | ({ nuxt } = (await setup(loadConfig(__dirname, 'with-object')))) 8 | }, 60000) 9 | 10 | afterAll(async () => { 11 | await nuxt.close() 12 | }) 13 | 14 | test('moment should be defined', async () => { 15 | const window = await nuxt.renderAndGetWindow(url('/')) 16 | expect(window.$nuxt.$moment).toBeDefined() 17 | expect(window.$nuxt.$moment.locales()).toEqual(['en', 'es', 'de']) 18 | }) 19 | 20 | test('render month', async () => { 21 | const window = await nuxt.renderAndGetWindow(url('/')) 22 | expect(window.document.querySelector('p').textContent).toBe('Dezember') 23 | }) 24 | 25 | test('render year', async () => { 26 | const html = await get('/') 27 | expect(html).toContain('1995') 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Nuxt Community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | ci: 13 | runs-on: ${{ matrix.os }} 14 | 15 | strategy: 16 | matrix: 17 | # os: [ubuntu-latest, macos-latest, windows-latest] 18 | os: [ubuntu-latest] 19 | node: [10] 20 | 21 | steps: 22 | - uses: actions/setup-node@v2 23 | with: 24 | node-version: ${{ matrix.node }} 25 | 26 | - name: checkout 27 | uses: actions/checkout@master 28 | 29 | - name: cache node_modules 30 | uses: actions/cache@v2 31 | with: 32 | path: node_modules 33 | key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }} 34 | 35 | - name: Install dependencies 36 | if: steps.cache.outputs.cache-hit != 'true' 37 | run: yarn 38 | 39 | - name: Lint 40 | run: yarn lint 41 | 42 | - name: Test 43 | run: yarn test 44 | 45 | - name: Coverage 46 | uses: codecov/codecov-action@v1 47 | -------------------------------------------------------------------------------- /lib/module.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | const defaults = { 4 | locales: [], 5 | defaultLocale: null, 6 | defaultTimezone: null, 7 | plugin: true, 8 | plugins: [], 9 | timezone: false 10 | } 11 | 12 | function momentModule (moduleOptions) { 13 | if (Array.isArray(moduleOptions)) { 14 | moduleOptions = { locales: moduleOptions } 15 | } 16 | 17 | const options = { 18 | ...defaults, 19 | ...this.options.moment, 20 | ...moduleOptions 21 | } 22 | 23 | if ((options.timezone || options.defaultTimezone) && !options.plugins.includes('moment-timezone')) { 24 | options.plugins.push('moment-timezone') 25 | } 26 | 27 | this.extendBuild((config) => { 28 | const MomentLocalesPlugin = require('moment-locales-webpack-plugin') 29 | config.plugins.push(new MomentLocalesPlugin({ 30 | localesToKeep: options.locales 31 | })) 32 | 33 | if (Object.keys(options.timezone).length) { 34 | const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin') 35 | config.plugins.push(new MomentTimezoneDataPlugin(options.timezone)) 36 | } 37 | }) 38 | 39 | if (!options.plugin) { 40 | return 41 | } 42 | 43 | this.addPlugin({ 44 | src: resolve(__dirname, 'plugin.js'), 45 | fileName: 'moment.js', 46 | options 47 | }) 48 | } 49 | 50 | module.exports = momentModule 51 | module.exports.meta = require('../package.json') 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@nuxtjs/moment", 3 | "version": "1.6.1", 4 | "description": "Efficient Moment.js integration for Nuxt.js", 5 | "repository": "nuxt-community/moment-module", 6 | "license": "MIT", 7 | "contributors": [ 8 | "Pooya Parsa ", 9 | "Alexander Lichter ", 10 | "Ricardo Gobbo de Souza " 11 | ], 12 | "main": "lib/module.js", 13 | "types": "types/index.d.ts", 14 | "files": [ 15 | "lib", 16 | "types/*.d.ts" 17 | ], 18 | "scripts": { 19 | "lint": "eslint --ext .js,.vue .", 20 | "release": "yarn test && standard-version && git push --follow-tags && npm publish", 21 | "test": "yarn lint && jest" 22 | }, 23 | "dependencies": { 24 | "moment": "^2.25.3", 25 | "moment-locales-webpack-plugin": "^1.2.0", 26 | "moment-timezone": "^0.5.31", 27 | "moment-timezone-data-webpack-plugin": "^1.3.0" 28 | }, 29 | "devDependencies": { 30 | "@commitlint/cli": "latest", 31 | "@commitlint/config-conventional": "latest", 32 | "@nuxtjs/eslint-config": "latest", 33 | "@nuxtjs/module-test-utils": "latest", 34 | "eslint": "latest", 35 | "husky": "latest", 36 | "jest": "latest", 37 | "moment-fquarter": "latest", 38 | "moment-strftime": "latest", 39 | "nuxt-edge": "latest", 40 | "standard-version": "latest" 41 | }, 42 | "publishConfig": { 43 | "access": "public" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [1.6.1](https://github.com/nuxt-community/moment-module/compare/v1.6.0...v1.6.1) (2020-05-09) 6 | 7 | ## [1.6.0](https://github.com/nuxt-community/moment-module/compare/v1.5.0...v1.6.0) (2020-02-10) 8 | 9 | 10 | ### Features 11 | 12 | * set default time zone ([#54](https://github.com/nuxt-community/moment-module/issues/54)) ([a818d40](https://github.com/nuxt-community/moment-module/commit/a818d40e005f349de5d47ba6d61546918f1ac7c4)) 13 | 14 | ## [1.5.0](https://github.com/nuxt-community/moment-module/compare/v1.4.0...v1.5.0) (2020-01-03) 15 | 16 | 17 | ### Features 18 | 19 | * improve types ([#47](https://github.com/nuxt-community/moment-module/issues/47)) ([6a26feb](https://github.com/nuxt-community/moment-module/commit/6a26feb)) 20 | * timezone support ([#48](https://github.com/nuxt-community/moment-module/issues/48)) ([978ee47](https://github.com/nuxt-community/moment-module/commit/978ee47)) 21 | 22 | ## [1.4.0](https://github.com/nuxt-community/moment-module/compare/v1.3.0...v1.4.0) (2019-10-25) 23 | 24 | 25 | ### Features 26 | 27 | * add option to import plugins ([#45](https://github.com/nuxt-community/moment-module/issues/45)) ([cbebaa9](https://github.com/nuxt-community/moment-module/commit/cbebaa9)) 28 | 29 | ## [1.3.0](https://github.com/nuxt-community/moment-module/compare/v1.2.0...v1.3.0) (2019-08-31) 30 | 31 | 32 | ### Bug Fixes 33 | 34 | * **module:** lazy-require `moment-locales-webpack-plugin` ([7c2ed2f](https://github.com/nuxt-community/moment-module/commit/7c2ed2f)) 35 | 36 | 37 | ### Features 38 | 39 | * provide nuxt 2.9 compatible types ([#36](https://github.com/nuxt-community/moment-module/issues/36)) ([c6d76ad](https://github.com/nuxt-community/moment-module/commit/c6d76ad)) 40 | 41 | 42 | # [1.2.0](https://github.com/nuxt-community/moment-module/compare/v1.1.0...v1.2.0) (2019-02-08) 43 | 44 | 45 | ### Features 46 | 47 | * **types:** add typescript definitions ([#17](https://github.com/nuxt-community/moment-module/issues/17)) ([231e8a7](https://github.com/nuxt-community/moment-module/commit/231e8a7)) 48 | 49 | 50 | 51 | 52 | # [1.1.0](https://github.com/nuxt-community/moment-module/compare/v1.0.0...v1.1.0) (2018-12-10) 53 | 54 | 55 | ### Bug Fixes 56 | 57 | * inject $moment to ctx too ([20fd64d](https://github.com/nuxt-community/moment-module/commit/20fd64d)) 58 | 59 | 60 | ### Features 61 | 62 | * add default locale ([d6170cd](https://github.com/nuxt-community/moment-module/commit/d6170cd)) 63 | * improvements ([#8](https://github.com/nuxt-community/moment-module/issues/8)) ([98c790d](https://github.com/nuxt-community/moment-module/commit/98c790d)), closes [#6](https://github.com/nuxt-community/moment-module/issues/6) [#5](https://github.com/nuxt-community/moment-module/issues/5) [#4](https://github.com/nuxt-community/moment-module/issues/4) [#3](https://github.com/nuxt-community/moment-module/issues/3) 64 | * inject moment to ctx ([3414a24](https://github.com/nuxt-community/moment-module/commit/3414a24)) 65 | 66 | 67 | 68 | 69 | # 1.0.0 (2018-04-04) 70 | 71 | 72 | ### Features 73 | 74 | * add plugin ([8932024](https://github.com/nuxt-community/moment-module/commit/8932024)) 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @nuxtjs/moment 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![License][license-src]][license-href] 8 | 9 | > Efficient Moment.js integration for Nuxt.js 10 | 11 | [📖 **Release Notes**](./CHANGELOG.md) 12 | 13 | ## Features 14 | 15 | - Remove unused Moment.js locales using [moment-locales-webpack-plugin](https://github.com/iamakulov/moment-locales-webpack-plugin) for much less bundle size. 16 | - Inject `$moment` to the context everywhere. 17 | 18 | ## Setup 19 | 20 | 1. Add `@nuxtjs/moment` dependency to your project 21 | 22 | ```bash 23 | yarn add --dev @nuxtjs/moment # or npm install --save-dev @nuxtjs/moment 24 | ``` 25 | 26 | 2. Add `@nuxtjs/moment` to the `buildModules` section of `nuxt.config.js` 27 | 28 | ```js 29 | export default { 30 | buildModules: [ 31 | // Simple usage 32 | '@nuxtjs/moment', 33 | 34 | // With options 35 | ['@nuxtjs/moment', { /* module options */ }] 36 | ] 37 | } 38 | ``` 39 | 40 | :warning: If you are using Nuxt **< v2.9** you have to install the module as a `dependency` (No `--dev` or `--save-dev` flags) and use `modules` section in `nuxt.config.js` instead of `buildModules`. 41 | 42 | ### Using top level options 43 | 44 | ```js 45 | export default { 46 | buildModules: [ 47 | '@nuxtjs/moment' 48 | ], 49 | moment: { 50 | /* module options */ 51 | } 52 | } 53 | ``` 54 | 55 | ### Typescript setup 56 | 57 | Add the types to your `"types"` array in `tsconfig.json` after the `@nuxt/types` entry. 58 | 59 | :warning: Use `@nuxt/vue-app` instead of `@nuxt/types` for nuxt < 2.9. 60 | 61 | #### tsconfig.json 62 | 63 | ```json 64 | { 65 | "compilerOptions": { 66 | "types": [ 67 | "@nuxt/types", 68 | "@nuxtjs/moment" 69 | ] 70 | } 71 | } 72 | ``` 73 | 74 | > **Why?** 75 | > 76 | > For typescript to be aware of the additions to the `nuxt Context`, the `vue instance` and the `vuex store`, the types need to be merged via [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) by adding `@nuxtjs/moment` to your types. 77 | 78 | ## Configuration 79 | 80 | To strip all locales except `en`: 81 | 82 | ```js 83 | export default { 84 | buildModules: [ 85 | '@nuxtjs/moment' 86 | ] 87 | } 88 | ``` 89 | 90 | To strip all locales except `en`, `fr` and `fa`: 91 | 92 | ```js 93 | export default { 94 | buildModules: [ 95 | '@nuxtjs/moment' 96 | ], 97 | moment: { 98 | locales: ['fa'] 99 | } 100 | } 101 | ``` 102 | 103 | **Note:** `en` is built into Moment and can’t be removed! 104 | 105 | ### Set default locale 106 | 107 | You can set a default locale via the `defaultLocale` option. It must be included 108 | int the locales you keep (or `'en'`) and will only work when using the plugin option. 109 | 110 | ```js 111 | export default { 112 | buildModules: [ 113 | '@nuxtjs/moment' 114 | ], 115 | moment: { 116 | defaultLocale: 'de', 117 | locales: ['de'] 118 | } 119 | } 120 | ``` 121 | 122 | ### Plugins 123 | 124 | You can import plugins to moment. See a list of [plugins](https://momentjs.com/docs/#/plugins/) 125 | 126 | ```js 127 | export default { 128 | buildModules: [ 129 | '@nuxtjs/moment' 130 | ], 131 | moment: { 132 | plugins: [ 133 | 'moment-strftime', 134 | 'moment-fquarter' 135 | ] 136 | } 137 | } 138 | ``` 139 | 140 | **Note:** Don't forget to install each plugin. 141 | 142 | ### Timezone 143 | 144 | You can enable [moment-timezone](https://momentjs.com/timezone/) via the `timezone` option. 145 | 146 | ```js 147 | export default { 148 | buildModules: [ 149 | '@nuxtjs/moment' 150 | ], 151 | moment: { 152 | timezone: true 153 | } 154 | } 155 | ``` 156 | 157 | You can filter time zone data and thus produce significant savings in file size. 158 | See all options in [moment-timezone-data-webpack-plugin](https://github.com/gilmoreorless/moment-timezone-data-webpack-plugin). 159 | 160 | ```js 161 | export default { 162 | buildModules: [ 163 | '@nuxtjs/moment' 164 | ], 165 | moment: { 166 | timezone: { 167 | matchZones: /Europe\/(Belfast|London|Paris|Athens)/, 168 | startYear: 2000, 169 | endYear: 2030 170 | } 171 | } 172 | } 173 | ``` 174 | 175 | ### Set default time zone 176 | 177 | You can set a [default time zone](https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/) via the `defaultTimezone` option. 178 | 179 | ```js 180 | export default { 181 | buildModules: [ 182 | '@nuxtjs/moment' 183 | ], 184 | moment: { 185 | defaultTimezone: 'America/Los_Angeles' 186 | } 187 | } 188 | ``` 189 | 190 | ### Disable plugin 191 | 192 | This module also registers a plugin to include all needed locales as well as injecting moment as `$moment` to Vue context. You can disable this behaviour using `plugin: false`. 193 | 194 | ```js 195 | export default { 196 | buildModules: [ 197 | '@nuxtjs/moment' 198 | ], 199 | moment: { 200 | plugin: false 201 | } 202 | } 203 | ``` 204 | 205 | ### Using inside templates 206 | 207 | You can easily use `$moment` service from templates. 208 | 209 | ```html 210 | 211 | {{ $moment(...) }} 212 | ``` 213 | 214 | ## License 215 | 216 | [MIT License](./LICENSE) 217 | 218 | Copyright (c) Nuxt Community 219 | 220 | 221 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/moment/latest.svg 222 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/moment 223 | 224 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/moment.svg 225 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/moment 226 | 227 | [github-actions-ci-src]: https://github.com/nuxt-community/moment-module/workflows/ci/badge.svg 228 | [github-actions-ci-href]: https://github.com/nuxt-community/moment-module/actions?query=workflow%3Aci 229 | 230 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/moment-module.svg 231 | [codecov-href]: https://codecov.io/gh/nuxt-community/moment-module 232 | 233 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/moment.svg 234 | [license-href]: https://npmjs.com/package/@nuxtjs/moment 235 | --------------------------------------------------------------------------------