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