Skip to content

feat: bundle currentScript polyfill by default if library needs IE support #3760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/guide/build-targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ App is the default build target. In this mode:

## Library

::: tip Note on IE Compatibility
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.
:::

::: tip Note on Vue Dependency
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.
:::
Expand Down
10 changes: 10 additions & 0 deletions packages/@vue/cli-service/lib/commands/build/resolveLibConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ module.exports = (api, { entry, name, formats }, options) => {
function genConfig (format, postfix = format, genHTML) {
const config = api.resolveChainableWebpackConfig()

const browserslist = require('browserslist')
const targets = browserslist(undefined, { path: fullEntryPath })
const supportsIE = targets.some(agent => agent.includes('ie'))

const webpack = require('webpack')
config.plugin('need-current-script-polyfill')
.use(webpack.DefinePlugin, [{
'process.env.NEED_CURRENTSCRIPT_POLYFILL': JSON.stringify(supportsIE)
}])

// adjust css output name so they write to the same file
if (config.plugins.has('extract-css')) {
config
Expand Down
4 changes: 4 additions & 0 deletions packages/@vue/cli-service/lib/commands/build/setPublicPath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// This file is imported into lib/wc client bundles.

if (typeof window !== 'undefined') {
if (process.env.NEED_CURRENTSCRIPT_POLYFILL) {
require('current-script-polyfill')
}

var i
if ((i = window.document.currentScript) && (i = i.src.match(/(.+\/)[^/]+\.js(\?.*)?$/))) {
__webpack_public_path__ = i[1] // eslint-disable-line
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"acorn-walk": "^6.1.1",
"address": "^1.0.3",
"autoprefixer": "^9.4.8",
"browserslist": "^4.5.4",
"cache-loader": "^2.0.1",
"case-sensitive-paths-webpack-plugin": "^2.2.0",
"chalk": "^2.4.2",
Expand All @@ -42,6 +43,7 @@
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1",
"cssnano": "^4.1.10",
"current-script-polyfill": "^1.0.0",
"debug": "^4.1.1",
"dotenv": "^6.2.0",
"dotenv-expand": "^4.2.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5309,6 +5309,11 @@ csv-parser@^1.6.0:
minimist "^1.2.0"
ndjson "^1.4.0"

current-script-polyfill@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/current-script-polyfill/-/current-script-polyfill-1.0.0.tgz#f31cf7e4f3e218b0726e738ca92a02d3488ef615"
integrity sha1-8xz35PPiGLBybnOMqSoC00iO9hU=

currently-unhandled@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
Expand Down