From 5295527fdd5980a917a0d75cda3a346c6b505ed5 Mon Sep 17 00:00:00 2001 From: liaoxuezhi Date: Thu, 24 Jun 2021 20:59:28 +0800 Subject: [PATCH] =?UTF-8?q?jssdk=20=E8=B0=83=E6=95=B4=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=89=88=E6=9C=AC=E5=85=B1=E5=AD=98=20(#2151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * jssdk 产出带版本 * jssdk 产出带版本 --- examples/loader.ts | 74 ++++++++++++++++++++------------------- examples/mod.js | 6 ++++ fis-conf.js | 26 +++++++++++--- scripts/embed-packager.js | 3 +- 4 files changed, 67 insertions(+), 42 deletions(-) diff --git a/examples/loader.ts b/examples/loader.ts index 801404977..04eb25d9d 100644 --- a/examples/loader.ts +++ b/examples/loader.ts @@ -1,39 +1,41 @@ -const __moduleId = (str: string) => ''; +// 这个文件编译不会包裹,所以手动包裹一下。 +(function () { + const __moduleId = (str: string) => ''; -const mapping: { - [propName: string]: any; -} = { - 'jquery': __moduleId('jquery'), - 'react': __moduleId('react'), - 'react-dom': __moduleId('react-dom'), - 'immutability-helper': __moduleId('immutability-helper'), - 'react-cropper': __moduleId('react-cropper'), - 'react-dropzone': __moduleId('react-dropzone'), - 'classnames': __moduleId('classnames'), - 'axios': __moduleId('axios'), - 'exceljs': __moduleId('exceljs'), - 'moment': __moduleId('moment'), - 'mobx': __moduleId('mobx'), - 'mobx-state-tree': __moduleId('mobx-state-tree'), - 'react-transition-group': __moduleId('react-transition-group'), - 'papaparse': __moduleId('papaparse'), - 'echarts': __moduleId('echarts'), - 'zrender': __moduleId('zrender'), - 'sortablejs': __moduleId('sortablejs'), - 'amis': __moduleId('../src'), - 'amis/embed': __moduleId('./embed.tsx'), - 'prop-types': __moduleId('prop-types'), - 'async/mapLimit': __moduleId('async/mapLimit'), - 'qs': __moduleId('qs'), - 'path-to-regexp': __moduleId('path-to-regexp'), - 'history': __moduleId('history') -}; + const mapping: { + [propName: string]: any; + } = { + 'jquery': __moduleId('jquery'), + 'react': __moduleId('react'), + 'react-dom': __moduleId('react-dom'), + 'immutability-helper': __moduleId('immutability-helper'), + 'react-cropper': __moduleId('react-cropper'), + 'react-dropzone': __moduleId('react-dropzone'), + 'classnames': __moduleId('classnames'), + 'axios': __moduleId('axios'), + 'exceljs': __moduleId('exceljs'), + 'moment': __moduleId('moment'), + 'mobx': __moduleId('mobx'), + 'mobx-state-tree': __moduleId('mobx-state-tree'), + 'react-transition-group': __moduleId('react-transition-group'), + 'papaparse': __moduleId('papaparse'), + 'echarts': __moduleId('echarts'), + 'zrender': __moduleId('zrender'), + 'sortablejs': __moduleId('sortablejs'), + 'amis': __moduleId('../src'), + 'amis@@version': __moduleId('../src'), + 'amis/embed': __moduleId('./embed.tsx'), + 'amis@@version/embed': __moduleId('./embed.tsx'), + 'prop-types': __moduleId('prop-types'), + 'async/mapLimit': __moduleId('async/mapLimit'), + 'qs': __moduleId('qs'), + 'path-to-regexp': __moduleId('path-to-regexp'), + 'history': __moduleId('history') + }; -function amisRequire(...args: Array) { - let id = args.shift(); - id = Array.isArray(id) ? id.map(id => mapping[id] || id) : mapping[id] || id; - args.unshift(id); - return amis.require.apply(this, args); -} + Object.keys(mapping).forEach(key => { + amis.require.aliasMapping[key] = mapping[key]; + }); -(window as any).amisRequire = amisRequire; + (window as any).amisRequire = amis.require; +})(); diff --git a/examples/mod.js b/examples/mod.js index 3c8ba03dd..70d9c22d5 100644 --- a/examples/mod.js +++ b/examples/mod.js @@ -296,9 +296,15 @@ return script; }; + require.aliasMapping = {}; require.alias = function (id) { + id = require.aliasMapping[id] || id; return id.replace(/\.js$/i, ''); }; + require.exists = function (id) { + id = require.alias(id); + return !!modulesMap[id]; + }; require.timeout = 5000; diff --git a/fis-conf.js b/fis-conf.js index de140242a..8e876d2fe 100644 --- a/fis-conf.js +++ b/fis-conf.js @@ -11,10 +11,25 @@ fis.get('project.ignore').push('public/**', 'npm/**', 'gh-pages/**'); // 配置只编译哪些文件。 const Resource = fis.require('postpackager-loader/lib/resource.js'); +const versionHash = fis.util.md5(package.version); Resource.extend({ buildResourceMap: function () { - return 'amis.' + this.__super(); + const resourceMap = this.__super(); + + const map = JSON.parse(resourceMap.substring(20, resourceMap.length - 2)); + + Object.keys(map.res).forEach(function (key) { + if (map.res[key].pkg) { + map.res[key].pkg = `${versionHash}-${map.res[key].pkg}`; + } + }); + Object.keys(map.pkg).forEach(function (key) { + map.pkg[`${versionHash}-${key}`] = map.pkg[key]; + delete map.pkg[key]; + }); + + return `amis.require.resourceMap(${JSON.stringify(map)});`; }, calculate: function () { @@ -380,9 +395,10 @@ if (fis.project.currentMedia() === 'publish') { fis.on('compile:end', function (file) { if ( file.subpath === '/src/index.tsx' || - file.subpath === '/examples/mod.js' + file.subpath === '/examples/mod.js' || + file.subpath === '/examples/loader.ts' ) { - file.setContent(file.getContent().replace('@version', package.version)); + file.setContent(file.getContent().replace(/@version/g, package.version)); } }); @@ -444,14 +460,14 @@ if (fis.project.currentMedia() === 'publish') { env.match('*.{js,jsx,ts,tsx}', { optimizer: fis.plugin('terser'), moduleId: function (m, path) { - return fis.util.md5('amis-sdk' + path); + return fis.util.md5(package.version + 'amis-sdk' + path); } }); env.match('/src/icons/**.svg', { optimizer: fis.plugin('uglify-js'), moduleId: function (m, path) { - return fis.util.md5('amis-sdk' + path); + return fis.util.md5(package.version + 'amis-sdk' + path); } }); diff --git a/scripts/embed-packager.js b/scripts/embed-packager.js index 0ed651baa..eceb9b0fe 100644 --- a/scripts/embed-packager.js +++ b/scripts/embed-packager.js @@ -119,6 +119,7 @@ module.exports = function (ret, pack, settings, opt) { if (file) { file.skiped = true; let contents = file.getContent(); + if (/_map\.js$/.test(file.subpath)) { contents = `(function() { @@ -130,7 +131,7 @@ module.exports = function (ret, pack, settings, opt) { } amis.host = d; ${contents.replace( - /\"url\"\s*\:\s*('|")(\.\/.*)\1/g, + /\"url\"\s*\:\s*('|")(\.\/.*?)\1/g, function (_, quote, value) { return `"url": d + ${quote}${value.substring(1)}${quote}`; }