jssdk 调整支持多版本共存 (#2151)

* jssdk 产出带版本

* jssdk 产出带版本
This commit is contained in:
liaoxuezhi 2021-06-24 20:59:28 +08:00 committed by GitHub
parent d6a78d109b
commit 5295527fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 42 deletions

View File

@ -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<any>) {
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;
})();

View File

@ -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;

View File

@ -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);
}
});

View File

@ -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}`;
}