我像这样(有条件地)加载了一个脚本,但是它最终出现在捆绑包中:
function loadScript(src) {
if (isNode) return
const xhrObj = new window.XMLHttpRequest()
xhrObj.open('GET', src, false)
xhrObj.send('')
const se = window.document.createElement('script')
se.type = 'text/javascript'
se.text = xhrObj.responseText
window.document.head.appendChild(se)
}
export default function () {
// uses feature detection from polyfilll in quesiton
if (globalSpace.Intl && globalSpace.Intl.DateTimeFormat
&& !globalSpace.Intl._DateTimeFormatTimeZone) { // eslint-disable-line no-underscore-dangle
loadScript('https://unpkg.com/date-time-format-timezone@latest/build/browserified/date-time-format-timezone-complete-min.js')
}
}
我更改了配置,指向绝对URL,因为根据应传递的文档-https://rollupjs.org/guide/en#big-list-of-options。
module.exports = (config) => {
const { input, fileName, name } = config
return {
input: {
input,
external: [
'dayjs',
'https://unpkg.com/date-time-format-timezone@latest/build/browserified/date-time-format-timezone-complete-min.js'
],
plugins: [
babel({
exclude: 'node_modules/**'
}),
uglify()
]
},
output: {
file: fileName,
format: 'umd',
name: name || 'dayjs',
globals: {
dayjs: 'dayjs'
}
}
}
}