chore: use dayjs

pull/4499/head
tangjinzhou 2021-08-09 16:59:32 +08:00
parent c0ae4a6b3a
commit f4140b9c63
6 changed files with 12 additions and 102 deletions

View File

@ -80,7 +80,6 @@ function getWebpackConfig(modules) {
}, },
module: { module: {
noParse: [/moment.js/],
rules: [ rules: [
{ {
test: /\.vue$/, test: /\.vue$/,

View File

@ -1,4 +0,0 @@
// https://github.com/moment/moment/issues/3650
export default function interopDefault(m) {
return m.default || m;
}

View File

@ -1,75 +0,0 @@
import interopDefault from './interopDefault';
import moment from 'moment';
import warning from './warning';
import isNil from 'lodash-es/isNil';
export const TimeType = {
validator(value) {
return typeof value === 'string' || isNil(value) || moment.isMoment(value);
},
};
export const TimesType = {
validator(value) {
if (Array.isArray(value)) {
return (
value.length === 0 ||
value.findIndex(val => typeof val !== 'string') === -1 ||
value.findIndex(val => !isNil(val) && !moment.isMoment(val)) === -1
);
}
return false;
},
};
export const TimeOrTimesType = {
validator(value) {
if (Array.isArray(value)) {
return (
value.length === 0 ||
value.findIndex(val => typeof val !== 'string') === -1 ||
value.findIndex(val => !isNil(val) && !moment.isMoment(val)) === -1
);
} else {
return typeof value === 'string' || isNil(value) || moment.isMoment(value);
}
},
};
export function checkValidate(componentName, value, propName, valueFormat) {
const values = Array.isArray(value) ? value : [value];
values.forEach(val => {
if (!val) return;
valueFormat &&
warning(
interopDefault(moment)(val, valueFormat).isValid(),
componentName,
`When set \`valueFormat\`, \`${propName}\` should provides invalidate string time. `,
);
!valueFormat &&
warning(
interopDefault(moment).isMoment(val) && val.isValid(),
componentName,
`\`${propName}\` provides invalidate moment time. If you want to set empty value, use \`null\` instead.`,
);
});
}
export const stringToMoment = (value, valueFormat) => {
if (Array.isArray(value)) {
return value.map(val =>
typeof val === 'string' && val ? interopDefault(moment)(val, valueFormat) : val || null,
);
} else {
return typeof value === 'string' && value
? interopDefault(moment)(value, valueFormat)
: value || null;
}
};
export const momentToString = (value, valueFormat) => {
if (Array.isArray(value)) {
return value.map(val => (interopDefault(moment).isMoment(val) ? val.format(valueFormat) : val));
} else {
return interopDefault(moment).isMoment(value) ? value.format(valueFormat) : value;
}
};

View File

@ -1,7 +1,7 @@
import glob from 'glob'; import glob from 'glob';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import MockDate from 'mockdate'; import MockDate from 'mockdate';
import moment from 'moment'; import dayjs from 'dayjs';
import antd from 'ant-design-vue'; import antd from 'ant-design-vue';
import { sleep } from '../utils'; import { sleep } from '../utils';
@ -15,7 +15,7 @@ export default function demoTest(component, options = {}) {
testMethod = test.skip; testMethod = test.skip;
} }
testMethod(`renders ${file} correctly`, async () => { testMethod(`renders ${file} correctly`, async () => {
MockDate.set(moment('2016-11-22')); MockDate.set(dayjs('2016-11-22'));
const demo = require(`../.${file}`).default || require(`../.${file}`); const demo = require(`../.${file}`).default || require(`../.${file}`);
document.body.innerHTML = ''; document.body.innerHTML = '';
const wrapper = mount(demo, { global: { plugins: [antd] }, attachTo: document.body }); const wrapper = mount(demo, { global: { plugins: [antd] }, attachTo: document.body });

View File

@ -1,9 +1,9 @@
import moment from 'moment'; import dayjs from 'dayjs';
import MockDate from 'mockdate'; import MockDate from 'mockdate';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
export function setMockDate(dateString = '2017-09-18T03:30:07.795') { export function setMockDate(dateString = '2017-09-18T03:30:07.795') {
MockDate.set(moment(dateString)); MockDate.set(dayjs(dateString));
} }
export function resetMockDate() { export function resetMockDate() {

View File

@ -2,14 +2,6 @@
const getWebpackConfig = require('./antd-tools/getWebpackConfig'); const getWebpackConfig = require('./antd-tools/getWebpackConfig');
const IgnoreEmitPlugin = require('ignore-emit-webpack-plugin'); const IgnoreEmitPlugin = require('ignore-emit-webpack-plugin');
const darkVars = require('./scripts/dark-vars'); const darkVars = require('./scripts/dark-vars');
const { webpack } = getWebpackConfig;
// noParse still leave `require('./locale' + name)` in dist files
// ignore is better
// http://stackoverflow.com/q/25384360
function ignoreMomentLocale(webpackConfig) {
delete webpackConfig.module.noParse;
webpackConfig.plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
}
function addLocales(webpackConfig) { function addLocales(webpackConfig) {
let packageName = 'antd-with-locales'; let packageName = 'antd-with-locales';
@ -20,20 +12,19 @@ function addLocales(webpackConfig) {
webpackConfig.output.filename = '[name].js'; webpackConfig.output.filename = '[name].js';
} }
function externalMoment(config) { function externalDayjs(config) {
config.externals.moment = { config.externals.dayjs = {
root: 'moment', root: 'dayjs',
commonjs2: 'moment', commonjs2: 'dayjs',
commonjs: 'moment', commonjs: 'dayjs',
amd: 'moment', amd: 'dayjs',
}; };
} }
const webpackConfig = getWebpackConfig(false); const webpackConfig = getWebpackConfig(false);
if (process.env.RUN_ENV === 'PRODUCTION') { if (process.env.RUN_ENV === 'PRODUCTION') {
webpackConfig.forEach(config => { webpackConfig.forEach(config => {
ignoreMomentLocale(config); externalDayjs(config);
externalMoment(config);
addLocales(config); addLocales(config);
}); });
} }
@ -41,8 +32,7 @@ if (process.env.RUN_ENV === 'PRODUCTION') {
const webpackDarkConfig = getWebpackConfig(false); const webpackDarkConfig = getWebpackConfig(false);
webpackDarkConfig.forEach(config => { webpackDarkConfig.forEach(config => {
ignoreMomentLocale(config); externalDayjs(config);
externalMoment(config);
// rename default entry to ${theme} entry // rename default entry to ${theme} entry
Object.keys(config.entry).forEach(entryName => { Object.keys(config.entry).forEach(entryName => {