Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
fix: rewrite default for vue script block
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Aug 6, 2020
1 parent c310dd0 commit 360b4dd
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
]
},
"dependencies": {
"@vue/compiler-sfc": "^3.0.0-rc.5",
"@vue/component-compiler-utils": "^3.2.0",
"chalk": "^4.1.0",
"debug": "^4.1.1",
Expand Down
13 changes: 2 additions & 11 deletions src/serverPlugin.ts
Expand Up @@ -22,11 +22,10 @@ import { srcImportMap } from 'vite/dist/node/server/serverPluginVue'
import { TemplateCompileOptions } from '@vue/component-compiler-utils/lib/compileTemplate'
import { clientPublicPath } from 'vite/dist/node/server/serverPluginClient'
import { mergeSourceMap } from 'vite/dist/node/server/serverPluginSourceMap'
import { rewriteDefault } from '@vue/compiler-sfc'

const vueTemplateCompiler = require('vue-template-compiler')

const defaultExportRE = /((?:^|\n|;)\s*)export default/

export const vueComponentNormalizer = '/vite/vueComponentNormalizer'
export const vueHotReload = '/vite/vueHotReload'

Expand Down Expand Up @@ -157,15 +156,7 @@ async function parseSFC(
}

// rewrite export default.
// fast path: simple regex replacement to avoid full-blown babel parse.
let replaced = code.replace(defaultExportRE, '$1var script =')
// if the script somehow still contains `default export`, it probably has
// multi-line comments or template strings. fallback to a full parse.
// todo
// if (defaultExportRE.test(replaced)) {
// replaced = rewriteDefaultExport(code)
// }
scriptImport = replaced
scriptImport = rewriteDefault(code, 'script')
}

let stylesCode = ``
Expand Down
177 changes: 173 additions & 4 deletions yarn.lock
Expand Up @@ -639,6 +639,55 @@
dependencies:
"@types/node" "*"

"@vue/compiler-core@3.0.0-rc.5":
version "3.0.0-rc.5"
resolved "https://registry.npm.taobao.org/@vue/compiler-core/download/@vue/compiler-core-3.0.0-rc.5.tgz#dd4f1816fcae34a81bc60e584f97993cad284d54"
integrity sha1-3U8YFvyuNKgbxg5YT5eZPK0oTVQ=
dependencies:
"@babel/parser" "^7.10.4"
"@babel/types" "^7.10.4"
"@vue/shared" "3.0.0-rc.5"
estree-walker "^2.0.1"
source-map "^0.6.1"

"@vue/compiler-dom@3.0.0-rc.5":
version "3.0.0-rc.5"
resolved "https://registry.npm.taobao.org/@vue/compiler-dom/download/@vue/compiler-dom-3.0.0-rc.5.tgz#83905e8601123a3654b90fbd80708a16530ce21a"
integrity sha1-g5BehgESOjZUuQ+9gHCKFlMM4ho=
dependencies:
"@vue/compiler-core" "3.0.0-rc.5"
"@vue/shared" "3.0.0-rc.5"

"@vue/compiler-sfc@^3.0.0-rc.5":
version "3.0.0-rc.5"
resolved "https://registry.npm.taobao.org/@vue/compiler-sfc/download/@vue/compiler-sfc-3.0.0-rc.5.tgz#374e52a6fbf8fb9aee1213026050a0f1c496fecf"
integrity sha1-N05Spvv4+5ruEhMCYFCg8cSW/s8=
dependencies:
"@babel/parser" "^7.10.4"
"@babel/types" "^7.10.4"
"@vue/compiler-core" "3.0.0-rc.5"
"@vue/compiler-dom" "3.0.0-rc.5"
"@vue/compiler-ssr" "3.0.0-rc.5"
"@vue/shared" "3.0.0-rc.5"
consolidate "^0.15.1"
estree-walker "^2.0.1"
hash-sum "^2.0.0"
lru-cache "^5.1.1"
magic-string "^0.25.7"
merge-source-map "^1.1.0"
postcss "^7.0.27"
postcss-modules "^3.1.0"
postcss-selector-parser "^6.0.2"
source-map "^0.6.1"

