Skip to content

Commit

Permalink
fix: properly handle absolute asset urls
Browse files Browse the repository at this point in the history
Fix #45
  • Loading branch information
yyx990803 committed May 4, 2020
1 parent 59c1ab1 commit 5ca0ec4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 63 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.2",
"@types/koa": "^2.11.3",
"@vue/compiler-sfc": "^3.0.0-beta.8",
"@vue/compiler-sfc": "^3.0.0-beta.9",
"chalk": "^4.0.0",
"chokidar": "^3.3.1",
"cssnano": "^4.1.10",
Expand All @@ -80,7 +80,7 @@
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-vue": "^6.0.0-alpha.7",
"slash": "^3.0.0",
"vue": "^3.0.0-beta.8",
"vue": "^3.0.0-beta.9",
"ws": "^7.2.3"
},
"devDependencies": {
Expand Down
7 changes: 5 additions & 2 deletions src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createBuildResolvePlugin } from './buildPluginResolve'
import { createBuildHtmlPlugin, scriptRE } from './buildPluginHtml'
import { createBuildCssPlugin } from './buildPluginCss'
import { createBuildAssetPlugin } from './buildPluginAsset'
import { isExternalUrl } from './utils'

export interface BuildOptions {
root?: string
Expand Down Expand Up @@ -87,7 +88,9 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
...(indexContent ? [createBuildHtmlPlugin(indexPath, indexContent)] : []),
// vue
require('rollup-plugin-vue')({
transformAssetUrls: true,
transformAssetUrls: {
includeAbsolute: true
},
// TODO: for now we directly handle pre-processors in rollup-plugin-vue
// so that we don't need to install dedicated rollup plugins.
// In the future we probably want to still use rollup plugins so that
Expand Down Expand Up @@ -140,7 +143,7 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
}

const injectScript = (html: string, filename: string) => {
filename = /^https?:\/\//.test(filename)
filename = isExternalUrl(filename)
? filename
: `/${path.posix.join(assetsDir, filename)}`
const tag = `<script type="module" src="${filename}"></script>`
Expand Down
12 changes: 9 additions & 3 deletions src/node/buildPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import path from 'path'
import { Plugin } from 'rollup'
import { getAssetPublicPath, registerAssets } from './buildPluginAsset'
import { loadPostcssConfig } from './config'
import { isExternalUrl } from './utils'

const debug = require('debug')('vite:css')

const urlRE = /(url\(\s*['"]?)([^"')]+)(["']?\s*\))/g
const urlRE = /(url\(\s*['"]?)([^"')]+)(["']?\s*\))/

export const createBuildCssPlugin = (
root: string,
Expand All @@ -24,14 +25,19 @@ export const createBuildCssPlugin = (
// and rewrite the url to the resolved public path
if (urlRE.test(css)) {
const fileDir = path.dirname(id)
urlRE.lastIndex = 0
let match
let remaining = css
let rewritten = ''
while ((match = urlRE.exec(remaining))) {
rewritten += remaining.slice(0, match.index)
const [matched, before, rawUrl, after] = match
const file = path.resolve(fileDir, rawUrl)
if (isExternalUrl(rawUrl)) {
rewritten += matched
remaining = remaining.slice(match.index + matched.length)
return
}

const file = path.join(fileDir, rawUrl)
const { fileName, content, url } = await getAssetPublicPath(
file,
assetsDir
Expand Down
4 changes: 2 additions & 2 deletions src/node/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
rewriteFileWithHMR,
hmrClientPublicPath
} from './serverPluginHmr'
import { readBody, cleanUrl, queryRE } from './utils'
import { readBody, cleanUrl, queryRE, isExternalUrl } from './utils'

const debug = require('debug')('vite:rewrite')

Expand Down Expand Up @@ -135,7 +135,7 @@ function rewriteImports(
}
if (dynamicIndex === -1 || hasLiteralDynamicId) {
// do not rewrite external imports
if (/https?:\/\//.test(id)) {
if (isExternalUrl(id)) {
return
}

Expand Down
5 changes: 4 additions & 1 deletion src/node/serverPluginVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ function compileSFCTemplate(
source: template.content,
filename: filePath,
inMap: template.map,
transformAssetUrlsBase: path.posix.dirname(publicPath),
transformAssetUrls: {
// @ts-ignore
base: path.posix.dirname(publicPath)
},
compilerOptions: {
scopeId: scoped ? `data-v-${hash_sum(publicPath)}` : null,
runtimeModuleName: '/@modules/vue'
Expand Down
3 changes: 3 additions & 0 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Context } from 'koa'
import { Readable } from 'stream'
const getETag = require('etag')

const httpRE = /^https?:\/\//
export const isExternalUrl = (url: string) => httpRE.test(url)

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
106 changes: 53 additions & 53 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -787,34 +787,34 @@
dependencies:
"@types/node" "*"

"@vue/compiler-core@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-beta.8.tgz#1f9d711f7faafdb13ece6e5cf3f61da52a8c1557"
integrity sha512-KGUJdqHZi1Nl1t3eZkUDzkllI7JSOErpOs6fpxJI7TswFPErCQVf5vnJkvwEjUkDRp9258ZHUa8X4Fwg7FiQXg==
"@vue/compiler-core@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-beta.9.tgz#4ecf5012d9fc5757337ad7bffcc9758e133c16a2"
integrity sha512-9wSyjY4n4+XySezEt8j+fe+UCht+HyqIT98lafqcZ/c7GVNNz87zej0VNR/tO1agwSdPZjApRv/kes7lcKs88w==
dependencies:
"@babel/parser" "^7.8.6"
"@babel/types" "^7.8.6"
"@vue/shared" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.9"
estree-walker "^0.8.1"
source-map "^0.6.1"

"@vue/compiler-dom@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.8.tgz#a1c4070f3992d5ad2ecec7a63c800630e70980bc"
integrity sha512-oOvC4ru+pg3hEdmnieIslfH6iE+cqAArr2XlJECgcJsk2Ss+SXMoK9vf/fcsGeIUlCgUhMuvY9iIEJml902F7g==
"@vue/compiler-dom@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.9.tgz#ef4fd5cdcdceb37d67eccb70d28d09cecf0a3425"
integrity sha512-KMWbE/O+177d8QSS46fyCYDipOw4RIoh9mo1s1CqBWQxHwNelOMU4NnHZ9TvpxBo+Dzyvzk9b9x/56KSKMcdJg==
dependencies:
"@vue/compiler-core" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/compiler-core" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"

"@vue/compiler-sfc@^3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.0.0-beta.8.tgz#e0509e578c3771d24793dd6c8b93e789b68e6dbd"
integrity sha512-rHELIv1LAvg5ctS0pptoqnKq/JL3xkUqH8QnqJ36x/o81P1Tu6mFdc9da0TKbI+RZK6HjJNHqMsEtkDLnpFXxQ==
"@vue/compiler-sfc@^3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.0.0-beta.9.tgz#3b85acf7c8b9792ffcfa9880cb8684fe679d25b3"
integrity sha512-J6C4l8GxP2jWRZFXGO4AwZcOzHUYNT08kGTFZM2V9GBMg3UaLmE6DvUPTPBTrBL55RTyaV2O+U44S5iYlyUi1A==
dependencies:
"@vue/compiler-core" "3.0.0-beta.8"
"@vue/compiler-dom" "3.0.0-beta.8"
"@vue/compiler-ssr" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/compiler-core" "3.0.0-beta.9"
"@vue/compiler-dom" "3.0.0-beta.9"
"@vue/compiler-ssr" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"
consolidate "^0.15.1"
hash-sum "^2.0.0"
lru-cache "^5.1.1"
Expand All @@ -824,42 +824,42 @@
postcss-selector-parser "^6.0.2"
source-map "^0.6.1"

"@vue/compiler-ssr@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.0.0-beta.8.tgz#04a3c2606aaabe836739843d7ae53676e8d0596a"
integrity sha512-7sb/sc+Ih147rMDpAXnvO7xo7hEZ78Wu+klr+149qKAfOudZbxTBH2iRph8GxTV5WbUY1SSryZ88MXLek+Owvg==
"@vue/compiler-ssr@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.0.0-beta.9.tgz#be8fd55885218957a65a333f129783c36a882657"
integrity sha512-WoOd36XPR5aY9es+pTuP0DVPY45sDQnXBxFDagNUOJ3VXu40NLwiXYNTRxRPOExkgEL0YcDNds6/bLqDWGD4JQ==
dependencies:
"@vue/compiler-dom" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/compiler-dom" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"

"@vue/reactivity@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-beta.8.tgz#d3f21f50f3ce48fcb828f2eb1031d6df1e6035f0"
integrity sha512-XXfGDCA7mLaEah8SJ1rFGf5gLlSKp+JH8YwvVWjXk5MkTYzgg/v9yQYSW42SrIxzwwYhTSpii2DKIVhMXvOKnQ==
"@vue/reactivity@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-beta.9.tgz#3a81a6a0dff47ee4713aceb5c77302620de471fe"
integrity sha512-cdAwMceme0ku8M/9npFx2jHMJexlOctA8bJA5LNF8ISaQcfklTGa2u7+iWZDfF6slxPjkeGKLpOyKE5qIdBgAA==
dependencies:
"@vue/shared" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.9"

"@vue/runtime-core@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-beta.8.tgz#c7c7e0ff1a288877fbd26a3b23091fe1c54e58b6"
integrity sha512-gm5xECAyn0ONh8+4dqffau8FBUoXkZZIwLs7PB19ffpvmZhh9cVrJ7KBvZg1IJWIa0VpoEm9quy8GzXu1ViT9w==
"@vue/runtime-core@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-beta.9.tgz#7b9ab834e0a566ec669e463b301bad327a95a6dc"
integrity sha512-SU8OFkgnRoWQNEdHqPqubIKrTWwR3f68SRxdyOvre2NoPPCf6+TWoNlrWMBFSROn0qvhFOvQybJt3002uD0uQw==
dependencies:
"@vue/reactivity" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/reactivity" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"

"@vue/runtime-dom@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-beta.8.tgz#78524abbdf89dc4b7b8be3b44d731353ef193d35"
integrity sha512-Sy/5+MrxxMRkumm/oFwCfkgBORbEHWCjyiJs26U29+PP2pPAGHiiDo0MGOkrhOKFqzfK/2hJghVmS+c2BRX9qw==
"@vue/runtime-dom@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-beta.9.tgz#cebfa7f9e417e0502bbb37b40b8fdc089fe70100"
integrity sha512-P0IUIShcudwEZ0ez1nQXyKY0hzrHfwxmz1b+z7ppeoVIDf3tKSwL1gCqPtQdYwWyfu8bTU8C/8DQF3V7jcyeZA==
dependencies:
"@vue/runtime-core" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/runtime-core" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"
csstype "^2.6.8"

"@vue/shared@3.0.0-beta.8":
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-beta.8.tgz#b41067dffcab2c05dbdd0fde38614de24937fc03"
integrity sha512-/HfCFvVCZJ8NPtY73TO/Zf+ZKlZXlMfqRn2j6DNVJBXEJ04ipwh8+zSxuIWRRfP+DiFRlzTjrhrZnR08weLyAg==
"@vue/shared@3.0.0-beta.9":
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-beta.9.tgz#b3bfd0553bef22d508d0e56cc305e25783e64017"
integrity sha512-ndn3/TCCzvmFI5zTpBTdKdNW2x+ibshvjcoajvcUXNRLlHVJNad0PdnvklPu6zaI6+9fkFpFC5AnMkLg5/KvSw==

JSONStream@^1.0.4:
version "1.3.5"
Expand Down Expand Up @@ -6794,14 +6794,14 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vue@^3.0.0-beta.8:
version "3.0.0-beta.8"
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-beta.8.tgz#2b56d8a7f650a154e82b4375176e4edb85a1286a"
integrity sha512-nx2rKQEuKSlzK+SucyCwmMenjys7uSiuVAnglcZ3ktM6RPgrEdmyhHfkeqn0XsBsdzGZGFXrfcjgJltilMSqpQ==
vue@^3.0.0-beta.9:
version "3.0.0-beta.9"
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-beta.9.tgz#f2cfcf5bafa8da9fb8b4fc8249f06674a38ed19f"
integrity sha512-K9EBlcTSRDIXo8IkRK5fY1X4cU6Z7iE7F6wSyMyzWWfuJCg1uO+xPEmRfp+kSioRk2QOtAJOqADpNsR+dM4bmQ==
dependencies:
"@vue/compiler-dom" "3.0.0-beta.8"
"@vue/runtime-dom" "3.0.0-beta.8"
"@vue/shared" "3.0.0-beta.8"
"@vue/compiler-dom" "3.0.0-beta.9"
"@vue/runtime-dom" "3.0.0-beta.9"
"@vue/shared" "3.0.0-beta.9"

w3c-hr-time@^1.0.1:
version "1.0.2"
Expand Down

0 comments on commit 5ca0ec4

Please sign in to comment.