Skip to content

Commit

Permalink
fix: rollup not work
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Apr 15, 2022
1 parent 8d13970 commit 02b9843
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/rollup/rollup.config.ts
Expand Up @@ -10,8 +10,8 @@ export default defineConfig({
},
external: ['dayjs'],
plugins: [
nodeResolve(),
Unplugin({
}),
nodeResolve(),
],
})
9 changes: 6 additions & 3 deletions examples/rollup/src/index.js
@@ -1,5 +1,8 @@
import moment from 'moment'
import hello from './hello'
export { default as hello2 } from './hello'

console.log('🚀 moment', moment())
console.log('🚀 hello', hello())
export default function hello() {
moment.locale('zh-cn')

moment('2019-01-01')
}
34 changes: 16 additions & 18 deletions src/index.ts
Expand Up @@ -5,16 +5,15 @@ import { presets } from './config/presets'
import type { Options } from './types'
import { generateCode } from './helpers/generateCode'

const ENTRY_FILE_NAME = 'MOMENT_TO_DAYJS_ENTRY'
const ENTRY_FLAG = 'MOMENT_TO_DAYJS_ENTRY'

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

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

const entrySource = ''
let entryId: string

return {
name: 'unplugin-moment-to-dayjs',
Expand All @@ -38,21 +37,21 @@ export default createUnplugin<Options>((options, meta) => {
tag: 'script',
attrs: {
type: 'module',
src: `${ENTRY_FILE_NAME}`,
src: ENTRY_FLAG,
},
injectTo: 'body-prepend',
}]
},
},
},
rollup: {
resolveId(source, _, options) {
async resolveId(source, importer, options) {
if (options.isEntry)
return `${source}?${ENTRY_FILE_NAME}`
entryId = source

return null
},
options: (options) => {
if (framework !== 'rollup')
return
options.plugins = [
alias({
entries: {
Expand All @@ -73,26 +72,25 @@ export default createUnplugin<Options>((options, meta) => {
},
// transformInclude(id) {
// console.log(id)
// return id.includes(ENTRY_FILE_NAME)
// return id.includes(ENTRY_FLAG)
// },
async resolveId(source) {
if (source.includes(ENTRY_FILE_NAME))
return ENTRY_FILE_NAME
resolveId(source) {
if (source.includes(ENTRY_FLAG))
return ENTRY_FLAG
},
load(id) {
if (framework === 'vite' && id.includes(ENTRY_FILE_NAME))
if (id.endsWith(ENTRY_FLAG))
return generateCode(plugins)
},
transform(code, id) {
if (framework === 'rollup' && id === entrySource) {
const ms = new MagicString(code).prepend(generateCode(plugins))

if (id === entryId) {
const ms = new MagicString(code)
ms.append(generateCode(plugins))
return {
code: ms.toString(),
map: ms.generateMap(),
}
}
return code
},
}
})

1 comment on commit 02b9843

@vercel
Copy link

@vercel vercel bot commented on 02b9843 Apr 15, 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.