Skip to content

Commit

Permalink
fix(cli): add module moduleCompatible option in varlet.config.js
Browse files Browse the repository at this point in the history
affects: @varlet/cli, @varlet/ui
  • Loading branch information
haoziqaq committed Nov 20, 2021
1 parent 2b5b1bd commit 0b1d95f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/varlet-cli/src/compiler/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from './compileStyle'
import { resolve } from 'path'
import type { BabelFileResult } from '@babel/core'
import { get } from 'lodash'
import { getVarletConfig } from '../config/varlet.config'

export const IMPORT_VUE_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.vue(\s*['"`]);?(?!\s*['"`])/g
export const IMPORT_TS_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.ts(\s*['"`]);?(?!\s*['"`])/g
Expand All @@ -34,11 +36,26 @@ export const replaceJSXExt = (script: string): string =>
export const replaceTSXExt = (script: string): string =>
script.replace(IMPORT_TSX_PATH_RE, scriptReplacer).replace(REQUIRE_TSX_PATH_RE, scriptReplacer)

export const moduleCompatible = (script: string): string => {
const moduleCompatible = get(getVarletConfig(), 'moduleCompatible', {})
Object.keys(moduleCompatible).forEach((esm) => {
const commonjs = moduleCompatible[esm]
script = script.replace(esm, commonjs)
})

return script
}

export async function compileScript(script: string, file: string) {
const modules = process.env.BABEL_MODULE

if (modules === 'commonjs') {
script = moduleCompatible(script)
}

let { code } = (await transformAsync(script, {
filename: file,
})) as BabelFileResult
const modules = process.env.BABEL_MODULE

code = extractStyleDependencies(
file,
Expand Down
3 changes: 3 additions & 0 deletions packages/varlet-ui/varlet.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,7 @@ module.exports = {
highlight: {
style: './highlight.css',
},
moduleCompatible: {
"import dayjs from 'dayjs/esm'\n": "import * as dayjs from 'dayjs'\n",
},
}

0 comments on commit 0b1d95f

Please sign in to comment.