Skip to content

Commit

Permalink
feat: support rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Apr 13, 2022
1 parent 66fe900 commit c776a1f
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 79 deletions.
7 changes: 7 additions & 0 deletions examples/rollup/src/hello.js
@@ -0,0 +1,7 @@
import moment from 'moment'

export default function hello() {
moment.locale('zh-cn')

moment('2019-01-01')
}
4 changes: 3 additions & 1 deletion examples/rollup/src/index.js
@@ -1,3 +1,5 @@
import moment from 'moment'
import hello from './hello'

console.log('🚀 ~ file: index.js ~ line 2 ~ moment', moment())
console.log('🚀 moment', moment())
console.log('🚀 hello', hello())
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -43,6 +43,10 @@
"require": "./dist/types.js",
"import": "./dist/types.mjs"
},
"./plugins/*": {
"require": "./dist/plugins/*.js",
"import": "./dist/plugins/*.mjs"
},
"./*": "./*"
},
"main": "dist/index.js",
Expand All @@ -55,7 +59,8 @@
"scripts": {
"build": "tsup",
"playground": "pnpm run --filter './playground' build",
"build:examples": "pnpm run --filter './examples' build ",
"build:examples": "pnpm run --filter ./examples build ",
"dev:examples": "pnpm run --filter ./examples dev ",
"dev": "tsup --watch src",
"build:fix": "esno scripts/postbuild.ts",
"lint": "eslint \"{src,test}/**/*.ts\"",
Expand All @@ -81,6 +86,7 @@
"esno": "^0.14.1",
"fast-glob": "^3.2.11",
"jest": "^27.5.1",
"magic-string": "^0.26.1",
"nodemon": "^2.0.15",
"rimraf": "^3.0.2",
"rollup": "^2.70.1",
Expand Down
7 changes: 6 additions & 1 deletion playground/App.tsx
@@ -1,10 +1,11 @@
/* eslint-disable no-console */
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { Calendar, ConfigProvider, DatePicker, Select } from 'antd'
import 'antd/dist/antd.variable.css'
import enUS from 'antd/es/locale/en_US'
import zhCN from 'antd/es/locale/zh_CN'
import 'moment/locale/zh-cn'
import moment from 'moment'

const onChange = (value: any) => {
console.log('🚀 ~ onChange value: ', value)
Expand All @@ -13,6 +14,10 @@ const onChange = (value: any) => {
const App = () => {
const [locale, setLocale] = useState<string>('en_US')

useEffect(() => {
console.log(locale, moment, moment.locale(locale))
}, [locale])

return <div style={{
display: 'flex',
flexDirection: 'column',
Expand Down
5 changes: 3 additions & 2 deletions playground/package.json
@@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"dev": "nodemon -w '../src/**/*.ts' -e .ts -x 'vite'",
"dev": "nodemon -w '../src/**/*.ts' -e .ts -x vite",
"build": "vite build"
},
"dependencies": {
Expand All @@ -15,6 +15,7 @@
"@types/react-dom": "^17.0.0",
"@vitejs/plugin-react": "^1.3.0",
"dayjs": "^1.11.0",
"vite": "^2.9.1"
"vite": "^2.9.1",
"unplugin-moment-to-dayjs": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion playground/vite.config.ts
@@ -1,6 +1,6 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import Unplugin from '../src/vite'
import Unplugin from 'unplugin-moment-to-dayjs/vite'

export default defineConfig({
plugins: [
Expand Down
64 changes: 15 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/helpers/generateCode.ts
@@ -0,0 +1,17 @@
import path from 'path'

export const generateCode = (plugins: string[] = []) => {
return ['', 'import dayjs from "dayjs"']
.concat(
plugins?.map((plugin) => {
return `import ${plugin} from "dayjs/esm/plugin/${plugin}";`
}))
.concat(
plugins?.map(plugin => `dayjs.extend(${plugin});`))
.concat(
[
'import localePlugin from "unplugin-moment-to-dayjs/plugins/locale";',
'dayjs.extend(localePlugin);',
],
).join('\n')
}
53 changes: 33 additions & 20 deletions src/index.ts
@@ -1,17 +1,23 @@
import path from 'path'
import { createUnplugin } from 'unplugin'
import alias from '@rollup/plugin-alias'
import type { ResolveIdHook } from 'rollup'
import MagicString from 'magic-string'
import { presets } from './config/presets'
import type { Options } from './types'
import { generateCode } from './helpers/generateCode'

const ENTRY_FILE_NAME = 'MOMENT_TO_DAYJS_ENTRY'

export default createUnplugin<Options>((options) => {
export default createUnplugin<Options>((options, meta) => {
const { framework } = meta
const { preset = 'antd' } = options || {}

const plugins = options?.plugins ?? presets[preset].plugins
const replaceMoment = options?.replaceMoment ?? presets[preset].replaceMoment

let entrySource = ''

return {
name: 'unplugin-moment-to-dayjs',
enforce: 'pre',
Expand All @@ -27,6 +33,9 @@ export default createUnplugin<Options>((options) => {
}
: {}
},
configResolved(config) {
console.log('🚀 ~ file: index.ts ~ line 37 ~ configResolved ~ config', config.resolve.alias)
},
transformIndexHtml: {
enforce: 'pre',
transform() {
Expand All @@ -43,6 +52,8 @@ export default createUnplugin<Options>((options) => {
},
rollup: {
options: (options: any) => {
if (framework !== 'rollup')
return
options.plugins = [
alias({
entries: {
Expand All @@ -61,29 +72,31 @@ export default createUnplugin<Options>((options) => {
moment: 'dayjs',
}
},
transformInclude(id) {
return id.includes(ENTRY_FILE_NAME)
},
resolveId(id) {
if (id.includes(ENTRY_FILE_NAME))
// transformInclude(id) {
// console.log(id)
// return id.includes(ENTRY_FILE_NAME)
// },
async resolveId(...args: Parameters<ResolveIdHook>) {
const [source, _, options] = args
if (source.includes(ENTRY_FILE_NAME))
return ENTRY_FILE_NAME
if (options?.isEntry === true)
entrySource = source
},
load(id) {
if (id.includes(ENTRY_FILE_NAME)) {
return ['import dayjs from "dayjs"']
.concat(
plugins?.map((plugin) => {
return `import ${plugin} from "dayjs/esm/plugin/${plugin}";`
}))
.concat(
plugins?.map(plugin => `dayjs.extend(${plugin});`))
.concat(
[
`import localePlugin from "${path.posix.resolve(__dirname, './plugins/locale.ts')}";`,
'dayjs.extend(localePlugin);',
],
).join('\n')
if (framework === 'vite' && id.includes(ENTRY_FILE_NAME))
return generateCode(plugins)
},
transform(code, id) {
if (framework === 'rollup' && id === entrySource) {
const ms = new MagicString(code).prepend(generateCode(plugins))

return {
code: ms.toString(),
map: ms.generateMap(),
}
}
return code
},
}
})
8 changes: 4 additions & 4 deletions tsup.config.ts
@@ -1,11 +1,11 @@
import { Options } from 'tsup'
import type { Options } from 'tsup'

export default <Options>{
entryPoints: [
entry: [
'src/*.ts',
'src/plugins/*.ts',
],
clean: true,
format: ['cjs', 'esm'],
dts: true,
onSuccess: 'npm run build:fix',
dts: false,
}

1 comment on commit c776a1f

@vercel
Copy link

@vercel vercel bot commented on c776a1f Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.