Skip to content

Commit

Permalink
fix: fix build index asset injection
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 4, 2020
1 parent dd7af0a commit ccce482
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
root = process.cwd(),
cdn = !resolveVue(root).hasLocalVue,
outDir = path.resolve(root, 'dist'),
assetsDir = '.',
assetsDir = 'assets',
resolvers = [],
srcRoots = [],
rollupInputOptions = {},
Expand Down Expand Up @@ -126,6 +126,31 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
})

let generatedIndex = indexContent && indexContent.replace(scriptRE, '').trim()

const injectCSS = (html: string, filename: string) => {
const tag = `<link rel="stylesheet" href="/${path.posix.join(
assetsDir,
filename
)}">`
if (/<\/head>/.test(html)) {
return html.replace(/<\/head>/, `${tag}\n</head>`)
} else {
return tag + '\n' + html
}
}

const injectScript = (html: string, filename: string) => {
filename = /^https?:\/\//.test(filename)
? filename
: `/${path.posix.join(assetsDir, filename)}`
const tag = `<script type="module" src="${filename}"></script>`
if (/<\/body>/.test(html)) {
return html.replace(/<\/body>/, `${tag}\n</body>`)
} else {
return html + '\n' + tag
}
}

// TODO handle public path for injections?
// this would also affect paths in templates and css.
if (generatedIndex) {
Expand Down Expand Up @@ -193,22 +218,3 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
html: generatedIndex || ''
}
}

function injectCSS(html: string, filename: string) {
const tag = `<link rel="stylesheet" href="./${filename}">`
if (/<\/head>/.test(html)) {
return html.replace(/<\/head>/, `${tag}\n</head>`)
} else {
return tag + '\n' + html
}
}

function injectScript(html: string, filename: string) {
filename = /^https?:\/\//.test(filename) ? filename : `./${filename}`
const tag = `<script type="module" src="${filename}"></script>`
if (/<\/body>/.test(html)) {
return html.replace(/<\/body>/, `${tag}\n</body>`)
} else {
return html + '\n' + tag
}
}

0 comments on commit ccce482

Please sign in to comment.