修复esm

pull/1/head
yutent 2023-01-13 11:40:53 +08:00
parent 3e574aa3f7
commit ab563d0980
5 changed files with 74 additions and 33 deletions

View File

@ -72,12 +72,19 @@ export function compileScss(file, mini = true) {
* 主要是处理js的依赖引用 * 主要是处理js的依赖引用
* @param code <String> js代码 * @param code <String> js代码
*/ */
export function parseJs(code = '', imports, { IS_MPA, currentPage } = {}, isBuild) { export function parseJs(
code = '',
imports,
{ IS_MPA, currentPage } = {},
isBuild
) {
let fixedStyle = '\n\n' let fixedStyle = '\n\n'
return ( return (
code code
.replace(/import (.*?) from (["'])(.*?)\2/g, function (m, alias, q, name) { .replace(
/import (.*?) from (["'])(.*?)\2/g,
function (m, alias, q, name) {
if (name.startsWith('@/')) { if (name.startsWith('@/')) {
name = name.replace('@/', '/assets/js/') name = name.replace('@/', '/assets/js/')
} }
@ -88,7 +95,11 @@ export function parseJs(code = '', imports, { IS_MPA, currentPage } = {}, isBuil
if (IS_MPA) { if (IS_MPA) {
name += `${currentPage}/` name += `${currentPage}/`
} }
} else if (name.startsWith('/') && !name.startsWith('/assets/js/')) { } else if (
name.startsWith('/') &&
!name.startsWith('//') &&
!name.startsWith('/assets/js/')
) {
name = name.replace(/^\//, '/assets/js/') name = name.replace(/^\//, '/assets/js/')
} }
@ -104,7 +115,8 @@ export function parseJs(code = '', imports, { IS_MPA, currentPage } = {}, isBuil
name = name.replace(/\.vue$/, '.js') name = name.replace(/\.vue$/, '.js')
} }
return `import ${alias} from '${name}'` return `import ${alias} from '${name}'`
}) }
)
.replace(/import\((['"])(.*?)\1\)/g, function (m, q, name) { .replace(/import\((['"])(.*?)\1\)/g, function (m, q, name) {
if (isBuild) { if (isBuild) {
name = name.replace(/\.vue$/, '.js') name = name.replace(/\.vue$/, '.js')
@ -178,7 +190,9 @@ export function compileVue(file, imports, options = {}, isBuild) {
js += ` js += `
let stylesheet = new CSSStyleSheet() let stylesheet = new CSSStyleSheet()
stylesheet.path = '${file.slice(options.IS_MPA ? options.pagesDir.length : options.root.length)}' stylesheet.path = '${file.slice(
options.IS_MPA ? options.pagesDir.length : options.root.length
)}'
stylesheet.replaceSync(\`${scss.join(' ')}\`) stylesheet.replaceSync(\`${scss.join(' ')}\`)
document.adoptedStyleSheets.push(stylesheet) document.adoptedStyleSheets.push(stylesheet)
` `
@ -200,5 +214,8 @@ export function parseHtml(html, { page, imports, entry }) {
.replace('{{keywords}}', page.keywords || '') .replace('{{keywords}}', page.keywords || '')
.replace('{{description}}', page.description || '') .replace('{{description}}', page.description || '')
.replace('{{importmap}}', JSON.stringify({ imports })) .replace('{{importmap}}', JSON.stringify({ imports }))
.replace('<script src="main.js"></script>', `<script type="module">\n${entry}\n</script>`) .replace(
'<script src="main.js"></script>',
`<script type="module">\n${entry}\n</script>`
)
} }

View File

@ -8,7 +8,8 @@ export const JS_EXP = /<script[^>]*?>([\w\W]*?)<\/script>/
export const STYLE_EXP = /<style[^>]*?>([\w\W]*?)<\/style>/g export const STYLE_EXP = /<style[^>]*?>([\w\W]*?)<\/style>/g
export const HTML_EXP = /<template[^>]*?>([\w\W]*?)<\/template>/ export const HTML_EXP = /<template[^>]*?>([\w\W]*?)<\/template>/
export const CSS_SHEET_EXP = /([\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{([^\{\}]*?)\}/g export const CSS_SHEET_EXP =
/([\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{([^\{\}]*?)\}/g
export const COMMON_HEADERS = { export const COMMON_HEADERS = {
'Cache-Control': 'no-store' 'Cache-Control': 'no-store'

View File

@ -127,7 +127,12 @@ export default function createServer(root = '', conf = {}) {
} }
// console.log('>>>>', file) // console.log('>>>>', file)
code = compileVue(file, conf.imports, { IS_MPA, currentPage, root, pagesDir }) code = compileVue(file, conf.imports, {
IS_MPA,
currentPage,
root,
pagesDir
})
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
} }
break break
@ -198,7 +203,17 @@ export default function createServer(root = '', conf = {}) {
}) })
.on('listening', _ => { .on('listening', _ => {
console.log('启动成功, 可通过以下地址访问') console.log('启动成功, 可通过以下地址访问')
console.log(' 本地: %s://%s:%d', USE_HTTPS ? 'https' : 'http', '127.0.0.1', PORT) console.log(
console.log(' 外网: %s://%s:%d\n', USE_HTTPS ? 'https' : 'http', DOMAIN, PORT) ' 本地: %s://%s:%d',
USE_HTTPS ? 'https' : 'http',
'127.0.0.1',
PORT
)
console.log(
' 外网: %s://%s:%d\n',
USE_HTTPS ? 'https' : 'http',
DOMAIN,
PORT
)
}) })
} }

View File

@ -56,7 +56,10 @@ export default function compile(root = '', dist = '', conf = {}) {
) )
Es.transform(code, { minify: true }).then(r => { Es.transform(code, { minify: true }).then(r => {
fs.echo(r.code, join(dist, `assets/js/${it.name.split('.').shift()}.js`)) fs.echo(
r.code,
join(dist, `assets/js/${it.name.split('.').shift()}.js`)
)
}) })
} }
break break
@ -65,7 +68,12 @@ export default function compile(root = '', dist = '', conf = {}) {
{ {
let code = fs.cat(it.path) let code = fs.cat(it.path)
code = parseJs(code + '', conf.imports, { IS_MPA, currentPage }, true) code = parseJs(
code + '',
conf.imports,
{ IS_MPA, currentPage },
true
)
Es.transform(code, { minify: true }).then(r => { Es.transform(code, { minify: true }).then(r => {
fs.echo(r.code, join(dist, `assets/js/${it.name}`)) fs.echo(r.code, join(dist, `assets/js/${it.name}`))
}) })

View File

@ -1,7 +1,7 @@
{ {
"name": "@bytedo/vue-live", "name": "@bytedo/vue-live",
"type": "module", "type": "module",
"version": "0.0.14", "version": "0.0.15",
"bin": { "bin": {
"vue-live": "index.js" "vue-live": "index.js"
}, },