Skip to content
This repository was archived by the owner on Jan 23, 2022. It is now read-only.

feat: added element-plus ^1.0.2-beta.59 i18n support #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions generator/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module.exports = (api, opts, rootOptions) => {
module.exports = (api, opts) => {
const utils = require('./utils')(api)

api.extendPackage({
dependencies: {
'element-plus': '^1.0.2-beta.28'
'element-plus': '^1.0.2-beta.71'
}
})

api.injectImports(api.entryFile, `import installElementPlus from './plugins/element'`)
api.injectImports(api.entryFile, [
`import installElementPlus from './plugins/element.js'`,
`import locale from 'element-plus/lib/locale/lang/${opts.lang}.js'`
])

api.render({
'./src/plugins/element.js': './templates/src/plugins/element.js',
Expand Down Expand Up @@ -39,12 +42,23 @@ module.exports = (api, opts, rootOptions) => {
const lines = contentMain.split(/\r?\n/g)

const renderIndex = lines.findIndex(line => line.match(/createApp\(App\)(\.use\(\w*\))*\.mount\(['"]#app['"]\)/))
const renderContent = lines[renderIndex]
lines[renderIndex] = `const app = createApp(App)`
lines[renderIndex + 1] = `installElementPlus(app)`
lines[renderIndex + 2] = renderContent.replace('createApp\(App\)','app')

fs.writeFileSync(api.resolve(api.entryFile), lines.join(EOL), { encoding: 'utf-8' })
if (renderIndex >= 0) {
const renderContent = lines[renderIndex]
lines[renderIndex] = `const app = createApp({
render() {
return (
<ElConfigProvider locale={locale}>
<App />
</ElConfigProvider>
)
},
})`
lines[renderIndex + 1] = `installElementPlus(app)`
lines[renderIndex + 2] = renderContent.replace('createApp\(App\)','app')

fs.writeFileSync(api.resolve(api.entryFile), lines.join(EOL), { encoding: 'utf-8' })
}
})

api.onCreateComplete(() => {
Expand Down
18 changes: 3 additions & 15 deletions generator/templates/src/plugins/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,15 @@ import '../element-variables.scss'
<%_ } else { _%>
import 'element-plus/lib/theme-chalk/index.css'
<%_ } _%>
<%_ if (options.lang !== 'en') { _%>
import locale from 'element-plus/lib/locale/lang/<%= options.lang %>'
<%_ } _%>
<%_ } else { _%>
import { ElButton } from 'element-plus'
<%_ if (options.lang !== 'en') { _%>
import lang from 'element-plus/lib/locale/lang/<%= options.lang %>'
import locale from 'element-plus/lib/locale'
<%_ }} _%>
import { ElButton, ElConfigProvider } from 'element-plus'
<%_ } _%>

export default (app) => {
<%_ if (options.import === 'full') { _%>
<%_ if (options.lang !== 'en') { _%>
app.use(ElementPlus, { locale })
<%_ } else { _%>
app.use(ElementPlus)
<%_ } _%>
<%_ } else { _%>
<%_ if (options.lang !== 'en') { _%>
locale.use(lang)
<%_ } _%>
app.use(ElButton)
app.use(ElConfigProvider)
<%_ } _%>
}