diff --git a/.eslintignore b/.eslintignore index c548fb5a568e75974bf88f4d2e50acf045456bc9..b478450fa31d28a0229dfd993cff9a20e2ed1a7e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -34,3 +34,4 @@ packages/uni-stat node_modules automator.js uni.automator.js +uni-polyfill.js diff --git a/packages/uni-cli-shared/lib/uni-polyfill.js b/packages/uni-cli-shared/lib/uni-polyfill.js new file mode 100644 index 0000000000000000000000000000000000000000..21fc12d34261ec7ef39b4940f79df6c5043ca621 --- /dev/null +++ b/packages/uni-cli-shared/lib/uni-polyfill.js @@ -0,0 +1,17 @@ +if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { + Promise.prototype.finally = function (callback) { + var promise = this.constructor + return this.then(function (value) { + return promise.resolve(callback()).then(function () { + return value + }) + }, function (reason) { + return promise.resolve(callback()).then(function () { + throw reason + }) + }) + } +} +if (uni.base64ToArrayBuffer) { + ArrayBuffer = uni.base64ToArrayBuffer('').constructor +} diff --git a/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js b/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js index 0f531ac0f29904b3bf5de9aeeeb13f2a7d00de43..432077faf78f9d28bec072a1a5246df0db46427b 100644 --- a/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js +++ b/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js @@ -316,6 +316,7 @@ module.exports = function () { extensions: ['.js', '.nvue', '.vue', '.json'], alias: { '@': process.env.UNI_INPUT_DIR, + 'uni-polyfill': require.resolve('@dcloudio/uni-cli-shared/lib/uni-polyfill.js'), 'uni-pages': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), 'uni-app-style': path.resolve(process.env.UNI_INPUT_DIR, getNVueMainEntry()) + '?' + JSON.stringify({ type: 'appStyle' diff --git a/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js b/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js index e13b03d7a8fac163f9abf73333cf395a01221971..a038a0f920bcf09bc977edfcfc9fbefad692b09c 100644 --- a/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js +++ b/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js @@ -59,22 +59,8 @@ module.exports = function (content, map) { return ` ${statCode} import 'uni-app-style' + import 'uni-polyfill' import App from './${normalizePath(params.page)}.nvue?mpType=page' - if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { - Promise.prototype.finally = function(callback) { - var promise = this.constructor - return this.then(function(value) { - return promise.resolve(callback()).then(function() { - return value - }) - }, function(reason) { - return promise.resolve(callback()).then(function() { - throw reason - }) - }) - } - } - ArrayBuffer = uni.base64ToArrayBuffer('').constructor App.mpType = 'page' App.route = '${params.page}' App.el = '#root' diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js index 6161496f3417720e2597e6ef7b933e7edb9f49c6..1a67f616ca78b337fbcb574e2858b26885e1fd8f 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/define-pages.js @@ -1,3 +1,5 @@ +const fs = require('fs') + function generatePageCode (pages, pageOptions) { return pages.map(pagePath => { if (pageOptions[pagePath].nvue) { @@ -11,24 +13,15 @@ function generateUniConfig (appJson, isAppView) { return isAppView ? `window.__uniConfig = ${JSON.stringify({ window: appJson.window }, null)};` : '' } +function generatePolyfill () { + return fs.readFileSync(require.resolve('@dcloudio/uni-cli-shared/lib/uni-polyfill.js'), { encoding: 'utf8' }) +} + module.exports = function definePages (appJson, isAppView) { return { name: 'define-pages.js', content: ` -if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { - Promise.prototype.finally = function(callback) { - const promise = this.constructor - return this.then( - value => promise.resolve(callback()).then(() => value), - reason => promise.resolve(callback()).then(() => { - throw reason - }) - ) - } -} -if(uni.base64ToArrayBuffer){ - ArrayBuffer = uni.base64ToArrayBuffer('').constructor -} +${generatePolyfill()} ${generateUniConfig(appJson, isAppView)} if(uni.restoreGlobal){ uni.restoreGlobal(weex,plus,setTimeout,clearTimeout,setInterval,clearInterval)