Skip to content

Commit 2e20b7a

Browse files
authored
feat: bundle currentScript polyfill by default if library needs IE support (#3760)
1 parent 78fdc07 commit 2e20b7a

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

docs/guide/build-targets.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ App is the default build target. In this mode:
1313

1414
## Library
1515

16-
::: tip Note on IE Compatibility
17-
In lib mode, the public path is [dynamically determined](https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/commands/build/setPublicPath.js) based on the URL from which the main js file is loaded (to enable dynamic assets loading). However, this feature requires `document.currentScript` support, which is missing in IE. So it's recommended to include the [current-script-polyfill](https://www.npmjs.com/package/current-script-polyfill) in the final web page before the library is imported, if IE support is a requirement.
18-
:::
19-
2016
::: tip Note on Vue Dependency
2117
In lib mode, Vue is *externalized*. This means the bundle will not bundle Vue even if your code imports Vue. If the lib is used via a bundler, it will attempt to load Vue as a dependency through the bundler; otherwise, it falls back to a global `Vue` variable.
2218
:::

packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ module.exports = (api, { entry, name, formats }, options) => {
2828
function genConfig (format, postfix = format, genHTML) {
2929
const config = api.resolveChainableWebpackConfig()
3030

31+
const browserslist = require('browserslist')
32+
const targets = browserslist(undefined, { path: fullEntryPath })
33+
const supportsIE = targets.some(agent => agent.includes('ie'))
34+
35+
const webpack = require('webpack')
36+
config.plugin('need-current-script-polyfill')
37+
.use(webpack.DefinePlugin, [{
38+
'process.env.NEED_CURRENTSCRIPT_POLYFILL': JSON.stringify(supportsIE)
39+
}])
40+
3141
// adjust css output name so they write to the same file
3242
if (config.plugins.has('extract-css')) {
3343
config

packages/@vue/cli-service/lib/commands/build/setPublicPath.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// This file is imported into lib/wc client bundles.
22

33
if (typeof window !== 'undefined') {
4+
if (process.env.NEED_CURRENTSCRIPT_POLYFILL) {
5+
require('current-script-polyfill')
6+
}
7+
48
var i
59
if ((i = window.document.currentScript) && (i = i.src.match(/(.+\/)[^/]+\.js(\?.*)?$/))) {
610
__webpack_public_path__ = i[1] // eslint-disable-line

packages/@vue/cli-service/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"acorn-walk": "^6.1.1",
3535
"address": "^1.0.3",
3636
"autoprefixer": "^9.4.8",
37+
"browserslist": "^4.5.4",
3738
"cache-loader": "^2.0.1",
3839
"case-sensitive-paths-webpack-plugin": "^2.2.0",
3940
"chalk": "^2.4.2",
@@ -42,6 +43,7 @@
4243
"copy-webpack-plugin": "^4.6.0",
4344
"css-loader": "^1.0.1",
4445
"cssnano": "^4.1.10",
46+
"current-script-polyfill": "^1.0.0",
4547
"debug": "^4.1.1",
4648
"dotenv": "^6.2.0",
4749
"dotenv-expand": "^4.2.0",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5309,6 +5309,11 @@ csv-parser@^1.6.0:
53095309
minimist "^1.2.0"
53105310
ndjson "^1.4.0"
53115311

5312+
current-script-polyfill@^1.0.0:
5313+
version "1.0.0"
5314+
resolved "https://registry.yarnpkg.com/current-script-polyfill/-/current-script-polyfill-1.0.0.tgz#f31cf7e4f3e218b0726e738ca92a02d3488ef615"
5315+
integrity sha1-8xz35PPiGLBybnOMqSoC00iO9hU=
5316+
53125317
currently-unhandled@^0.4.1:
53135318
version "0.4.1"
53145319
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"

0 commit comments

Comments
 (0)