diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js index 667f256f55215e806b2ccd5f46a6c00bd60dfc2f..6502daed8d1d200059669f8857466857a7bc63f8 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js @@ -1,5 +1,6 @@ const fs = require('fs') const path = require('path') +const webpack = require('webpack') const { removeExt, normalizePath, @@ -121,8 +122,17 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa if (process.env.UNI_PLATFORM === 'mp-alipay') { beforeCode = ';my.defineComponent || (my.defineComponent = Component);' } - const source = beforeCode + origSource + - ` + const source = beforeCode + origSource + (webpack.version[0] > 4 + ? ` +;(${globalVar}["${jsonpFunction}"] = ${globalVar}["${jsonpFunction}"] || []).push([ + ['${chunkName}'], + {}, + function(__webpack_require__){ + __webpack_require__('${uniModuleId}')['createComponent'](__webpack_require__(${JSON.stringify(moduleId)})) + } +]); +` + : ` ;(${globalVar}["${jsonpFunction}"] = ${globalVar}["${jsonpFunction}"] || []).push([ '${chunkName}', { @@ -133,11 +143,18 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa [['${chunkName}']] ]); ` - const newSource = function () { - return source + ) + if (webpack.version[0] > 4) { + const { RawSource } = webpack.sources + const newSource = new RawSource(source) + compilation.updateAsset(name, newSource) + } else { + const newSource = function () { + return source + } + newSource.__$wrappered = true + assets[name].source = newSource } - newSource.__$wrappered = true - assets[name].source = newSource } } const styleExtname = getPlatformExts().style @@ -186,8 +203,14 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa }) delete sourceObj.componentGenerics const source = JSON.stringify(sourceObj, null, 2) - jsonFile.source = function () { - return source + if (webpack.version[0] > 4) { + const { RawSource } = webpack.sources + const newSource = new RawSource(source) + compilation.updateAsset(name, newSource) + } else { + jsonFile.source = function () { + return source + } } jsonFile.__$oldSource = oldSource } @@ -205,8 +228,14 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa }).join('') }) }) - templateFile.source = function () { - return templateSource + if (webpack.version[0] > 4) { + const { RawSource } = webpack.sources + const newSource = new RawSource(templateSource) + compilation.updateAsset(name, newSource) + } else { + templateFile.source = function () { + return templateSource + } } templateFile.__$oldSource = oldSource } @@ -215,15 +244,22 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa }) } // fix mp-qq https://github.com/dcloudio/uni-app/issues/2648 - const appJsonFile = compilation.assets['app.json'] + const appJsonName = 'app.json' + const appJsonFile = compilation.assets[appJsonName] if (process.env.UNI_PLATFORM === 'mp-qq' && appJsonFile) { const obj = JSON.parse(appJsonFile.source()) if (obj && obj.usingComponents && !Object.keys(obj.usingComponents).length) { const componentName = 'fix-2648' obj.usingComponents[componentName] = `/${componentName}` const source = JSON.stringify(obj, null, 2) - appJsonFile.source = function () { - return source + if (webpack.version[0] > 4) { + const { RawSource } = webpack.sources + const newSource = new RawSource(source) + compilation.updateAsset(appJsonName, newSource) + } else { + appJsonFile.source = function () { + return source + } } const files = [ { @@ -258,8 +294,14 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa const componentName = 'plugin-wrapper' obj.usingComponents[componentName] = `/${componentName}` const source = JSON.stringify(obj, null, 2) - appJsonFile.source = function () { - return source + if (webpack.version[0] > 4) { + const { RawSource } = webpack.sources + const newSource = new RawSource(source) + compilation.updateAsset(appJsonName, newSource) + } else { + appJsonFile.source = function () { + return source + } } const files = [ { diff --git a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js index fdbcb3490df85dfc43f3261f2a12805e5c4e55a2..984597dd0ed61a9b84f03f7da739cb5f0ae123df 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js @@ -1,4 +1,5 @@ const path = require('path') +const webpack = require('webpack') const { md5, @@ -113,7 +114,7 @@ class WebpackUniMPPlugin { source }) => emitFile(file, source, compilation)) - generateComponent(compilation, compiler.options.output.jsonpFunction) + generateComponent(compilation, compiler.options.output[webpack.version[0] > 4 ? 'chunkLoadingGlobal' : 'jsonpFunction']) resolve() })