"@vue/compiler-ssr@3.0.0-rc.5":
version "3.0.0-rc.5"
resolved "https://registry.npm.taobao.org/@vue/compiler-ssr/download/@vue/compiler-ssr-3.0.0-rc.5.tgz?cache=0&sync_timestamp=1595985482307&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40vue%2Fcompiler-ssr%2Fdownload%2F%40vue%2Fcompiler-ssr-3.0.0-rc.5.tgz#878406c59daff362ecdcb199fb9467a769ca8de5"
integrity sha1-h4QGxZ2v82Ls3LGZ+5Rnp2nKjeU=
dependencies:
"@vue/compiler-dom" "3.0.0-rc.5"
"@vue/shared" "3.0.0-rc.5"

"@vue/component-compiler-utils@^3.0.0", "@vue/component-compiler-utils@^3.1.2":
version "3.1.2"
resolved "https://registry.npm.taobao.org/@vue/component-compiler-utils/download/@vue/component-compiler-utils-3.1.2.tgz#8213a5ff3202f9f2137fe55370f9e8b9656081c3"
Expand Down Expand Up @@ -687,6 +736,11 @@
sass "^1.18.0"
stylus "^0.54.5"

"@vue/shared@3.0.0-rc.5":
version "3.0.0-rc.5"
resolved "https://registry.npm.taobao.org/@vue/shared/download/@vue/shared-3.0.0-rc.5.tgz#cea2378e3e37363ddc1f5dd158edc9c9b5b3fff0"
integrity sha1-zqI3jj43Nj3cH13RWO3JybWz//A=

JSONStream@^1.0.4:
version "1.3.5"
resolved "https://registry.npm.taobao.org/JSONStream/download/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -1047,6 +1101,11 @@ big.js@^3.1.3:
resolved "https://registry.npm.taobao.org/big.js/download/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e"
integrity sha1-pfwpi4G54Nyi5FiCR4S2XFK6WI4=

big.js@^5.2.2:
version "5.2.2"
resolved "https://registry.npm.taobao.org/big.js/download/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
integrity sha1-ZfCvOC9Xi83HQr2cKB6cstd2gyg=

binary-extensions@^2.0.0:
version "2.1.0"
resolved "https://registry.npm.taobao.org/binary-extensions/download/binary-extensions-2.1.0.tgz?cache=0&sync_timestamp=1593261419324&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbinary-extensions%2Fdownload%2Fbinary-extensions-2.1.0.tgz#30fa40c9e7fe07dbc895678cd287024dea241dd9"
Expand Down Expand Up @@ -1910,6 +1969,11 @@ emojis-list@^2.0.0:
resolved "https://registry.npm.taobao.org/emojis-list/download/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
integrity sha1-TapNnbAPmBmIDHn6RXrlsJof04k=

emojis-list@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/emojis-list/download/emojis-list-3.0.0.tgz#5570662046ad29e2e916e71aae260abdff4f6a78"
integrity sha1-VXBmIEatKeLpFucariYKvf9Pang=

end-of-stream@^1.1.0, end-of-stream@^1.4.1:
version "1.4.4"
resolved "https://registry.npm.taobao.org/end-of-stream/download/end-of-stream-1.4.4.tgz?cache=0&sync_timestamp=1569416272686&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fend-of-stream%2Fdownload%2Fend-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0"
Expand Down Expand Up @@ -1980,6 +2044,11 @@ estree-walker@^0.6.1:
resolved "https://registry.npm.taobao.org/estree-walker/download/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
integrity sha1-UwSRQ/QMbrkYsjZx0f4yGfOhs2I=

