chore: Struct adjust

This commit is contained in:
草鞋没号
2021-02-18 16:11:08 +08:00
parent 48d5a92e6d
commit 7b472f36c6
9 changed files with 40 additions and 73 deletions

View File

@@ -1,3 +1,4 @@
import { sep } from 'path'
import { get } from 'http'
import { green } from 'chalk'
@@ -18,3 +19,23 @@ export function waitOn(arg0: { port: string | number; interval?: number; }) {
}, interval)
})
}
/** cjs2esm */
export function cjs2esm() {
return {
name: '@rollup/plugin-cjs2esm',
transform(code: string, filename: string) {
if (filename.includes(`${sep}node_modules${sep}`)) {
return code
}
const cjsRegexp = /(const|let|var)[\n\s]+(\w+)[\n\s]*=[\n\s]*require\(["|'](.+)["|']\)/g
const res = code.match(cjsRegexp)
if (res) {
// const Store = require('electron-store') -> import Store from 'electron-store'
code = code.replace(cjsRegexp, `import $2 from '$3'`)
}
return code
},
}
}