Skip to content

Commit

Permalink
feat: handle <script src> in index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 3, 2020
1 parent f29037d commit 63b4de6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import resolve from 'resolve-from'
import chalk from 'chalk'
import { Resolver, createResolver } from './resolver'
import { Options } from 'rollup-plugin-vue'
import { scriptRE } from './utils'
import { createBuildResolvePlugin } from './buildPluginResolve'
import { createBuildHtmlPlugin } from './buildPluginHtml'
import { createBuildHtmlPlugin, scriptRE } from './buildPluginHtml'
import { createBuildCssPlugin } from './buildPluginCss'
import { createBuildAssetPlugin } from './buildPluginAsset'

Expand Down
19 changes: 15 additions & 4 deletions src/node/buildPluginHtml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Plugin } from 'rollup'
import { scriptRE } from './utils'

export const scriptRE = /<script\b([^>]*)>([\s\S]*?)<\/script>/gm
const srcRE = /\bsrc=(?:"([^"]+)"|'([^']+)'|([^'"\s]+)\b)/

export const createBuildHtmlPlugin = (
indexPath: string,
Expand All @@ -12,9 +14,18 @@ export const createBuildHtmlPlugin = (
let script = ''
let match
while ((match = scriptRE.exec(indexContent))) {
// TODO handle <script type="module" src="..."/>
// just add it as an import
script += match[1]
// <script type="module" src="..."/>
// add it as an import
const tagAttr = match[1]
const srcMatch = tagAttr && tagAttr.match(srcRE)
if (srcMatch) {
script += `\nimport "${
srcMatch[1] || srcMatch[2] || srcMatch[3]
}"\n`
}
// <script type="module">...</script>
// add its content
script += match[2]
}
return script
}
Expand Down
2 changes: 0 additions & 2 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import LRUCache from 'lru-cache'
import os from 'os'
import { Context } from 'koa'

export const scriptRE = /<script\b[^>]*>([\s\S]*?)<\/script>/gm

const imageRE = /\.(png|jpe?g|gif|svg)(\?.*)?$/
const mediaRE = /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/
const fontsRE = /\.(woff2?|eot|ttf|otf)(\?.*)?$/i
Expand Down

0 comments on commit 63b4de6

Please sign in to comment.