estree-walker@^2.0.1:
version "2.0.1"
resolved "https://registry.npm.taobao.org/estree-walker/download/estree-walker-2.0.1.tgz#f8e030fb21cefa183b44b7ad516b747434e7a3e0"
integrity sha1-+OAw+yHO+hg7RLetUWt0dDTno+A=

esutils@^2.0.2:
version "2.0.3"
resolved "https://registry.npm.taobao.org/esutils/download/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
Expand Down Expand Up @@ -2255,6 +2324,13 @@ generic-names@^1.0.2:
dependencies:
loader-utils "^0.2.16"

generic-names@^2.0.1:
version "2.0.1"
resolved "https://registry.npm.taobao.org/generic-names/download/generic-names-2.0.1.tgz#f8a378ead2ccaa7a34f0317b05554832ae41b872"
integrity sha1-+KN46tLMqno08DF7BVVIMq5BuHI=
dependencies:
loader-utils "^1.1.0"

gensync@^1.0.0-beta.1:
version "1.0.0-beta.1"
resolved "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.1.tgz#58f4361ff987e5ff6e1e7a210827aa371eaac269"
Expand Down Expand Up @@ -2539,11 +2615,18 @@ iconv-lite@0.4.24:
dependencies:
safer-buffer ">= 2.1.2 < 3"

icss-replace-symbols@^1.0.2:
icss-replace-symbols@^1.0.2, icss-replace-symbols@^1.1.0:
version "1.1.0"
resolved "https://registry.npm.taobao.org/icss-replace-symbols/download/icss-replace-symbols-1.1.0.tgz#06ea6f83679a7749e386cfe1fe812ae5db223ded"
integrity sha1-Bupvg2ead0njhs/h/oEq5dsiPe0=

icss-utils@^4.0.0, icss-utils@^4.1.1:
version "4.1.1"
resolved "https://registry.npm.taobao.org/icss-utils/download/icss-utils-4.1.1.tgz#21170b53789ee27447c2f47dd683081403f9a467"
integrity sha1-IRcLU3ie4nRHwvR91oMIFAP5pGc=
dependencies:
postcss "^7.0.14"

ieee754@^1.1.4:
version "1.1.13"
resolved "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84"
Expand Down Expand Up @@ -3389,6 +3472,13 @@ json5@^0.5.0:
resolved "https://registry.npm.taobao.org/json5/download/json5-0.5.1.tgz?cache=0&sync_timestamp=1586045666090&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fjson5%2Fdownload%2Fjson5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
integrity sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=

json5@^1.0.1:
version "1.0.1"
resolved "https://registry.npm.taobao.org/json5/download/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
integrity sha1-d5+wAYYE+oVOrL9iUhgNg1Q+Pb4=
dependencies:
minimist "^1.2.0"

jsonfile@^6.0.1:
version "6.0.1"
resolved "https://registry.npm.taobao.org/jsonfile/download/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
Expand Down Expand Up @@ -3556,6 +3646,15 @@ loader-utils@^0.2.16:
json5 "^0.5.0"
object-assign "^4.0.1"

loader-utils@^1.1.0:
version "1.4.0"
resolved "https://registry.npm.taobao.org/loader-utils/download/loader-utils-1.4.0.tgz#c579b5e34cb34b1a74edc6c1fb36bfa371d5a613"
integrity sha1-xXm140yzSxp07cbB+za/o3HVphM=
dependencies:
big.js "^5.2.2"
emojis-list "^3.0.0"
json5 "^1.0.1"

locate-path@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/locate-path/download/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"
Expand All @@ -3576,6 +3675,11 @@ lodash._reinterpolate@^3.0.0:
resolved "https://registry.npm.taobao.org/lodash._reinterpolate/download/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=

lodash.camelcase@^4.3.0:
version "4.3.0"
resolved "https://registry.npm.taobao.org/lodash.camelcase/download/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
integrity sha1-soqmKIorn8ZRA1x3EfZathkDMaY=

lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.npm.taobao.org/lodash.ismatch/download/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"
Expand Down Expand Up @@ -3649,6 +3753,13 @@ lru-cache@^4.0.1, lru-cache@^4.1.2:
pseudomap "^1.0.2"
yallist "^2.1.2"

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.npm.taobao.org/lru-cache/download/lru-cache-5.1.1.tgz?cache=0&sync_timestamp=1594427519396&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Flru-cache%2Fdownload%2Flru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
integrity sha1-HaJ+ZxAnGUdpXa9oSOhH8B2EuSA=
dependencies:
yallist "^3.0.2"

magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.npm.taobao.org/magic-string/download/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
Expand Down Expand Up @@ -4297,6 +4408,13 @@ posix-character-classes@^0.1.0:
resolved "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=

postcss-modules-extract-imports@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/postcss-modules-extract-imports/download/postcss-modules-extract-imports-2.0.0.tgz#818719a1ae1da325f9832446b01136eeb493cd7e"
integrity sha1-gYcZoa4doyX5gyRGsBE27rSTzX4=
dependencies:
postcss "^7.0.5"

postcss-modules-local-by-default@^1.1.1:
version "1.2.0"
resolved "https://registry.npm.taobao.org/postcss-modules-local-by-default/download/postcss-modules-local-by-default-1.2.0.tgz#f7d80c398c5a393fa7964466bd19500a7d61c069"
Expand All @@ -4305,6 +4423,16 @@ postcss-modules-local-by-default@^1.1.1:
css-selector-tokenizer "^0.7.0"
postcss "^6.0.1"

postcss-modules-local-by-default@^3.0.2:
version "3.0.3"
resolved "https://registry.npm.taobao.org/postcss-modules-local-by-default/download/postcss-modules-local-by-default-3.0.3.tgz?cache=0&sync_timestamp=1595731916732&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpostcss-modules-local-by-default%2Fdownload%2Fpostcss-modules-local-by-default-3.0.3.tgz#bb14e0cc78279d504dbdcbfd7e0ca28993ffbbb0"
integrity sha1-uxTgzHgnnVBNvcv9fgyiiZP/u7A=
dependencies:
icss-utils "^4.1.1"
postcss "^7.0.32"
postcss-selector-parser "^6.0.2"
postcss-value-parser "^4.1.0"

postcss-modules-scope@^1.0.2:
version "1.1.0"
resolved "https://registry.npm.taobao.org/postcss-modules-scope/download/postcss-modules-scope-1.1.0.tgz#d6ea64994c79f97b62a72b426fbe6056a194bb90"
Expand All @@ -4313,6 +4441,14 @@ postcss-modules-scope@^1.0.2:
css-selector-tokenizer "^0.7.0"
postcss "^6.0.1"

postcss-modules-scope@^2.2.0:
version "2.2.0"
resolved "https://registry.npm.taobao.org/postcss-modules-scope/download/postcss-modules-scope-2.2.0.tgz?cache=0&sync_timestamp=1584620714522&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpostcss-modules-scope%2Fdownload%2Fpostcss-modules-scope-2.2.0.tgz#385cae013cc7743f5a7d7602d1073a89eaae62ee"
integrity sha1-OFyuATzHdD9afXYC0Qc6iequYu4=
dependencies:
postcss "^7.0.6"
postcss-selector-parser "^6.0.0"

postcss-modules-sync@^1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/postcss-modules-sync/download/postcss-modules-sync-1.0.0.tgz#619a719cf78dd16a4834135140b324cf77334be1"
Expand All @@ -4325,7 +4461,30 @@ postcss-modules-sync@^1.0.0:
postcss-modules-scope "^1.0.2"
string-hash "^1.1.0"

postcss-selector-parser@^6.0.2:
postcss-modules-values@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/postcss-modules-values/download/postcss-modules-values-3.0.0.tgz#5b5000d6ebae29b4255301b4a3a54574423e7f10"
integrity sha1-W1AA1uuuKbQlUwG0o6VFdEI+fxA=
dependencies:
icss-utils "^4.0.0"
postcss "^7.0.6"

postcss-modules@^3.1.0:
version "3.2.0"
resolved "https://registry.npm.taobao.org/postcss-modules/download/postcss-modules-3.2.0.tgz?cache=0&sync_timestamp=1593589593474&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpostcss-modules%2Fdownload%2Fpostcss-modules-3.2.0.tgz#1ca870d197cd09a7964253e12de2aac906c94256"
integrity sha1-HKhw0ZfNCaeWQlPhLeKqyQbJQlY=
dependencies:
generic-names "^2.0.1"
icss-replace-symbols "^1.1.0"
lodash.camelcase "^4.3.0"
postcss "^7.0.32"
postcss-modules-extract-imports "^2.0.0"
postcss-modules-local-by-default "^3.0.2"
postcss-modules-scope "^2.2.0"
postcss-modules-values "^3.0.0"
string-hash "^1.1.1"

postcss-selector-parser@^6.0.0, postcss-selector-parser@^6.0.2:
version "6.0.2"
resolved "https://registry.npm.taobao.org/postcss-selector-parser/download/postcss-selector-parser-6.0.2.tgz#934cf799d016c83411859e09dcecade01286ec5c"
integrity sha1-k0z3mdAWyDQRhZ4J3Oyt4BKG7Fw=
Expand All @@ -4334,6 +4493,11 @@ postcss-selector-parser@^6.0.2:
indexes-of "^1.0.1"
uniq "^1.0.1"

postcss-value-parser@^4.1.0:
version "4.1.0"
resolved "https://registry.npm.taobao.org/postcss-value-parser/download/postcss-value-parser-4.1.0.tgz#443f6a20ced6481a2bda4fa8532a6e55d789a2cb"
integrity sha1-RD9qIM7WSBor2k+oUypuVdeJoss=

postcss@^5.2.5:
version "5.2.18"
resolved "https://registry.npm.taobao.org/postcss/download/postcss-5.2.18.tgz#badfa1497d46244f6390f58b319830d9107853c5"
Expand All @@ -4353,7 +4517,7 @@ postcss@^6.0.1:
source-map "^0.6.1"
supports-color "^5.4.0"

postcss@^7.0.14:
postcss@^7.0.14, postcss@^7.0.27, postcss@^7.0.32, postcss@^7.0.5, postcss@^7.0.6:
version "7.0.32"
resolved "https://registry.npm.taobao.org/postcss/download/postcss-7.0.32.tgz#4310d6ee347053da3433db2be492883d62cec59d"
integrity sha1-QxDW7jRwU9o0M9sr5JKIPWLOxZ0=
Expand Down Expand Up @@ -5267,7 +5431,7 @@ string-argv@0.3.1:
resolved "https://registry.npm.taobao.org/string-argv/download/string-argv-0.3.1.tgz#95e2fbec0427ae19184935f816d74aaa4c5c19da"
integrity sha1-leL77AQnrhkYSTX4FtdKqkxcGdo=

string-hash@^1.1.0:
string-hash@^1.1.0, string-hash@^1.1.1:
version "1.1.3"
resolved "https://registry.npm.taobao.org/string-hash/download/string-hash-1.1.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fstring-hash%2Fdownload%2Fstring-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha1-6Kr8CsGFW0Zmkp7X3RJ1311sgRs=
Expand Down Expand Up @@ -6012,6 +6176,11 @@ yallist@^2.1.2:
resolved "https://registry.npm.taobao.org/yallist/download/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
integrity sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=

yallist@^3.0.2:
version "3.1.1"
resolved "https://registry.npm.taobao.org/yallist/download/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
integrity sha1-27fa+b/YusmrRev2ArjLrQ1dCP0=

yaml@^1.7.2:
version "1.10.0"
resolved "https://registry.npm.taobao.org/yaml/download/yaml-1.10.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyaml%2Fdownload%2Fyaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
Expand Down

0 comments on commit 360b4dd

Please sign in to comment.