{"_id":"import-remote","_rev":"168-11b899526cf5d5a39c84e0c185c8fc26","name":"import-remote","dist-tags":{"latest":"0.1.1","next":"0.1.1-alpha.2"},"versions":{"0.0.1":{"name":"import-remote","version":"0.0.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"^10.0.2","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"d37cfa22a8fb7841a6c13cab783a1d334e3bff10","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-XrlON2aWV67QUH7iEwbFffwUpxdp8hcsyviE8fXLlHT51loCNG1B8XnZv+hI1l/cZifHewRi1x/rRkjiJJS5eg==","shasum":"0205fa345c15839a1b78c511935d0aeddbe4c773","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.1.tgz","fileCount":16,"unpackedSize":78860,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe1hn4CRA9TVsSAnZWagAAExkP/3cfT+DPx6Y4+PK8nIiJ\nESOIXqhebZiz/mwjgsNPJLQMfJfjYxb9e4O5POUtYBNtwdjx4Kf/c8uXtvvU\n7o94vMVEKc/qwHLTSvqM8Qdt7KWTr6os+Zf5jEnvCI5i2vLNigZHxX164oub\n4i3ZL/KkVTOjDkd/ZSKuBWmZnlUA5jbbyWTX4/O2ZD2mbs/T6NSCa4cUyIHS\n3OHGgfdh5IoNvFG3b0Sm+e3IMBS1v5A8BiOfvoWhhvPXX5l4UxH9er4/JKNz\nRX4TyDApnqZKaKA7HZsAsuM/L4GTG6ZWuCaTSX9boBfHlBxWt+i7QYTdVJBV\nw+CROqnEESymltZtntBG9vukCZRF6RTKGhX+8qdEU1IJLjj/dz4GFUfFvZHe\nEPIEam23b9SvMeWcHnS8EHbPOmOUjSUO13QdvU2QXSV0qSFzF4lVyagTuipw\nM7Eiqfl0VCXTlYTHIzHgjgAHMMuxZezhPuqPc4kpTZ25SOmXEyJdtF2/818D\nKF/S6qbGXs3m+jG4Y9zzw1esO5uXWG4DPsVeMbNufz4ZahhumvYYa14o9EGs\ndZgKV4ekD+iP9BUVaB5b51GK10oz/z9PgVdu4wyorZJLvk9qLm05dYkgXhT4\nmCOSuemQZHnBq1LpsImi9adP1iOgD67lGPvPjZYgO+qOVNSCDKo1Cf40FVAg\nljr5\r\n=5XsB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIC2H7oQ7y4QrQzleNl7fErtHiWifF4i0BeXQlamh9VneAiA1aq1IGwGIRPNeqhWNhQH9rwxgvqJdhSXEzPt3477fPA=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.1_1591089656320_0.0454038267214234"},"_hasShrinkwrap":false},"0.0.2":{"name":"import-remote","version":"0.0.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"^10.0.2","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"07a7d46287aa0e8959bad94320127d4b5585ee69","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-e7kpTxak36RYIp5QxKk4yhMEO27JVsJ/VYPk3WM2Falea+INXlA9JiVYuIM3ZcfwtJm/5kJu4e71Uk6rJ3pSYw==","shasum":"4706310905b1568cbde7d37c90e84b811f0c271e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.2.tgz","fileCount":16,"unpackedSize":79985,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe14SSCRA9TVsSAnZWagAAh9AP/0X7bz9bpo2h8ga8BQl1\nC/KF7DC4e072S/0Jvr+41Rvo4mXB0liICREANEJ0Wlg1YUYOn6SSSqdsZDh0\nRKSajTEWPVNYeBiwjnazNqXCTXUUVBoHVlBK11NHyenfASMnlZoh75RSVUqA\npj3qA6mBfo1/1otLi9Gy3zHiw/gudvYZGPKqS63Qr2Lp/BFTG9nLQTUyafzZ\ny0JLXDkqxQpCYx2Fx2H1gCaerg3CpB82h/UJo6MrMvP1avu9DtVj9G7Ssixo\nBAEDARDQe2eO0N+/ccnNw0ALjJQ24bN7NiGv0OXfsvxfLqv/InMRe9ZJB0jd\nP8Xp/ht5jHOlWoDsOrybEqsV5NE+u3uq6gzKBNKYsX8Nc4EoItP4YBij9Gcr\n9wliRDUQfWT2gQdT9mZfeR+0hwSCPa9oAQ+iwoLCmN5Uh/nAwRwm68Phn8YQ\nPoHg+jVlI7GmQa43gMYuEl1BS1C9LCosMMzEyU2zuooMLQhuvvGA60jJralz\nVL+8RmjH085nIIbTVzOpiQjCMxyMwDtF/81NXQpt02SPEjtFnf8sfW0MKpiw\nG6G6xT4MPSkFbxb6qPuZQ7dfX5S9ONTHDOEf9eNnKM6hLkigfAhz5kzIhIrD\nrvhlGyPBwxb/PN3zHuZ5LHXfKfIp5QY2HSLvYz4fD/qoaQMT7ha7NKUyiyHO\nt07g\r\n=S593\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDEATQ00Ttd0nL/H/956t13p6EUiw+b1BiTqASeTqlDpAIhAIS1/u91HqPkAY5LSXRAu65AWnKvt47Wmc6mpbipCBgQ"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.2_1591182482148_0.8411619093046199"},"_hasShrinkwrap":false},"0.0.3":{"name":"import-remote","version":"0.0.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"4b9502cd9e3f74b268d4bfb96ab714ba24bac76b","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-bbsZ6NsUy2O6Sd71HDl5DlNcXS2F4iEBCfQSh+cVX4xY1JOCtRauVGNToq4jjLF2YQSxz9ybLH3id++hRHkNRw==","shasum":"49693168d3900cfef36736174bccdf68f3d0de72","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.3.tgz","fileCount":18,"unpackedSize":86390,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe27iiCRA9TVsSAnZWagAAgWcP/1GefCeej+sYNVbjviYu\n+u3z52KDaBJcGKjRYSrDJVIlIPEgrtMMGzOdcN/WNWnW5eNxTuNkJPzV6P2V\nKROrT5oqzHrnPbcj4n+jl3OayDPyVTJiL+vVPc9AvI1EXvHRhD7Fb/sN2YAl\nkv2TWNdcsLV7qhUS0b1I9Hmra9jvMKDPcDzG3bEAJV+I1YunH4YSN/x3cJgE\nxRQYzKpOTLfHdGYgwU85Z4MjdZfZwleQf9S1l/v9UueaxAs++I0GlRGa2yhJ\nJi0DB3F3ugDq19hE9C3Y8cFXU9JsaJ7TBpcMrADEe/G/s3rsgOiyzODyM8Of\n9hctSLpqgVZ4+DDWLEJ2pMaUX2cNGDNKnakgFJD4jB9lsvBmDGsOSfNzCIGm\n1pKKDJgsoJbURdEpmOR8mBI0war1Pz91xcfJKuMNljx7+3as/59I2eu/2V8i\njeFP73TZIXTfefAyJAqiG04yUuqtKo14vuUr9JdZG9kgCpdJXi3qs5L+/ppZ\n2JXMUAF6lODRRw155nkaX/wrXe6dJC3hor+kRsn6N2sabQsBUCi4XaYih1I3\naJl2EY946ktOm4K+7OgM0tl+DQ3ZGbopGEyGQi92IxoZSFgE1ei5LOE2PX/N\nu+xaFuRiSUW+830pSNm+1EZcqNlMGhrUYrgM8IAaAdD/SGWP0Ly19hKZXnyC\n27A6\r\n=j5gG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF3njoZmRbb/OY0fe7f5wuumXS1GriSYH9IG6jgz1+bSAiBGx7FkwSKYUZm+U2v6NFvQA2h4QC/gldZ+Au68ikClsg=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.3_1591457953440_0.09257089558970644"},"_hasShrinkwrap":false},"0.0.4-alpha.0":{"name":"import-remote","version":"0.0.4-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ModuleWebpackPlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ModuleWebpackPlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: React, \n    'react-dom': ReactDOM, \n    'lodash': _ \n  }\n});\n\n// or register externals only once\nObject.assign(remote.externals, { React, ReactDOM, _ });\nconst test = await importRemote('http://localhost:3000/test/index.js');\n\n\ntest.dosomething();\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new HtmlWebpackPlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ModuleWebpackPlugin(), // Generates default index.js\n    new ModuleWebpackPlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ModuleWebpackPlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ModuleWebpackPlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ModuleWebpackPlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"fe94a861e963865b17c29ce38ce4c75e6a906faf","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-bD7I/8WranXkxvrQmMc/6mG7DBLZ/3ZlWnVaHoFZtpRrcQpxpAk1fUT40HqT8pxEgjZfDooyZkKSpjfdM8kGdA==","shasum":"d17cab6b22af87b708f5417915eca89285887fc2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.0.tgz","fileCount":18,"unpackedSize":91873,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe3IOqCRA9TVsSAnZWagAAamEP/0HY5cKm1sRxPp0mKK3X\nWKPhxNc2wdmWb41krNrwr2v1uL5UkofqEr2ES5Kah+QKes7PnkKbDh0viGkr\nOG6FQPyXBeC5JEhVqCWdhJVOsiMaipibcQBXicbZ0JAiQEVZPviILR9hPo82\nKfJAzXgHBalvxMANWSDLk/AD2Vw/fy+jcCsTaJy6Firppv2v03IYemd/np4U\nMAv2ScG8APEOb1qGWw//CKY+VQSWXh6DtSn7GwfxvK0UUzIfl7tp6SB32g/b\nc1HYXA4SCRzjH07hNzYonUmpGRpl1zD+BiOz2ZQyk8qq3DOiWKbtgabgZIgj\nDY534UXE/bSnDhoEAQ/VY8q9yZeb6U2Lx8qte1/hrC49O0ZEanZE7Wi73wPk\n+qq5vW7qnUTYDXfbIUSDcTb+r4vkLTlulAWUSKUCpJm5nG+jb5nodOk885yH\nv1kgfOG420/eljheIr/bdLGt+T+usvdntI0AFyMTTUBybHsHxHOh2Vwv0E+O\nja40Sn/xw+wq2L8to2d7G1OrGAHfatSycz6R77jBmmzWaOy+w5ymY5MGQISb\ngzsjfrYIT+U8vhgiwoHHFiXZbwFvkSOfzP1jAJwPqbVI9BtMfJqUq1OT8PRk\nDO7voGnxCFvvE9d+CRHP2QI4YU2kbazS4klswfAINCoyLrIo3TjXWVbVqIzQ\nJHfa\r\n=a1LL\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDLTbzbsS1kFQ83vrhuGtJy0i2XCv6fYN5OLaoVThG41QIgfG5M0vdMChwIza9YPiIQ2bBVLIUxx//HtI/eYvwxF4Y="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.0_1591509929979_0.061283263585754755"},"_hasShrinkwrap":false},"0.0.4-alpha.1":{"name":"import-remote","version":"0.0.4-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: React, \n    'react-dom': ReactDOM, \n    'lodash': _ \n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: React, \n  'react-dom': ReactDOM, \n  'lodash': _ \n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { react: React, 'react-dom': ReactDOM, 'lodash': _ }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"88293decb54bdb8995ab901998091d9172dccc1f","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-JQsfyw/2F2SegNPfdrjtz2qmj/+E5tf/OCutcRd/WaoKyF3fo0LifK3V/3lv99Tz6AL+kyPWiPxDJjeXg24EfA==","shasum":"9b1dd31e1ac5760849b4a049b3fdf8665df34fb5","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.1.tgz","fileCount":18,"unpackedSize":96189,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe3MmYCRA9TVsSAnZWagAADmsP/j+VO20+ziNfLi1Lrfde\nO6WXuyU77STAvgL4UnsbBWTyunenTdK091Ksn1AZqZ0jW5sXDi7P093VYjSV\nU0y2c9DzbN4o8IM0lPjfF5yHazIooKobNaFraYzbVkf5xvbx6QAgPnjwdeHL\nlKE9sGv+PJ/RTHb1Gfl8JscStbI186PRx2pkqjkoFasN4uAGR6GYQoMGTXbJ\nmm7U00Lwc8k4zh7yDBBd6Ff5OQ6lz2KPTbaSW6xK6MWgw0+ZbHyfuKSp8HVd\nbgtN8DwppGPJU1OtFWTMPSDpk1Ve0cfTyU2/hcmcARAag57a6PVaTmrAqdhJ\nxsdAD5nwL2DQKAt63PjZ329IWT00y6Kn4q21d3m6RpwTUGV7V9n1LiuxUqrw\nX74+fNzYzRYhUjifhbWSwIH8bPvyoAjaFabMtJf9t9NV9gyWFkkyFJFKOExo\ngJUGI5VcDGP54j8HouBbEc7Mc+8E1WJDwKVd60wBZQzlwaioRAeWaGt/KcF/\nnjhYD47kfh0YUJMRswy3aS/ETImibxBvxKHoL3NQWNyLgpyrDf45svNpGclC\nGfGFUSCffbiKd9x/55BBllNzEip9LdY1mCmidjgCJskKYtAFJAS0rmc8SaDt\nVVbw19f5M2PFvXRMbGuD83wMATkxv/YvgiNqYVeljPz0CZZ1bJYzlG4cr+Pg\nCtUf\r\n=y4bA\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCJsaEaD0ZG1S7x7x7HxOksF4yv9oI4FjFOWubqDHfIugIgHQOPZbJYjcCuITF6/Vw1wEgYsKl2a5I3dPX+4VUtOpM="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.1_1591527831784_0.659349561260328"},"_hasShrinkwrap":false},"0.0.4-alpha.2":{"name":"import-remote","version":"0.0.4-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: React, \n    'react-dom': ReactDOM, \n    'lodash': _ \n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: React, \n  'react-dom': ReactDOM, \n  'lodash': _ \n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { react: React, 'react-dom': ReactDOM, 'lodash': _ }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"ca5f8449950f017adcacc3fa4edc0cff8c9fc54a","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-fvJCd/glsRyjxLN4h5yWL9WL6DAWLi4o4YjA/OsW/mecDmBf+UyI4enHJzHaL02OQ9LZIP7aSt5WWNjTzafiBA==","shasum":"36755e92ea0572e88a383d8713f450e1092efe4e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.2.tgz","fileCount":18,"unpackedSize":98135,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe3Zg5CRA9TVsSAnZWagAArYQP/RAuRNjSh16me2xa5D9T\n8Ce5wKg6k423LrDXz+qYeEdyCekxJp3edZaG5g19AKryhe37GWZmhhKRpiHo\nUPT9Xm+Mug2CjOhSPhP8uJwhgmk1Q78UPbpjkEJ/9HhbFK7R1dYhwfsdNKv1\nwXMueqEFqz1q2gEydEoUvY2V3WECFd820sxAMHZL/kJSzPSaq/Wx34YieI6y\nrhg6aRkNM1h+i3T/Za1SOpr2jSB847OzfXgTa3htjJXQHdKJ8wJtLUf30Nrp\nD1VAZLLx+rD+vRDCwmzXfsRKskV/Uv9F7jwzTtH9KLv05fC7TYlaJg83uPJk\nE8DCe4j8TvGEmIKHxt7mZauFedSRju5Vk7e0zIXCtoAUCJdmqis+FRd/eh/H\nfnRWLcjFSrFaVjmzvlaUaOBBHenEsUPUVlGfGsquEyWMLsEsPohwR07aQhBV\nxWdeTH+KjrW0bO3Y+s/9Wo2aCx3IvutcUipdknCPZec5d5Adkv04Ndaf/Mbq\niKyRgU6Qnpfrqp5aBHYE+MmFYsIDT0ENwZDcPGiUB+Bvc4TRox1iabZAZxhN\njYv8OQHM9aW1Hs3B2H7J5jwi1UQC7rQCTGRuqbOsv5tFcsFjsB7IkCowKC0M\nl0MD+PGDBnUIGpTd4nGAKYcXEF23orl2wuafBx4S82kvTPTvqYi8QtDTyqus\nwC9s\r\n=isUy\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBlyaDkvzA4ihG9H/rO4du4UqW/upO4BKXcL3aQL+C4SAiAy1hil4KVGt0CnkD8U39Xjb7Ga8YNJBCgkjOJ2v7eSJg=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.2_1591580729093_0.15048080834947575"},"_hasShrinkwrap":false},"0.0.4-alpha.3":{"name":"import-remote","version":"0.0.4-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: React, \n    'react-dom': ReactDOM, \n    'lodash': _ \n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: React, \n  'react-dom': ReactDOM, \n  'lodash': _ \n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { react: React, 'react-dom': ReactDOM, 'lodash': _ }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"52cb5baaa208bb7430d23905dd1d8054f99c7f44","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-+muLA3j1mpi/taTKGC0K367y+VtfOqZFj8V6pJBs6RBuWoClaVEqGkReexwbNQU7eNOwBmJaeGIKxNFyMppLZg==","shasum":"70e59fb21e7f42e550c0d96521f9c5a033d60c39","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.3.tgz","fileCount":18,"unpackedSize":98135,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe4dtPCRA9TVsSAnZWagAAXl8P+wcohcr9nYa14sgaEo0p\nJBs/X6VJ6/+sbZ9Y59g0hNygqSCYrCRqKxvZjxsIP5mihj5ut0eEJ69MPUBq\nbTKdDdZ4e7EFa7bmvQbfEXgBsqZ5hojn1B/4xUTNH6shvc9Tr7AyXuvbXO6f\nMjF6OoXci9WDkPFqDVALDuiXmgIBhqPD0k1DYK7s7sB5QstaCgY5pGayhpxe\n0ALyGW+xvA/B/SJbvDlRJQwZJX14GJk+MxgC8jvK8pBaGhve4zqOK6aSOVpV\nB5Wso2vpG8Bb2tQM4WFq8CbMqrBwlvIV3iyVkRlliO/HKFpShXR9WSEHYh9f\nbDtU6hz+U0tp3H5+0AgHG2JnVneKvOIYUZwNo+Ib3cAuCnTaeMbfnMx7n8mY\nyIJoKqQvPfQFKAfJjtg7h4hMpgWARmApr6FowXIiuRjzdUC384TQ10xVooFu\n40X2dhOudirDpvD3W+Y5CyEcGy6WsOegVq0+jW2Z0GXpG4KWmLx+7YfM4P+I\nbqMf9/2xTFCNqGDIO+GKIZD+vt4ydOTVC5ehjBYHQMVJH3qwL3msHJ1tOvCz\nFoNJWhENxQt54oEQ6VK23qAcrBw5gOPHBgd1afOT9MUoEY1kFmXegoYPVJEY\ncDG/kN9zXLvCUeVu/4S2mcY7qwkynqf1uSMzwx+Kz4sHsTRQLuKwPvdU3y1F\nuE5Q\r\n=WEKW\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCDmkMUPdIgdNY+XDzPVT0SCOg3FA9XcSTlppqliSU3TgIhAI6MU65UtTcMUcnZwOAVGapvzvrF0M9ZCPsoN8yhlINt"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.3_1591860046654_0.0901197296596068"},"_hasShrinkwrap":false},"0.0.4-alpha.4":{"name":"import-remote","version":"0.0.4-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: React, \n    'react-dom': ReactDOM, \n    'lodash': _ \n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: React, \n  'react-dom': ReactDOM, \n  'lodash': _ \n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { react: React, 'react-dom': ReactDOM, 'lodash': _ }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"a76b31e7da9e06c38af5fff2e3f45c44e8ab02f9","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-aXFuOyJ1OoHnleApdRuei0sXODUprqkompa1BFBVIOnmbdrChxr7W0zIFfrlz93trO/Oy9QF0KBrPDSkqOI6Hw==","shasum":"3a446f94768d78162ebde09bf8be3ba43ae4586e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.4.tgz","fileCount":18,"unpackedSize":98853,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe4fUYCRA9TVsSAnZWagAA33MP/jyO/svMzRDukv+/X3mu\nP2i+z9Y7uQsDaZ41fvZalsEu+4CbBTPO5V4sm4RY6ZA4cnLSwEDQwd5B+ee+\nHuER7xM2Q0TzZ/Cs6dRK3aFlb6eTqJM97l8GgL+jsO+5abbsQ5IoBxOqM2b2\nDJ+WroS/0sW+reEoUco4OARWeySQm1hzlkr5NM2tEPJMZIvGwTtFtlxYeHDq\nYiGY2qvNhrZHTKHIfC4zJWJudVgR5zWS3yjGNTlpzvsr8cPGT/nxjkTVxifm\nHQGLjeMZi8P/luc8jbyis6FJrGwJYsFMEDaX9aRCDpC8esiG2gF1iPmeHp4z\nXnZSwst7RpfLLq4PbupolX4HAmrkYIqDGrirF3snwJsmIkKXoyrA+hQjUfkO\nyTuBuQ8uREyfTNw+AROhXijeADLn8fGrtV7KgNsiZQq31Q4ee92ET4nlYGrL\n3EElNtvFuPUM/AfMLrBYtoV+gwACqEvRM1Uyn+FMZagGsAn0ZjSqB9GazrH8\ndEW+vaSLkJlbtGEp7DXEDxkuBM6Yiro+Nhre3D+uJ4tcnO7Vn6ZKLQGeb1/i\nJfV4HUpY+5MOWiMbOQv6pOuhSvpdzaHQzfgnTMdnS546OzaDKs+vLsRaEyI3\n/lHzXNLxzB/o40Z8wL58DCCSBJje8A5Qbe4zLBVxmpZAWyXzq0Y9k9/KDd/j\nGw8k\r\n=uUXw\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDuyhz5TvIw4zbmWYEwFYFxdENL768E7gzMTOY9PdZViQIhAIsBatpmt/mlx+iOPoYYedVoa2tjRe4+RiVF4eRgW385"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.4_1591866647869_0.428392856472551"},"_hasShrinkwrap":false},"0.0.4-alpha.5":{"name":"import-remote","version":"0.0.4-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"8e50c79902920049a85cd8598957af744f200f88","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-E8uwsykqNrrK4RDbMNMT0z23Y2fOTyd9Yvfg9w/+QFv5diFI/Xfew5Mrb9Kr6yMRQqJAgvC5BH/IBidspCS/vA==","shasum":"7748420ed99b832918c7428b47fcb43e9f50d900","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4-alpha.5.tgz","fileCount":20,"unpackedSize":112475,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe5etKCRA9TVsSAnZWagAARPQP/2Y+qAkuHBXhYUMqNR/0\nVkNBn1uTqbmL0FAxFEs8g6AXjuQvLvIu480Kn6DVDXM4Rl2+xCzrzdQhRrZz\npZ8ij+U7zvl/A0TXlK0lKPOjyHgxK3Ol1beEb4+6olXCs9sxQPOgeJd/KvUz\nTImC0ge0eZblubZN5I07x+IXjxkenZPdW5iMbiw733VFTx7/UN1xlntuhEzO\nYKjDmYYpJugCscw3+TSOj8pZ5ESwRtaW4KuH88P1DLDkdNYg+sqPc8O8EIl5\nWdDFPh/W3KVQv0zD0MSIM7EaF7srimnffQeDrhY+cjTFIDW34UOtzZEHp27L\nBPNVtbst8cLuiTh542nm8QBlcjh5ONxpGJAArK/ulKbHCmWffh97hP1Px0xC\nI8z9BkYsfih7je41zrmhsvmURCOO2s5l6Q5BJRdSKa8FtNOApJnlXmIifKg7\nv5QI9oqPDwurUz52UiOS8cp24OgpIQLT9CN2eQby6kbGEDP8QiYr77iq+noB\n83eYIWy5KLTIuJHKF0cSKW2jSgf+3aICou44GzylF+n15yi/yaSMPmlZppp/\nRysv6iXoGRNa60hQhvQEdaXQcxTtVtLKcMZjiz3Wrc15bAl+DUW9irz4SOMA\nSqIQYag/gxixONoh54Cno42FEDRDCJjiCyDO6jQJQSutE4QnEViSpohHe+RZ\njDNj\r\n=ksil\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD9fqRhkMujnSBlizxtj0AlInCpfSR/lxVpJJ93e5jHqAIgAs0qSkVkNDQW0Tn43QKrsp1YHKH5B5qCAQhsM67I+gQ="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4-alpha.5_1592126282192_0.4994973352230476"},"_hasShrinkwrap":false},"0.0.4":{"name":"import-remote","version":"0.0.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"ca03102034940aa02a5a09bd0dddb2f543b62da8","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9DtCVBZMfvlK/lTTzp71x267ZSVC9BAPWS++7r2a0B4ZT42zf42D+pmROEvisKe5l4rD1OoiSrRAuFT1VN26wA==","shasum":"c568f126a7b106fd56e68f87227addd0e3749872","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.4.tgz","fileCount":20,"unpackedSize":118696,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe6bGRCRA9TVsSAnZWagAA74UP/jqXu13wCvDNk/iAMB4q\nFvkGbsSvuIzTmKYcyoUMueVt9znWRKEMu+3GrSWvnk9CEu+ZdhgO36Y6U2xN\nTaB4VOxE/C6AK+1kAIyqSAD1GdnkqnlAdDqZ7Dwbfhe968mzA49I0SMWSQ6M\nN8qSWuRnZAmLLraGeBuvnKZ+j1rGKn04TTp+5ORtTH5lsAHhfT2ojq/8z+Pl\n6QB9VFf3NWtLhvHlNJ4JBVg4T0B1TqdfYdCzK3efRaQIV4RArkfQ6ygo2nyy\nq4hpEIMs0AM0AZ092LN/WUg3EaldHmwrkskejR0uRRr0gHdBPbb7Mkzeau0F\nnehQKxKh1ErYy2NxUwf1XsPnLyjnN8LdFZjqP6C8wK5t/fFISnmKkdlPlptg\nfupqWXDMFdPG/QeIYJFgMubxNyzGtoQ2BilWogpozmfM4g6qhUbdX59oOmvs\njgTc7eDFj39Dq7kYtTd590alq0NMBZQsYAja2/RjXd9ACSQUzVh4kYV9KVF6\nL/7rh2wn4jQlDrRhYDjuWd/QD0tS3AnQcLVczJ7iOGx3jNydILEkA7LSGi6A\nwT94UuNScaeBkf32Hhhovb30+dzCG2E5ZA4x7lBLWP0V94RRYAkCJbA8cm0u\nevPMaZHgduHovvKVcfIrP73fCWkLFQzazPz+9Fu/nAcd3K6vp3YaQul7Rhdl\nxsVS\r\n=xwLn\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCXm46D/LLeY21vPdyDOCWP9rywyNLsOgmNgzOr4OFhbQIgLI/f9y+j42/86o8Wtbx6t+D72wTpPAgU6ZSeOrIWumU="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.4_1592373649308_0.9165065668731653"},"_hasShrinkwrap":false},"0.0.5":{"name":"import-remote","version":"0.0.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"63debaaa04e3e116d325ca3d758e6e1be9091476","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-K7RtRDXqIznj8cdYWNm8+MAcQeY5fkWquhrDzM1OJ91w4xHG2biA+3CwdhZp8DCKMzidQT5FKQ8QBBXDLArLfA==","shasum":"d294399861f7689793941286026d077f72dd79bf","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.5.tgz","fileCount":20,"unpackedSize":118696,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe7Hw+CRA9TVsSAnZWagAAr88QAIYBL35ZuhDqkbqQWzEj\n7KKHa/PXvfQ3tmogzmYJdqTNu9X1WO5Cizx52u8rSodXjKwsziwJfRnGms2j\n6wbJj8/49dsc2OjQf5bWNI6/cNtSWvF1nT4L7wFF6RII5yhfEXuTuVK9HmD9\nVAtFqOlYmo5vzn9DwYxHEFZIcgNXDNIvFWxg1js2Ld4yKlrmf7f9w+H+ZyA7\nd8aEAHX60iOPyneu13yHFhzQUKpRXD5UGb0g7eJ/X6qTVZCqSLtXb2jBZAzg\nZyUxuQ7I3CDTDC+l1CXl90Lmu603Y2guduLlYSM3hWfTxi9MpN37diwdSqpJ\nwA1CMeHZJaUm4Luvjc0q5VzoEZ1M+IljOdZUjGPbEw6oTr77dUJ/fHTNyP6a\nd2GDxsH4VorY5DPJ/0/XT6GiqFRoC1wPoYJKnQSBGaHoTdrR0BvHlaMy2TUV\ndWqQQUwQ6OMV4x9BGCIQeMG+c+5R0/qmUWLZrcnozABgOs8VlIzo2qxhPT4T\ngDPORjxqGVvcNihHHUjunywXxzocTFlH75MCteHghD/kaAZnugwjzo117xXl\nT8yPSBbKysvGacWujh6HYW/2LoXBvc+VShbgQCvdGtBr4AHtJrrg+KvZAvNC\njxRPnoiUGVP1XsEjeAf24IjnEhFIjYTfPfP7XrX3ahQ1/nXZOwx7I0LrmDS/\nOsjz\r\n=Q9z7\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCbhENCs9Df8RmSQarxIxu0GYOKED23W3fGz2xsltIfXgIhAPhTflGswyQ0hME7+tO/HhUyTxzYUE40YTr/uSMSBT//"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.5_1592556605901_0.1923225118157068"},"_hasShrinkwrap":false},"0.0.6":{"name":"import-remote","version":"0.0.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"94f33d1ebdf763f38d6cd06fea19414c5b36d45b","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-pvYOf8Bt9AY4MP4Tb+fFYu/xUaNFgCctRq26ovlHH1v7QqTj4Jpxt/87rMR3aeudNFnqjuQBgMyo1+4upVag9g==","shasum":"680962e08a6ba72789637dce6a938cfc603b71a5","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.6.tgz","fileCount":20,"unpackedSize":118696,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe7ZVCCRA9TVsSAnZWagAAIiYP/19XnWfCE2yY9hdbatbf\nQLT7Z8mie/gSqxZUrbJRL2RotfnFJTBpHFUsalDze7YW2UTGamz7fiqnLna7\ngX+fpAByf8uTiLcAe9IUGTsslW2j+iEyhfD8OUv1wGw8IySfuYqmd+K8RFt7\nltFJUSw90zxogNcZFKHhlrrK0a1eic8yoDLKixuj6ONLiiHlgRbsA84VG5+m\nB9j8wc1iAr9i6SWOT8TeYrGHatWJJ6B+O3MdqeEYDzhy6DtPtxeGOUmuS3OY\nOvExb4Ivx2FDySxekvqX7EXBYkidqo8iLvSg3I8MHZcNkqlvvDB8sO4MPOkf\nCDdwJzncT+Ane0pVJn0Bxq9YkrAG7qRVRLP9m7DLUQO9guXtdfXQQ3BrTiG6\nLNaYIWrwWT34X6nV2iqLbwvqTz5Uj1nbHcPk0FioYAXda/QDVQjFzOMRgH1r\n0VpvVDbDWpXYz56uG/8FfYRL+zdfV1hvvkT7gkpq5SVjQCLhaEf6AazWDdmX\nBo0MYo92+8PGbwPddHGOR2vUrvDXcuYIlb4FEbmt09cK5REv3xKvRJPaxsnV\nOoGcoyXQrVphgLPh2yiL8FuQWeTKeKoIY5cLw9L9VNBFsMusU82our06cz0M\nHmFJRvMPBGOGDiGKF9TuVF8kYiWwmMa2MUoKJ0wtS7HIu/1gvmwrJIypDqgJ\n1sSQ\r\n=rVBs\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCsy/wBrRZhQaWHBtWSA8rWDizD56nPJTs+oAxERz7cMAIhAMSZtZuOpdsP4ZUMajub0dCexgxtJRXqnA9Ckv2W972r"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.6_1592628545620_0.617885944847389"},"_hasShrinkwrap":false},"0.0.7-alpha.0":{"name":"import-remote","version":"0.0.7-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/runtime":"^7.9.2","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"94f33d1ebdf763f38d6cd06fea19414c5b36d45b","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-UayZnPRUJbQvFjLAIZnF+qPiGeH/7ouWO35z/DzMGP8rzBY047zIHTtoAlDFoSqfN4oVEB28zh+dhdRg+KKVKg==","shasum":"438dd5f3e9eab51d3966746e3a7180d06c0d0ef7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.0.tgz","fileCount":20,"unpackedSize":118704,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe7ZXnCRA9TVsSAnZWagAASPYP/AosIAKAmVcX9KiHwyQ5\nYwTmmIMyTgztoKIAjnuL9r/aq1lEDXYiwOORq2fsSvp/JH6uJ8eV9TiQV76h\nnSGECO+33gMALxTAo1vhG1AUga5xsSK8IYpbMbbzveCb/UNTysL/TOcWskkL\n+81RICYPVA/XaW3HBLZxYgUQ0nIypGiBbf3c8TvH754fpfgJv0nbzbTyp04Z\noNCxSPgwtzYFAgwOlJs+m3Pa6QWr8lsfgm96cdzwXwhA29sXexewdZK9yh3X\nu9WHp1O7dIIY0nLy32d531ekT5nhLEZuTtlFG4bXkYp7zZQGWfGZmeE1HwHI\nRJHXIwsh1qhYhAzJ6+lasxjw7Fg96qv5EcIUkED4qsDqw1xydrrwavTWA4Yi\nWabjbpPTpoPx3+tTCiJIWLN3K6H4qf0LkhSSJq4XYXnXS/7GnBkeo9lF5WgU\nE2EGq22jth06AO68gHWENZ0G6fZwSYSamiimjD7bnsLUq+3Hzmw0gBPBjMrr\nlIR+89novAfxkkz8jaJpMyjq3qT+2VLENhe6bD7dPORjJ/5g4Ds6BL3Vu4jT\nbC6soWnagjw/PROKLdUBOMeCO4UcRBWgpUXJxizAFrf5iqIqFFbkd0L+Bc4G\nONSmEfmVNm5XQqYSUt9y3jzTlwDtYAoiDiQVFWYSnpymY5M3fxfS+zOw56Ij\n7MOm\r\n=0YgR\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDFY4xNWhWaJ+zM1VSOKpTszMGz8+7ooSDFEbmAKdxLDAIhALXsWDVziL9XeHh41rPecdMWSrW4wPSizmW/tHJ1n1sd"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.0_1592628711511_0.21965351161046742"},"_hasShrinkwrap":false},"0.0.7-alpha.1":{"name":"import-remote","version":"0.0.7-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"6a930b4b5f557728da7a164240600a0c3986f9f0","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-ucq8BdJ/LqLa8iCfdY1HngxXJr4NGQ86DWkmMZpo5JiHaOTm80rikjtB0iTQxbWhBRLyZ7jIyOKPDZHncG3nNw==","shasum":"5b2cdc59d010cd0158e2b1c4059757c48f8aea6f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.1.tgz","fileCount":20,"unpackedSize":144108,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe+AzuCRA9TVsSAnZWagAAPe0P/1g4Dvg4jkcurok86S0F\njDgT4JuRm6M12CiNDGEQ+M+f3koSAd6BL/+GmWzxyHeuIl64nlsum4kf7ueR\n0Cs48/1bMLKrnotllVcbKItUaZNG77hGEFlT6pD7QDX66XOVOXHZCsrJCZnF\nsxglYWdnH4sOjloUQuC+Wjxl/0/YrYZenOHSh7mTwitxjj38vJNNJr6YnXhI\nzbmzOQGsIbKTzf8r14CQR0A9RbRBAET+TXceChK5paHNXIDfIvN67XL223K6\nTRTEDxMQVCXeWWlM2sepZv0+w1n2C/FujgrGumapgXJB/KADteMkkP/03Ibe\n+FoaygTW8fMFwESuNAe79g+I4S0Ea+KLKJjEyeqlR5i45bhODU2bheWDI5mo\n8eRezIACON9XgJxtkBvh1QgX1kYWy+RlSP35WLAU6LH191VvZ7zXa94uEaS4\nPRA0iTNn2kHJ0Fd5f9qgHkUOLIpbATTSquCKHsONr1abpHDf7DyBjiDv7lcp\nHilvWWA5rTOEwuNwDOwSrFnoTM+3vnTNhaSvjFVHaYGConQpqKJfkvOPbw0B\nQEqS0sdKqdOk/l50QtUSUPfeCIzpl/kEFZHrVq5may/UEwPL+7Uac6UD/RWe\n72jK49tEUf7eC2WLpeRUbVvLZH+ho5avQoskPzodTrGPo+0O9SbSBy/9AcV7\nXw35\r\n=UBgT\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDPOS4fT0bxjd/KdJgIUHF/fxsTBH1kGQfsiHbjq3DWNAiAv0W4kRtHkt/5MdCeMKWGQ3/pn1uqI10sh+9o/N1ZPQQ=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.1_1593314541943_0.4362540480209218"},"_hasShrinkwrap":false},"0.0.7-alpha.2":{"name":"import-remote","version":"0.0.7-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-esm":"babel src -d esm","build":"npm run build-esm","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"d3d3037f87aea088064f3035cd3482ac63ede8ab","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-k6mcGVwvn91iMLKVP+icWM4Jj8pekQz+2QG7C87QNi2pWMe6XxkRRs2SrxFBQWurVSJ9ZM0ByfTRFajFByF7Tg==","shasum":"92713a63e94424d1ff5defcc2e016319f42ec808","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.2.tgz","fileCount":20,"unpackedSize":144054,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe+C/NCRA9TVsSAnZWagAAZH8P/A7QS/yUUwGZDgQLzPau\nElGbIUKd6VyjZMv6LTdlMhBGZJTyjGQNcNQ6idBlSlCvJYRLNiMWREt1gCdS\nSPCltCphePiDzg9+fiCyo3nsXvNnxUKKpewr12LwDwNCJr7GXDwSoPAGCHr4\n9am1lXGsYbkXn/vnUMP6z0wpyD9bUFdhTJZ1Qkkjb47wxzIREdH6X8WegCjC\nwMPO04Y5CexTR2+z3NjmMeT7XhHVs/juk+RYcGvOan9yDVxOJVsGa5xgkWmO\nP7PRu7Eklv9uHtZ/BWRiz85XoOdAdVAu6Zy4bhssA1pIxUDGVHFV7PP5qCpb\nJYCXT41tPQ9g96EtuAcVlkSE1jY4tfCORUBv5E6OgFet0lgQecKsVnezPisF\n2gCopcmRPB/6wYXrPYPILXIek3M9GiEtq1AxrgLPsQHSgYsCRyzInhux5M8k\nEyHXQ6EOuifhjDxDiIB91WC91XZLGOkGYfk51KbF6P37lCcUIdLSk4SJ92f8\nkU4wDnG5hLuRSECt8xJyevDyIm3gte6r6tA2S1yKj35hXIIE++372hcc4jpS\niSKpNmaZzLzDLCY/1/920NZkLUNzM3KlmTAeEeZd3xA3pvt+m0gZ9IldzW/I\nSCv3CD6MHmi4I8nE9xFzk2tdvnhwQLfH6dtYlSu4KLFhEVHq7MpLR1WBclGC\nA8sp\r\n=qD6c\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICgGsj9WC5xN1HGWCFO2aKrPqvhHLGWnmbg9Ja+IPTP+AiB2loe52IA/I8jGHS01U1fSnTwN8olSwNohmc3OGX+59g=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.2_1593323469512_0.8062299321475466"},"_hasShrinkwrap":false},"0.0.7-alpha.3":{"name":"import-remote","version":"0.0.7-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"7bd47de86d3a8276c470eac138d581fad51cf35c","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-DwRDLP4+pUTMgc7TvLoky7QS2fS89wYbG0RI3SdJVJSG8vsndwF1jUgJ4iUVlSZOhXbP+ObcvIPP/hrydpyNOw==","shasum":"6e5b5014526de977a9881bd69fb1fcd9860c9bbd","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.3.tgz","fileCount":20,"unpackedSize":145789,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe+YXsCRA9TVsSAnZWagAAt5AP/20uVXAyNgD5ZkpbTjtf\nafwQDBcr0YMhWnEkFZd3ectdS1bZNdbDU4Tp8V88Y0iScQzIKU0tI62dmnQJ\nsZS4idMm1FQysDvIVhUVnlVpLO2IskacottZf7XWLwc5NtQltypxlSFfge41\nKEwAKPfzEijl/V8+Gc9RxNAf8A4ZGJdeVulDevWY5cl1ayhHzcUwnqNpVZag\nR4iDUJWA2K2VEIwtRt4pvdgf4Usgt94a42Fjb/kYtLgahmLKMGCBHYL/UcA8\nnxwzGFYjXXRzxdwO3mgv+RtI9CVjYvGp9laRyzlFEVnoOiEVkXE/f0Qfr4xW\nl4FvLJXiiyqhVJwoop3bgeQ17aRts22Cp4ci6fqOHDhPii5ByRmWva5J8vQp\nChdW8rca2fZDWnX0qJLBzB1D8mCQlw9wFlp84Umbo5kjQeGSCD1N4zzC/smg\nFypJjPkKsT7895JHjqs3wxWwlpGNcpGj7OcWvyv1rzGNqgIAojSG4RZWDWM7\nWBP3wIjJUL71EAxc5rdt8nPBoMYuMKfwQZvN4jJE9UZXhhwSuoW9NRAtP3tW\nWQ01R4D7zPy0C39+Ff1cuwFnxc8hr38LBWyRvcr3gGFQIcCdz1GjYInDBFKt\nOkBewGMq19D2idB7OcUpKw2cn7gE/oGjPvKaesAuUHzGFa0cBbMGRzrtaLTv\naY+i\r\n=jKnh\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDIZeXg67RJh+yho+qt830cyjngBNLDBO4FV5vlsA0UMQIhALFens368a16vZmTF9M1W+EmaOucys6RlkyIyKhg1mwH"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.3_1593411051849_0.8521293158787178"},"_hasShrinkwrap":false},"0.0.7-alpha.4":{"name":"import-remote","version":"0.0.7-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"e9ead83be405bab29f4d44c708c0ba7935974844","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9bF8WIwPlwSdi2su0v0ptFSsUJV7DFq0pyaWYmRTwsR7WZhkX2c1Z+y2iJJSa+eleHRKv/X//ALIzE877c9FIg==","shasum":"92434682493864499e65c6c52c7051656982bc2d","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.4.tgz","fileCount":20,"unpackedSize":145879,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe/TRcCRA9TVsSAnZWagAA2y4QAIdNRSM0HP2jtH3HLxIJ\ner7Byyy7f4SEpi19YSE4QBNr8Bey+rqIzu4W4pxvUBW0tT1/x5O2bvIc5pQs\nUOiQXu1gNMXcOb7PMjpd3bc8Przs6UVOG4IYZQRcdmLANbNo8Oiefyv9xBYC\nR29ofZIGi835ALFKtHAAb/RO9QbWNf6rbfO0a+Yxt4ZLeJ/3NS46SfkMMRtl\nvLa397jWyGSMGY8ZRuYSVIiWdhaiOQZ+is1W7ieki/yKRfLLASndt+BCan49\n8zZFMDKQGtwbk+x9GE2BR+lqYmMk3MwPZb6TtPLlSAtOW/E4bEL61A8SyW8B\nxwOPGVVPYFE5Fge++g+M3FRHevkjTUBi4HB9Q0IAM5TT49QU2XwcaKDgOjg/\nOoNjMbavEil7USaqu7FgcWzFWrLZEgD6VdQz4iYZAELLyoKojiNumlRvdnQs\nGQhdDrZqS+ExaeMzqa4cTJ4Wt27wqpXvsXt7xT3BmtuRrXXEU0qdFnbKxoZ5\ni3UDjt//wKznHwv27QhgTXn+elVFScsVHjwwlu2V53pQf+gazsIPHjHweG1X\nxLY2lIFsSyh6vwi2dttOfoO43wogmK2vJTZmmV+Oggo+AsLnqt2RHzcaDT1z\n5dmYpxf3y1zsVCpTNueax1Ynv4vF3Jdk13uu8wLdgoyhUF7YQlKN6us8elE4\nqcTq\r\n=KOSo\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGAZh0jbW6iYvEDDxADa5iGvKWbI9PlzLP7zeu30H1/TAiBTsiHv0TAAH12lkyUxAhxVL7z+q4F5CnhRJ2WvVKtxFA=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.4_1593652316440_0.08912665054407953"},"_hasShrinkwrap":false},"0.0.7-alpha.5":{"name":"import-remote","version":"0.0.7-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"f09fe9601398bdd302fe994342545e9ce5f8e329","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-g9VT9XIjnYHVap5cGIiBUy1fxaHn94ky8pQjSNihlPjdPUJcf4zq+DfeHE2YD+VWPEANb89BuZrhYV6q5EAzHg==","shasum":"3fd9adb56748dcca070f218684c4f1d5488eab3e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7-alpha.5.tgz","fileCount":20,"unpackedSize":145826,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJe/of+CRA9TVsSAnZWagAAqIwP/3bzQf150JjKEX1uTmU/\n4ncXgbhNBdr3kIWC/wFsmk8jWwaZq2JT/gfZIfM9nTgWial1cx+DCtVNsX9E\ngepSZpgfeJKJnyyQ5pFjhHqDvC5JyeiiFyyx9a4YzFSy97bM+/MQkKDxRr/H\n7pGf9/zkcBdaTROrlTgM+w5j4bLv2oDgX/nUENmTIb8RL7uD+DctW+5IHZik\nHU6WcHAYzxaA/B5jC/38Z90A1nWFzZRrzZLqi+VD40yi5qsmOH83MXEr9QgU\nEs3BgXnIsSjll6VPW+09SenZ4Bn6guY/ExZb1IXGrtLsbHC8XYKKtM4H2FUs\n4UMMhVLaIr6/gKehsJCySXrekPD5lmCL1Vrc6CJf461cFk2sbUOQs3zpFQtI\n34A5jIh4vjlOXTo73q8iSzleQpfdtDxSOe0eyHe9G2iUlJK+deqWyaVjd9TW\n6R7r26OnFL39A8G3igFgl1jkneb7Y8uunfvjmOauvge82PIUmIGt6+7pNFx/\nScuPyyXiPIPBdTZhr3g/7HfaWOCYdwrVjO1UE+yA8R4cqLS3x0Rnv1KzkF7k\neM/TS3kTmL6Ds6FuUMYLGarckwoLDeBbpUPr4jh57IR/fBa+DszQwg6XHVuH\n45APinNJUsw2/7q0kbTSUosT6yjK891BvurQv+lAEi4x7v3v5w1fcpYY1s94\nglVH\r\n=CHAv\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIETZ1XFO8xVjFWerVQ2P+MM0VS0ebHnM8udC7QsElChoAiEAjmMZS5Drurv7f6yC3y5rNjOUCBKNF/Xyk6DwWQNiW14="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7-alpha.5_1593739261432_0.9272356843498526"},"_hasShrinkwrap":false},"0.0.7":{"name":"import-remote","version":"0.0.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"8790e3a4856d56b86228715de057230524730a32","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-ypx2chPE5CUp1w9P95he+pAOX8Mxob+MBrLSh13in8scETMinV6MDgaeB+99xBXLu9OPxrqQLfHU3g7rkfudWg==","shasum":"a794b8c3cca09b050bcdfabbe618f30d7bb5ccd6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.7.tgz","fileCount":20,"unpackedSize":145818,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfDBOPCRA9TVsSAnZWagAA/VUP/1rahAD9U/Mxlv2ZRZ1m\nnX5O/Jk6Ev8EsNAr1ha0+FdL4/QhKG45ntczQwLbtUCJp/29uXjxJ7Qt9XLr\nMH5a1DFYdOM6y9HeMA2rkte4i7EMNIpBt4R0slJQIuXF/eKxl3W0A1gDGKoY\nnekUDWASHW2uU2yIlbrVey/coWKdjwD1Hp4yUvlY31cRE3XF74WzQjHUwuJy\nNhxUUsrnRfEsGJlLZ6yCEt/6KF/SE9K4gG7ac4bCzcGjNRtx8w6GlpR+v0Jo\n5Xv1eOpu+x5dQbKeWkMkqfQ0+lf+BrO8YxVcV0s4xCiyNIxfYC6q1xPFnUmL\nlg7b6Cs4yQXpTOyl+oISYkIfwSDdgSFqaNLGXzB7ogWhWJz03LLongLbZJ6h\n5DAcOOu6hBefRLD1rXJAUdhC1gS6dqw2nCOwsRiYBA1aPDKj+BFTpeGXhhRo\n1Fddi2y2MJDQBT/IXTN4Pid38cH8qsygi80QK3f4ZPhhZg6DKie/aplaotQk\nw/wKxZgMk/jVJnhwUkJo1Dq9lTShKx+i+2J9UDYgoItWAmoUn6hiodW5FOTr\nS55GQVqChr/+Zy9OIus4IMY+NBL54txN09X4jnI2L3PvHA52dkEi8+mEE/8w\nxtX6hPsrWDXVGfXe5bxn05hKqOn3C27BxEu/Wgoskv+PUAhac0NnrHCji79L\nndlo\r\n=tx14\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAPQw4eEcSixwLrgZdZUfbOU96WsTsHGfDcFMuTiNqHeAiEAsbwrcEiP3ISzgFtMXEJsAoAHWSvT9ZP7AecewZtsP/c="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.7_1594626959018_0.08083203583778165"},"_hasShrinkwrap":false},"0.0.8-alpha.0":{"name":"import-remote","version":"0.0.8-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"e61e875ba0b19ea5ccfce41e5a908cc7a2993d7d","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.8-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-xtOyPotFBU80hkGa85JMWPPkzhV+3B3yw5MKhxHMCn1rwFtuZLSASDzmxS6GNf6JejKRZL+MZh28X92p5mzNBQ==","shasum":"a142cdbaf6d115cc0bd560e726b71d605d984123","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.8-alpha.0.tgz","fileCount":20,"unpackedSize":148065,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfEQwiCRA9TVsSAnZWagAA7JwP/2qUX5Bby/cyWGknLc+X\nSPQime/RoDAqUkfpkJgFM3i1n7GW17CivYQWn8YOF9110X18OY17kog1UnG+\nziOE8O4ArYHO+033rWPZHFbpSCvfy8pvrc/ioDTz2R6Ka1qg1x5A5ORB4k0Q\nBLMr87SPSA3IEGueyF6Ffte8KYG/JdIV4kaOGi5+UsgCK5b7w4k9AloJtGwK\nJx17Ic9/LPXKnxWvfajR6/iR1yMNpyTsNGtsS4/XN/96SVjLf6fNGIF1Wrsr\nFKEJt49jIja9wMpmqmS5BajvHkpK+a3Vj2gyJVgmnbDYYJIrI7Kjab5aWl30\nEuWi3y7L8JozPy0+EEtvS5pUlBTQX780xzC5kEXLrYQSsFQ3lPs063nFoOxw\nOGLKm3BsgJWnLzhFOZ5Oz7bw8jUMvwntbKFpF61KNStLXGmAKdE/P6P7GCQf\nRmNmdhzVfKGyDDXyt3iAmeTL1BRtbJ1+u5oBHOSDQ213DBESbSrungeFCTHJ\nPAYzc4qvoisMkToDep/0RsnmbdBMTWG7P5bUQjc45Uya/Zg43HGXS+JYB247\nwyb70T9a7PeQPYsYbELT73Y6rurUfuor/CpQw/Op+f9P0lle7X6dPITRUcTJ\nwksbcPzYXdwlTaU3H81hMGccLkBJERkyC1PsCO3CcaFYYO3ElVLVsz01uIub\nAIBf\r\n=I9UQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIA1XyHwsy/Vl6SsipZQvpPNjZV93tQ1mlLHpl7h1yLdiAiA0e7gFuiAFB1N78uJdvdl1EFOTUHaYTKWumAqvDD4Wqw=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.8-alpha.0_1594952737837_0.6822496598825383"},"_hasShrinkwrap":false},"0.0.8-alpha.1":{"name":"import-remote","version":"0.0.8-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"cbd7bb250f9291350039cc04b368c9efd89360b7","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.8-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-IDSkklddkm/rfApSHiaYP9Cu9wKR/1Lrq1B1PIatvUjJFU0RuPfoXN9mIEv5MEzD60LNc6rW2I0/nqwQJWEHWg==","shasum":"8cde7c0cb13530e848b00d10598efeaa73905e3f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.8-alpha.1.tgz","fileCount":20,"unpackedSize":148146,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfGW0ICRA9TVsSAnZWagAAC6cQAIHHkV+lC80udCBY8sWQ\nIW3iPv8iCxhkmLM+6YZRe8CIf0f6KxV2utRmaDYllVnjgijxuWdEkMFTBUg3\nrJBr7LVv4POkk1iZ/nqqHY99A7Z5uakWaDSwAFn9WZcBMRXEnKtwhOTePx03\nXI1hcethEgPsVoxN0+9oQhce63sQnlsUAsDAT80LByhEmJh5tBV+sOSKSWqz\nUO8h3K1AN8xEA17CZAIfXEEPGJVtHl0QmD9ZAUAJfe5aR5s/4odcCrfSFdDE\n30X8S6K0j44e+J0Mny+3fqvltgQP3s24IjbPFZRb667dz2T1f2KwDXl36xKx\nyDMWGaIDOYQ9PYR+Qzd+GLaMsxzdVQQKZUohjqNYAE4PzpLnyBg0UvPG1J8R\n8TAkdvwL4RG1HwOFNYrXbVRWnXIyt0T9BhSO5B5TKo9BEX8rbcoAFCRtsKps\nkWXOI1UJ8yOraH8RkLEq7yW4gYB/pvOwYHYKb1a6sHoe5eF39HBwJAsZPulz\nz0NsN35PrHdGsnSrP0UcXgPx+Elx5LuRM0pw7ils6BJELiAZsMsS5WMNtEjR\nUzF3Nh5jvWI+p6ny012/PEYcJS6nh/Hp1tB4oqX94esaJdddjAmi6CL81SPq\nPAbSqQvn+VSvE5HrIsaBCHj4WevacGi72HkSqp6kcPHb24T+UIPvkUCxHpqa\nnqLr\r\n=wbFk\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC2av//Md5SOOAYCYIHXAecoJ6JntXcrJHASZOpcuVHYwIgAodJyo+6Bgp1NGrszuf6+Fz93xwfwzIpGR7hHiCY6hw="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.8-alpha.1_1595501831897_0.551672696399311"},"_hasShrinkwrap":false},"0.0.9-alpha.0":{"name":"import-remote","version":"0.0.9-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"171bb8c95963a617156364fa3f1c954265aecee2","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-5AvZW0BEgIZYr0FxJzCq1fgUOUhie1NAoKr4jdyX3i3bP1VCBEZQdiZwEmfDx+iBCDg8FBE/ioWSOKqWv/qe/A==","shasum":"850fb248eba310c7b2297a2cf4e7448c652bd086","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.0.tgz","fileCount":27,"unpackedSize":180937,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfNKorCRA9TVsSAnZWagAAnD4P/iHwFrmHc2ukYTZJz+xs\nJPstFDE/lPdrCrPZ/NVZgiQ+34gr0PkksHi+cZNZiZ1vww4Y0e8FMvcY1X/x\nMKTp67417kpiimS7/c5X5dWobx04qoGLojI/LWHRs+lY6fZSVfXdljQqo18i\nN5sQsnzfgNdEPM2jj0n9mIPV02rcH8ELtIIDaygHkg5hWglzDO2ObBW90a6E\naqVeaDuPT2t9ymHvIwjkmiOYsgdr8R5ylEiJd9ssgt5ktzdWg8wry/5i3AHK\n7fwesgJsdPhUw1nKhQPhivZBAcgohhvZkAG+0+isnUrHF0jzjYTen+oJeXQ7\nB8n6EdZYGzky+PoNnbyaMyXYwCkL9eKmCjPIeFkolJyOX2QIMZX8JEK5TDuZ\n37VakbgnOQ3loMuwV/Hni33H8rDx2205HKcleofN6Y1HXOFB1EcbQVR2vsNC\nTLIz3sKDestXxtl9PiE1jStJqKWpWRzqrSOl967r5PnuZqAwlq+lmHzQ61iE\nPJcE+lBhaa6ZzoFPdmGTKWqb0JvS4uvQ++iH2Mvf3rukKXodiOooe2+X0fyd\n7xVnd8tH88oxy1FybETZPTcqlS6soDjzEKoU9k2B/JeC7GvLqjItKOgPLHO2\ntIFE70O1l2eplqjulLvqCHuF/g/SqPi62m63cksDIzn2w/lfOjWgHXoiid1Z\nu/+t\r\n=CL53\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDQ6E8nrtvS6h/L3UYZM2HPmJ1F5GJ1d8WQt4D6EmOliAIhAJJGYiWVtbZXrDyetpDl9G7qi+Tm5oEJaXQLa0iRl8Hg"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.0_1597286955103_0.2880019342443443"},"_hasShrinkwrap":false},"0.0.9-alpha.1":{"name":"import-remote","version":"0.0.9-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"dba4f3b71d06290a5144b00d604f08bc6ae71d1b","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9jEBU7AiI/MHq8DTcA2hnLRwmi5qWf83XX3uMiXtrPYolk2WOJWc9fQoMiM8wrRuInxaI5pD45b0imvbjpv4+A==","shasum":"fa65ec04601ff521a958adc1fa97139d884569f6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.1.tgz","fileCount":27,"unpackedSize":181184,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfQ2xwCRA9TVsSAnZWagAAd2EP/j8X5LhLEgA5t000t2xw\nEyl5AsIuWkMMmsjqbXLyfQ7O7pBTAwWKMid+UrSrDhIpfjWCAvTTXSjtMxVq\nXaNj+cvbAINJLgOdOYF49Tv+FlFbJ5Ibtjev0aeo7RKU2QjZVkMWTg5Axd0t\n+CZ7td4ksvcz0YakWZBDet6qLSQMy1776cgv4Mje0prlwUMlxquh6eisylcj\nS/EDr4tDlLyvEMI50W7aI26frhd4PaA1QgG8e5kJvnTuIcDYCiC4KnvSiSUf\njo+hmQSGbZijJZVlR1uqO0WJzwMstLjYFUUdEh71tD9dA0HUKpUTzzYmwoGl\n5ij2ojstpE0G1lHnFUkHmufER2XANoS0yGKTPiFNtzHHPawBJTzAUbVCcLvi\n8cMt+tO0qrgyRkIEA1J6u6n9IOSg0hsN+dAaT7IXmuht/2bF+Q0GG2gHItYg\nKGsYhT9abbclOhiUseN+L3Ka3DVczsehcCNy2/Pn7x+fGNChIpfDn/gPE5Ub\nbNiA3RpOnmVOrZUVPQo3lhQRWaULgd8zhvLoWX/m0nm86zgRZgeW50cT9/Ba\nobWrOe8/u0Hg0zSVKt+DEbWNf5pBeP+MyApz725b2HvNxhGydran4JYpdaHG\nV5ZMUPOE1sm5oajy0/LL8Z1CzfZbOknLUxbch6pGWWmyz+eK48wjdxJr4T0n\nrK3U\r\n=zSxj\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFcIJpYUmj70xQ9C0p4yJjLD4YmCMbvWj8eco9PL3wctAiBNZRbjjWG4nZC1Ay+T+/tsTlSOZeGpv7PJ7pha4sLVkw=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.1_1598254192376_0.7601762619241001"},"_hasShrinkwrap":false},"0.0.9-alpha.2":{"name":"import-remote","version":"0.0.9-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"aaec227a204e6493cbaf1a289d702a3df64dc72a","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-7UyRTPwAjeOYV1Gd78U0bWCMhjds8svFeuL5xhQTxhMV1mH72Y7zYgK/tuxR+m0U/0zz2SUKIEzPacpxXVev1Q==","shasum":"bf4d4e9bc382de53e62fb3c910e634bf5fdbcbc9","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.2.tgz","fileCount":27,"unpackedSize":182371,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfTgnaCRA9TVsSAnZWagAAz7cP/0ZSHF4UjjWBsUPcHQLr\n5V9pLL2nfR1/VBZ20qQH8iue9DS5ft0ioJAzw+dGthz79Ltf5c4HGEE385Vh\n6Dk2cGaxYNmjmqfRVSm/q08OCkiTgRfLMmPy9Z6skU7MgagBCoYDsEA5Powe\n+H4WWcEhzwK+5KB+3S36MfDuJGPaQifuanbsZL9jl38CZq+fJVk+lcbT4zLL\nGxpimARQPHRTnlU4UHNuZzJwJlOWQzLVS+2w6YMH3n7t+Ton14P2hD9AUsAE\nn2uKMpFpeZu0M7QXf3DrqM6xGKZXLeUldFWyflKawqydjd/sgXcGrWHBDFyS\nkiL2oHt9cNXy7U3fq4LhTVUum0t1zWJSLKKgaVIwA7VCRpdta3vsoiOivjiu\nuCamLT2M8fjloh5SJ9jCYN7p9zl0329IWip/OVjA9anyicOODwyQgj7O4Pa/\nrFoMqZ2RJv/tpa9VbPm1En6GQRWJkkKlMZccG0G31hCdCZUylq14d5dsQjtU\n9djSRXJmFYty2JFv9/4CmzFEurEjowFSwvAI4tA5I2knNOsu56nfjlC0NdCW\n8ceUX4BBBiVlPdKsXA+GZX7xadonbXiPBarcUMJVYXAO/4tpAI9QA0hePKiP\nq0gamECAJC1YYQrSgYB1f9lZugNWC+NRM1ZDIkhs+IdpvdCTMfX9wnRo/Ugz\nba1W\r\n=0Wpg\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCkjW6cOJ0vzcPjCpQEyW/7+Ld+zfGl+U0m0X42nwzObgIgEi7ahaB4wtXTJA38Jd4+cm6N521a/T6WKIAJiDOeb/E="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.2_1598949850420_0.9096422434327323"},"_hasShrinkwrap":false},"0.0.9-alpha.3":{"name":"import-remote","version":"0.0.9-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"517966680c7a00ef84f83698d86ce101fe7ebd99","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-MWh95D52SQeBNXBum8WvFU0dw0F0axUJ6zpB1sgBSrnTTwHFffnII3jcPTRpKUmkeJH2k11ZFGOTKPvjHcW0Kw==","shasum":"85129e1812c1ec1ff4a0efba5c1e267d8c1fa589","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.3.tgz","fileCount":27,"unpackedSize":182489,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfTguYCRA9TVsSAnZWagAAavoP/1qHtNl6JFvdyGG/2vK7\nLHQ6fRvuxNS1Pvv816EtbLwhwjbtZkqlVu41ik9jPR8Vf9EK6tAkyfXrdYnO\nGgEkiviV11oTMj3Y4XZxRwS6oNGf3SiihYkVJxZ9pAdUGpX4C4ao7hqq3DKL\nLT6pSNowFsbXMuiUSgx8KFc9IIBrw64abq0TeeOT3EEAnzX4yMp4qLht5ldc\nSE4PtAVvUJNxoNWTflDAfHNUqVC1muzTtnJyBPd9ZRseCtkHjfsBg5B6ck24\nnBvUx0iwM3T1hGAHNEyKP4PDuw09u6JL2rME98goEwj6EAT/TXvjVOnGVVaD\nK5LuA7i1/gaw29A9uvbnHtnQbwYKhPgVFV6psq9kIsJTkhgCo0WToJpnd1UH\nc0XG+mGOCcCzcIdvi6bNCfaAOBzDcxR2t2ePO4miuen5nwzuJ9JJ06IJM1O7\nuHBRAd+bElIRcbQXYah98gv/pRWlsSdhCBw7laoMIQP4iqh00bcA1tJovAsO\nEvE86bWlxjCFXwhzaPcLw7t1KbXC4AP0Ev9beMzcNgbNfMnHQ4A/pKrqqYVK\nE7B7/R8ro03zNBuaX+yxizpKnSL98E/f3i+rdsJaDMMBiClCsVJSRmIa6FyA\n8+T0kI4tw5zyeWYBytzBRgyG6so41UZKeAyHHesXU6/2ZSNDBR7RdBZY4+YL\nvjdM\r\n=VIHR\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDdIScCAhboywMqTXvvvbWd4ai7RY6EiyVxcDoERJLpKAIgIrdIQecHB+iVL75BCBxrfvcgEfKZE85TE/9uh0/2QNQ="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.3_1598950296358_0.1931138610534322"},"_hasShrinkwrap":false},"0.0.9-alpha.4":{"name":"import-remote","version":"0.0.9-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"a3503c0f90305f9cb7a495ac0fdd822748e49041","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Nmpy1pVhb2+zwt8Og0naGFheZ4z5cOEcndtNCIa12QisdGpL5xGfmLutjLk08MDZjCMZJC+f7mX2RMDbUihAlA==","shasum":"22ab2c11c016e7f2ebe2a766b03f9471d5556142","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.4.tgz","fileCount":27,"unpackedSize":184233,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfWdKKCRA9TVsSAnZWagAAi9MP/2fgD+1USEvrjbbcboo6\nNQXDNgMFUopE7CqETGaBQ0HYu15mXxf7jZ0omIWGfd6k2eFzxNN4k49v1OLI\nnE/5sDW7bYpUDmIwsD4jgNHMwFb/YalYH/US0OimL/ZcLFzV/S53MwW05tPh\nKGgy8C0BN3iRgqA+NH/kFCEwh6LgP6NOi1+eqeJy+/yK+uc7dgRHrSzBebc8\nuWnaHdinL8SjVtkCK/Ux4I3Y9RTC8AXDwHLir2WVKOfmnXFQE8geKbWWK0KK\nx3Gzuu7QZWbu639riRewj+xGZIOrX6/l6V+eVE2RXaruuI/OF+OrYur8ErXz\nTRyFk0Zzc7EuqJrafPP43ga9oo15KcpOq/7FpFLvMOKSZzC5aGnmCf5SxOIy\nzy3ShYcGmvUtR2XADKQnLe9YbqEUB3cH7U/6u8AmaSn2ci6I9wl99tYm1NP6\nGE1x0vlG1x7rX3vKov0hoiY6dqvVWBsZN8NTEYxpvfqL2Fv52L5OUhvozNpL\n4qLy62DyF4iUNN11eirPssUB+tQ8KgL4U08nx9jjPCY9aZvVTxaT+Rjzfos6\nl67sMoNadD/cz7017Zr/y3Ne6TKE4VuLqckiIXeFsS6ghC/2XRxEaPUww/qO\nucMQKu5dfHg2kqRq6sKj0l38rnTMBRcKq9cNEALBRCDGCQx8WJyOJeyU7IkR\nKkpV\r\n=8dIv\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBFh7yaGxDlpfopm/iXIvJLn4cWN9eRuQrRRxINK87rPAiBsBIhsO8uX8QV6Cw1FXetAbkr+bz9ZObrgpcB/aUFVKQ=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.4_1599722121602_0.39636583684712123"},"_hasShrinkwrap":false},"0.0.9-alpha.5":{"name":"import-remote","version":"0.0.9-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"aa915429611f74911ec2facd27abdf519c4cdde8","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-aP8lvndTAb6ONhWSBgUuUM7Jx3BGMhoW6UDCr8/3Ke7JQg6ZO4o2KSnJfD9kmO7DzCHyW3XM4nMl4t8m9f0QaQ==","shasum":"112e6a0886ab330a22e5a6752189ab1839fde7e2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.5.tgz","fileCount":27,"unpackedSize":184226,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfWhdSCRA9TVsSAnZWagAAhZwP/RRLei7v7vyklHm6odU9\nb40eYB24gF9YA8UQP3N3xPLjTPfXD00KY8ymH9T7ry8gbsC6fZTwgrV1LHr2\nrckPHiSh8v+mIlA0d/4GrCsnvqpkw6npROv1z4QY16q3rlB/NHqwiJwU19GD\nL4bWEHX6Ade6/OkomnHcfjZsjLqtGRjteRJngA0jtYCYrFgmoDonpFUCP6kG\naiqZ3rnqFi+MRoTjWrz4GBtB1WxKWsAa495v7Z+xeOfhM1GeZ/p8OL51tuyL\nY69VXL8EXx5Ij6bV7SD3ZiYpvkpzx2ukL7jjOxVXayvbUGiOIYLnvzQ43TO4\no5YnxhvkypCnt8kmnVsUXgYbBPjWEmRRptjG0r02462sO6ASKxrpjIPHu6Br\np8ZT1vSiUD3FQJXJNsdFL985afjAWA4jIDi/7xwKwc29nGT6PQT41IH/1gUT\nPRpI4Rf3Gllu/S04McrGI2b/LbW+2OkaCPxDXo2AkRE/flcg1qBjG/NzB9RG\n0VwivVwURr0KuT6U0KrZD8P3j/xKVACjua3s6E/8ovJuexYgpelew9KJnkb5\nYqxOyhJ7XTOzC2Mpm4k2f2k42t+nAR+ZIvy0Nv47CXv/eULLnFBpsnY6aUTx\nJN1DpkPSBGrxTbqgUZOb30knobqKcH61S9tRNwfvgE0sGRHQjWFtWj52Jj/N\nM82y\r\n=JkzM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDd4ZKjHvVo+U+yTukZIIBj+Yhwk6zE3Pi3DJtHnlXwtAIgItZLKH4uuapLxLspux0w4DLOG3C7C/L8aWFHc4NKGI8="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.5_1599739730393_0.082283388537159"},"_hasShrinkwrap":false},"0.0.9-alpha.6":{"name":"import-remote","version":"0.0.9-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"ca8692ee82074ce4f39fe654bbf6da1c4ca6170c","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-siG0ePnlZAaYKwwpqi2hMeEzjdWRzJiI2is4GI85NgZbv1INiMP6dD5UcJEI/QaF3cnF3pmnPqYyHtquIAyuMw==","shasum":"b4faee9669c4f360b4344b542cbeaa912aa9fb6c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.6.tgz","fileCount":27,"unpackedSize":185275,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfYHnzCRA9TVsSAnZWagAAzJUP/1yrHnchVcn8gKVkaFrp\nwjxcoYHO111EuwAvT90Iyde1LTEg61GJSEfT8aCIC9AFX7O4S9icAGVRp6CK\nl8HdI6QlTi7DdePKVK7vXGC1w1JQ0kuep2nH7t8VuxcA+wVlII9giWYPIBsJ\nhKhNsZ46Ezl+3zjNuVNyLMhBcM1XbwCNxC/IqoS290QwMoQIftMo/q4N4p/L\nJCmKnI4vnFcVhYKZDE3Op+Q2FGs6tpKk8g4MV0ghVJUT5/x/zGRl2twsoFaG\naGlrLfl1zzlhDoAFjxWlybX5HDiVlWfWg0v5cN7mNVkXUq/KjSLxquWozMVW\n+438dWGcIIS6wB5v1HL6fuGIM6Gdz2WiiRiU0TPaVCZ0EpFGxM0EKN1TDnzb\n2lAbR0oYIW4bxqtT/MvJppcUR6GFYuro1/yAvqNT/T6K8Sq444tdYn9LtQlR\n7rtlq3/H/vCaTjqn3pMenevI8YbBk+B3dupf5B5TDYelh49A9oRYqiKX+LYL\nwpoLxhNnK5e+Em7L1shF3O4qVNsmtgnA9xODGF2AAyhU3El9Vv+TY4SaTDsc\ndG6AbhQSVLhixjvJqb5+aHzl2woiVL94+/GAbohGQgYTwZx13lL27iiOuYj+\nJgqbslJ1Hz0om9ZzXGwFjLCo0Pm0oxsS56bmcx+oRMwu5K1OCeuAUnb5jjcA\nggwL\r\n=Ij+l\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDYYUQDB14cmTjXlPFPBgi9s4AeUH8C9Yu4wbzytfa5oAIgGlcP+Zg5GFNFNjAODfmlBIx7/jbgbqEa8GZUpyrXcNs="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.6_1600158195182_0.13389701642355067"},"_hasShrinkwrap":false},"0.0.9-alpha.7":{"name":"import-remote","version":"0.0.9-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"165caded6c7ba249008e30e3d28f3a8ddbbeaf54","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-zx6xqLqJJyrhFCMSlA5LaBzGF3Wvm0oGCkaXI14VEAw3JOSPxMwR1KhAoS0IflrXWwNmND37Erxu826ox/XP0w==","shasum":"9a9f00e334cac07a545adc974b67eceaacc09af9","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.7.tgz","fileCount":27,"unpackedSize":185480,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfYboNCRA9TVsSAnZWagAA4VMQAKFJQW8N6LA9fXukgsSu\ntvnyU9Uc3LcNTvMx8UexIlAmgfgrKL8VelXRS3QAI8mOo3hGfPkFpf86nSX4\nPYBJkLuZhLDK+Tdd3xJW+pB6XKcPgBwLhDOPvtXiNe/LnMsizlnn8x3xETH3\nI/hxbZancUnIko2kDin8yLqSavSO144gX/1FvY9VaaX9NxzCfZFufktGoZmA\n3q6x6/9nA8ffdhoQAvlkZX1hjv7im82HoB2mxCr3J/vKof46m7EmtUJrt9IJ\nbxhjWOBiTzH7ggnoHQLbj2yko3RoYB/2DfoTkouKE9kyATZCsEgKPtCUuq8M\njl4WYKizdfJb/hR9KfIzWWZT+nFMRl1jRRKvUEVx+kbU4i1hIoDhI+ZN8k4y\nlovQQh6fPHU8Gpxkb63OkkqloJL89l7e7U1ThO7BP44hKAul+RhT30TRpGLr\nQ5yFry89ev3ULCv4FcW9d9Eqyg0MkseHU1F0Il3114L/sTnTw/yazYpbmaOj\nT89QfR1yjMndjP3h59TMSMagna1dmQAUTi7YqNkiMEFjYHII92BX0MglyKYs\nWeQ8fnFo3MuwTnxAWuoLfNvRlAOrFB+GI4zamxLPBW7hiMUGsBMBotnDByfO\n6/FSsRV4aZGUq5OJwKFcJDIV8SVqNgCvrHqLQkosZTwYa22xStEMBCjIvRls\n9E//\r\n=VV8w\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBc2jQm+Jsa/ay5euSYn7F0Di11T2caehkUycR2oB5UmAiBVyI7+Bl7SHJer3aLLDyfgRpYTCSRzzWc+syLe3t5rUA=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.7_1600240140644_0.2886277202598948"},"_hasShrinkwrap":false},"0.0.9-alpha.8":{"name":"import-remote","version":"0.0.9-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"49fab7f35b9d5ba394d8d9826485ec9667afd4ae","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Up4qf14XHFLexd+ugjO4D4xr3I0VVHQnFI0tmhnKz1ARADqeLtlOXVsNBvBKjwlPfKNNAFd+PB55LHRV3lFJLw==","shasum":"452f287480da26abe5d73efc30beed6f449d1c8c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.8.tgz","fileCount":27,"unpackedSize":185472,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfkAgXCRA9TVsSAnZWagAAMZ8P/jMAdLEm0fJGaLxDeem1\nIvZrEcwU1jzx5L7I2tchNSf2DK8tjFH/akkEce5yLPptD06u5fHTSrfOj71d\nbMYgSqy3TYCqux03oDod6S4mc0YL+xzVTNHKZgweBs8qledSGr9uBaO4n0uq\nAiLcdONdHZWsViVi0pU/85TRW+6iOVX0EjyhJCruX5DjUfUX7n10F7Zbc4mK\nk2hlv54jtQh1lrI03sIGRBf7gfhnyA/mPGMin1Fx1u0kRNVl5Ib6zFQsBZ3a\n6QgnbAW+FP3DoVbjh5wpTv0DhwD19UQP1Q/CJcb5e7FEU7BtSbrhPjK4Dl2L\n5KGXYkKX9FdOPasFrT9dW5BNV56kWqOGSy29QizVW7f1HF2gEBLETtHIvO7q\n8oELm3+hqi9oarspV1QLvybstZomboYMsgZSwq6/b4xId9W830in2TC+gsdj\nxNmaM1bnb4rndWC46kQ52Xy9kSqkwTgdf/BULzuAi/Q3VJhW0FF2KNFh1ZlC\nLh6lp3gbWC2z+Sy16Xg7QypSILohK8q+iJcM/B21/WQ/ijxRKG4avsDllgyG\n2hpNKJMQCVjsRD1AAwpwR7q42lYTrFY9uxZluf/KeFlxOCEEEwgrFHXfWQXi\nvR6QJI2NinOAf5u4Sb/WB2kowDwC7rSlMwMyhuA/keB6b0wXZfYeesTCR0JI\nzy8u\r\n=/i6K\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDDFHFyDmxaQxzbl9gCSEzBnkg/e1mEaHYyew30iX8SjAiEA4WxKtBI3qZk8qPbWk9PG492SegWVbX7FdOHXlMiJwaA="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.8_1603274774799_0.9301253984794862"},"_hasShrinkwrap":false},"0.0.9-alpha.9":{"name":"import-remote","version":"0.0.9-alpha.9","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"5cbbc99c901b9d2acedceebbc9f91354bcca960e","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.9","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-FRlt46e0e3THZLnINHJFR1GRHt3zxiGlq300uvKBBitaq0EAF64RCA53mI/evekbKeH8u7Pf9ERcz370dsdLkg==","shasum":"cbfceea519e2261467f8d6205b3fd8be72071fab","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.9.tgz","fileCount":27,"unpackedSize":185916,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfkSwUCRA9TVsSAnZWagAAfbYP/2Z6jej8xDx7pqhbsrpH\n3hgK2WDscf9afobffUopAbC003la8BVFn0pzPa764JaTKDHFrS7vQgFn/mHS\nHx0y+2jZfaHudszrRHeuThuUjRQNr6NEA/QaDhaNe/H1p6ie5AEK61rFssvK\ns8rC6B2FyLIIFFxD22kEY7ysFW+5GhRi7VU1Bg63MQJCDdfgagzi0WVhXZG9\nbJ+zvQf2IxnCuruRXkzvXP848Tn7OL62gngK6oEjETkecCKcrG59hrAxbd90\nYJZnC7ebDNWNZDr8WV8PKgQB/3WtvIEpNlyHxNqsN4Lp8r6FdQmRqsujLIHI\nhfimlBALdVS76bLttEEHlcaJPZnU3azY3oF2L0EbvJr3ENIGarbyzso59jaV\nVj4oEkXlYKXXuSZ/hdxGMXFZu0S/eZrGUZZSjNHwVVeFBvWsAyI8WJXdkEi/\ncxQrQa/a/gjA5cI89pgETR3VirpLTU5APUQGs2RDBOtZak9ftIaNCq9y3tNF\n0JBjiyxIZ4AZT+iu6eVPzBybGtsELJm2+BQEOUMO+LovyX9PI/4SJdkfBMUc\nYUpUDylDihXRzK4SMuNowoFuqc5Ujsvp7ZrlSF5A+yz+NQ+++LbmE7mobm4A\nz2PC0EPmpy759dcn69rMg0BGoC6bm0xsx1CDoGLV+8gO+EFFOsPlaRR0DMsj\ndfXS\r\n=NJpm\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG0aDp7Ff9sEJG29YkA1W4AVJulILGrNuoNOpLFhIdBVAiBb/DwYmLuDEeu0LzmDOKecwE26FX0nsXDe/8huF8BB1w=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.9_1603349523731_0.09107490804684426"},"_hasShrinkwrap":false},"0.0.9-alpha.10":{"name":"import-remote","version":"0.0.9-alpha.10","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"dbf06d4262d565f19026424c13d0b3468d8f46bf","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.10","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-kLxZAe2QRcPxrRqytJB0zwjBP22u0+HfhU/DH+3ILcEL3kz8R9T4yTwDrAacbZDe3A6X3NvqeBKJ87QoxN8rfQ==","shasum":"46122fe06da24aac809097a1fdcdd39d530de20a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.10.tgz","fileCount":27,"unpackedSize":186114,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfkXDaCRA9TVsSAnZWagAAhiAP/2IAk2u/9ZkJZPzMv0J6\nSKkl7Z60E2W0RYxU1fHM8EfatdTQQ3agVeUh3S6W4bUhvwGSFaRtUtBSMYKo\n4h1JuNwo/OSNc9eAwvy9EEYh1hDVDhCdeEuAj2gG9G6L+168z4VBfIOcEvg2\nFFJUjmo9UiVhwNfO9F5wBBetO0ryXkmR7pTkPt8GouF2xukYEClQYN02uPAO\n/I9keKN2xF9cNpIKc2fSJJSlgwdg9kELBkHdeCevWQBCrN/fwSvIifPhWVC0\nSRoRty9IYjc3KsnJ/HO3nXk+JZDd+hSOEgi7B6TjbrFxOXkSTa5LqqeddWMa\nUujc2dtHs0A0sdptG/AIQwPh0qQ9H2mogcKfwLGN0mZ/0YwzAkr2kWXVCsJJ\ne8H7mks5i46lLGKgD50iFCB5RCN7d8hxVilJgwLgIGc6WFWTyFaZtj8fb1cK\n9N+4kSaCAc0i0zXamFc/3XbTDkq5E89/4c4DTtXNzKtJHThOHTP6N27R3HeD\niOiKrabMLiIGez4haRLRyEpoKj27/Vwm3PNrnFCtn07AU6n0J8LAUEny1Cb7\nRR2NPRyEu6OVXFlHfZCU0sp16zrAz6FasZa+ekQBnsI3MRsSHnWPu5IKwRVz\njxRjv6f/UyRE2dK3XV1CxwrSyGpl5bQzwjcBCi0/RGpHoMOQRUMa297BEEn9\nHQTR\r\n=5Rvw\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDzg5ArShlUX1YCs/kTcq1oQmjtVyJfxSbpFQ366ayjcgIhAJ3buN0vM7PY9aAFfs85l19Xv17L7Viz7t7RpjPiX4Ml"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.10_1603367130171_0.409572665353956"},"_hasShrinkwrap":false},"0.0.9-alpha.11":{"name":"import-remote","version":"0.0.9-alpha.11","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"54555accbf7fe850f9dc00b00070edac879bc6ca","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.11","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-peWqFDA69yp4lALTIakM5Gj88MjN6uU+QwlfUuf4KNYb7xINPS1kjCw7xZYuo/ygzLkswM3oFQ9CP9LZ0fEnEw==","shasum":"a9459e51edebf9ca64bb973020aa121bac2de61a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.11.tgz","fileCount":27,"unpackedSize":186280,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfl+HOCRA9TVsSAnZWagAAPFoQAJXiGD1DL0gaqhtaeem3\nFMbUzGd/oeAgH7MFVjwJNVtU2sg+Tlh5l9vIGPddEGBYQQYsTxYHfx21JL1R\n/fMh0Vr0JkucBu6IdeJvYpCm8dz9ske0aLYjCFHi1eUZ7ezF03vEiW+JIGeo\n5PfKs61LfWzw3SYGa95ce7WdDrZ+3orTCyH0BO5Z9n4gA12Tz8oh+TisbP1s\nGbpm0fs7g384Sf3EwLQlg6MIu7nBGzNkPOhLBTNcUaviYAFaADPiYcQpqBF4\nU0utGkC6ZLsUv18Kii1tSPgV55NfWHXvbto7XzvM98/Ph5F0RVpnBhp3p3vX\n0I0RdsEwvMgae6DQ8zG63ajOLfi3FaqpmCqMk/xj2mbOPvUdny9CgQkXtfSs\nAAB9dV9xLi4bP9ez88JhjljhciMDBbesfshxt7qJwSf8jbl6rW5/pTcc5f/B\nPTvDzN6xUNPIgx1yczjAW6qVA7gaiRze8CWCbar5q0SKbJZnoCXoOoXtfT/Q\nAP4uVF648NA3ussuF0TEKAeSML8u9et7LUVBzlbb3s2PtpsRSsTu8jVBZHld\nd41+qtlkHwiQ4jrO2rOEpCE5TGg/6Z6qHgDX9K/ynVrJhELYoIwfPJHmN21S\nRbpMDg+e+ia2A4Ic9bJYVWnDch6ppS+hY8jbkbYkrlJ4LJgv04W3ug2Hq6uu\nRh3a\r\n=XHYB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCbBdMvbD0oevOuaqr9ovYtNghDednEqWaWx840uFqv3AIhAPs16FiFE13kkzTD+fM80Vdi4ZJePQZmjv/pYNnH0eI2"}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.11_1603789262322_0.6451598639723466"},"_hasShrinkwrap":false},"0.0.9-alpha.12":{"name":"import-remote","version":"0.0.9-alpha.12","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"28707b9b16301e0f5527b62ed9508a5e28a1bcc4","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.12","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-gmjuxSV0EC02PTYonDOKddAJiAnJSSRhz+RmSsOcmb4nG7+LO/j+EhcE4DWZgtukZzmB/d03c4Oxbc4ghpkS9g==","shasum":"54addb9a8bf7e4422b6439fca63c951986939128","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.12.tgz","fileCount":27,"unpackedSize":187834,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfmM1bCRA9TVsSAnZWagAA0IwP/jKDdCaFdg4DPQFtU7vA\nwqcEjZk05IZTDFAQGZok4OFpLD6OmFLfqwOsuptYAtiI5YNrtJ1f6DG0yts0\nv4Jcpgejso5f9SNrU/GG19n8Fajp4UJsjsyGRKhPqvrwsEUvkCeHpM67x5V9\n+x/XLxKkCar1kjQlp/thMPNezihcJ0m6PHFxnpYCSw2jIBA16gITU9vyGuKV\nTykJXL4XpeKLuNC2JsiceJ8mHHUmKe8BdBUY5GXTa+fRcP3la0zWcCv3V1pf\nMS32XaOBa3bBXLH3XJoJCtKf8PFek/tHFKj7uPZlVWp2qBi4sRyLC7rgNOJw\n7fRs5QuCg03L+G7B0mh2lG5MxOCF9b4lnmF5zSECI9ow3mIduQHvbDv4trtO\ngC9T8ebcsDYsHb3Y8qLmJjHoyf5T1OOvJVj1xu89whQipSRNH/INjoeJTYdK\nCNYYxlwJhusHpHI9pTLhOpGSZiXjA3fW3jO0aHq1pVHhsvXC5HUpvwwgeL/C\n06lVOIW9TxMF62tfr2YjBas8JbGVfEMncTgyDFVPeDYozTbf3HNMopLUvdF7\nkxL360nec4sskfgX3EpEnOnvdRQUKNGEcIE4CknCe3NSX4fj9N1aG/QLAnMt\nsbedZqUYj/jp7R2vebgp264b2c7zSgLWMEHAw9jYGBqFMHCaacaHmN1iS3Dp\ncV86\r\n=6HMl\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCokgGqmAXgdQAofqiYLWi+kRdYDO3JUc4C3r9i7b3B5gIgWM8yqvVtz+/9o26Jcr40mpAhGHmzKxQ+4Sr0ZIsvaDA="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.12_1603849562569_0.3022487874704969"},"_hasShrinkwrap":false},"0.0.9-alpha.13":{"name":"import-remote","version":"0.0.9-alpha.13","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"bda391fed0dc858439d7d70fae888b097376939b","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.13","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-mwSyTitnD0utwrHRl5cV7XH0XHiJA/lPjGJwveGFEuLTqcVQxBtSaVDOsPOImkEjS8FBak5OWgMtiEJ5HT08/g==","shasum":"a40d5df9737f28a2da8df73baa24bccd4e59b105","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.13.tgz","fileCount":27,"unpackedSize":187857,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfmi2HCRA9TVsSAnZWagAAhLoQAKF4Cb3qf7eN9q5ag0DC\n3jCpNtPvt7N7BmjC2DA7FkRk4/BPavk7jnhEWprnt2T6smsycGQ8swQUiI7/\nGV/lr6J8VlNNXtYFGCZRpjwq5GU56THs6WaV/nsHwOtlp5bqxdFu/1gmxaQj\newcBpoqY9QwtAKxklc99a+Es0Ptpm9o0eI5C90VGjLQ7FuGeK4wi3UKPsOra\nHbiQp37ZE9z3J1jGBiqsP9lWjEF6XpBih+GeOrOieflKOOIEpGjGvo1pJSqk\nWnobYFLgmsbnyrJ5n6sEwEyQwGTA5Zc9UZD9juXqcOrn+0EGL5wUKGd9Sy1F\nxkVSLWeLEVIHDSSF7qzoPLRsulUFIBwQ/V38PIl27RKKcxbAofJYNldMamYX\nF1xX4nCYZ12NZHdvYRtaisYfat968ysC2Y4m1BIT4Ndffgqf55GlzfY/Hnuf\nZKS97CdfLixUEEnDlsSB1NjTefWFbjRkAG7IKjPHpnh9TmcfPCM6h1I9vNRt\nEOct6JDSTlCreFnbv91xesEf9bpMd7Ys123iZQBTdoOTH//3yLtAUdJ5DP+J\nPEtwtj9XGsTjdPs/bWPu/C+R0Yi6hl91bO4y9GznyBRlfvc3Jf0vFfTlkFH9\nkC8MVm7I0Pk5OZp0sxKyVmEjw7K6D3n3zn01Jn4xNt++Xrd1y+7kSzw/qV6C\noyOp\r\n=doOC\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCZWUQpZCwGH6tf+6Az83lW3iYpi/F9G44cBwQacn7z8wIgNzTdIa2sN6tVwLm0FxS8+vlJK0s0AXXaJPn75ddo3V4="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.13_1603939719362_0.2109443261294821"},"_hasShrinkwrap":false},"0.0.9-alpha.14":{"name":"import-remote","version":"0.0.9-alpha.14","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\na webpack plugin like html-webpack-plugin, but the template and output file is js file\n\n## install\n\n```bash\n  npm install --save import-remote\n```\nor \n```bash\n  yarn add import-remote\n```\n\n## usage\n\nThe plugin will generate an js entry file for you that includes all your `webpack`\nbundles. Just add the plugin to your `webpack`\nconfig as follows:\n\n**webpack.config.js**\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: 'src/index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  optimization: {\n    runtimeChunk: true,\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n**src/index.js**\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\nmodule.exports = test;\n```\n\n**host usage**\n```js\nimport importRemote from 'import-remote';\n\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // regisiter externals when call reomte\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\ntestIndex.dosomething();\n\n// or register externals only once\nObject.assign(remote.externals, { \n  react: require('react'), \n  'react-dom': require('react-dom'),\n  'lodash': require('lodash'),\n});\nconst testOther = await importRemote('http://localhost:3000/test/other.js');\n\n\ntestOther.dosomething();\n```\n\nor \n\n```js\nimport { RemoteModule } from 'import-remote';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n## Options\n\nYou can pass a hash of configuration options to `import-remote/plugin`.\nAllowed values are as follows\n\n|Name|Type|Default|Description|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|The file to write the output entry file. Defaults to `index.js`. You can specify a subdirectory here too (eg: `assets/admin.js`)|\n|**`templateParameters`**|`{Boolean\\|Object\\|Function}`|``| Allows to overwrite the parameters used in the template \n|**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting|\n|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|\n|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|\n|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \\| 'auto' \\| 'dependency' \\| 'manual' \\| {Function}`|\n|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|\n\nHere's an example webpack config illustrating how to use these options\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      filename: 'assets/admin.js'\n    })\n  ]\n}\n```\n\n### Generating Multiple Output Entry Files\n\nTo generate more than one output entry file, declare the plugin more than\nonce in your plugins array\n\n**webpack.config.js**\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // Generates default index.js\n    new ImportRemotePlugin({  // Also generate a test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n### Filtering Chunks\n\nTo include only certain chunks you can limit the chunks being used\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\nIt is also possible to exclude certain chunks by setting the `excludeChunks` option\n\n**webpack.config.js**\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n### Long Term Caching\n\nFor long term caching add `contenthash/templatehash` to the filename.\n\n**Example:**\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` is the hash of the content of the output file.\n\nOptionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512`  or any other node.js supported hash type\n* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`\n* `maxlength` - maximum length of the generated hash in chars\n\n**Defaults:** `[md5:contenthash:hex:9999]`\n\n\n\n","readmeFilename":"README.md","gitHead":"c138e98a19465700a9a9a384e7166850c76cd232","description":"a webpack plugin like html-webpack-plugin, but the template and output file is js file","_id":"import-remote@0.0.9-alpha.14","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Ruqb7hT+j3XlGil0LhvbkD0DEESZlTWIlE5hxRs/Ro1ERcgBePUig8F+EsaBH3yiLKog7NaN2rNlJemj3ALt8Q==","shasum":"e02fd3ee63c3b907d6da2ec7867839cff6df9e56","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9-alpha.14.tgz","fileCount":27,"unpackedSize":189589,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfmoMjCRA9TVsSAnZWagAAc1QP/2f5DGEDkTzXqGrTMd4v\nKQfbuvTmg29L1Xjy1y/aOtsdxFr3MOvjjURrGwReSXnS5uDukFzkdFFOXkFp\nqAYpSTnRagk+8iVZ2n2w2raANA014bvmEs/U9VL4waQrQDCTBILwd6e9y+w+\n8hoVIhEUbDRZ9xlHRIMSoWg3/F22bbvgSFVjEpjI17pTIfTPIlMUT10JEsYS\nLp8Hw6zq8xAEzyptauJq0b8FcN9G0FS7BXrZhByPoUAfqZ6+08MiT6t1edzr\nIDOAp+2U3Uso0hrg0ecxnWnREjGGEKCZ4dBfPAmvoJiD3UWmYmfP3/CnkwKY\nWY1Omt3a5wk/UODQJcq84aZ08VozKI7nSlBNlJyHXy2UG1zEVzhNqiJqmZbI\njB9wqXvNerQHVrNqhaEPNlZ+YoC0EOJ1X0Q+BzrUwgdQdr1bxraUXNwa4uJs\nqDhjCM2ppxBOJN7QC965uEvGkD8Xuy5q7EG24GOm69TGgOkW59oUGjmxo1fc\nSiPNJ7EHdar1eXeOam7aEZJM46HFvq+AGZg+faGeHdXDqd4NRNXsPu5aR2Su\nQYPhaBcfdMlWAELgFaCGX/RNV25nTrJFOjvX1PzGpcpKtUkHFOE6jh6jOVdK\nFVtMFr42GAG6eyGEcqcscGJX7584LYO0KYif2Hc+DFazfdFPu2ikjGDehKx4\no8XM\r\n=MyxP\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBptiLDpzI8sFeZjJLSmPfjSlzFhH8axnBwTRfkf2Nc5AiBXwhY3Fb8BfSmjD5ft4ui2p+bfRSudhsJjfCtuP0M51A=="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9-alpha.14_1603961635463_0.9220653928835489"},"_hasShrinkwrap":false},"0.0.9":{"name":"import-remote","version":"0.0.9","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.11","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"898361c88a79373b1d2db577cd08da5683c78d45","description":"","_id":"import-remote@0.0.9","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-lEAIkEn/0hBL9Ui6vo2fWAEivUBiSPg3uxi4hEpEUWDM2h+vZ5p1WanzcssyI35NYB7mhcv1nbmH5M/RdAODWg==","shasum":"a4fb11f7f66560200a9ea403c2c18b9d50d2e5b6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.9.tgz","fileCount":27,"unpackedSize":195817,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfm8MVCRA9TVsSAnZWagAAWKAP/3KaBpdEw5GKe/svnIvs\naDpYWdniKju+BGQ8SGl7ECor0praR9ivNG4iKZeWAaLNu5BglT/gp+VD8WUG\nDBXvzL3v21bC3Kn4npBFVa1MPMabu8F/B3UWLV5DEZd29o7WoTstD+gUFuBz\nAWhq5q5sjLvd7jrSKyrFPiSMWJNJSg8oWuXkK2c/7SBwOHhxbhGRr2BVD0tW\nTbxjZIv9ddkjEhACBEwZt1b60G8gZYn3s2u4W5RaKBvuPEB1A8Cx56l8pLgD\nd3Z6VLjL/bOlKLruzV4it1/WNT+PlSnMAb/Eshi3eH7OyJr4DdfVZRY0aIwR\nwc8u8RfjwDtw6eC4nJCPQcbvYa4uDO7NJIDU+BGcrNWCqZBqwVC7O40dCDUg\nxMGZp7wpoImiC8AQCOwrQ3zp8N7Xv9hGNB4tVHo87+RC711sa3h3Vdp9Gv9X\nxrN9Qn+f51aX56C47xnBxbV2pC+19o9C525o5qRcNkQ2BVUqbY6KmgwyZIaZ\nD/mFeVWosDApoWTmynv4b4OKUqvG+gNOHTQczEb3H0nrlfIdY6z4rO7kHyBq\noFjXRv3UZ4GvNvKioYd1bhjFs9XmdJZfOQLtesHnjlfYlwbRBrcB+MKhZvGJ\ncZowkwFDHryMTycsS8Dq4qBg+UrVQF5IiDuVgNccrJWGkgK4H4Fulfzh54WD\nRXzz\r\n=sEBU\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAizQoXgm4X1sDRKdCFWqJ9LZbz2eMsquTFcP99tiQ0+AiEAigHag04jqxr3l1ofETuem9TYANZ6Xho6DvdzGwLlDjM="}]},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.9_1604043540521_0.024404509881490233"},"_hasShrinkwrap":false},"0.0.10-alpha.0":{"name":"import-remote","version":"0.0.10-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```","readmeFilename":"README.md","gitHead":"af403852b04b8603a412703838d6be98ccb2bd9d","description":"","_id":"import-remote@0.0.10-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-sw8jmiG5lLLcLYi80qeFRjX1EwUh/KY4OOrGdhW7LAvdaiymSEa43La6H7u/lwHHSYJsrBU/QifaRqF73N+Hgg==","shasum":"263b9b85531450765181cafb54a0848c0e274597","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.0.tgz","fileCount":27,"unpackedSize":201367,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfokG1CRA9TVsSAnZWagAAMmsP/0W9I274jBHx9gDZt9in\nCMUGbF9GcBSrhtAbGXlOCkWt1lTqta/cLvqL6xmrA8L5AGym32JSIpVsp1KU\nFR5DiEDihkrHOjNd+SH0250bGTMNDKmUbNKXXDnKrn7qe8iz7j+BG9SkTgML\nl+90spZbcGSfQU7v7K5oh8neDSiZA21CknV2rta/GX1gby9nCrQvH1BioVWG\ntsYDSQbcNi8QYtMFb2hTvNW1KJeApd8lADTyvTHAAZdMoIPz7HQPfQuZsqMu\nrBCsDtvf2oLHVmrusLqG0AWX4TexjqhcrV+78OcFSIo+4fqoSa7Gsce1tBGy\n53DIabdWdos6RJklTtMU3e1JQCIVDYD5uthewlcaq1uH4DFj2pfqcSbE5KUd\nIlIqg4o89TteBmOQfeMntgZMcj5sPNHbGSlzk2tSW0tY4mBM/lnUZEc2lRX7\nJtng9naH50nbqvy+iAtew29Z771B9LrmRmEdGtiD3sOC/5ssmk4Q984Fm221\nBdXgpZmxz3BE4cFC3EcPXjBRss6zGIQDGwl/z7PbUc0xKRj8dhcIGTuxgnJ1\np6oeGHbZRmikewm/HyLxX6UAL1IOywLOoZpBGwXFF4Lvgz1HqZFoaO25Jm6X\ncw/reVRDQrU22OgcDV2UBXkiHFlnfM2QWm0YWwLBMOZ3cVDhXwv46k9vwiit\nh06i\r\n=gGny\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCtkg4AUEUht7VrIGr0/nlr90vQSvOOHt5Zai+DAte3sQIgdqtDxahuo8tO5qSCLPwth8AcP4FxffKr7gTMhuzYnoQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.0_1604469173046_0.6669877052204869"},"_hasShrinkwrap":false},"0.0.10-alpha.1":{"name":"import-remote","version":"0.0.10-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```","readmeFilename":"README.md","gitHead":"9e0980a73c28f30fa51976d556898c54340e11d1","description":"","_id":"import-remote@0.0.10-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-g+jJkfNlzGUpXGruZOro5T8+6IY1kikn0DoAR/VJKARWMFa+REgMcj9x+dMREyQRwYKEXneH4yvl5iuwKe5jog==","shasum":"0f3a623d6587a98ce9b834ff7704650ca4562839","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.1.tgz","fileCount":27,"unpackedSize":200219,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfo1g6CRA9TVsSAnZWagAAOmMP/iCRUgcg1TOGIka7cRsV\n3WNyp9fHeUdixml+IZRhkm+37I8pr1acteuGotdRrjty+V/gOFHYW7WFiSuM\nzJxj13Pe7LvBn5i+QO52f/YwPw7F5WfKDYqT3d5eX/08VLhPlMcF/RC6tNTb\n3CHTk2GMAh35BWuf8Kz0zdxaQ8CTm9hUvduwXbsf4ZOUrxabPuzJrSy8a8J+\nlHfvG481l7RwEbWqwdZSdROYka31ZlSjgpii6dR1XIHgG2fNhHICG3S+HPXR\nM2pSr28vSsHs/bcCpuhL93iANznnZvKvaJ+leebVLmyrplUr8pm0uLgbBRrZ\nBUQ+fNQxRUtoGp4UZJdj0WekKHJ39fjfRM1h+eno5tKLwV+hW+Tl1hF4raxU\nrR5tuetL3FQq73kGeLVTzyn8SOn7vmd+NVA1Q3Jjsi5AeVyT6l0Rq1v4+Hx2\nUPWRqOsfir4o9FI1NFnYHtHFlSnI91rtXfYpJaOYh9FST92T+L0PSseM/5a/\nyN7E3ilqJEuSHakhVzpZ1Gn/FYnd+XjTQpA+81arcquWcWgCyq2NeYUWaIlQ\nDubKMTUhNT/0wRCXTVuUQTefZMJUlHrfpb2gfhsSi78MRp/PU1w7yiaKWSkC\nAt5yIUGLv8Ee5URpQ+rs1+AzZ5u4hAzkpMBw7jZIneeniU9uvUekOeun/Y/0\nIl3w\r\n=bxDk\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG6UcrYYhrqHzmxgpegV7FMDQPnF8bKoyqL+AkIwQKTiAiBkx6Qwkq4XyFmfMieuYcr+6Q4TNO99uC09JPAXPWoYog=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.1_1604540474122_0.5868862681474685"},"_hasShrinkwrap":false},"0.0.10-alpha.2":{"name":"import-remote","version":"0.0.10-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```","readmeFilename":"README.md","gitHead":"1dabe804f437f31688f622331623b71c54ab1d60","description":"","_id":"import-remote@0.0.10-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-dByOWs4HGR7suHBYZGQfrn80rkdFLfLPRekeZTh85TS31TEOHmLIYL5fztJaEw5r+AB9Km1fpjrHI09yj8m/Dg==","shasum":"9445f69c3f75fdf084e1ad4e20188b5a523b4757","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.2.tgz","fileCount":27,"unpackedSize":201971,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfpQjSCRA9TVsSAnZWagAAifIQAIAv+M0AlSrzJA0KwOd+\nqOrHn7OATNM+kAyhoTu8dXI0ddDtJ7+5WbvbCd/e6k1lGx/+0aIBzCUEAnoR\np3WFu5X+zqYwrewojEdYw/0/wU4Gr6E7OmYVW7gsNPQ2MHUfisyb7WOff/I/\nqoy4CUyVOPBhsjVTt41P1H+IwpbNSI5s4n7K9KaJ4645L9BQATr2ivmgknyZ\ngx7WoiUb86RBoY9J8nGm3WOGWfpBozFC/Sy8YSeu2HUot8bNvMSX2irZ0saZ\n/s82BesgKj8TcK1cCp3kk5/0maRbokfl01yPSpvlXdomOK1No2WfNGCAsjD8\nF3u+OUJ5/aIGOnADhylTxMcAL/wjPTmAJk5X6UfrCmixwJRnANCwg7uzz2gQ\n6IuveAk1nJ9YvJgtGkDEVVjJELSYQ5mK0MrsuStdGh6zx0urRnIypq3DL0F5\npbQuXuQRL9bc0W7IEhQHa4gCNZiI5IAZbWrXH2wx3xQVtCZGmiZI8/7slEW6\njkfZbYsTUDD5WOgXLx3fe4gTVeQT3OEViV8ecS/fteTbkHUoUHNSTlYkwuSO\nqXRT7qMWcTcprRYVrUgIRQCl3PLQ6KKmA89EmqMQdcRXInP09Bgp8fUiRIYB\npDmWfvAoe++kxN4gDzNwL73D3cvaRZHJtnyOeJFSyf3k7h+RosV9gzuMZ2pV\nUfGz\r\n=mckx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFSi7lnpuMkaELNq88z/75u1VmhDtnRsXcfvaUFIm9OpAiEA+MS+6sz0xRZc8kkd6JFn5+0CUXuYRdNLnlr9Ki8cQms="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.2_1604651218116_0.6928997771825756"},"_hasShrinkwrap":false},"0.0.10-alpha.3":{"name":"import-remote","version":"0.0.10-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```","readmeFilename":"README.md","gitHead":"85b6260b873cf85150f84e3d71ddc6e1fc6893f4","description":"","_id":"import-remote@0.0.10-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-O65qiclwfzCFuSWa7VISjuIyyObZ2gm3iXUUBwxG4E3RxLBjQIJUa2m5Xq696+jq3Ruj92TdtKUtLkt2Vp2Mfg==","shasum":"dea2c0572c6a73b5c3c6b39cb6e84f3150bcfc48","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.3.tgz","fileCount":27,"unpackedSize":202761,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfpQ+eCRA9TVsSAnZWagAAhFAP/1pE1bK20JjXCKAuGfrU\nHQPYoZkIDSPqXzumJK58DalWzV5aRfJQyQbra6JDsUaBcuTT816Rbb3wNMxf\nXJmG/iYOcG2FIjrbpsmT0IRAqWb5Yee0OzxG4TyJPHrysGmMkl3IuqqtHrlv\nNmjlHb4B3UYUqVsQcojRFHmWFavMhZ274XDlGGcl8mEN7zbri2OXaWNCuJP4\nxHUW0FnM1CxEUs9oq+MhCG38ktGkhOil/aXSxreFxo0wl2nP0b7ko3jm2kDX\n0tGdI/89ACI7X/RsP9TJoExS36Y0iHTpoVXoSFnhvcF1UbFqxNFfL6n9Bq+m\nbOV6N02/T+OXYb41r4x8lmqSykqB9tG3eAH14hkysEZJfMDz3FZdBbrA11gK\nK6UoArH0tzZTIePMmhK3qHcJ7SMIZrKEp61TdsQmgYW0k7Msb1tTeRpX0xfj\nuSJUk4OTLnxeBBbjX9sc5+66F9cg6wRK++neV1BjEfGKzXOeWyivxWmmITdy\nc+Xqh0rGJ35kSDkN7pofDLRVIEkTdw5ZbO7T+a0CHbaHYQOnB+5ZiQ6yQ1xv\nbFHm2VIlucUxD64GRTdPAYv4semeAJCWsoQnZwfHaJu9RRRf/0WMn5pzHvKh\nBp+7IrL0q/Ub8vJF+bw5cwu/6oScbC9/SEUhwbez+4x1ngEXk11/jC5iDu4Q\nPT38\r\n=VruF\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAelvxagkaueiHjy+lC7aJRUA7Pv5PVXBUQv8pFRhamlAiEA52Q3Q7NZ4b9gq1XEc9V4z82h6QZ4igPAQjK+4yEjeAQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.3_1604652957714_0.7634279497850138"},"_hasShrinkwrap":false},"0.0.10-alpha.4":{"name":"import-remote","version":"0.0.10-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n#### RemoteView的props\n\n- `classPrefix: string = 'import-remote'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `bodyStyle: object` - `body`模拟元素的自定义样式\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"4baf642e5fd7d07e3644c38d5cf3835944a2b504","description":"","_id":"import-remote@0.0.10-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-ZI6joMoFs2lKASx/90twq+yo+HdXov+TWJUvHUrPj/VFVpAXeRBVUzEr2HYLj6oTcTBkfNsWal4cpZH/O9veTw==","shasum":"c9d60ace75170268b0fced969e1c0345c74e80cb","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.4.tgz","fileCount":29,"unpackedSize":208323,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfqLBdCRA9TVsSAnZWagAAoQ4QAIgOmWTW0zD8CYZlK2wV\nklpaG4urDIJBoWWxuD6bxX7rircSX4wyQyOaVct0SepXRPwmA9/H0XP0W+tu\nt3x7BX34bsXDUQ4nG30gGxW1X/hZh018exjiiOG1GKE0tofw8kzI4H7BcqI7\nWH8GXkA/N1pRPg+YLT1/i/rOUFlIDql1zjr8RHscHQ0Ij5VbfrYE0Brr48os\nG3a9/ZS6wuDQK1H9704aJeyD0CS7OXmmRVU49n4ZxlBLvp/g87IuIGYNokhQ\nhFlNUj8Li82IG9z6q6jNlZRohS3DJdRsbzVDueZKJ5NfZIXW0aVssZi038QA\nPFj33Z7YwmpiEWdlas/n3p2tNXLNGgRCcjJmEIn9+3uS/5NZBbLUsdXKAAvH\nWqdNO0uHCfuraNbmMnbqFfSp9488GaY9z+55++vr/p5BTJUDLf6psiEZ0eqV\nkksqRrVQmLTITjqUoMhMnozXV30mZoueclepZRsXb1XIoQMBrTVKlx6S//lF\neyR9KBmsgxk1o5pJ4XRPbrd+qBldtkztKBkvdjkZiSsGv9ZU/7gXEfCJ7nlc\naqnwql5oZfpUUvUae26YMdcHLg/9QrpwlbIOvRPdhQb6AMGLk6r48qmecfQi\n3PlPStsIwws/Ys3z53aVXiJ5R9dD6F9YK8lJaUu3Mg20GXnBxRZMJ2Hie39u\n/uwC\r\n=IxU8\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHKmnIB+8oTjsqrSPEyNEsjvXnUpQYZVYaMxHJ4rkJHbAiBsZ3Rsem/lN01KR0uFE9f07ionUKREL3IW2R+dveS8YQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.4_1604890717264_0.37336980274824194"},"_hasShrinkwrap":false},"0.0.10-alpha.5":{"name":"import-remote","version":"0.0.10-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `classPrefix: string = 'import-remote'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"9dc969f1eb932bcf077b6823386f862f1a862248","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-wS0tqa4C5mfO0c2h5lYZcZmwb8fx2iaS7w8glDM4haK0uZ2HPNj7njv129KupDMzdE6wkm2Ub2I08Sfe1Jts5g==","shasum":"7d2f5f00a064f3603d59c83f1b26fe230335b1ce","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.5.tgz","fileCount":29,"unpackedSize":213182,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfqPT9CRA9TVsSAnZWagAA5Y4QAJrbXtjX2cWVR42HWAPI\nTGnJ53X8LPVjKlCDKkYKYFhZlS2oDLrOx9CGR3U7+Gl9PaYkAKtiqw8XYfpc\n+mzkVcR7ujAQEjZMAP8tRNnFtFM6C23bKTfTw5A/e4ZeDkouQn9gMbwBOKU0\n45nHqABY1tkC4+mfpCsiyEah9fqkxpUZRF5ygg0SZ0Ws1rj81QlRjLn/BQxF\nlBPE+RZvDi2PHcn2hyzaGoVzQJI688BkEoN5KGZ4RyTRPswSuUhx5XYqCkoQ\nvfXMVZfOzoUB//PQOUJlQ5JGjrAG0HJelKfh7Jq7ZWyDWGOtXCjUW+MwDCrF\nKAfsgMR75GJv7eWvF6pOQ3F4X43sSW2lPi81NrYqfG+AXei8hAk60eeM3k2K\nXbqriLtInrdSSlY6Fmk/OCC1vDlPQhv2K+1tssLu3sz0bY+ZGM9iRZLTpig0\nAg/1NT9SdbUHKikOlXO25H25XQewWdL5H48HCKNnQP0NzLkr8kQWZx9/eJTe\nU4+Uzxjj1eZugjb6YJy6TJ6tZMyEHremaSjVjWi70R9je1REFr5Sm2IY2n8C\nqf6Nu91NVg8Wn5DYRb66J6yAIPtNKYUvFTK/QfnP+w3To3fYXichSu8YZflO\nEtJuf/q0lL80pdNlpMaqbRctoAPF06rJXrw20u8pTXEu3Blgb/67AlbIfO3s\n3h/9\r\n=3MWs\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCR7tLYNo2EUuC4w+J82U4RHaKq6S7LmCEl9yj5CM5hmwIhAJehMnF+Al+uBGXAYacpJm1p1OWk/ZiM0ynzqzI1b6bV"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.5_1604908285087_0.08382813106887377"},"_hasShrinkwrap":false},"0.0.10-alpha.6":{"name":"import-remote","version":"0.0.10-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport importRemote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await importRemote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(importRemote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await importRemote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用`importRemote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await importRemote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `styleScoped: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `stylePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"9cb1bb25c3dbae6250191e0afa63d895d327e968","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Vje1cd1N9/cMfvbJnH1Hxf6tl0fcp/DJ4zqDHz6v5c/887S7y2v4TxOCViO79djMhxAXFnIyqk2iDcASfo/tQw==","shasum":"b2f5642c888441673b21113d65c534ae89284675","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.6.tgz","fileCount":29,"unpackedSize":216950,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfqkT0CRA9TVsSAnZWagAAzWkP/2pPs+sCuEAP4u7BoegH\nbI+9Efc48QF8PgWzpmzOIUZObMgfhcIgnddPhY9AraYZ+/qVbrpN76iLq65r\n/0wX0xFKZzFpyTXarjip7gqiGlfeHpVHCa89JE2Rf8TOX4xBiu0Ji6G1xlG7\nGOVxzeotSjkY9CPzgBsQFyJeP+3M1RMEmZcQkzOKfVTo3rvzLcsg8WDbvB6z\n2arN28pChGYr2b1v8+rDCSYrU83AtnqH4Dxd4XZIzcX+x8ji6G7gG2LOg5ka\nGc92N5SKAmZOaapphM60WW37bpcZUiR2VLo1Wwh1riLZdY9VssKa/Pr+HqI9\nL63h7axyv5az5uNgeNDcDEyZKmp8BcAvcDSaLQtJs74z8FTIJBdKMzgpGKCx\njg0Zmru8WXDmS1flUaK5x6dDuNCb2De4bZxZ8TJd9C0pJ8/McWgzny3fZQIq\nkFvemy6rQJkRv8BrkRzFK9+VVUzNaUyL07KF/3SW9b6o7NfsqGf+vKxfN0SA\noI50ZQD1Ni1r/+hempS4md/aGG6hYOQWZF6YSBVuWL28hCvy3PZtFb4TF2Ps\nEw3f3Q5IqixPRMeDERsHM2ryZtwSGcB7X3JEcGCCzY/auqnZF7VS8X7k13/E\nSiVDqSp9PGotxBZYv/foN2znrXqLCf5Cn52ISCqYGlPw9I6qnkRY3zKZ20og\nICTb\r\n=yzqA\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICl6GcPt3lVZ9FeCO3yHIOHERJwY+/7B5By0zNVYzxK6AiEAmhe3vrC5P5Koq6v2JzMvzZjNv1wdQv46ivJSSKGQeVo="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.6_1604994292361_0.3485073453087919"},"_hasShrinkwrap":false},"0.0.10-alpha.7":{"name":"import-remote","version":"0.0.10-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      // 如果后缀名为html，则将被忽略。template建议不要配置，使用内置的模板即可\n      template: `${file}.html`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"231735178d07308692479145a59f4cad215f464f","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-g7qGk8+iHYkVeB/1KQWjYqJU9cBwbrbZ4cK9erGJGpH2ZWZQL7mE0giwPJ5PVeQ0imuW7w0BQc3HP7N5bZq9EQ==","shasum":"7a3f56c75b880a2f951a7431e5d251bbfe2830be","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.7.tgz","fileCount":29,"unpackedSize":217111,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfqma8CRA9TVsSAnZWagAALFYP/1Nc8R8SA9jmDo3lw0Of\n9wV7K1RHluSTKkdvFlq1kqEW+m7+VRiTogWp3NtmzrkZVPD+vFCnNbEVXazh\nOptH1IGM2f5s96BkBe+ubXA0X08BMGVSupN2LkcbEPLZ0In6j20P4V7M+wu0\nFIGORs1Dm9zRZq68KBRWHj/6FYeDZoX79E49mmVgfpn4FYaMXenAxseEQax/\nM2LulnTzdwgKXC4dANLhyZUDYy7WZpDiAJoVqQSffV9VohQvtGhnzXqQ44xq\ncHfIqgkWwBlJ1UOXqdnddRSHflRUHU/gkN6Z4f4f4tt2G+7YjejdwukWnnur\nE+ymP2qfIZzA3L9NvmjMenBYRsN92TUhxpXiphGSRpQ+jJBscXpWbB4pVMdJ\nOJixEK2SlFYgKUefgg4QRFndQ1t5wXG/RlovP4V2yckWRYxsaxR+L7M1Swdr\n+KgR2cFFhqBZpWZqoviI46Pwfrp8l+ijNQIjR8VM0Vb6us9IT8Fyqgl52+Uk\nUuw1voisKPt/vUIRXXYRm/qZG4BCztxLj+DE9Gn7xoJrJimdBO0wbDlRRLnL\nYI0BZ/LAFlXPcMkxIvLsQbMsAl/IT0KtEsO225K84+LtGMsVax4dmIbfJ2JT\narhDYALfASq81/5HpKL7NaWtonMnSmQu51pDxu6BLt996pc0c3YC3TvYVbs0\nVUKn\r\n=AxyT\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHQpanUJL2GD9HukbG0zMjZde9reDMaLkS3cTW1gsgCcAiEAyyVAUc8q7yXiYfHwALCnXfjRc3LteTfjEhisJmtZNro="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.7_1605002940107_0.027241807487032643"},"_hasShrinkwrap":false},"0.0.10-alpha.8":{"name":"import-remote","version":"0.0.10-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"7817bfa4c8bdba40c2d9ff4329ffbf0b69672205","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-+YxgMqvp0+bEidPefs0ju+S2+A72Yr4M6+6IxY0yTSgzOSmKAy8unqFtbY4XCZd4lCXCAJ0ijVoctqzzrQohwg==","shasum":"803bfab8b692d40aa8ab0588c92ce28732a194a3","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.8.tgz","fileCount":29,"unpackedSize":218057,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfrOX+CRA9TVsSAnZWagAAQbAP/RVCSySCIeBxfCSTxzWB\nplWC/9k2QeI5ADg3B3nZiWAdbvPun9WWaiQzaD28MSFIaG0q2BYE+atq/FV4\n+P1WzGUmrXAaCFr/gH+fU4m1xVkTxI29g/o2axrEVAfR796zi590/unutaMm\njc0R6LaJcrTTvv8h2Xs4yy7aaFck2E/N0eebuK/64UZVEPxgXhGLiXHDcLH4\nYpRjb8P2AXsToFqRy7UoozuYU1C/m3KqB+w0Gxpy1rGWCo1Z1kpMA6VyDBrQ\n7axnzbUjyywXJYJ9yRdJte9G5zzGfq2hJkRywytEKkUYMLgOBqnMKbIrfwYw\nR+UGih9G+rMhsAMQLH0m9PcwLlFMILoU7GJLvnssKSgllwyvyOhWFdzevgkA\n7V2JzHlzycBUTj+uGjW7I1LL1F+lbxfMAPtUP4fLISSzRGbS23G2Oxwjv6Qa\nCoQSMe3wkpRfjL+tVr/lt4xsEI9sjmONERUh0tjI8sYnj5YOBSyFAV/X4O7U\nJM2KxulI2d/NY6yN9saHaU8W7CK7xMVZNbS2wR/STZSQBflMRw374HrxZsLc\njGLavSNzPTWqjzF2sK1l+M+ln5yuk5hDS3S2cnnyNwDuzEpMIAY3vHiTe8/Y\nhBdTTNiRrSalwoNdP0ZKdadDJZdR5V911McjPyTPgWeT8lUWHrF9KwKoSdwu\nCZ2E\r\n=KfLd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEYG9ZOBzx2cV9YSZeDfHYIuj5p0OEAekUb9cj6CQ2jOAiEAzXrAaqmq35San01IpZkY54d6GtE8pI8Yb4EgiZ1HBf0="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.8_1605166590151_0.5030001150314785"},"_hasShrinkwrap":false},"0.0.10-alpha.9":{"name":"import-remote","version":"0.0.10-alpha.9","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"gitHead":"6f081b5a19d0ff0efbbc22b052c735e165cbc37c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.9","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-PFgUQIS7zD+UMWVshiav9i8GRrY/bfFqdfpolHVN62Wzd/+vBZpFf8wpUX3vPwRB4yVhW7kjgUKWggcDBa/gzA==","shasum":"7197745bae34d1129f7c7ef7eaf17ebd47cf463f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.9.tgz","fileCount":33,"unpackedSize":232924,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfrgeGCRA9TVsSAnZWagAAiZAP/RG9xJJWEw90QSFmqHSr\npnb+kjOyLalGOZDAyZBRJGiA4UPF9N+nCXWulqs4Fcu+CkXoxQ2fycWu3/Uq\nF/yuLMQqhzlvK8VDdaZBgnr6TQIMP1QVxNT4dcsup+tiD+O4hq5GUhlkxYPB\nClk1NDoRi/dldZoMbWQ8kjXfHRpzw0vNc5ZSTsmmJCge1geDowkfsk5o4WZq\nfPbXcndDiC51k4i+Tk3r3hvlWCh3m6/K2+5WNe71RzpDCPHscWja4RZvpD6F\nzQpiS9iEd9B/E1DMnIMW9eHGZ+2QIVoS6m+kOqOmQMl9KWf7n9PhZZJBPfyR\n998AI0ap9g1oFGq6vZKG2nOs/Xx4zXSrvEBH74vK2upmQsrl9GGQJqDOr2Gn\ntsvQ0XyEfo/8GKe3hV9UkGPc4/xOhwdckJjvfwQHRFvSIaxz0iOK9vsD4qdS\nIJ0QxV6iE+oLg/TVYvEa9bZQtr1YvlW+PriErvlQWMpYgN+1bfyvDjm4UT33\nwnC6sqWFg27y9rDSopcYXKEDLpCBfnvsdurEK+vDbfsIEaqwSxAGll9xTbZY\n20MCrsrYiHFSU+dxepa5a4FXTS3kUjrngOCqe6DmleH4ioy0ARFv0tk9JiS2\nnY/gsCigy7v2fDxDi913quJx3YEr9I/W8gXB0PsKsy6uPfWizUWTb+c4Geh8\njIib\r\n=yep3\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFVH0PnvIvvaZNs4Ydou1HfA3tj+Vi9UVSp9i19P/XVyAiEAk0PtcVmhOeMlj+6i9r/iaAodphIl8Xl2Iv8nRfUQh+U="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.9_1605240710242_0.9487904218719716"},"_hasShrinkwrap":false},"0.0.10-alpha.10":{"name":"import-remote","version":"0.0.10-alpha.10","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"cd6f50ee7508e654677ce470a3c639240b1b08b2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.10","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-4bkNZStnd39DQ6UrQeqWAVWWJ5vIcGh4hz0bqIQSJYvT7fJj3y5dBoNkiH1fWBupsMHgFdfAgnerohbVGfouyw==","shasum":"078969074c9446255d0ce03615660885d45e578d","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.10.tgz","fileCount":33,"unpackedSize":239898,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfszOVCRA9TVsSAnZWagAAUMAP/j61AskJAHyehFFbdmKv\n5bDAt7E8gbt8wsI0YImDa2YA7XnwF/SDSw3VotasNec4Efk48VUC966Q++sH\nTo3OwLqxX5EswusVa5Y/O+rVpYPHRwsIbAG+xxr7muGaBLV5gZA1sPGn+gzc\nV/B6OwBDMRFXNNAKBP4dkciTbbkMl/PH48sRY/+gofw882+i4LTDQItS8d+N\nHuUixmzvHTe+GMkocWe/Fqf90heyzmgCoIw4R+wUtLdr/Ctr+8sd2e2spYx6\nY4mh28dWi76gFFYB/ThWpiMLIyuCL+OCozc8AdlkypXcYuQQjSVPByUJhxt0\nniCwJmC6199m9wup/FmiFC8MVtGTM8NEK4R8RGxRVMlJwwdYBcupeDUSfM5R\ngrJtdOi/6BrNWzsuETAnsZ02KSyDhgBSbADX9nuEOUGxlzyiq7uL0B2HGmr9\neqCNpGRfQnCjqSjn9047au6VNrDPkVGvhGPoUNlbUp41CriKNPfahCngQl80\n95HuFhpURjgEwmiES8HPhsqyQNU0dlCqu2yMdk3tG18Ufy/BURKpqoW6L6gZ\ncSGxq1GfQI+RguGVcySLG4UiRe7cujtqbTM9XqgtJ+lw6L+MV5bxbsJK5xsU\nS2NjDO0H8t7JD9WWoosDKJnFBJaWMr5HS6vDKvgrYc1dnAue7pWhlMsQsV0e\n4je9\r\n=X9UL\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDhpk0VXwPFFl5zL2nOunF0Peo/eHgSsgtWfzoygGaW3AiEA6rLlOmZOQYI2OTO2Pzth8BLnMpZUUnaBEWVKVjHKqQw="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.10_1605579668867_0.4770005001894462"},"_hasShrinkwrap":false},"0.0.10-alpha.11":{"name":"import-remote","version":"0.0.10-alpha.11","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"3c0c9afb91e7d26b8be3778c842bd04f025bd814","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.11","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-NEtjlWE5siPifaIGfzHGxYwTK1oamHvXitBd89JL9JDyep4Vtjn1uDBCU6/oiCoJjZweTHCw6aSR4DeIYnfs0A==","shasum":"8d7b1346cbf7f9026a8619dfd41f72a496688e2b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.11.tgz","fileCount":33,"unpackedSize":239937,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfszm2CRA9TVsSAnZWagAA3x0P/3AMr41OTIH6RNMqlRJX\nNhD4wYA1DcZj290sn/ReehVurkwqeiGeQWcy1jRGrKFcfohtqwFg5T+ihFn9\nJBarnfeN5+o2lLt/ZQE/6qUWH99m0KWYZ1DT7JOrB2mD81FeIsTb//o/EtRv\nRmf9gkweDZDVvmVjGuIQal4Aw5tDJPinclODAG7xPul+ctXkJMgdnBm0akkj\nTWJFTqzAelNJA7huxJfBP7xVvEYmK0EHsLbZyg3tU/d1QBh5+08DreM8jSy8\n2IbVtLzijhLhCpLkdvtFLmgbrmv/53jEn8Zbd9jNaEa7MvlRV71vuTrYBuxg\nFTmIpCW/MKMQxfpdQZULj2YX49RFx9eXpZIS2/fcE11CBPLkqjv0/gozUfwB\nC07DK+v9wtAOTh29YW/k7iFxjm9EDBQYkAxM9ci9ZK7+Te7XBrHIM+UeTqVQ\n69+3tH6CMfAvb5Oaovl+v5yjAp2m/P713UAYSDtpmhFb+giJw8puyFrgdE4e\n4NSA5smAysNxyIYsigEgMWLAJpHi6VYvufY9PshbUk3Ge4MyeE1tAlbN0iyS\nKXxCOgrfMIR3VWR6N1mg3BDDDc1B0wzpzTL4mmA/8bt1n9kAoXGrIK7+FUiU\nxC+lo7f9dPE4+I8H1kFoQ8SGKi5kLhaCHLtFVu6bAu3zIg/xuMFH97OGE7r4\nBUaC\r\n=oH4M\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAMc96+KJyOOOQI+3xuZl7GCNtOfoFGLcmc5yzkkAHkOAiA0QeVrLXyiPeGLP1H4rrgsFa4hJuCBBpJMZ45toirz8g=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.11_1605581237983_0.7110370716851888"},"_hasShrinkwrap":false},"0.0.10-alpha.12":{"name":"import-remote","version":"0.0.10-alpha.12","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: '[name]-[chunkhash:5].js'\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"f51d469b055eed4391bb1b603961442720dc1720","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.12","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-TI8Ca0RRZYYC0N7JZQiJXtvnUTwPtJ+JztDHQkR9wDamzQPcZFubnASEZV1FrmfI4Mn/O0hF4HaKG2YpDIX3Eg==","shasum":"0b93f65a12d120b94a97ab86cc5f3b9a3c3ba0e2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.12.tgz","fileCount":35,"unpackedSize":352493,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJft3sqCRA9TVsSAnZWagAAL60P/2iDEKRUUfseMcEhtCmD\nz3qz7kT5S8dnP5HZGAX71Tu4Lg10Q2ufzzmG7TLqc8HP/zynbr472X2w5K7y\nM7fKFx4AbzRMHQ7VbwNGMmxtCo8wwb2Lsyd6Sq9kqZHFi1fd5fkP42wQpeq/\nVaqzM/0F+J47CSBMJ5P3cRtU1wZRq+RMKBz2jSNPvTgWtz455+uGyk6kC2qR\nrGuW+7W7XADXJU0uGzgad+RMBLKN6COiwSaanZ0PL4mbP69INbBlsPPPJ2Wv\n6PaefbPSAIqlribXiuxMauejW5mwV6UKzPg7CQa+g8SYunP4oI8OqR+pyLEe\nTancgJMakvSaNg5HUrpSVMZ6Mz82hGojDbL63i6z1uYR9vLtsJ6HmryFJpIz\nqYb36WzOQrD54Wnh5jMhga03QV7tqGMh3ZaB+2laulrkFNNmJMaXFq1rFBTg\nAxwE+M0yz1jC/6AotKqvTdRYbj8s+rxxTMDDuetbxt2AGq5mmCeVb2ykvbzL\n/qVP9WYX154uOxN990g1WfcwLGcgXxHPZavHJoMfARzc5s5MoVNBfjnTyQLm\n+UqxN4AqJAwyKU3KF51pUrR7oMYoE3KbuuzBi1yMQ1S08AjYk8v5Y8OL3NHU\nX/wHBhTItQ6NYe9XkyGJnj9pnDr5opw5VwwL35lxXpxS1Q/wWf9bKK8uQjHT\nDb/+\r\n=iePm\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF9x9GBwn168Iv9ici7pBReyJ1VWjNTfdzYnHX5QkHT+AiAqEKmmq/B+SsD+bThDqlCLFs/R0l+jW4vyCGvXcdLlvA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.12_1605860137924_0.7193088463861297"},"_hasShrinkwrap":false},"0.0.10-alpha.13":{"name":"import-remote","version":"0.0.10-alpha.13","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"793572a75f4d979c5ed2bd35d0e6733778bba5d0","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.13","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9ga+j+4w9llnlV2BSGvcdBTessLJkjyFg8j8cB/2NKkCMQZlyviSB182huAGNmsED729dMMDnp7iBycqN2INWQ==","shasum":"6f28983192c39b7b64e7ab0717298e4142ed09e6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.13.tgz","fileCount":35,"unpackedSize":352533,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJft4vgCRA9TVsSAnZWagAAT8IQAIYZvN/GdV8YiwCv5KKd\n673QmdSAa/BZu4X8ogvPkMMcAh0GP1vHD9r7Vc7G5W9PJ0xdX2ZqUwkNS38k\nXOH/IN3q8zKTRRVkuT5Ho4rCPSm5m2VgBFsgs4zePQbeJWEkWQtaozbEmJui\n9nN2ELn9Nr8kqU3HsIA0uChhxOJRZCfJiCkQGk838i2OI1Tv+sF9ZG3o6ohh\nt8vbUTT8NWwvXsqU+F+ofjlqr6sWUh/Ibja/J2m/Xiu+Sm/imTS5o/qySI5o\nGu3p1bTCbueGctAPeDfzY7jMEzDNNmQezjapF4OsESpAi0nHPH1I3tqTDd/K\nsHBT6GBnMuNGd8GrwCvGs9/Q8ug+1H1vS4bDPhJRiaK2nsSXw1aygFceM8nl\nMjGWMhHXvLiX/d4OIbrDHcxTieop0kqxF0OoL6UWCaX1epJaEyW48eN+i7qb\njokdlXRK2PKaXOniUajvr9SNBbxevIE4l2xitrIkqsPR8h5ozGQ//GPmuggR\nLmUYiJhWv1c+hvCPrtpttDFF8j4mjh7vFiMh/kxViB3Ak4+lJJpnaFbFl+ub\nRPLoCVVFrxhTyThlP3RW7fRKDRbOi5RUu13+/RcDA42/xcnQqELOBC+mw4dG\nLbg2GeYMu/eCkVWYPdnDDCqLLFsmqxmf5KCt5CqKl+CUuVX+4C72z2ZwdOSZ\nFIzC\r\n=XZyQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIE8REFS9x5paIyOKUWSVmFyLrlys/dx15XFrl2ag8ISJAiEA93QOqF3716uPm0fXEtC25xh1J9bOrpV8TJ9pkqmn2mI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.13_1605864415920_0.9647515348800562"},"_hasShrinkwrap":false},"0.0.10-alpha.14":{"name":"import-remote","version":"0.0.10-alpha.14","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"a5f49fc3140759382bcd6a2c15488915ae8de1d2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.14","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-11ps/kOxRQC7hCT4fC9Hr0uwqRQtT3IvYM6PkDofN3fFAxIpcExSD/RY16WHZhewILJMlPiE7TKKcoaqmzYtEA==","shasum":"aee3288dce0756ee647422687e9f7475268a6869","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.14.tgz","fileCount":35,"unpackedSize":352343,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfuwkvCRA9TVsSAnZWagAAP/8P+QBUjoL9TSjg3G6RS7df\n6q/7M2I30p7zywUspVK+GRg511IdmS3MVLSYko1Pu/x9yvqEJ99u49Isw/Mk\nf5OTTSjmL41zvtoJogBGx14qiomaueden0WVYAmfqHAHjXpzruRdkbJzGJgm\npDRrOHqBNBzWBDpnl4X0IWSyAtN+NckAsGckDEIhvkw3rjENlr753HQqne+d\n/8ECXg9a+3MN4Zyq1k91Fq0W512rTeXWdQDU2gFCXSuPBBkV7d5A95iGiA0s\nxqgcGIKBiyADMrKEtbE99eedHkGQzKW/AM7jJ1plZ9rHTQCcgEF3uPQXj6KR\nAFH7wCbfl2KGqLzGr1rIiCCWPI1sLAifzNsAWTjbC6k/hzgIjuMCJz/2GDo4\np1d4REXETRGrgYN46yDP4yiyQuKKg7TXs98pRaLp9SGsVtfhcEHmTvLSTPOM\nibMi8giwdmwKA/96RHfrbfoXuWJeVyYyzZmGHEXJ8vZITGKv5Sn+to4fnbIf\nwyzmVY5GNVxcpj9yaveRUPzihbK2BsqTXzSdtKDfB7MUDJ1GfVJzhL7mQwPa\nxMIOCI2EgJdBcZ+/5fIQr3UtxraiqcqaCF9/JyLRd8s6lmnZOAyLeLYYYnNA\ngzX7v7UMcFSiz/58uyaEAJoaP/fNRLT8LGKFVYu7OKCbgBPQzS5FqbZdxITq\n129L\r\n=tYeC\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGVPoJRtrmtIfNz6UrjAynmYdZYm7N7apWiJy943vzceAiAAvwSRHVRH92ELgIZHXGFA57tWW7eXRyy2COTxnJ1X3Q=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.14_1606093103392_0.9489445555249736"},"_hasShrinkwrap":false},"0.0.10-alpha.15":{"name":"import-remote","version":"0.0.10-alpha.15","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"c514eaed7719267568469acccb2b1ff71ba9a865","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.15","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-84ePKckUjljLkRkIHWmOG95xAriuipimafcAsFUxsgJF42h7kdHk4YG19UrNDzo6+CAIkQuiy1JBKrjlCpEXgQ==","shasum":"20290820fabea5a69e6b1428d0690cbfaee3370a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.15.tgz","fileCount":35,"unpackedSize":354710,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfu04fCRA9TVsSAnZWagAABoUQAJmj4wGWuhHorjGRKOHQ\n3O3Av5yz0t2xG0wYQkVDOh/RlBcSVA3jvmrNdfrfB+HXmE2TqpqVFFD1zub/\nVo+q0C4kfiqA2F/qrSYqGA+BdNd1DeLotgSWn6QZDwLrLIQ7ORwg+PSuM7vL\nP1XqZ/LodN1ML0tJzDWVm9D/KeEA6wlqhVs86sOvgzBeblNT5WwE7Juhl8sz\nmNUEq/KZnTIKQuy8J1xzXCDGn1NPtI+ojCpMMpsF+6qYOhqCeVejlKLuazEX\nBWwEoR4J/30ZJuJOEgvtbSGIRbV3kS4fdc7fO07O26pa6evpITOrMpKjJHJB\nnjEmnFueZnqy5xtMNx+7B8OGMWZQS+TeEpCW4gePvatDdfUeGY2ghzqZq4OX\nDISp+Z/1eQWkq2nyIxF0m0Faa4Vn3n/kM4vEk9rgi7KA1FF3dgyxjdFNb2G2\nbPC9cJsXbl5yO0D62/lLM/FyWGSi0Gvkcf6GfilKVcfgDgrG/mysjYOWIEsp\nIOQibTyBifD15CYAjCmbntRau51U7SLNXghCiAlOL2dNncqFWQuKa32bkdb5\nQhdKqcHEOCCTA9+I0Qu/EvazngyxyvKyPEbdOYkx/ltd76f3M7Uogtd0Vcin\nGvhRka1I+dkO02W4NnEQTnhfLXcqDD9LPa3YeA5hO1OJbK8uo9y/hACsn9HF\nI7G8\r\n=ulSc\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAaWs5JETeUW5+qziOQRItYFti72zG1VL1DpC3/U56fZAiEA/xuItJdBYn+XkxR5EwYylwKLM2W5+Z0XgIyqgcV9JBQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.15_1606110750991_0.5553411547471274"},"_hasShrinkwrap":false},"0.0.10-alpha.16":{"name":"import-remote","version":"0.0.10-alpha.16","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"8af158ca724ddbfdc3543aa12e81afd927900d71","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.16","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-bORxLOurgs8kL5gU0ItlI6AUKD29epVGh+rsuo5pzo3aKgcFIcLnGERjC1iE/aZ3rtGB5ccK9VO0yxjLNnoQVg==","shasum":"8bf591ccc1a024ca2b65b49760bdf34904ae6133","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.16.tgz","fileCount":35,"unpackedSize":354710,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfu07WCRA9TVsSAnZWagAANfwP/2NcBPr00uMg9+WxJXKx\nApaHNEsRRiiWbwzpgXdrxZNr0eMNQ3n/iZuo+65XzdPnXpDFRjmTDkCCcgdv\n2WuU3YuEl0hrHQWXB7k04E0EyhmGa7FJOLArSF6+dt9szIzLarvFqwrRXV8I\nxiY8C3cVHftqkZPzVfLz68ho1w1UPkLH/yAJxfwS9Q/i6qwIf70q9r01dPvJ\nUX0KmgZIeO0h94klY315xt5ebuLhneryMyhgUKgkwMxR/+f5muaTKKWLmKpI\n8hw8aA/yClAFKR8LcoQjbBYPkapo7ykskIWDyEIRWaW5OjJe2hzlYr2CCB3d\nuEDtbgBb30zZBwS2asbLro6xEaoO81U5l2pBzQGqAiSn3n7gI+RvhRGG4MkJ\nTkjN2UTbl1vPhzf79tqkoKwj26LHrUUhH/SuimV4Wa3oeCd9k2D9ysyUOGlg\nSf+5MqysbrLbHAW66H5CsJtHHqwJ85CwlCxwLqZoQZoXz8eL1N/qacGgn2Eq\nZCRSgdFtJImLTHoGEkWxNDhzz/XlKP46MhPWSrskfsk2bFQiCbV34VHm3H8P\n9emlZ4v7dKo5g1YbOaSaEKnXuAPEyNZCf8y1EE7alxFFYWX6j/JKB4fb6uaM\nF3YdsFD6rMjMyMDx8sub2PQGsrbR2gDdXZ8OhZhrQkbUONNCRc2ZD/C9Fzhs\nTkAx\r\n=mYhz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFXaKSRYswXK8ap0C4gxnIqopo6UfYT4GYbW7kcpgN84AiB4pGMPrXg2LIokaBRHLahviqLZYiBA8ptr920086vDpw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.16_1606110934352_0.12930963303815224"},"_hasShrinkwrap":false},"0.0.10-alpha.17":{"name":"import-remote","version":"0.0.10-alpha.17","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"b6b91e7b28028f79748e18ecc6ee76f8e5f6bce0","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.17","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-GO3HkqJ/CebpWfA+24X+Xj0KW9EaLFLAXnsgrgmJvL9oWt7WLmEso9+zbz288JVltFxL/HJdMQY0ib6v7ksmMw==","shasum":"927d7597bbb3d54f8b473dc4c4a4d3b85d6f2cd4","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.17.tgz","fileCount":35,"unpackedSize":354716,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfu2ovCRA9TVsSAnZWagAAc1EP/R8L66hPerDYRjZGhPdi\n74XqbHrTWRz7aEeme6MnlAHbvhy98niuChx1VUry1pfFYsnuyoRRE7lNKSp9\nF1z9KZsBNDfK5OtnDTDXvtv8MSTlK8jvBvEKsiYWfKGOpiz1ajyrqdbOih1y\nFkNEyXSDRTKgsVurRvGL8d4n85qbtuMUuVswzoW4vfb264yN97aD8w+os9nD\nmH76qOcwogQ5Iei65UoqjDaEKqV/i+VD3yGgCNLz9/DNMVC6OzkqATW2rbaH\nRE/iDPi222CSXVUgV0168u6WiShdpKKSvOFLmvBXIigS0NN9eN2WbRCUQwwM\n6qvbWWzEfak8uBmxAu5HTjXJ0BY/Y3dPrOVOg3O0N08ciAsJOwDLO8hBjlCh\nZr6zmfYshJ9/AQyioS6B5869wTgEGNGUi7yatGbphXFrLtCmcsmWm90vsA0t\nnvUqHHYseE++DFIyCbVW5wdXLPwrp/Zp6mR/Zcksm02DVAEx6FfjvvxJSoDg\nng8NqS/IpPINxARRrCFAMYqVyTHWMSROF2V/zBy0uo6UuJphf8AFIFlYS8ba\nqOk9IwrzjR6TaTXPomCKE5+MO3r5fFmE3KJTlVrWUadANA8EjekiT4J4lzZT\nKoBc8qksIv6iQnNQAWZwnz3q3bhj9E4Lll3iWEpgf1INZK7Hu9clVj8sQinW\nap6Z\r\n=reQ/\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHR8/ttWdTfM2c0mZKrZk4Noz4dh0JlOWFg75pi99VtQAiEA4x/qihcs4Gujr5r1wU37/jBELxD5r20O738DJ3qExKI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.17_1606117934876_0.2893592559953755"},"_hasShrinkwrap":false},"0.0.10-alpha.18":{"name":"import-remote","version":"0.0.10-alpha.18","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","js-base64":"^3.6.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"6e86b27bd909d03c14d51afef1b599e739140aec","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.18","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-K0+PMPmwJvxsKxrHHwq/9MqE9XzAwBvMxRYnXuCrAA9HXCcOhzEGTAmsgfBe1tsnOtNwBNPoCSieJiw5ulAJEA==","shasum":"170cde67f15087eeafce28938e84d7c05fe29564","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.18.tgz","fileCount":35,"unpackedSize":354944,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfu3PFCRA9TVsSAnZWagAAZLoP/joLHj2EoE17fmeVD1Ek\nxdT208rf3OR+6ry/iDmXjop0U/ikmL25q4e0lW0/iFoZk2VBOMRkNeMJqXlf\ntZOvWvZcBVqaJJgjVB1qXkJ3OE7O+/NuhJ9xgDrPAXcA+XOn4//Ev+olegcw\nIloCIzq5VhKD8DnK+ViQ61eW7YQkUMxA0uFK1+RT74ymaJOpiC7kfkRfo6Ty\nXv45OqbmYgU9VaKNvW7n0dHoSaxdHr3ynWpfz30FewyPt9IC8Uph9fy1xmp9\nKVTVsX8ufcMdRGoZFdllFR/8kXx0DOAQtTA3wELc4zdBAKhOZKL8ZCTolq5c\nknbuoDPtatP3WQh0FU+h0QAX9rfL20j8K6exnBHZhkQQ1AL/YcutmmCakAoa\nlbAvz1H66w5pTN7mtPvO/yeMq8dIyze5KsrluWwprDXXfABOlYd4ennmTrTd\nukSf6SGdnTnY5/Yx1A5D9dh9qqyvWjVXUNh3kq7SIH16I1eT0AxySyxwbSjK\nFGIEuSrE4hOKTGkjzwGpSEZtCMmJYnUn6/YYsNIxj1NsXh8WszFP/EwSeWSf\nYaj383N2B7kiJSHr5Xy0W/RAYk0r/G2+SwjCJ9/nYRWucWiJbZYCzzfpKwYq\nZMX0qgcJDlZRfZ8rxb+TvfW6qLZMgFnVFAKpmQCq4MmbJEAGFsfynQHgg5Be\nZn/x\r\n=C3w2\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC+ytwXePj4usEj5OrfB9ARUUZ526qXAJp+RlS0n43ANgIgaYr7ab9DeDBCXbXMveiPVGlLvtqeQbc9YfEtFpKv96s="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.18_1606120388777_0.9987741439028721"},"_hasShrinkwrap":false},"0.0.10-alpha.19":{"name":"import-remote","version":"0.0.10-alpha.19","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将场上从这些公共模块中加载依赖|\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"88b7b53134904dfb716a42cfc695b2e8b5ec73ed","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.19","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-euUTJzYdE+8pBvpKYK+MUP0lM/h4+oqswy2mHFK5iogXJl8huyQsxPXUGGQKOQE7QyRrHNuqSmD/RZYameo3XQ==","shasum":"992622b8b1420f6369557007f63a9ea1d3fb2338","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.19.tgz","fileCount":35,"unpackedSize":355062,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfvw4mCRA9TVsSAnZWagAA+DQP/jY4wFaBj8xgevdXEsAB\nDmZCpDVBu8z3lE4i7++/kXPDGwJxAYnkRpIYVSLofVisCZnnqkEtvuEwA/br\njqbfnuW1lO8VrPZLF3m2ib47tdsg0Rb+prh7FG8deEnrzDCvNwgDGuI0tdHQ\nzAu8HgCwHWtdfz5NfLK1khKP0Z19D5RNQfFQUujPjlkpFu/Ti3oQ6XKN9KgP\nbzeNHQV4Le1wXvoYxWFp0HIrMHnNFOVktopAHS1vBruBSeAruevtYgBLXOlh\n9iYb9qcCaXTLCgBPaUtc5cVkKr+ZK/hC3ih14ErsMOhlRZLRBD3Ve9EnhQ7W\n6Sp+tBCzk2DYMOanq3D+ZPsGM6Txb0DoTS/fCKIL68hhWqVTQYV00MyLaouH\nzxd8lzVeGZ/7rOGtxrb4YwAtqPzeiy8cNvaAHRuA4eSW22ud1hl8+uCnKrop\ngUaRr4FFRYffxQVxj3H1ziesj91QRvigoFEbf4P9+sKly22NhsayJ2M5AfcM\neCH70ajvHsbtJ4iPjWa2cdRhqRxJY6YdkzEm4eyoKK3vw0JXur4N7JgaWVyx\n6QnP1WdXbUmcoo7CRL4rqdLRbb9SuwelUe9S88MIS5E+0e4LmnxuwPgTImVk\nFo35Kq2g2xelK3tz6M4E8GglcEB/5QUT2Z2iPGMTlfSGV/bl9u0vTqeVjXH8\nNUHH\r\n=GM+3\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBb2jte1SDmBRL7AyYxhkv+0WNSnrNgQkJat9vM3U4lZAiEAjVcPkNXlriaOog8BQAgS9bk9W7zI2Yn24b6vxKEv4j0="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.19_1606356518217_0.6176832314690079"},"_hasShrinkwrap":false},"0.0.10-alpha.20":{"name":"import-remote","version":"0.0.10-alpha.20","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"117d168fc02a86379969295d17c007d3146c7e53","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.20","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-k9IpbI2NWfslnrqo8kLV7wfKMKR7UxGBuvWPep0xYLTy30M2yNirgA8UbERoAy8FVIfJJlKSb7B5qdpZoyPz0w==","shasum":"d2fdc3a03d08b0a01e9c6b8952ef4b5ecc1adb44","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.20.tgz","fileCount":36,"unpackedSize":362418,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxFWECRA9TVsSAnZWagAAjxUP/i7azYmciT4W1wI2YXqt\nhFV+9r4fxvfKUNi5m0HhVipf0rqvLge9oDWrpBEGo7tq8OObNRpZMTLpvgLU\neCXxk/+aBCDqlgZduJ6VhtipWDWQHnIPjzlVHk+rwIlNdGMb+SZaIe7fGwkj\nIbHM5fMoEF2ru9nNSMeunSwB4Nq/xUAHj02W+JUByBJ2iKnqcniYRWvTtUQ0\nTig4ztKzYGdMZxl3tKi01h9J4hvzcNurZmL6l6dw8hE6hWIPt6Lsl88kPnH5\nCf+baO6oeyxOq++8BAFlTO2mix/HgT7u2hEZcGBLDbi8JlnVnTaFQnkdVE1y\nelnGZiQkNC+r5z7t0v4iTQMD8o/YGfxchZEYjlf19c45YyZ4MXJr9vIVJ81Q\nfcbU1ObUTtkeyCNBgHD0Bq5K0aNt+oXmnc/qpSAtiE9Ht06zkY2pn+WiRbce\nZW61OX0pT2r0ljOs11h0uYvYhTH+ncmrOR4CBNg5m5BF4uq6jvpTDL/N52d8\nNMrAvJy+OIe26vZCj1LzMYhmVJs6gUwyWBhBUQcEEqSGo6rj/bD49FYLr2Fs\nCiR+/wA9cBtPVWqZFEgowLLov6SU9RIVgfoU09kmksT17+g2Bn1RY4wudl5h\no+V5byla2kSHY/6PGegy+y8W1xjbBJQABCh+B+kqNwVX7uU7P8WosIqQrbk8\noVto\r\n=dwtB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIA9ds6mNlkODgqzj/+GGKmdWVnceyrySBKHGdd2DgqLMAiEAqcG0fz/EuJ+837gxMkHzC68+Zdv41Ow4h2u7Fy8fMK8="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.20_1606702467979_0.2618939769260389"},"_hasShrinkwrap":false},"0.0.10-alpha.21":{"name":"import-remote","version":"0.0.10-alpha.21","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"b2f50d782dfa7462216a1a101b6ac26ea507a124","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.21","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9dvCaDE6uAI2OLdcgV8y5SPiGW5tvruIpmVJFp0ZLBGOfw5gakLaUA3Qcup3Nsoo1729O2bxgR0BlZn2hMSpKQ==","shasum":"9f93f21f94d804adf6edd6809f8de513cfa1f08d","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.21.tgz","fileCount":37,"unpackedSize":364574,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxJ3vCRA9TVsSAnZWagAAkYYP/RdHJCaxP05+dajp8rbt\nrB5yp0lMXUN3YgaJmUTTcDh6grdvpN+lKP80IOWHtpJl16Sdc827N3nvwFpF\nQoa6bvy0b+a9v4qOu+g6pBbsZNymUAyLLQ8GMy1WZBcZJc7Fzc4ffspYgiDj\nkmENSUsFwhDo6aDjUqwerQGqI5ThqwinOvH5mDLt10lm40uj/FJMKuyl5/xz\n9GsoKXoyp90tNYjwPe2GgXxkq3wnvPuCBq1V4cFLfK5dRkqXXE0kCx657H4g\n6Z5EPhITlsd3xbEd8o7wgFGFvxayVQtpk/L7iCzIuLltCa3gY2IoGXLL/H/L\nwp79QzepjKd5phyc9s90UG99sFCvmIk73AC4Xg/lAF0/SYguXMn+MROLtU/q\nH6jGqZIprwr+MNVnqioeEGCBnJVHDPxrpEqWjMhZCQu4P45OR83KmSbm3/fJ\nPtyRDi0JGd74sHLzsKmI29+cnk3+qRnC0Z15kOYGH35ewQNrPdw1cl6JsXTz\nozJDCW7/lmZMcmPpusMFZ/9Q0dD9nNH7YO8VziQ7kSN/GrckDI0l3Z4F7HGW\nJOJ8mLi1Zp2kervDt/u4VxoV8YZRT94/DMnZ4zs7BS1dHnyz7poUPZWaQh0b\nbba38NZiWCi7rHvvUC8VgHF//hu9/ae24LaYsdO7aE1irpPYnngT9A5bJtAw\n8G14\r\n=nGQo\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICtu8SXhtHqJ6W8vqXyL4N1dgxG/tQbatX/Nbf+BeQ7IAiBkPYhuKd4Q3Ug/4RjO1kKNUHFbw5Y6un8wDiA5o7P3UA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.21_1606721007193_0.4289886847267266"},"_hasShrinkwrap":false},"0.0.10-alpha.22":{"name":"import-remote","version":"0.0.10-alpha.22","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"f61111305e0b5aa069daf705f03b1e6a8b8574a9","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.22","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-EiavzQF77ZJLRg0vHy5rB5TJf4U+tfNVTAGq2rVrkDMWL0IPuJdT+W5aO44uYrtkyN/FQqJZZ/aMInCRa62esg==","shasum":"fc53cb7c7f3cbaae3c4d320acf2a8029b84c6792","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.22.tgz","fileCount":37,"unpackedSize":362997,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxLSpCRA9TVsSAnZWagAAOKoQAJ3z6CMsC5fvDgeNd3Pm\n001ZbSt+HGqcdQsUtnKTo8QXdQY7mAMLRO8zkEJk47OdaH0dkcavp4RlrIj4\nIaRuE6hs5D5SdrEpBUaafsmhX0ds9ctr7HtaJjpJrGlKZWmjdAptd5ieOMCU\nHM0tJR2/JLhaRP6ZzmCraw7PICogHCY6v1PxeiFW8vY/1nqv9oC7Ub+pWAki\nPNENrqdx24a9+eLdCK4waMA7vqmoGLl+jN3Y1zx3I02/SoXR5O2bz4nrrTjI\nIglha3r5cn5Z6j/DN+gIgDTqEiCURhI6v0wFwaJFjzg6gRrJIg7JpnrEILVx\ns7mR8qSOy6h7EsPLouv6VOcu9fuOYr9JqDA+gWcoPj4eEB0AlQs9GqjINxzc\n7FXVdifTFmzZZhldHvcUE8ttN6pS+GZjb/UQM+obFxaL2Hm+hFJaEOs6gdrU\n77O23XePChS06lX/eqV+1rPojpoz6r3sdljiWLtWgeDWOp3bbaPbBoWuIrw4\niBYik3a+ThEYZq9Il28J1wB2PmTPx4e9nlNgS5DG42WdsNr8H8eIsl192CMu\nBD/e3AkkXYgEnXA8CKNjDMKlssHJzSpe0uR+V8xNs7u//13otsqpwwBSoWRw\nvObDKQ7e4lLctC1pIubjKkQjStBF8SL4f97hYGgKWTwHkFTSJr5Qs9XpElyi\nfgzA\r\n=R1NN\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBidi1zf3ygAGzJ6j1moaJaswgCbgOSa1qtxVftJGIMxAiEA/AltcDJMfo/MC/jk1iwhPhpyqntIxiCyAiTnJKXtuyI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.22_1606726824777_0.9938702287314207"},"_hasShrinkwrap":false},"0.0.10-alpha.23":{"name":"import-remote","version":"0.0.10-alpha.23","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.1","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"e7cce3a35790683a38a763c2fc12fd2f48fc6f97","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.23","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-N6NPM7izWiEpCw5KLSk0iWcbNFZwJJOvCV2kyYG3rsLeMTQ/T5Mm9jT/FqNEksm9NxBN2F3fvFZV3kBDHekdSg==","shasum":"17c8c2a97a40aa33afb7eea04aeb671a03dacfc5","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.23.tgz","fileCount":37,"unpackedSize":375324,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxhvlCRA9TVsSAnZWagAAZrgQAKTflOJldJCXi6fxdXGx\n2e08TuwOYOBVPk3esKssihIm9uN/qvvTmn1ROJAtMc5O2LTl14fCl7kxliOa\nBEPRoCZLkgjOlzxAJEFv/niIWJxm0dYHmEcH0MbY/gfuK4hTZhNjeuSK8q6f\nWEC9gVWti4hXok5CAnOl2e2iQUoyOImE+7v9P2QzNqV+2XSx6xenrODx+4RF\nxBQqwflq+NCh/xHVWzYJUzmQ84bjJ7qNVm9hFbPRg06rhXygnYnIOom8UCZO\ng6lla1jC6JV2lzjg3tmXCIEQu/VJOuMmPDDfb15pUB7zNdNw8eK0ZqfEjClc\n1DvMVMJE3yqCqsumoxXjlVXm2CYup04WTeZT14YT3Z7GRu6LMsdsKIfWgcrt\nWNpeD9UHK8QQj36QhMnnKmasHUtUMP6ghl1f+PMQO24YxTY2J0xWe6Mfz84T\nZRheAbOV399fGrX88HIz5SPL/I37Fr5/dnijyHJtLgNvNPRaoSQNAQlmeQuT\n/y7zptb2MaQ/q6IzgByQtl/fVsW7r9VRN/tRbzvue8pE8p+e+9L3OC5H7/aS\nvD/MijW9mSilF/7LnPLYP5AjIv0M7ez8qCFe94SpUufMwU8Arx+cFGL62uTK\n7g5V1gPZjWud5Jfu8p48pb75YB2WMV9bbcfBtyqJvAx9j7rLzNLwrMzS6/vh\nV6ad\r\n=QY1J\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC6ZmjhLS7JSNb/IP6pErNZj/cGPJxNcP8pre+LYdU5fgIgP/H7FG/UW3H3bdYpW7O2GX+xg2KgX2igrU0WXOxI0kQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.23_1606818788371_0.8424525509813694"},"_hasShrinkwrap":false},"0.0.10-alpha.24":{"name":"import-remote","version":"0.0.10-alpha.24","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.1","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"a2932aeb46af21a2bbd779e729f04023681bb45c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.24","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-RdqhMGJguVSVLxSp3NUnm6IqYcWdafvMzn8I6wGYCWhyv+zkZ49J4jIwv7kkzgwXaCGV67F21E4vpZk9JW/vqA==","shasum":"b09ce7f645cecd54f4eb5c999eb7fcccc4c3627e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.24.tgz","fileCount":37,"unpackedSize":375435,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxumRCRA9TVsSAnZWagAAj1IP/jgSa0cF1rlYisSJe4fa\n3R0XWNW4vjOaKNxQ17nni2Uj1y/Qjei26w2UXpT9EunLcMlX60lgWPBdWfi2\nuNcP3UXWx0WBDC/NP7yQALdGbIIC5+hRjt92E8OYUYiRx1SplX3p4+/Dwth0\ngKMd5mGhUeWnHXMmHxRpOYJr4JjGTRV/ZQImu39xPvohT075FbowdJOtYenq\nRi4Cj1Ng8su0pV2VwYIWr7x5W1i4M2PYqZEo6zXQ512+w8c38p+TORHXI2tW\nNBTepvD22q+zSIC+IdkoU2mBP+H+ry+exrLLoLkUXK7HJKkykSP10f6zUtKi\nZjMPg5GVMwM3oKJt+6Lr3YSetgHG9yv6IC8+OSAbwadYauSPTtaZ+uAZnSIv\nDMEH2694za0OO8ltLu8Vw9bQ461aRSY6Dswbx0QzbRjnfozWgcZIyP0DLqKz\npzt/anjAYmsZY36RMXBuSQZ91eSR4mTkuydTDn+WNP3k0mpcncjZYPew1T+K\n/h6hyNlweXF7iWaxIBX0zJ95lp0l1I5/QsddRODJMUd1ZTEH3pg4/R+wUb8f\nAi5FkVLxF2LWCcdp01yHLMRyXKxk47Ci8jCJNKAiwx4KjyCG6IPACrRqRtjO\nFNgmTbTIKUY5tWKsnqoGg3VEse1yx/7+jVgED5AAPb8WeoDdWG72iE2AL3D/\ntYB0\r\n=aiqS\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAwqRA5SS7rRAAD+J8E+gAAIZHHz4i/cZMmLm/g4xVlXAiEAkNqT/uorW8C7fzT4o6uUPwMSyXxXOEWUXkqWAt+u4nM="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.24_1606871440685_0.8860736355823726"},"_hasShrinkwrap":false},"0.0.10-alpha.25":{"name":"import-remote","version":"0.0.10-alpha.25","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.1","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"fa5b32c9d666a5d77c853bb333b64ec7c17a84fd","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.25","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Zxuig3+T02tM4x9WA9+QkSVLySv4ocWN6hYIMHXyK4c3bYe228fMjiy6Yv4C3EwiWLtuZI5CzzIihRmvrl0OLQ==","shasum":"7d2280cfb2686b352924ec6ed6bfd239e4c8e61c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.25.tgz","fileCount":37,"unpackedSize":379039,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxymLCRA9TVsSAnZWagAA/i8P/A9IPh2mWYUWWYdkBBHi\nhdbwGqg+mvVhMCAIrUsL0q30xCzoKlf8LGqaBqNpYh7/214IzblEK+oep830\nEMfplVfzySCJ3TEwf/8JPRmKtVAhTkKsHLuL/gJB+al1EyjrvfCA5qnlQALs\nLKHzrffVZ73rpH0Kx5SO9WVA3tM/rT0DSwgC3gFuZgkS1DC0/vaf7nPt+7DG\nnn0d9YXTl0ZzpqRb93biqQVPvQDw3yXcyeovZ2pd8rG9qvSaNsHSv6XK30EB\nN/RA4P1SzOTMTMAZ3V7u0key7aZpOMRtI6wiuJy90xKgv3pmfKOnYZsGPCsE\nfE7ZP3YYNG2b7hPjpySOvpLkvZRLd3tQQ/iTgkj08xGs/XVCTedHLr578HpR\nZsCI4EZddj6c4SpaM9IfHkdWmQLKsTNlyeuHp7pbTCBD2N6rubI5LNgDUP9e\nPwTKXYXiShsqSnngc1wAKNr84G2GlGTPN5H4lpNrQQaCqJrl5mY+kb18LuTA\nV+0KuCrV36R34rkTPhusHPP6FCT/APaoibiCIivWO0RWBwZQYRq41Cveczq9\nlPQpEKGkZJIlHNjHtkBKvKdFmG0qQaxKtqIUfTwbf6YD93qrMNFP0uabZVQt\nDUVQ3QTQtraVvhznvKPHccFIiUUjSHgbzmMhQVoLnxA/9ZqoVx1+rwYAx8ba\nMN6I\r\n=NMBd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDsI2MZiQHR9ROE46DQ2EzzIEDQNlBd7yievr1TNDk7EwIgfxcLF810GydecoMbxUj21G1WlZrA0vXDvYTuTjBc4tQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.25_1606887819235_0.5803727099446514"},"_hasShrinkwrap":false},"0.0.10-alpha.26":{"name":"import-remote","version":"0.0.10-alpha.26","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.1","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"a953aa125b9f597305663517aac35fba0df889b2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.26","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-gOY6X2XlSl5phqVfQpgojdNRbHPu9T+off6kf0S8ahzb7CxYWvcVMFz73NT2R31kdTbtr2o1/yGmv9A6p3o3hA==","shasum":"bccedc82628a9b7a9a00df21a030eb59fb8faea6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.26.tgz","fileCount":37,"unpackedSize":379048,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfxzM/CRA9TVsSAnZWagAAe0YQAJYlcC4ZR7lsJcink6Yy\n/nlhyOLQ0XmhRCnxG48LDWtGpTDM/CXyCjLd6biXk+qVAbXabP4aOlMxqVuz\nciVubIgup7Q7nht+UH4OxnwTtTrmxNfZ5TS/ZPWHFSTcbPardUT9Isn2ayQn\nX6hPi3UMo7n7W76zUNExA5+ZmKZV+Ax1eDsw83N2qTS82hDaoONfBqHsMjkb\nVQcPAhn4GDOX4ZNKiBptivIIRv/KRDBp21MXfQp5g4HLPCuWyZ8iD427DGKz\nRkJeSC/llilUBMERCu5IPQhuY8huzx7i/pvfwCSSGGRL+zXwezeqw+ISWtF6\nXbJD1jNj5HCHWkAGWok616hSs0cHJ3OzUxfrU3yMO9uxax0GBIk4MPLkfoMK\nAzz+sanZDMadAdDwiyGIM7uCQQmqrL2xf8oeyES6OU66F0cn83zl3aiKSL1A\neDDBMnOpszJAFLiZ3tW83VeKtUPbD3yrMson+Lv/Eialb+9XjIY/AEM9crQ0\nBERXe0LbLPhyf+XH+Snj/4s7ev/V5bACos8MdHWKHyxAvSjX7cUEXA9YNO7G\nee/7SeIaHL5XpJ6SCCUlPL6r5Ewdydl+07Q/MtF2b1QXlc522UjQms1KPNqW\nyHG+covRErNUge8TbcNmn+BC/7UNYzHYfo2/DB1aNOYW2cy39KPQlVweDwHY\nxcHC\r\n=xguI\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDp+qKtAbcokmwMuuKLj4SdLcOhjAXlZ7dY1BYawt2l0AiAC/foQe4U1uPAO3NQTdenBanZ4251wWCFe0fqCs3ojRg=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.26_1606890302277_0.30777429374856125"},"_hasShrinkwrap":false},"0.0.10-alpha.27":{"name":"import-remote","version":"0.0.10-alpha.27","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.2","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"490cf60e2022965a5a8c04c4a20d75bf2b4bfae5","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.27","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-/3QlmTr0M79BEh809dwPat9HZd/u56/IcBPurxe9yfDXcZQb46oZeSZoeoJTDo2XxIXrvFpAlL13Qy1g5aT3LQ==","shasum":"da85cba0179d550eaf82d0c37bdaf652a97a5f8a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.27.tgz","fileCount":37,"unpackedSize":380226,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfyJRhCRA9TVsSAnZWagAAWz0QAISLxGAHEspaw3gl2odw\nHKPgq0J7UMiiFRY6ahW3rak2ExeaDAv7alO1YXvEHbSWEpwHj/E6k6xiuzYt\nJ3hI3bWNbJ2QF6tz9SP458lmQg0ImA2i/1613fOOLyKLsNNQv2KH+U7D4gY1\nv7vpXx35NG6lzd8/b7jTVxn/arseRZ8TGRYwjCI1zb3sQW2iH5qF3TzLsYTL\nRnxEBkCLCCX06G3m4cWfE4l3qtk/wsfYa6z00mcTOP2tIXNmEPs1JGhkOEiA\nP/bWu0dU1WXnCN2RmcB3BQT24mxjg1Cfdoko5EI6+JIESIhy/fP+PfKUyQoX\nqs1JdC+KIdUJwKSFKmRS7Me4p9Nd7GZ5pHmC9U0SXmPZd+IgKIP4ZVh11p7t\nhw1LfJQGXY0kB4L6qcuc7IUhQG7CBdkhspNHGw1qyA085gupR4dvlXtEFTGj\nkDjxkdBMeLkiRzt7kqpgz7k06yOLwj2zKDliJn4YiU4rkb/vHfz/Q8AC9kTY\nsFm7Dz2PKgRYUgSeO+9++xTJaJZdPo1yvmMXR/xQFQ0HZYmiR4hK6o50cgPr\nzpnkmbdR3KYO1GwZLglMWuFgZvw2CKXtJ28CS5MOIssrXepscevKEUCBuH0U\nFexsM0nQ21doO7k6uUCD7iMz/OvmpaDQuCqSIXV75ViD/WcthTIsnl5VzTnW\nupNO\r\n=srdY\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFyyMxgVGJrznl4PlxHrgs8r1brxWtVJdpJcDDYnV7VVAiAmj02IGGhgp2gZNmqDV8x/+hlPUpGtT5d/o4dDY0i49A=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.27_1606980704392_0.8174399853077998"},"_hasShrinkwrap":false},"0.0.10-alpha.28":{"name":"import-remote","version":"0.0.10-alpha.28","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.5.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"peerDependencies":{"webpack":"^4.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.0.6","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","css-loader":"2.1.1","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^4.0.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"4.35.3","webpack-recompilation-simulator":"3.0.0"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"afc1d2c000b2526d3d90a89161ebbbd3c5d8aa4c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.28","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-23p3r9qvsC7wBmspZ+aaQYCVqRf+3ZKofy60lN8/3NGfzYqsy0NO/pkAzUQxoBkJjMJZ4vOtowhS3BMdtbTJCg==","shasum":"151da9073430f8896ae02acab29fe8926975e2fd","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.28.tgz","fileCount":37,"unpackedSize":380468,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfyZEICRA9TVsSAnZWagAAVCIQAIUlwm3JNj2PSeKrVbAv\nbXBJf+xpzxFfFgaZMk2aE8ejYtRUKhDQ2bF6LLdh+PW5jk76HowESoIuy/nb\nxK8ORAVg0ghilelblb/I6X6rYEabIIjmhckhCrSfZe+kgzOaUNTQ0KjJssF3\nu3NQ5qL3G9mE07UD7bISOWCDTO39OrolN1MnFJ7HS38kIcqHPlPeUSMMPHPv\nr1ri9047Odqd5LY8HpxFeLYzCbaPjvm1bxTAIdMGGpESzaTwe5p/KlHVcHnf\nv6drYkiFsw/WNnt/AaIuP2aN96yKJZ76L3xGixV4rsj3Qe82dsG4e8p2Uzia\n6S+/ByuJPPGcpiDfTAhYOmyIzvwUreFRP/bzMK7HKCrICtEteiKlUe8W6Kh7\nH9CtikyIlcAAD2+EXpLugiX0IDSoyp1gpLP+jCmLtvODWayiqOlVp68aqsDV\nSun9sDvhTyUXl2D7YdcKlEgXWP1wK5y+366xYXJxUHwQxsfe3R5+SMIRnsB1\ntQugikkCbXDCraSz04W/kNL6/n6+zWsrpXzJ5Pw52shME6l8tPQmXLmA6jNA\nhix+D92eqUTBUNODvJrnben6J4BmBa2O3c2FkO+rB/PPIUDPkKQ94lbJCk5g\nUDkok+rUOetv6ujfBzbHEoXRSNmicQ9bhDgzESX/FfgQaDY1fuvBRvIHg5S0\ne9ls\r\n=faJU\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFHaxYM7emOFxVoeo8t5BSlHkXiUrdrqCMsQCAvd1ltkAiEApR/dWxKxmzblZWymghVIDvGULPNTEixbJPhmMZv1Zr4="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.28_1607045383642_0.8188880573688584"},"_hasShrinkwrap":false},"0.0.10-alpha.29":{"name":"import-remote","version":"0.0.10-alpha.29","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"83757135617465194dde1ea201890913b71ac897","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.29","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-S89zvkmGwr0ThGJ2wj5CF2Bg4dwaN7VjSpciLJWStOzsDcRMdQfbMf4Hlr+DTVRAJHn/3DR2cZyOQ+MoTH97FQ==","shasum":"907701fd6419f0064081c8970a4f8415fe52dc46","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.29.tgz","fileCount":41,"unpackedSize":901322,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfzd+OCRA9TVsSAnZWagAAXQ4P/3PAsX+ACYYcsgzW658L\nXLswRguOUCVQh72CqjpRy1uk5gL922CY/6filKVaBegsC9nnG50TlxGl21ro\n0hXrn319EjOc0IyeK4LoVq5vNxfK/vb+vw8CMEmHMOB2oD8b53sgORMblqoX\n0/11Aji2tjjvv426RsumM4XcrHN8OY3/rRoQCtP7SH+tOaz2QfsfSHwZRp0Y\nMMUOXByod42vFTijaANKg8qqn0DrGHCr5xbPobXjRzQQNs6SpOqmn97SbnMa\nQSfScjVMkkWJ4BpOWsnO22mIKuCRNNHhl70pCo2R83WAiM5gMo6liyoJXek9\naG/pG+FmXHp/zY1R9txEZzwFj8GodIo04iQYa1UJk8N161aum5zpmy9ngkHw\nzRm8NNY11K+nb4vkp6wjlQVR+ovorUCVYGxyPNMTUpO5ZQgHBZw302F4t9KK\ndgmJym6KhNAN9FqgbZ5IRkFixut48fFbZJvDGDeacsFWyONYt/DzDcqWKDXu\n4akeikZsTaHdbfr5+u4EkfLPcTKzWhDjkPgjZQsxUpbFRq2VqPWDC6DJXQBw\np4Fi0r1K3NN6sj60+uDR149fdIB2L9XW52skTVunuEYcqMGS0MyOUC8htsSc\na7Js/yODzNn/iEW60fzcC5oMQ4ar6NBMwfHjIUtJ74HMoWNrl963/SXWgLGA\n9OYH\r\n=wX4l\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHEGbAAht2rzLQymux++9k/YkF6WfsWWWqFtXoCpQVjvAiEA65kFs4H9OQxRsO5PpP2aXbKaKtnvGXnP6+V4Q5Sk55s="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.29_1607327629875_0.31500932638548007"},"_hasShrinkwrap":false},"0.0.10-alpha.30":{"name":"import-remote","version":"0.0.10-alpha.30","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"2261932f6b6f56940264a9217dacbb61800b64fa","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.30","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-wCKYuFkL3eqhpxntU9YWX8W9J6FCRRUpXLWSIPYX1yMcP+Xuui5LJknHRjyoonwu0K5pqGuCzeRIrxThQOIJWQ==","shasum":"776697150754c16f3da09b93d1f16e48bd8c29e5","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.30.tgz","fileCount":41,"unpackedSize":901668,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfzex0CRA9TVsSAnZWagAAFqsP/1DAmtf7RaQkrOs0Xhc5\nBflgqKNQ1od7mWBCKw5rYedavpFiEX9bAPkvJg16DBaz6EXGO7zzd2yfKRIa\nCUZIqjQE22sblacQPwU5sHUADf9J2RcDf09DHRr0G86QBR/zj+FDTjfO+3ub\ntd5ElROirwK7ro/iEgvlw1EPVkesyRy58mfIWU6NJ3cyvon/rFyh2O626LLg\n+uJJCvaUzPyIMAGKNJtRoslhZLiArSC5scxoCV5W/obSdlHBfwc6nkBEKTAw\n4ZIxtss/ZP9zWH47NWZ+jFYCUGi9YQCRp57AU6VJ/ehFPfwnE0u+yG91QSWx\n3d7Bw81RoY1f6LyvXf6Zb8613ciVu3dEOFFE9/Li4QvxbGLCX0Yqnl9nOutw\nHuq2DitEQD65Ar0prr4mrrOrlPDWZgr4ZM9rDoTVLVwNNqihk7QY4ohOO0cU\nK0RIDNCIOOBjFFJg87MhK3TTfYSNDZh3cdmNwwgV1kagpMR4yJjq1Cuv7Pk6\nRSvLu46fSV8tkb+34FatMG31zbFTaiSX82EJoosHm67amnf0T7U98J9erwPe\n67Di2jwqgsdE1FxLzPo1giDrA85eKOwr3i06M+ofd1MQa0ZgN/Kby8m+VNd/\ntUafdWzxS/BbqmCwhyOf3a5DQ5/cx2XerRDS5peEyzFxVB/g4BAJDZv1HYpi\nRbzm\r\n=BSId\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEEzLjBNPvQO8dEjySxjOZam8gfJy4abpyaVaAb38k+GAiA1QzE0easK8UDbhr6CDvPpHLYtvpMpVYWAEMYfCWQORw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.30_1607330931961_0.23560989836668988"},"_hasShrinkwrap":false},"0.0.10-alpha.31":{"name":"import-remote","version":"0.0.10-alpha.31","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"f1781757ddd8a559086bde5c9c8d5d2c19f01031","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.31","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-8Sj/VebZ6Jy4arOF+ltsBy4quKmxInV6nqDOmu4+fHhC66MTam6PBEhvLMjzzpgSJLg3/epOJ8JYl38rDUQsVg==","shasum":"a7c5623a1bae81f3cfce95202c441fc642a3869b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.31.tgz","fileCount":41,"unpackedSize":900606,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf0HC8CRA9TVsSAnZWagAAWpYQAJHMJVhdObNIcB0M89VT\nrbQ0iww9P/vlGzT1J99mERtrd68eamPZZkR8zQGxHyUiEO1basyk5GgqSIHZ\nZk0+EubtXIZFmKGwjaK2FDHS4EG5hyEB/lWgZaPhiAQvtzng/5d/knph10pI\nXaIUACrgQ7v6jMSUWlVXJMRbjtzv/B7NYy0u/T/YyzAUfnJ5ZGZqLdW5oqQT\nYw1pD0Ws3DpFKEXcrHUJcl5IPhqrkJtG3bbX/A0MGEICBcFDU78KL897Tx3t\nccsj3iF+4HSetMffYado7999j5Gg4mOWuwhp9hKUiK52iB9Pot4MVjgnGZdH\nx+Snv+aZAz67zjW1c68ad24IIXDn0YCQR6xbi/SIG8ILy8LNHNvGbF4aeMYE\nRACIhPf4SPlV1hkndiU+dtytJEcUI1vfbkXamVV7AGZYU1NcSd32TWgiSfuL\nEYlSaXPGaOptJN9ZlNTL4q0+Cgn+f2cCVQBS1NuzpSFNklk47aDd2Wbh/lG4\nTmGzgV4panu4uXio8FsbeS335/xeK9uY28rJjpiJ5YNtC+kizi7SckZZYVIZ\nLKMCj9ju/yX/9zQf5xoKnTDWXndoz065TTPNQizuBjs0CsMnzAIUSyi9tY9M\n4RE1GPqUpmiobqR6SVI9XOnM3D5xtOddWyM6mb4VyKSuGtQ5JCV95XBd4k8z\nbJvd\r\n=EkLf\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG/HoM9pn1N8ZinlV1VWKfm1qNtLVFVgR6m+nTJXbak4AiApxTMlSLV2bRFzFdK6X01dNeeOn5SHOJ+j9b44mfRO3w=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.31_1607495867950_0.8792078887153463"},"_hasShrinkwrap":false},"0.0.10-alpha.32":{"name":"import-remote","version":"0.0.10-alpha.32","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string|{ name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"1e6d4c63c3070746adb8ffcd0428f0b8e7d028b5","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.32","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-ACMjFj4dmcohx80A0mmkP5vXj/ApETzO1406ubd0RqMCsMkoeScPHObgmtplCFwmrJX9Z0wIV8NUcfCXs8PmYw==","shasum":"dd1cdfe9f77937e0374a65ed2431cf1f34160737","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.32.tgz","fileCount":41,"unpackedSize":900948,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf0eaVCRA9TVsSAnZWagAA/9MQAI4w3uiXr49gykpoTIV9\nNCvDzdKFF6CfFv5pENzZIXWOhTsJYpgmtomhTAryjmnlne0hGwHcxi+pI9Ph\n3I36lCNlltLO7ixKZyDVi3exYLKzSeevYpijCPsDY5Fc5HyPtQ0r4B+BmrWn\nj1tg7qtD77pX4Zn5dwEg3xlyPaVw65H98TrVMmnN0KUQ8BeaEMje6PkX5EXG\nSRajM5ss78oK5XTNbAdGtkTaol85W9cUGBs7Fxxry5PrknZZp68vNIIIDu8o\nfpVjqF6L1vbN7r7vPcRMuAPJEzY1/wD4+QlQ9IvpzEvfW34Liwbhd0cba0bR\npLs7dofRwGPMgYGdCDt+5Iu81JcN0fF/m5/RsY0R9qDCVjacgLnvEJFfLeOj\n4y0zcMw0zB82/kt+1ko9uasZF2AjS8JAKw+WabBZw87yXRzDSrrmFV84ydpF\nTsu2YawX4rbZPZZljdjy8t+HmTYAPY1o8wcWuETm1eyAQWjPrSqvjs17I3wq\nGd04z+0ukMEfIVxdlz2Ii4lf0FKD4avO2ZiLmMYXxR1I13XF5rcjFYD2IMCL\nPceCbOxyYYzWtA3+RASyQaOKIprzxy6LyK9mqYET5Q/8nvWCJWVj5xHP48ay\njmrA9eURquIdV6jVi80oSamAMgZA0M7/H2mv593fgiyx21rAp/PRrPVj7G8d\nwV/1\r\n=18UV\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHrld8l6oUd/79TTtC0375u0JrxxKInJ6zIbVHsR+lGHAiEA2ddkDUaHF/rRIBnT65tHzyVO0mKchSYTnKbwlETqOD0="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.32_1607591572826_0.1339561092412882"},"_hasShrinkwrap":false},"0.0.10-alpha.33":{"name":"import-remote","version":"0.0.10-alpha.33","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"1df9d2dda16d9e5b637c9db9aee719d75b46f19a","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10-alpha.33","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-++P1KxJzPHjpSJF0ivlDzLrn5N/n8zJhYos2QV9HPHY539GN507OBv625sz+vbNoTSP/Pmu/qFWNCPI/4rBUCQ==","shasum":"94ded663d46365e97e7d5a5a0b33005e94327672","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10-alpha.33.tgz","fileCount":41,"unpackedSize":900782,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf0t3OCRA9TVsSAnZWagAAkEcP/R1u+04Mhd94M+L1gBWh\nPu1/IDK9oz95ULhut2AlMdCO1OoVFkp0sOvxOYklo1C9h4Eol7T75onCI0xS\nRU/gXl+6bEDhYXAdar4lK5Sedz2TBKSMFahl8Qf9RRSDh0d7JbMbtaZpypQL\nR9RJ1MbnvJg/a56pLgksz/RjVVGnFVAwBCUImuoUsEwMo1vYLFoZIue825VS\n+aXObsJregraFuuNfLqpKOK5Y0U6cRup1oG0G//4sP6WCv+nP31zTAcV2k6i\nJQPT0G88/e0VHN81defPRuqs1gAPm3TuWYcmowpOZcA97PGzDjr1Zn5sgZxx\nMOQ5xKxnqBaJO1Dc8FuAIM0LlTWWZEpBV93vlp6JtnMZ6lcRQl1qGPbkBE9u\nC2EvVeOBcvL0XqyrP2NMcxBvVeC2K3uRzIYXh72Pe1UDlt62noiRODDLvt6I\nuMQR3csPLNi1TBnkEjnXitlHA56xbhPB+AaZH6xk6DNO0P760fW3uR+NOhkP\nUN6RAzhsDQhIWMJkX4IHMPSRn6ctRx+YCg3Ue8iVmJJ+fyqYdUdQqLXoij9x\nW8VXrd7DpcYELqv2WNHU6s5Trvn89svahQHwjKqpuxc9xkMevRCpR7vTOUWs\nGW6q8jJjHH2jEjxmz85zE77HzqdppPeHe2zEs4qY+B8bzHOK4CO5YyeId3yG\nKu5H\r\n=Q/E+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIA1OClmRWInmacLkh6qf+ZcHV3VnZL8Odx4QJur9z5vTAiEAhVaWJmfJW8yMe8tDrPSvMbGMOiRazyaa2YR7o3SLyIg="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10-alpha.33_1607654862069_0.04748913757741935"},"_hasShrinkwrap":false},"0.0.10":{"name":"import-remote","version":"0.0.10","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"f0f3d0f9220e48bb5e6cdc5c17213acbc4ef5070","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.10","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-g+vRo9cZyuEQROGq0N4dqCFs6/TcYVdpqKDM2H+lTeZCoG5gPhAITeVi2K482x3UWUyj8P730GEA0aMDmMet4w==","shasum":"eaab8c12779230c539dd722191ee6efa3baf06ab","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.10.tgz","fileCount":41,"unpackedSize":900745,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf0w6jCRA9TVsSAnZWagAAc8oQAJ5uqV1n+eyj4zBQlGk2\nDKMMj8vimdTpkz/EtCsTrNrN4IJjce/3WTlFlf4WeAf+5K8XYYQVeDphIWvg\nEeH4LGCdznTo+t3UbOkmtFXR0vkz3W5+NxdYEfHBFfkeH9aLLVv6yEa8mBhy\nlu4JkQfnIKYCoc3gPSwTQJAeMewcW8OEV0WV2EwkG62Zw+PYisBulBWcyW7g\nUvhBlSgQB0dfrRD6Tpp7j/g/zLKZ6pFIaHjRqty7dLA8uADIXWL81u1ypHy+\ng4jJcEnKvh3EPxI9aLjpfJh2tJVcxQO0XhddARBM6y7PMVDIVRIVPqPeZRt7\njO9RWoXbB6Zcn0iOwIAgjZ9SvxYVFDFt1+CuusefQXQBLJheLN+zv1mqajVr\nr6Te9/UuTigAsCW3YmKxYc/KBR3EPtCVBvR2ipYVbPjJryBhQoXr1xOKOc/O\nvRn2uUtKJFrdp56yGH8Zs/HeznHV284nigUegI3KyW/g/PYxbG/t/JbHT1DX\nhmVlrgjuNUb3KiElQDsf8EIF1bbLHoixXvug8mJPP7DEiNaiVD1QW5H86YVk\nfcwIOK1ioUBogkszgofI4YEmOaAhcjHCCAdec4mgQoy4MOLacC2Z8UUYq/ib\nyewuQwsW83SmbHrqgsM6nsQQgNbb+GUZV7CdFBE056ltOoibpyHudRgWNoLW\npLrU\r\n=SqNP\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICwNfI3gpdj3WvzsQAEOD/zNnEsxoNaqtykQzcYAkfBwAiBOY5UsKcv4pltCVdnQ055GTOLM9JhMbrvztHV45NyLmw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.10_1607667362556_0.48838817744177554"},"_hasShrinkwrap":false},"0.0.11-alpha.0":{"name":"import-remote","version":"0.0.11-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"6983195b5545a6eb483abce9347492dc9c1fa8e0","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-3TvaAeKJ9TsAzPlE+aZZBHz0BLaR74Ov3sow56VqpYVtpddLEGGtA9yHplliZF9BouCTlUeLO+/rte1bhskGvA==","shasum":"41c1c1b9fcb5965ba7557a61fc2862da9ef11ab0","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.0.tgz","fileCount":41,"unpackedSize":902648,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf1rhjCRA9TVsSAnZWagAAAYMP/14zQhK82DxZJwYXjCBb\nwJtR9ceYswafNSupRErUEW7wAHXkYjT8wvO8+U+3MM2C9ncdLCGqBA8ANxTy\nE+ZEQe2tA43KRUTunjgIkfT5kO0Iy9M/F2LzHGvHiGpKuHiGN+KO1QSxF4iw\nBp5W54/1VjWNLS9jreSzUScQCCLZwjpHfkNjaUWyOLj+MCTRneGdzm2jpcMb\nyobgmLjroKxbAeJIIC/t+sUEzKhYoVC4nS2l42/IX7x1coxbS72T7pDwRmY7\n1AEBXjjwLHuH558c4BgGR5pqug+bLnIzGBS+FaktXKrH9e/otOVrCAe+do3l\nImbNA3LlmJOvWmoJRHI09lQ4VdvMANic8YFyrDUkj30Hi3fPbwURVwaw44NL\nmFQQooqYXoe5OotIOZtN956BlXYorRgClQWYqUkVFvzujOOK2mrt65eCzfph\nVHffJnlN+o93nrVyUTRnKGAl6zfY051QbPm6GnNQoUu5J1U+3zKff07X94fI\nXZk9kMQdEiCDt/A/A5nGpYCBpCczpPvOzx0fFtALCxEEoRBaQEzqpCmE4/ry\nB0x3FxuFx9WqBAtcZDBE9sWCT77fa+0pM1tVx4AZ4vJQO7sANtaHHQyK/BeI\nke29SDCRNmajyV8ftRcW2aetbFST0CpjPoNmANLZNr3kBGNoR3M6HnnT1xuJ\ndyaZ\r\n=I7xc\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCUZivcdZZ/0KdwEBStB0WS6uwW4KGyWoBxlUk+yk09jAIhAMaoPfDWjp5DB7D0jUtZF9tIhbgJwnSDUFyD/Me9c8OG"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.0_1607907426675_0.37941620577342494"},"_hasShrinkwrap":false},"0.0.11-alpha.1":{"name":"import-remote","version":"0.0.11-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","object-defineproperty-ie8":"^1.0.1","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"2ed1fb8922718d0fb7906e132e54904b437cae5d","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-820/4IYaTWc+V37+eOeTkFY0SjUrqYg+waRaKj+dXcFW11CaYrLz0bR6pGbOaCi5IyFQhcnmPDEhdU/PaWCIOA==","shasum":"94f014b53aba0f5fa5c1540c4aee2adba22cbbe7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.1.tgz","fileCount":41,"unpackedSize":925083,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf2WFrCRA9TVsSAnZWagAAbQkP/iR+l9BpdNxvpssxjt8Z\nBHySo0b5nZYfF9ueVMVvZEbtsvs69Kmb2DpCBfQKjJEQ8HD87KTe8fmOzQ0/\nmL2ri7K6xdAQlKXKIrHaURr53UOaVVc//PYHDnqAkOBPCcf70kxbPCS29eSF\n7dQNfHwf4YtwYrQjgUH6QGRtxroQS527XoQbEOgN7VrYqwUq4HTCLiFjLQDf\nPf6szbAgmRZGK47wLPKWfoarng5LIDO492s/PhI+32hzDhR+To1dE7NMn/ic\nd2/wMlVuQcN+bi+32GBIm9VKPjIDk5g0nSjoRrqiREC26BFT0oqHZ4eSNcxi\nj4T49MK5jpbs2QysyexMNmH4scVFucgtPRKcJCUaUPpuAqzvO9We1ZPzgZOr\nBtN4sBgqkBu73dlDzdc35zT8C2aNTMYg48Co4VEHgVL5IvEYswLPQGIq7CEk\n+8wfWV8hOxp9ZrBHFKONH65WumzDxT6P8U4y2hf3cY0LmxGbkE4rO7herQej\nptdUxBtss93AV1osLOqAv3rBSlrMBNp6vy6wE698b490dkfekghypes8DXzt\nPpITK8ydkmHaNFb06oHQp6DNqPrt5fkfKoqUh/cXNTKn7lVtclMN4FRYqbwB\nJcJJLAijL0MPbClpSTXKIrqAO11dlb1UL5FU4qDObYHXDR+UWxdrnAQe9IEt\nRvi+\r\n=6E3M\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIA1BVypatCe6TEplUV6HeXZ9vg2cdCXOFOHuRf5iHlJEAiEA1S24Zj+FJQxXZdTFdbnvxaDgsSmwdrClAjkM6oJousc="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.1_1608081771144_0.9093161213209855"},"_hasShrinkwrap":false},"0.0.11-alpha.2":{"name":"import-remote","version":"0.0.11-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"5337df3c861d413ecec0b2aa41dbccadd0d79a91","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-zsRqzOH0PIL3Ysfr8Aij9q6mdIC7EnE0BgdVLMpn4TRPvFC6BnFDD37rNMbdq1JEKhkOJSKxGcjyiE9Qt4A/aw==","shasum":"f3ecd2b2a583835272abe684d3d2d97025d2728a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.2.tgz","fileCount":42,"unpackedSize":926582,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf2Wu4CRA9TVsSAnZWagAAZwAP/1/JmSwY4V15Fyvdg8Pv\njR6fK2THSxUolJSSil1693f1SPm96iP41YOlAvhCXfOQJaWsKmhEwwpsoVOB\naRPQbA/3x+Q5j8vptqI3tftANPLo09c2QHHUbitCiRi6jkEXsyvNjiJaQt23\n2HSwDe1YXsQBffjig1rgjLOJyei3ulqHMIA4DRRu3hSeiq43vx3mc1nbpkG5\nyALPGZYu+opOQr20MeVBWaq7AESYr7MI/i3A5TqO9J6t28gTJyAa23VMIjIS\nao8aVvTR8Ipi50kCpH1VyrVg/OPORXCu23PX6Gv2+bf4yJr1lKdNanGqGS9Z\nTUf+S34jo7YhXzrYHA3ikg1sF3B2NNrEp8/OwrHtLvC49ICxab/h1+yVduJR\nYCZUyGRw/zwjronRDWvhLmaFiqH1UZha7U/rqTEb9RHJSwJG8GJD6ORhY2rQ\nbWAu9udAx25cLcRDygCSfEXE1CtZ6fD0LqsTC1M3WTK34WYKWS7uE0kPsAKs\nV0mtgxArKJ4F60ngkKGlrAe3eIQu6FUS6s26iKEBFp1YHsKw2Hv5d7k14iEY\nQDA3jt5WQ2JCnL+2knxwfwRiOOf/T+s4vh5EI3oxKucOpK7WUIfqIzmE7YCQ\nFRmjX1FdEH8Er980eLX3fIfpSG8rLqsZGylX2DjHyEURi4azs5+ln3my2/xH\nHfGi\r\n=Gc/B\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDS49dKZx0dOjN384aoyIV9f/m4Rka2Nv0MzW7OetCrOAIgetmagAWhpXNLdgY01QAEMQ+ZUT3VxTOMHV7tBUbREbY="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.2_1608084407497_0.15575004166695416"},"_hasShrinkwrap":false},"0.0.11-alpha.3":{"name":"import-remote","version":"0.0.11-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"df40d52849083e624918de4c00969fe97a837284","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-MPMX9kNcVi3y0UeXQLUr98dgiCrzsbqQ5/DGtf+CqlA0xcwLhruc8Klc291Ho4loPC20ciNmne6Odv0Y9lPbEg==","shasum":"0df47eccdadae9684aa24aceb5d0cb37b0c363c1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.3.tgz","fileCount":42,"unpackedSize":927389,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf2b3OCRA9TVsSAnZWagAAXFcP/RLm9rwZwKBcx8OsBPIh\n8Y9uuWozm4F3n1WDq0s29iEo+59opww9H7fe10s26YLdFgIyMpwSBE0v5+pY\nWJvev7DePNtk45kMGAs1uZNaKDelC8vLQB+olQyldD29K8I0ymmFf1RwszIH\nTTLFQQlg+bMlRcRL05F9QwjVWgngpSwAppInIEw/4gpWuHaKAiRkA9a/GW7a\nLc7+3wuZXuhvMElTP8KyUpUYCf/5nPuhtU2nJ/9EcMwfik4BbffD+pkj/Q/0\n0s8WBiGrbapV5GOajoMSW0+ldVxHSQiBslGxOSqxMZ+fqcmEN5Uw0RACr0vK\n+E0oqlhxn1SS6rv7E9hLooJkLYm69BOryIqme0tLbM1MmNYxiBT5pyiiqrFU\nn4jnnJE5fWyi97KC6fRweEf7NJ5VHUaL5PPkzFOHaadJkvIRFO2gzz3yGwER\nNh39zMLvNyeTbSBGYVqcAZpGB6h9e56FjOMx/xbohJXyn8JjnS8CSurUrQ6m\nImhL864KEhJrtiuAd41cgg6rFtPebv/uOrf+hY3wG8oxHaLquUgCHuWWfKLA\nCxZAhgeicHCNJR5r6wGKMhy9iMhHZAjX9glf1+9cfyJnY+gW2caRa+ucj8pT\ntFpnMNtTq/DdFuJCzOsNRQ1OyAQc3/tF3vDebwPbPtLxx8D47L1fEx0YduYD\nF111\r\n=g02d\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDB4MnLlwUjF3lZIpMnQUT8cOC7m4Mhdm7igsOwHoiSeAiEAo14AalWRev2Yv9yNiDX6FWgZu4UetvTVOUQNtXATp7g="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.3_1608105421450_0.500523261193375"},"_hasShrinkwrap":false},"0.0.11-alpha.4":{"name":"import-remote","version":"0.0.11-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"2899ef31d2034030cbef206383919209aa5b24fc","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-9mbzSUwMuTNnv3XhaYr6V2Wmafk7hswAQftJSrmTv9ix2ol0lYl/PHPwJuQG5/XeUDpJb3rMI6svhIJl/zyllw==","shasum":"89da44fb29e93ee14b95f932347fe4b19e0d760e","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.4.tgz","fileCount":42,"unpackedSize":890741,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf2x0hCRA9TVsSAnZWagAAy9UQAI3u9PORSzAp+whlAhVQ\nzegYMiHsSDRp8qcPtvwbpkst4/F0qYqeuu+9XlHWQVgAoVgD9QnBwpujgAAh\nTm4tHTL0as9loE0s6nox+G3MtTiKa763Q25zE6M7zuLjKtYG/6DmJU8Y3zuf\n/nedax5q30gtYVEeSZlvgwlWBUNfLTvsILjy5BDdc821xgR84mhsLUw5ZJcL\nyzmIPi4T9+edLxSkNxy0ajxfEZQWHc0Ov6gKwEPyGQlRxHj1pd73fM3t+n/P\nvy8ZbP5o+yU8AY2UXmpG4XyD4771Rx1bG1RkzjEnbWbmpJx9nu0WaKAzomyl\nh7B6Jou9y81VtNVjdICQzNg2LcTxztR3SLDY+dGAIJxsa/FPzM3902M55h/V\nClFajNRnpP2q+Dz73qP6Le8+4b/3r50dhtUxxBAHr44t07XudPpU2MvpL+2r\nqNLVGVwlvjweDP9ildglzsZDAMq6WkswYn2+q/ewrICYew8dEUTeeiEF12Xp\n+C0VY74o8S8D+qPfvo2i6SAQBqrr/JBNCpyPweEPdBnyREZZSqdS3SvhY6W1\n6MwYaexgFJyTNKozH/ik20fLQ3rCuQBUjLkuUyePMlRj8D+ZiUesGqFDvTe2\n7AYnwuXpXnoAOCJ3mnPCBKq2x2xCin2wZiqf064rlVwnJ1k3L78urmypQpci\nPedF\r\n=BIPO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCFB1CQvPJOWvrcoxHcnZvqLZ7N/DAz07VRgLhxHRXHbwIgEUSMx1wRIWv1wAW4umoibaCn1E5n5QUbxEbc9+L3aCw="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.4_1608195361021_0.8072388695589134"},"_hasShrinkwrap":false},"0.0.11-alpha.5":{"name":"import-remote","version":"0.0.11-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"2447204fb810a4bf32f12c776adb03c1ab6d81c1","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-8qvSnmY5E+jZo+5KqJbVbx89+DHCYWPjkB0Dz2ZVs+duoCifg9DE8iBNdxso4ZyiOGI03F9MuG/N1I4pnLqvTg==","shasum":"11b0b71646dd1043fdf320aed972805d17fe79db","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.5.tgz","fileCount":42,"unpackedSize":896849,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf3HZ5CRA9TVsSAnZWagAACWcP/3kjZdrftvLV+k9XdGDb\nLfvBcedW0BXUa7hnLx6VSDFVt+56cT7+aa9O/4w8zfJDEtgASfyOIpILRKy3\n48miURonzTEgZudgZUZLkR6GwiCEakKWqX5e4VGcBzBn6/fnnoMPVeh5JM1U\nD126AE81HQjGE6op+YgdPNNWYxxzXBJCykRZ3jhL55T4NKBo6J+eQLGIpKSb\nPXW8TXtWZF5I9kIdI/pBQSjg6MmYQBEQBwHEZUZNRjnITIIwzYxOTJ0qrbt1\nzKU+pmDKONxJXtEA7tZwfpRN6mQDCrMjeRNh11PzXrs39fQI/a5kR/O+E5ch\n4bqPddlXOAru+HACKpmK1dQFEVlSrxt0Co5lDPAb2xU+84hr76BA9u+jORZY\nlZNDrc6mVZuol0KmPnrs7d4sBUnXYLkUzLbzK/F/9cEMhdjyCnGTbD0IOW1d\nDWDih/GtoJieI7wj/SBnAUGLTIY+g1sl+bsNCX8EOGqMj/sqEHrRfqx1e+n3\nDvUmksnCF+oI0N7O0LE3tOceiYOLds1shim3VXKd6THgPvtB+K3qobeOtx43\nBFxNGwS2Y5m7ez3vjYyUVxLAMWzXBm3X37qsk9mHkEKOC+UJp/p/In6cljCL\nN1UKRIv/Q3UWWaT0Njj91A2OpLD/XHkucvinWM1SipZdAHFaWwwLzLfLxCPj\nioCM\r\n=2sK6\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCaXF9sC8/4xtzEi4CUYGzvmubuwEdzejsWIDldHiKsXgIgD49Mg9yJnNkP1sCyxVSyM8Yd6j1DMPKLsVQgd2x0eQ0="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.5_1608283769108_0.9976206431733636"},"_hasShrinkwrap":false},"0.0.11-alpha.6":{"name":"import-remote","version":"0.0.11-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"5e12ed2df04d17cf32b6f622360cb3860e4d6e01","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-npUQ3VBRQPYBJtsExpfYMfPGGGH5bY3RMnQu2dWHf3lrT+uDaX94OlCO2u3+8uk5LDfeMg3Inc2zk8uTXARupA==","shasum":"229d395c3caa36104bf6f59de1aa845bfce90423","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.6.tgz","fileCount":42,"unpackedSize":896849,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf3/jdCRA9TVsSAnZWagAAFCEP/0lgQP9qJXot8ZxM3vBg\ns4pGjbxaIa5A/X4aY8EG6s7D/atI7IG0mZRzZplLFwgeJ7Ikx/UB6TSf4EHk\nSEDKRh08kHLwYn4Xo7UOXwigaOr9dEa83y2J2RnDL0R/cTT2R9m8aTji2luu\nHU91143M9JQu3RsSqu8lbNlcbzpz1R/xvANhZ0TF3Uwu/WNUICEj1JbH42pQ\nmYjnKeGwEUYb3mwn5X3MRtzuGnOxckOvFRhTYRtHZNqf4f0TNk/tVCBjDsth\nVhW0Fcsh4VcQfZJOuQbLZYETpzdPJdvlTjzc16oChZcS5wskg0TpFckc+aJJ\nOuk4teav+2g0c2cUT6Mnpt2CVizfYfDaOCHnUbTo8cMUTF+sFTysBBxQEvpX\nmrczybzpgvCF1p+o2C23N5jJKhVDJrZU8IROPzA2l1hWPsv2H18u+MJgsijV\nNjTrNXTS4j1FMFvzZ+SCl1d63crj6MqPcKculni2TxhbuTCzbjbAkotYVX43\nEG57LXE9e+6Ly0dgWICHlwD2fxA1mr5i2G01jjL5pueTNC7VZI5rj1CWUGkM\n766FmHqUko3HTHcItK9u8i60IBeBlSmunZe3YdyTj+ntMux1CqNuR85cBeP/\n5n3RksLbwQOE+EbCT2W2RLdwNRP/CrNOJqKJ7IAWQGD5Wewv41vWnxp7Dl/s\n4Rag\r\n=15gQ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAvmj21U8j5G/Oxre4nNHfwcYKhroB2WSmTQFVzXTti0AiEAmfOEcjUHarD54Gw9Fo9Hps12jlFjDwLYTK8SOmVsgQc="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.6_1608513757375_0.6707359375774218"},"_hasShrinkwrap":false},"0.0.11-alpha.7":{"name":"import-remote","version":"0.0.11-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"572c63b4cc4b6c2e26edd02c25f543d558a8d272","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-EtsP9K6YgfRQLSo2OLtHnbyONI9+bmejqoyy3Yf8yz13IABoNgcrXDM0MAeXR0liFtMwsrPeUSz2euXgzjpDmw==","shasum":"3c86337e7975188d39ec81ee3997d562410015f7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.7.tgz","fileCount":42,"unpackedSize":898406,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf4GKsCRA9TVsSAnZWagAANboP/2lacCUE7gVZYm+cY6FG\n/dbudUoj4BtltOetjcyvxJqgo9JfJrPPpEwxT43UnqYcvuu6YHEvkgOvI4L6\nUgo2eV1iVl2GkgxRY2QORNKka7GjEgo2yXjKIGgRGANV7uYaCtcxUAFt06n9\nigb6x9xcZTNaIqAH/k3/qIVW5KxMXXdZK5IsrdIz9vYNcLwbHscG7TPaL0wc\nlbd5LCLZW4oP7yg6C/GKJl1lFxc9fhfZmzECAbkhUah2mxlsyt4tulUyUsTc\nPad9Bqruan6gGdEP/dRdc78eqiqGVIFvU4KsaA6Subn0yYrgomSK19KH1iYE\n5f2qkwNVGXx/hz+XZ560zOcmHftO+0qCS7j9KDcsiKnrQq/sTGB8liWknwAm\n2uvDMGjnsP+rgtyFggKNTmZ8UgP4hAM3TKA1Vm0T9xZWd0t96ko0XCHmAnTe\nV3moAl2ErQpXFYoDI4c/2EGVeqkG7xxtaDdsjWF1B5qc2pPMHQZtp8WMdejL\nnaid9FnA3wgnUhl54um0jweLPuXAE8L4knpwnnSc+XdBsd4HCSNd+VaNI4Da\nwSdeuYDJXMOw/z856HqZ7vAg9vRzkEA8u/TFyZ3LuZKvhwxm/6cB4kd8NK8J\nWYF0dkb3Y8dAqExcJIZPITnhvR1vfPz0CGzCipuRY35HeLXq5JokXtTPF02N\nizqU\r\n=UUOa\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICMf/Z3oZGs7jES6UTYTuesmMFquuLVmOS29FWgrGCn7AiEAypwRsnmURn7x/rtNwq+nOVhG//Rbs44DDm7eZ5Wn624="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.7_1608540843818_0.2561852498906083"},"_hasShrinkwrap":false},"0.0.11-alpha.8":{"name":"import-remote","version":"0.0.11-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"83655f8f7211c2e245836c0e82fb76b0c35c1a6f","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-bjALFEhfFFIsbWb9I5m79PsbuKKGqN2okDmj1TQL32fAERJ577R8vN0Z/F5ZJl7l0u2J9PZWQFDkibXbzal77Q==","shasum":"dee5c013e85ded84948cec5cf9e2049806a56d0a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.8.tgz","fileCount":42,"unpackedSize":902394,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf4VuwCRA9TVsSAnZWagAA4oAP/1+p9C7AkZB9oL2s/Bw+\n4si2x2FOtGhyb5iaupHq5nFXdhD10HytrqkKlfmxfnZUvPJz9QSbphlRk1za\n5EWemhRA98mpLo8L/Mv9iLPoBxq2F3M2xcK/vGn/H5Fui+WjydAXKbyBVdCS\nTUbA0q1yjPKv7RQGDOoftue8UYnX9liYSy/MM3lHafNfO9D3BYVgks1TYkhS\noCOwixXDL+oUBEMwL4tUUCEpVfmUK7pfebCXy/WEaa1sBeTdiQGRxnN1ga7K\ndAcPUin17iXMr4r6C3m+MkwHN3xEq+FPbWEO5fKFcSrX7vsQ8HpmLqkT3OPL\n3f38zHnByd6u8gCei8ypAprysisDoNbl7Pkzk+Jc4MU3GgaTzhulBCPmsb+3\nT7fat+9EeuPxUIljRtnTGUbnKiFklUvQ2vpax5IZYtwoU0uS9omgiPl65gsH\nY5kSxL2SFkZ2hNxSWRpNMQBVI4Fw/uWfs/dex1Y7DKyRJCWAS+JFG0nEEFw7\n3DIvTrA6Ht/MRvFXlX0Pb6TWMv+XY/S61LU3E5tHgjFsY0HEqIRWRQ89aCuu\nfrC04ByA4dcx4pEreAkqXulXHVmC7/x78QZazSUxCZg1wNqrJSwab/eP9fnB\nYpx4K9QDjf8Hd7bzyqmJ9LBIZDtL86mhn5VLPDALIq9AfvV8RAo485KHwAjW\nDqn/\r\n=oNx0\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDGO3ICfc7LAXROG1opgixjzNA+vlTaPtneiNx/+XOYgAIhAPMIFggU8JNvOyoUl4tmfSDY5iDyFfAKpLwA3gph7nzJ"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.8_1608604591573_0.6864419476226382"},"_hasShrinkwrap":false},"0.0.11-alpha.9":{"name":"import-remote","version":"0.0.11-alpha.9","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"83655f8f7211c2e245836c0e82fb76b0c35c1a6f","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.9","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Cv2gJizZcI5pjuYeupuukZZiAkuJRkhuAJWkpjz6MXgV/5iNmc9encalLQd482SKBmlJi1jFblrkbkctWAVSGg==","shasum":"acd34d0956d8ddc1a0e01781e08793553af0f8ac","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.9.tgz","fileCount":42,"unpackedSize":902014,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf4V1vCRA9TVsSAnZWagAAdoYP/2uCKWWHn857GbwZqZuV\nTYQ9YVLpjpENrIRgbl8cv2u8+00zVtPlMnxgeDJTi1PGmgM3aCLfxw/BHwFk\ncz/mXTWCpgCuvwCU1pjoOqJ1IkwaSFuBAG5nmwdcnYL4FNekIMmy0trnSpXa\nyAXuo9SvCKokTFdVMKhVustjOxdp0vrG+uGbCiCH88TERQfNqxqthTR4bMGk\njquf5bTVKvP4rAqxzyNrZR0ezT2hCr7UiqvTrLy8rSW0FaQ3ffqdM2yBXZoV\n4q6NJbTPVL3c2nLLGLNNf+9fix5fyifPU717wTZNzhXx1qejJkj8daElnJHC\noOo8XumN4PJUWr2TPTmHp7JoIOjSW0+J5QQieP2UIV9PmiT6M0wHn8IVc0mC\nCMHw3L5FAMWqrm3yja7R7ukm6xgJIMYIeZnyar9e27B7Y8bt/Y7KuKlYspJx\nzKtp2y3E8z2YaRyfTMlbu3nzy4mrxo3o6ghks6lSUGqycxHVTDBFTvaMNy+w\n74Pvb29k+zoEYvHXrLz4yHNcEhwe9/3HTXgrnivzhRt4yw9SCY5kmnhprOHd\nuAxaYlEDvq9y/3cYAWyPar5I+iXxiBaGfjGmdP13Q6XEbHAuwJO6gnfzS3PZ\nFklO5kBYYmB0/SIrvT0owWfvjB9pKWMWIjV/2kcd2WzTmahFly69Fu9vrkD6\n+uo6\r\n=EQMd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDU7mCjOMxf/AKAfE71BT2e5YVWKeHrzWSzgQ02W0B7ogIhAN+yzt83oegUlFkk8Up3F6i1fhsdP1ZDkjpDCSrSfyh5"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.9_1608605039221_0.6792551362305452"},"_hasShrinkwrap":false},"0.0.11-alpha.10":{"name":"import-remote","version":"0.0.11-alpha.10","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"97f8acb8e51548688434e90d3db3df29f1087636","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.10","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-mwnfL6PSwO4PFEDPTQGMYVI/PMmCc20KXZuOAiZh18hkeqk1Nx0rSfDfeVdWNeZR0+YfmXn2v9ZSFa1Ppv0zVA==","shasum":"26987fcaccf72e3aad82841a04545d6fc1dbc871","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.10.tgz","fileCount":42,"unpackedSize":905228,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf4cvECRA9TVsSAnZWagAA82UP/1BEnq7M1DhrlCMTdU7Z\nLhC1uXaXyQE0amcKdBGgp7lNnQzm96+tsHAfuVY1xCH3UNY4wwKjyUHQJuGa\nuR3cQRc7RFP9CEkI1FlPYClHfKIb+hoLTmWw82VKYTD3N4JspDE41CzyK0ay\n8tsOQZ6MAI1xcC1jxmdi1Y5wgasrrxSL2yf3hsVQjZ62QqmwRO+wdT+V/ZKU\nTxS8lOwI4ZRDZS47AVl2B1hYHYCvRQnXnqPn4xbgxxLrP1EmHRLb3MEpx6nx\nA8w40mZA9qre0MB3pPPujCbw1tKe6fuzk6l6qoGxwSWsSG7JnbNhk3MHdkpE\nyoVwqYGXnfXnOtvY5jvhoVZf023jYxDVdGt/bGHftCxoZ8f6YrQqnFK9q0Ui\nd7+9KDK74TNnQtIKLcO8eDXFH8zMQkmPd5EEsa2XfAicICNg8kaZ1tCGBXVI\ndDrFIIHWifK9rElZmX7zUWu+IFQzDHHOd6YAH+sqdwC6UycYeM+HUl/gQ5Ia\n7cVijqjiuKUMu1iyOydsFAjvTalriNx3ypTkOKU+CauxuxL7lQqS79/MqFZ0\nL7IEoyxXOKycg6q7RNKmVD+bV0d5mMWEFM50R3dl+SPUDw4qgdGY5lcCAx2/\nd/xS/XnRMxln0DO7rVwIsnIm3I/8bSImf0uHXCPyxTYCHvu9tMeEaiKjDBk2\nSli3\r\n=yTMo\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCj2u8TYqEdnJuffiw6t9Rak1dlP8gfYBWMr2TQrCp7kgIhAMNZhvlW27hUpvh+tLDZtUdIlrEHccKyYN7WIGpNoEbu"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.10_1608633283840_0.8118034925379691"},"_hasShrinkwrap":false},"0.0.11-alpha.11":{"name":"import-remote","version":"0.0.11-alpha.11","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"6e7e80b57af21b66bd359b98430b4457d0c4404d","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.11","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-PuGOoMKnwqLGuvYBkeb3qHoBRiCrWPZWrCPuBCCrsbSv9GKqHQqA+f6fGepa9Nw6pij2TMAE8ewtLAa3osWuTg==","shasum":"8a55c09fbb7ee890f82880655df956345e1b8a17","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.11.tgz","fileCount":42,"unpackedSize":905284,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf4uS1CRA9TVsSAnZWagAAwxwP+QFfMppOufA7f7AVIYcN\n3Kbp8EuhwyU0AMzbaFuiSF4DoieRSED1/mDa69ThO2KXcSIzWTQiTtXoqFIk\n7/tpE8b557PqX13vQIQ55XFlCu24VwYdqD7Aqjkq69e5s/50sxAHSnkhf5OC\ntugS98f0g5aRQDZQ09AV3oLdfvM1nFtEIWOhuL2esMkrugki3Tva1r/I6Zu+\nU2h7nhcxWXCUL2CTCZf1vz9cY/5QUe+5Z/DPjN/INaxWcg+qcvHTqSptLqMH\nZr8mPqFlzzBabN4ZvnS+iv9j9BlpJONReOOIf2L1Yg/fJKDnF0vAnCOrmUHc\nAN8AZh4RPtoHQZR06Whv7jj+7Jn0PQ/t2kca+F49UD9EvSWMmbEesZjwJ01k\nB4XPfvKntevu/9Wpqa8aM4OHGUJLsRfTgc0ohuq1DvK6a6zE95jY533IH5Cs\neHzXTWmdC/cdfI4Z004EGHu8o7Y4xeISxCQO2+jzbpf/MQAnwG0QrLQHy11f\nHwuGOtZKmyrbLElu7GUZ9IW3W+XAXfJzSxmkXhJgVWPAyARypVPHJ4wZr0on\nBtkQcAX+OIRohHmZesm0ITcJpuSArJken3zgDMRz0cJzrw22mkrRLLgebGAF\n4+snZl05uVCpcI/zrQdoRP5BzOOjN09Zq/kk+QIfOZhzZOxEKDjSQQb4CiNl\nc+2z\r\n=ixx1\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD8hXUd9MbLznfPLmlRT81Xei3aI1bb8vqqSp97cWfHjQIgMm6grG2D6TzGxdd38nrQDwVyEfBfpYl2KsHLeTi6oiU="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.11_1608705205318_0.7660764056637765"},"_hasShrinkwrap":false},"0.0.11-alpha.12":{"name":"import-remote","version":"0.0.11-alpha.12","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"9043f6634bfde6801f2ae61557562422896dcef1","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.12","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-piG2fcNPoL9VMO7cZXp5k2nQZuT5nu51fCK2fUywy2YjX/kbPHCFAOqxzyBC5COnbhncXpSiWtBh6sD5ZEKNOg==","shasum":"3c965fadbd137bca06b83dcfd0ff1aabfe708924","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.12.tgz","fileCount":42,"unpackedSize":906001,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf5AKUCRA9TVsSAnZWagAA4OoP/jy5rAhbimkks5yhk038\nfqg3C0eVs2U7HLvsD4TNbG/77tS9c149NTx4lvnCyn8VWpussejv0XHNxkVg\ntJm3QRmz99SmjzwkrKVaEzdCrEAtkAgD7MMRU8Wow2+Pcots94G4WQlu85C7\nR5eE1TbqG9SFh4LLMvtoMIWnGXDG6pf2sQfTsruEtrU5FI0Gh4xMVeO4Fn7C\nhqkaPb1ZlnQ0HAp0mbForlVjU7UBxhHTjag1l6lCLuC8wsE/owYxUqZnYDfd\n2DMZo8Eq1Emby+V22Z8YTxXfJ6jkSBUXMIxHYTysG3SNiFkG4DEqJ8SqxGOf\nk1Ut1/0qSLBS25R9MVQaUH7EbGg3Ks8/D74SPB6a1vP2TGQ1urCN+J4Hh1S9\n0OyLJ/gCcOLle8t38z+gBxg8i2/cwxWuEZawzV0cqOWrZ1i7AQPg8jMLc8Xc\ntQjaF5TeumFvn/LgFtbcIdWkdzuKQhIV01dgheGCFjID6/mmpZ/cXoBlq3sF\n26d/DM1psRc4AnnE18ZpGK/9KVtHtuimHd1I/4cMe804bhtAksa97+goraES\noLxJ3tOK6eFIqbpaPmetz0IsF7GKTEZU2FPA1YlAtfECj8Bfz+HRxZ4FkuPo\ndKR4ouqyT0nmOx/WQqRyjrqZkDJ2KeAdryJBHC3YGyRblC4/uddieAX4SvTQ\nqtL8\r\n=hIQV\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHS+qI4nfwEMT/y8KhlgdCUd48Y1z7NrRdFXoJwrcTXOAiEAs0vQErOs+h7uvHJGVvEmKQgrjpqQkEXgtrKlH95CoiI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.12_1608778387560_0.8608587837795145"},"_hasShrinkwrap":false},"0.0.11-alpha.13":{"name":"import-remote","version":"0.0.11-alpha.13","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"82eaf33f8ed89cb972fab0217530531ae98bcd8d","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.13","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-EaoW/Ygxn9MFf4EdsBiO3XMYdWlJ2ta7yZZ2kObr/5juwxcQZcyvycILjo7EGD+JpGhX7vKTh8eRTz9U9LnDrg==","shasum":"9fc52454f44b162145864704f57087e167a234df","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.13.tgz","fileCount":42,"unpackedSize":907930,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf5ZDOCRA9TVsSAnZWagAACwMQAIIEGWv/lTm+5uHpHTIg\nnDrLNyaqCBwX72bWepIqvs4ADn7fV1N74soqt9U+kuDljeibBKzeA4RohgrP\n/DjL3TCSr8Zw0CfRAni8A1ofV8KgkPm6mfw4gedQ8Ajg4Jg+0Nt/iLZvv7kt\nwEx7R8IVKuqpI7u1gqJhNY+kYudjEe+LvjkjsOiG63fDlfjT+Mth80pQagWC\n7lQ2s+rzw41QLwws3Cy3SCJNqZWPxAXXo2oB00cO/8PAEkAuXbzW857+ynuv\njP3IMxq9gwuAgljM3lkUNcssk/Hxpcxq1XdLz1NtD0+PCbjmugRzhxk9uq0X\nf7WggRWzyOVDREJEnISNQ2WmqKvnSaUdQDzlbOVOQpg///AcC1xNMrbxUrv+\nadLRRtLB0T+MDaJJorx19xgpwXC/TybOrvRK8uXlxm86FClI9xq6lGxh29Tw\n0qk6vGS1wfbX5bJ5jbzk/0c/qmcTKsZYE57E6UyYjUp9WPrT10nl7FAtPRId\nMwRQSM+2Jo7uoiFCNaWPAg25CsuMOeRhIoX1vHGH3/skRVb23hpWN0M3ejCH\nl0cH3ggTuXfdRJNmu9TKnXUroKQUppvZLfT2YSjewRWM+KzAN5wpeT9rmrvW\n2rhXF1lKTMpiVh7lllKjiIm6LDVn+huVwaRMgF2ZE7Rip3/xR8y4B8EzEGVl\n1Ua9\r\n=kcGz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCq/BfDajv8jnwCJDIq9WtghcVzDviVSNlWUBFgx9df5QIgFVlb5D7IseDbseSoCTcEuMwwYStAGPWdiuDke0T3w0I="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.13_1608880333324_0.49572286495049567"},"_hasShrinkwrap":false},"0.0.11-alpha.14":{"name":"import-remote","version":"0.0.11-alpha.14","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"a5cda6fcdd4e73e5686a83c7e530b6923df298b6","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.14","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-siZQov2NOGA5JLhaQZkD/L4+hRWKbg+SUPFZmrTJZtr3WjEGPCz7sdVlq9lOEXppz9SxAuW7M4xWEo7DeQNJPg==","shasum":"5aed4a81c53bdf9b522dd91ae0eb9e6b6f480c7b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.14.tgz","fileCount":42,"unpackedSize":908663,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf5aPzCRA9TVsSAnZWagAA7t4P/jRHY/MVFpdPLCzLAsy/\nbalnT8uJhLp0pjcrMF10kMtciom37kptlCufq12qMnDu5N8gD5q2F2WnXdGt\nNpFua1uTZ7JRRkuW33dBiY+FYQP3daTlS6hRNcb5B28TTKH9V1ZsdDToOMY4\nz3thwSWVMbDuJfFWXRLYWxdQeQcfJQVuZrh5RsSU3LO43l7LqUx5sMiwpV0T\nxkDzXa/zGUkcOaMF6pWfrZl96eRoOcuRW2KskcxGCXRM3aso6F4vhilsQ+6t\nZVQx0Mq7b5t/QDW2krMgXECG6EBZbjJovrz5QSUbcFrudXNMasTcOBcbflzI\nTtGasqFAWgOmpHmObU/ws18KDzKMaR/zD7nO2MRTQFRIrwSE6VFZtjDIPydq\nn90XqcT5olGDktbEI924wM8sxTzbZ4cYZJirvT/q1Y5eWtu6mZZDQa3X+W0p\nibgLZDaSnynFsG/3xUaLuFbeN4/CjayM1dmrmmhM5pCczi8qad4VRd/KDHOa\nITAGPItO+hhw5nNSuw78U1qwUlCCFCZeqQZ7q0gsUum+LyTPWsxztWKoNvLV\nFsG7Es/jJ5Im0TBaTHIVsb+KGCPPHio02iFLKagS3bNsgR7Rc4yOHGjBDxDy\neQSoAWmlFaYxLfPAq2UxzgkDx6SUXdmQIvjT2obdnSeZxWQQ2Ck7izR3FlSj\n3xlB\r\n=Pr7z\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAgnPedpMbQvCWSpmarxK1qoifPrEEJh9u1gjDkMZObHAiEAt+RygxfHBwompfN1zaj2zrInuos0SlG3wm17/qBaooQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.14_1608885234792_0.07518630090443135"},"_hasShrinkwrap":false},"0.0.11-alpha.15":{"name":"import-remote","version":"0.0.11-alpha.15","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"9aabe6b8e35fc81936d8e7159d4adf1716af6757","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.15","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-lp13wAzGd1LQ3/fCWQ5QTCeezE/vupdIMLtLWWSxxBijfszulzpX/ZAJFgbGoomgiDws5OVeNvtqx76pgqpsgw==","shasum":"5431db3c92cc25aa4cd67937bcc6ab73e7834916","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.15.tgz","fileCount":42,"unpackedSize":908637,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf5a6KCRA9TVsSAnZWagAAa4YP/205iksUaANplMoCrpWr\nLHIGYteF85wIPwNIaIQazcoGxA15caG9QBrnys6jssK9jqIOWbPfx2oc8mKf\nvAEDtZacmWpgDAA56hFckEF51jVEEcesCu6t+p3/fLFJOuVE0vS6jeJy3PDE\nqNpIbBrKpVdbpmOJPgIUIsANt8ry514FA9oc2O1XNu9blKCtk92KWTnnvPU6\nELwKQc0Ad7828dTPQZ7kueX4tz5n5/f8IKt+etb/u6I3mFY5GvIn0Q1WSu9j\nNvXDoN1/rrlKwxeJuc28TnS1K8w4Sx/aIXYbMp+xdkVwVvqsfISFI+nzrfIl\nH/XWjqd1d9iF0PuU4gTh4Z4BnxJxWxQdQAGwAvZkUNQKsFRc9mxJX7SsyVTG\nMH0xZk5+zKTuwbWtDmc4i6//3zJTRhlbEDdezvBvSdr+8NQAivc33Os0gPpF\nKZHr+rsxyjVTMcfHMumSmk4xpJDFCscyEijmazm8Po+4/6YrQzTRaELIYZLx\nCFN+vzSl5d7qpjIh0bwMuyFexWaYx5kXeRc7YGPqRBl0dglm7YHPAgCh96QI\n/nQibgy1afHvB0nNgSBkfvlnppH9N5fEJ9Vt2K8spJUNcfJja3L/serbUzcS\nc+RXsL2qzXtFzCtw8XuuDI3QdgEQX8VLJpS2aiENZeriLyrARJQgYrOGUPcI\nCW+m\r\n=oHzg\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEtf7NsRPH62ZfuwSWaoBQQ6/Cxq/S1T1iyCLf3KQnTgAiAQUNu1XSxAqToxPWnerRgQz3FhC7kCcqsroEz7N2+/ag=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.15_1608887945679_0.4330116686182903"},"_hasShrinkwrap":false},"0.0.11-alpha.16":{"name":"import-remote","version":"0.0.11-alpha.16","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"06a6dc5e8e0c71b886e79545b31f18a49c20e075","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.16","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-VrrUz0ma6QykB/9QFmz/im6t26++tXRkrDGw2PlQmgAFs+gAopPeaJCgt4A0k3x2YFDMfd/0jbk2wtS7OdSijA==","shasum":"dbdf67db14849f87b0ecbb2838463a2be432ac96","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.16.tgz","fileCount":42,"unpackedSize":911265,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf6paICRA9TVsSAnZWagAAEcIQAI4k/KwidYcVUY1sPkX7\nWej79bLYcYX7HVPofGRktjy9+rhke09P/tfJ1HaqxZ7hAJ7jjJI0J04vJvi6\nPCo24LWXKqMCZeykK1U1XC52JGEROYsoNvY3jbEgNTlpOzxtcBnn27XVDXK/\nWWo8p71SADVELu7d9+GsB0beRGQZ3Z67SKD2Oe15JVy6MXU7EK+DCx0//jy4\ns/5XeepFSBMr2fVWn7YemOwS3DJZNW5sIkhX9hlWSoNKggi5xo2qHrT58462\nv8Krt6RC/s4O9wOQbDfCDI+jIdzsLwQHiUQ2WSqbVXCjrOwzq29IFkrfZddk\nYFojkXjfnB8lVJT2c6scOr+CRFHqeeilZDc8/ezQP6nljFJjK6ScZ9ETiS7u\nS98vVYpd3C579ANzekKGb+QY3oJyspw8CK/oNw6n/5+2zpLug53TbPTcLklv\nq824gr92IcLAK066U8d/x+NprJYDZqkq6ZPC+nEBdueixuaPrfIf7CpPDKgP\nYBin6e8xeaxvMhd9PoTDb9zc0kgYwBV/UrL6EXOqLT+GIzcemU7iwxpkRVHt\nIhyLzg0WyKtzytdZSqULroWM4nOWskVWKMVTJPCXi5Ywm4HW+7vtd4VzXsw8\nGNJ07dH2QTeSwdNdoz74F5L+gu4w+jmAx37hSEAvVW0IRVtG/hjnsHph7acC\ntsbH\r\n=hmpe\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFjK9nhKjYaexW4NvvnPxT9QPVC0ahr0sSvkHHPhbCxTAiAzud0In/wXDWwuwY25AJqfPi8TKpQSVDY07VJ0G3ymmg=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.16_1609209479710_0.3543286692219565"},"_hasShrinkwrap":false},"0.0.11-alpha.17":{"name":"import-remote","version":"0.0.11-alpha.17","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"db96023af5e79caae98e24619aae4e77258f04db","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.17","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-agrFMxmjmpYxCirq+FqC5iet4pIT6UumLaKC7JIpfuuqrf5wdLRSNthZNblVpAtpksw8k6fNICWwKM0VQLU3Cw==","shasum":"66ee65733704903df85cb385a9e3f450559009c2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.17.tgz","fileCount":42,"unpackedSize":911277,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf6+WCCRA9TVsSAnZWagAAPWcQAKD1lOUxtX3i9irqF2/G\nbP0IgLltAgdWWkwDy1OwCN3vt41OjVzZZga/hexmobeFfLgXL0IDCcPmVP8t\n9fZ0cqfS2v7115UqSE4hGHFkqZ3k/VKdcNAsc2h8imQe9nxED2pAvESb4cPN\nygCxEiMzzWmY/eirNoVSNknF9AQ32+SI46UDey7PoR1pEZl3Ecb8i4e569NF\n7/AaFgkeRlKHQhpHZc6isiahL8X7/1NECcG0o7vTOKT5WydibLdFSuEvtzEz\nGwAPvpz4jkT2sItOfSdJuyGSUyomBPPX6MihS55UgRXAhG6g9TgOeT+qcX35\nQ6O120JGfp64vlMzqsEn2lfmMrSBP25UQXe3Zb6dL0gX19gtM11N7pk0hgbK\nK3HvxXsHM3sVETNeatktEgSDoFAqkIV52KP9XHg/vli2G/kYlSVoDh/i+QYW\n/GCeUU3CMlGnEGEj3b9eVzsESiUPQvmOFGuylt1vbJaKQj70TusXTbiwirVA\nIIh7tNFVwDizsJIA6tygD6I4iGrpg220/wZTTJVO3Fxv/d1icOptmuqSvGIM\nwC3M+ZbiVsOVs+bZn7xcLOi9RrgQsBp0gTiD4PkPPZpz3T5BZKMDjasgQZKg\nniLaqQxJXPXNh4c4hSlnOqdqI/Usmn9xUoC/Yn+AlFDeTzFsq6hm2csW7Blq\nbyTS\r\n=+I7z\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDiZGuelnHmidhp2HXlT+Gr/lbeGmF8++KVuK6SYutq5AiBqZjiErOSKLkhl0KgoWmLz1TC4VW+LTHLBQtuzQ7E7yA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.17_1609295233978_0.6094600836656914"},"_hasShrinkwrap":false},"0.0.11-alpha.18":{"name":"import-remote","version":"0.0.11-alpha.18","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"d2746323ae80f73e8e963d64dbb98dff47f4d9c4","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11-alpha.18","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-3Nd6aBXaAL2P6JhDzk75zZuWcMV9Vb9Ofx76w9Uvob+5iBfN+Sb9Cw5uC1v1A0Aho7HaOt3c4M9TdV1zKR3I/g==","shasum":"a19dcd6bea8fead65a8c42d47aca87be66e149c2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11-alpha.18.tgz","fileCount":42,"unpackedSize":914979,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf8qs1CRA9TVsSAnZWagAAXt4P/1ENBBxK+7kILaIkhz9G\nvKFBV3TvIaHOH7ikWyYJYgIsv+FBKWCm950td+auIoXFXJ9GBTZc9PD29TIf\nfAhCapc1IGGXwnVEsfaKwU5s0/p28vVM0sMXS7iopdkfWNWfWV0OlFcJYVxn\nI/fYhTv1Pio/pizUnpdt/szER1O5L6PfV9XGUsfNW5W0igA69Y+QIRpOLV2Q\nbaqKiWj6o8HMwUwzt4jSDPWaAtq96bicHkyHHKpUZvPaRN42VHWjz34gOQpj\nhXQAWhS/OwJj1y6ACFq0lVA4LbUi9vp7kLBwUYdgdp37DfFiMFlstnahU0Se\niOtScDk/F6blhzv6oFkHh2ZRnRrp5GXSChcBsuz76/ytI2nBipdJc7LVFETr\nSk6d7EVYcZR5pCvLurWX+uOTRDMu6DTxWFN65UsdpOKyJswLJLKy1yflE67R\nRwlJrNvqvXGWvEXCnCGAGOT9boWvbKyqFjTXN3o3TCh44y2AaCIY44+OXhBA\n2pb9VODHMWz1nQ0/LtOXKP36c5AuXx7PS2Qzg6e0zMr8w+PZXj8RKFrbSCzZ\n/3AEAG7SQ+A4YSNZFIzR7hRb3InXI16IU+FeJdavslUdwSRz7tTIcQHUyO8m\n8pQRAM02ynAA3pAm4KhQDDqO4Zxkl/cXrtjztY3wgxu+lyADMGFsASYY8W2n\n7LH0\r\n=dQyG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIE8x5fCKIXmyrPdN6zVbBdMY3U6WFL5UpcyxpknC9J3TAiAj9be9/tSiAwyfLAC8dMJqrXh0scjcilChgSB2dYZ7JQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11-alpha.18_1609739061072_0.45951733690244656"},"_hasShrinkwrap":false},"0.0.11":{"name":"import-remote","version":"0.0.11","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"6c437fd91b15239356a1fba4e27efb9d81bbb1ff","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.11","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-MMA7BebOJFRZg87H4ItfXwXwqOSB5DgkQAMmmcxFOJH5xsPkDaBuceA2noe/Y4DMBQY6UBt36I/oo0jdrmt3GA==","shasum":"153830f7fc28b3ce59df8de799c31f74791e5a64","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.11.tgz","fileCount":42,"unpackedSize":914942,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf+/sFCRA9TVsSAnZWagAAuIgQAJ1dEv1zQWxuDU3pcI3W\n3BM1jBEsyKkMDACp45rkiEu7juI2dtTFBIj+sP/r04Nton5ruf1yWhPYeBX3\nLEHb+t6BQa+AklQrvqFfuwG4TeHRwUfAYed6AlZ+FW8qUdGs5Y7lOJTiZlKW\nS0ObFGCJ4xSG+7Q2cbM5GmiThqUnJPMZKzZlyQKWFcz+YIZTJ+m89SwFUa5Z\nw9NrJvQ2RNBXPCbP7LKOimu9q5RPdmKWKFeafImaoPYAM0o6Lg/V8SeGJCax\nv+C1lV2JNS+CXltashrnApnFgYPnv3zgtYxWd9G3tqpdKJHrezP/SRArpZkg\nMbIaOP51twLD+IYjs9Q0WX71iHyX5VDNaTB+y2LT0kaUMS7bBIJd9A6D1Gr1\nq8XOSPBLUy/ShKjpsuelwEgDZubzGX8Wy5ORlXWOCIeY++kKhENDIOxqijqo\nRyYOO3hma8d6ClVXYyVAy7dB2wl6jtlyMQu7B0LCI5FeIgfCUCJ8C52/+BLX\nrLVSDk63HZfDgMI+A6faF93iknfMkDX18zWr7EFbEkAAVbWqQR0N7m3YB1X0\nk0Ksxr9A/3/SLUP1AeRvi26ZqPf1MsTaApEHwO/9zzrMJZ5ukZjGdagmxyPh\ngZEyfkn+0mSrCWeJLfVLMis7JUmrB3OfdsqXDV8141pq4eAWOzyKrIgA58OA\no5w8\r\n=poY6\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFZ7PAzK2GMPZxvHBsMhcnGTVqYr8n14IMV+E/pzAYPMAiEAzXhhDTqHWAGjq8z78w2BFzX8C76FuQL6uiWjGSo2Fws="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.11_1610349316988_0.292905573797946"},"_hasShrinkwrap":false},"0.0.12":{"name":"import-remote","version":"0.0.12","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"8c4e7f60a7d5fa60d4338d59aebb36baa02e684e","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.12","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-07Ph1686nYFKU2TN/wgD/XyZZ6DXb1Li8d90DMsctFwvlBHioJV9HUzIbJZt/8oh+UFqwXe5KEzo9Hh3NY6O9A==","shasum":"d04403f60f93a87f47829abf98b7c223fc62ebe4","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.12.tgz","fileCount":42,"unpackedSize":914936,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf/qhwCRA9TVsSAnZWagAA4goQAJyUlWoHPGe8U/yN2AcE\n3e1losLy8rWW4sZUQMHrudUWcFUmHHk/osEnkvPhwZhmf0O85LRlAF1G46CM\nP0pfL9EukpQMf8nXKCJGuKQ9B7MDx1Jc+YwAAIGudkQ0GByd/MB3MiHo02Sv\nq5fdf18ofsaztoa3PPxfGtsuF7lottL4nfmELqAgcM/2zo/sf9sZaoQWEhgV\n5evqM4CDUs68AaEZACm2LLs5JH7ZGNgqZSBKFVPufCG1iAVdmFzyyIE/lH4g\nTxK/vGyAET81siseD1HglXIqyES00wEocjb6Rat3NC/DAc0FsKV9TVy+AGIj\n7YpKFlIpdQdZSLS+cVPD6d2dCm3jHo5+U0c/fIXJ5YvN/ROJGNCkSZb8FOzq\nxHbQSMg2cwOo2tQcniabxPgTk8v+jxxBbreJEUVpBOQxzni3SN2cnsX5Zlva\ng8xoM1e2iK65EkE7pCiDFmimGFlmU/ZCmRV72ijaaAdas9lnDoVPq+YrN3TA\nlo4W+z+lUwSP2xRxEK5GWumd4uUFHHLTiZAI841tH1BVjcXdnw2D5SlQoSj+\n7qF2TOoZOX/x6Hd1QcSorR9aIWAQvllQBeBxPHp2pd7YLDRARTX7CZNaMmZB\nEfGQBKFtbHdCCjxgkV6ZuUfUKjKofgv1GlDBZeD16i2lAxx9Y8NSz3Z6y2za\nXCgZ\r\n=xz2O\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCsYIpqmdDGb91GSeUGXyAgJri9YjhkuXMUwiw+t0ScGQIgAgTKUpASBrOgdMEmWVcvMAYZmTX1Zd1Y3N+0f0AFp6o="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.12_1610524784164_0.4800389608966835"},"_hasShrinkwrap":false},"0.0.13":{"name":"import-remote","version":"0.0.13","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"a0b5885bc33658a1d9e0e030c03a08cabe67b300","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.13","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-uIqYST24gYjJnGx0udlE1epcxsiHS9BsApwfygP6EUBmVYVZtBCQUdoe1y+cGrTvh+qcLBpQv5SBuNd4lu2hjQ==","shasum":"e8e423907da1da8cf4a592eac189d54e8a584c30","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.13.tgz","fileCount":42,"unpackedSize":924284,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf/+D/CRA9TVsSAnZWagAA+aAP/3tYb8PtNr1avxXSCdlf\n+Iwh+/DzBbe5jaYifrqCDxcL7e8W3S+cvLe3JiHZ66qDhWJXtfg3XQ/JMpJA\nVQl+KVKNJGvPSFPawbvS7dvEzh5RAbGSAN9c7ejTRgUhyODgcRvgO9e166dL\n9t+y7HEU7vrP+qKsK/qG4r1O6C+Na4Ke3YSbxAof8uaq4V5czNE1w4EMdjrc\nwQa5MpPSJwkPuetYoYMgck0WJfoDEXqWBzQRicU9uiRqpHRZWLyJTPTvzc19\nzFHVKOAfLkNcxKBMRJ0p0CfkNZIoKd+8eIs4D+eD4wNlUzRp+/TdVQOCYxla\naHGyk/ALCZ0J9/bRg3h83ga8jAQJ3ECbkSygXQQo9PJoSRK+M8IXtVQBw6e/\nB7SDbGqe8uQja2GxxpfFcSBC9d8GYGKaL0v5dq/lNM69symE5+sOTYYW+sMU\nVC2+dkeimQ304RZOVkttPsUfSbT7yCvqEo25QX6J1CVtX6oagT9qXpeq1dhM\naIQ5gAyAS1IQ4kxSHO1rKzre/A6UwBY5j7xogJSPqBVczN5PPMTfSvWNsLJi\nLh6TG7cnkHbpCKfTh8Uqgwp9N1MggFCdbOXYKViozCjhXh3tDgcMtxTM+arD\nT0MGJwD8geTaktz6GPMZ+YXLOD+f9NebTgHeH97UHy5JNt0ExpEbagOFGGcr\nr7y9\r\n=SYOJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCsJ/ryE2JzKGcMQBM5ysbt6o5tKMjwBnHNjlG7vmBJAAIgRD0nFu9xZQLwOuReIjBFcl6fgTFsN2d5Yu6d4hSfjdE="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.13_1610604798532_0.12534259913459112"},"_hasShrinkwrap":false},"0.0.14-alpha.0":{"name":"import-remote","version":"0.0.14-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"ceba78cbe8eab17fc43e0bd5408e0d49be373042","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-HvdW44vHvIBYEv7WwMGujKiwkyoCbhLn96pkCaZ+aZMMHKOkxJgw16Jku4AAjnovMPltaoj9TCZ9FesAM5rxDQ==","shasum":"96850183e8c11f8c8e7cde07889e667f05ea6a9a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.0.tgz","fileCount":42,"unpackedSize":926452,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgAD1OCRA9TVsSAnZWagAAqIQQAIToEM9lNzPpHJjJP5RA\nqutTPQN8NgMnOSiLWqsywPI0/sXPhmsY88RRdSaaVEZFOrjRkJUqYQIaZYuJ\noGbU2XsMk4ZKXGSBazNretFJEaxweNRGbzwJmxCrk8oK6wPfyWCk0/OAq7wj\nVxAqbpSmgu4/NJWOX5uyRm/OvHecHjQ4s8drVKB8Y4o/8X2/TVkOMeOEf3NN\nQwK7gqW6qR25Pk/QuGaHGnAktm+CQlvt99sSaIv7K6HmtHWtCDCsQznZvGan\nisdHmomV6OxRs3nJmOGPfVZYQfTTOPAWN9u4r9yeyIdYD/XWITDf7RZFvaID\nkN/C0Xe2kcZoRdgmq85Wveisz9QU50ydo7h1EJWXjQM36IVmZbZ54UqzAMFv\nuo8eoGJjKY6wvl7SVH5/GsYbQ7CTcQodEiMB6AfTAbS4SOBTJ3JcXXrlypuc\nx/MWGRki7mQ2tR7CoB12wOxESh1yP/0OqzLKDADO1/6DcvJoWSaribsyRPsG\n1v8RaXwcHEuERWoYVRvtvdE6cn7Ub7d1xB47TeUIZDazpkym87ZHR5M5iuqH\nQoQcmV/shEDkLPBtT9b86wxZViA9zekgG6+PtWhTh6gJ19iD7e1WbPhbyVOR\njkrgWpBmU31rekuVVownRScVtPg6wfUMx97uZLUazL09A//lcSZshPwIfnGh\n+pA9\r\n=CTQ1\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAURQ5HVnnhTss7QZuDGEDpoDiHzvLgLAoreP81SKnt1AiEAhEbrIpwxjyc8dOORDZRFakE/XOBDh7i2u9k72km4Hgk="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.0_1610628429799_0.6297223471183557"},"_hasShrinkwrap":false},"0.0.14-alpha.1":{"name":"import-remote","version":"0.0.14-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"4ef5b89367de3b2ac4e7cdd037f85120cb144742","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-2eRkQ+5el6NXQN/2yA/J4OkLOHOa2eNCqFlcyQHRw1Wcn5CNakWvH/122nlsXbrX2RjoqRkyLB/AV9gIW10ByQ==","shasum":"bcdb5a934c6a478edc5b2b17e93c3a4c734747ee","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.1.tgz","fileCount":42,"unpackedSize":928041,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgBq/LCRA9TVsSAnZWagAAuAEQAIKB8j+5FVPb7wXAzYkE\njsl1wukRJG7YYCMdwg/LxLnx9CplseqLWZsM1N0eqnLZuQIHboam4pDrs5cv\n8rgYgn4Ez6G+GUdXIBBJgK7bqzf55cv+LoE8jReLI1ocLKGRTUzsO3KO03aj\nCBJMvaWcmrZCLmmwCOuz558p5LUfCppBp0U3qkfQAIyIWWn9wAyOHr4Dd9yb\nc5yuTF39ymw1IubCRViiHZyAGeRhsqy5nEFZMUsIeCHR1XBokzLJ1Ym+W7dC\nx02kk3q1Ky6tUkgcEUlxLF1cxsXVwO+FItq/KnBhVYRMxXY9p9tBcM/II9JA\nmCu4LwgVgsUNHpOWIK0swYJME4DkjMNxV2xA1PpBJ2vrw5XmYGCvyfu7eCsc\nL7KG3h0ftgM5wukD1ZVlx3uP5Njr7sl1fgrGssmFMCYF9PKSigjE2nwZ+Xa+\nqDu5eZyjmTBAdzzPndlGcXTGhey94jO3s9VKTTQUkn+XumCmMlbAfvkGO7HM\n9hiONXQmDKFnRwlIAGkQhrW06NfVVk0p1h44ZxZu0yex+LsqBI7Dm4lSobfs\nbaodM98Z/CM7DST5jyVw2GI9Uyo98EKlA72nRETrT0897OojHlOvnZId1pug\neAlv/OxQPMr8OpKUzfR02jcRFvwRrT0v6s6TVUJ2hv2zoCEHcNc8w8r4Q32P\nEUdA\r\n=ongK\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCLJyxCctdlM/sfS2/an5gpwMtpNa5rhfeIf8So+j8/BgIhAIrq+kwnXF62IVl5k9tjzFCBsTU2h5u6G94NWxxN+ZYA"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.1_1611050953866_0.9184492788918155"},"_hasShrinkwrap":false},"0.0.14-alpha.2":{"name":"import-remote","version":"0.0.14-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"81e22be8eeca6f958ecafb4169cf490a75e6f006","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-DgHjUyBr1wD0y431IiBFFXKOGyMG8ymVefKtKNdsuFmSYfRXeF+tBJqP1BLjiizv2VL6x5X0nM4WwOm0Sxl6Yg==","shasum":"417ce1783cb787ff5fb031236bc5a281418abd9c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.2.tgz","fileCount":42,"unpackedSize":928266,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgCSO1CRA9TVsSAnZWagAA5RwQAJTjUskdt6e+SwVQm5DC\netthQRLurLZ59nfrGz9ExbwpGNJMLGEBpQZQhCWzW1cCPu5r47swn+BpW4Rm\nZxDf2zkMg4L5EM7wySIpc8iztFudtAx1t88zh5Our20m22PBgEtFPon1loeE\nWAh2haZinJTbasTdvj+D9U+1UeIAgruRhBYs6Vy1mOPlwc7X1sPuUPEMVBRN\nc3EXpbWX31icFDFy+PIqH9cCpXkWbgnE/uYHKAjekUMoWcywmCH3YUNnilpk\n5lBt005d7mrLbNAjCwyWSEahPrIoUpUgpX/CwPy4xX98BiDTOlML8qALuVqT\nPKTHWvwdIzUEsxpMtayBIx53/o3a5rKERJJdSQtlVzJC27CQ0nN3LDg4Jpxc\nwwvzeAxtsJgwikkOn/wrQpn7mqZh1qJtnfrl5QUCq4Eei6KwAQY1S+aWiLwg\nvse0jPqEy1/kGUeDKfbxbKBlzK1XBxG7IbO1X+8UgFLInE335W/3+oZk0UiE\n5K097Ufl0WBB9sEa3gWn7Lo7JZB/5rM380TgNwXqhu7OWneoFarZFcCEY+V7\nhWXe11nx904Zsmy0yFkCJzMmHAZU5zJUhgtqHIk/xcQQrIPkVlC6TL1KUKAp\n7s7wDYUpiAOcyoREqHfQtvqfxZDW9LCDtFJoJWfVy60To7h1oLcl0Ov7yMZ4\n/sAZ\r\n=xWvK\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGjoIfqGQxG7py9EJJfQvmMIHnIXqxZgoGvanhpMPLTVAiEA+w+SQun81ff7UGt7bLu5X68shen6PfVpMSWSL8Zpc5M="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.2_1611211700686_0.05772234199454207"},"_hasShrinkwrap":false},"0.0.14-alpha.3":{"name":"import-remote","version":"0.0.14-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"7efa39254402e49c433449b17237aba50ddcc328","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-0g1M4zoX6yWS3kSIJxgE/axZSrbgkAurj2dNqEHCByqPFqAK/DUfCIkU84rhlh4Agvnj/vY/UyvHywwvxgCM8w==","shasum":"5208e764ac4b06423ddba0e35019efe9712ed2ad","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.3.tgz","fileCount":42,"unpackedSize":928570,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgCmgdCRA9TVsSAnZWagAAypEP/0mU82Gf+fLizN5UggUd\ntV04IloVvPz+BP92FGaRzIEVsZPf6IDIIMgrkGDpx/6m58Hgzvqy1+HqUedD\ndO4sxM5CtW+jNxyBC/CTCsJKBLW+mtZEWvYft4aB/gKRASpcYJOLpPWij7Pn\n1AJZwZKKaE4BxIID8DZRVbd3oHmP0zKKYAbM4lPpeVMkVav0QWEXf+1q+8Tq\nWBOqCvwfMXqO9UU4nKzjEK8pQWr3mx/kKEb6IFQBv79kAX/ikL1O1zhkT7pm\n8K0ilxBNPbvcLeqRp+9mSkAcuBR0AUYDZsEWwUIQgP8pXTvtNpbQk/cKflrH\npN6PpuUsZV0Gat/8tS7TnQq/8oa9EKDKSqFhrhnGiL9SRN/3tIIqxZMPWWXs\nPwJu1QjBMjTQ4iQfq6F4UW0jSLnOtf08be2S0Y6QTbAZxYZAd54mQNhGcb96\npijpZr++L5ESMc/E7KV24/HKs8nvHaWlseVkevLIKczTzwLplVdrUZXlBGMU\nIlEnONlOArfmlRxDgGvT/cQ5gbCPD7cuJt9RTEJMgfla4mmNjXJMjIjyhmUB\n++GXzMggmiyz3KecBmqnkupsabytcG4C2skGW6BgQIf/2u5+NtQIVr3CnJ3u\nQoVI1r38zPG9eM/bcWM8BwnlK3geiheUpu8wcKeP4bTo6H2VAIao2FiAga0r\nRM2x\r\n=viB1\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDFo8hByplyB9PfqFzYAFx2jJr0vPiXfPMDRNayybuUZQIhALNV/TEIJ2pYKn1lVZecTciBw5CHu/CWL7ZUte4d8Khr"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.3_1611294749313_0.2585899980790294"},"_hasShrinkwrap":false},"0.0.14-alpha.4":{"name":"import-remote","version":"0.0.14-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"227417fa474dd7469c6b818f42ea784831fe498c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-PZULYASfGWZCR80OEqTECtbpOqYcwmYFhc0NCeQjFQhhS0SUeoxy6/k7qy31DPHqsf5VX+8Mmb0JoDewnmgRig==","shasum":"31a57ea5d058de72dbd5c0151747b0c2ffcb42e3","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.4.tgz","fileCount":42,"unpackedSize":929612,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgERihCRA9TVsSAnZWagAA+TAP/Ru21YPtsOIW+p1KrYx0\n62NX1bNHvUAgLHpMcHWtZmtRHTVt5viP/NSaSWeJ30/m8pnNrPuDpJMv/TZY\nmzPltEZX3m6VTNBOrIgfMHWfpB1XyotQftGZP2obwCeuVTanvqfMsHV1rM6A\nmY6x272XzTD+6SQfQDjT9Q7OdUnDRGBNDJPZKS6C/0lhMOYOsOQeMQDjjE3E\nOFzIMlzFXj5hGOVbZC/Z7+WQ+zUBzv0NK7i+ASEqbBLqX3MGZpaCnFU+MbnG\nBkFvaR/jMr7fJYM3xJeEHgqrO5CmwLeOf92Mm3ng7Kob+N6LLxDJWy30Q8Ao\nv5xCgyNyYVtjvCRRceeMbij/hDTkWrCGKIyDrKVCthdLU2El9Tl7zVALNioc\n1sjOJrr3ZU87oHDzIkdbaH2GZ3xbCaeih0rpFQrUtCBPYan+CU+o8GOeErw2\nOvG3qSe7mpwZD3ax6JlCGaEdUp0BB8es4FxJac9BEHTmoZhGQdLj/ZsrO/xN\ny53GlFv3gpTgBGRXazNwNkG/lHPJKtzi6RGZU2KYFhvGIlEvKA06wd5WTLqo\nhTSjEZsGBl5utMIqmijAJToanq0o1SQVKXpOL3FAj5VTtutnzVsnBeHgocm/\nxj9utHPUJY8aOMWKhbg3SaaennIgFv9XSj9i+9XIGeODnO81ExsntDNRQWAZ\n1P44\r\n=9JFu\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAFQKJ+75obsNHGfbDTh0b/lIcEf0Nbm27fzqmsRBht+AiEAx1Dsu/hW5/41QmLhMmDRdoiVoSMqJltwLb10BuZDhuo="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.4_1611733152682_0.31652227231427843"},"_hasShrinkwrap":false},"0.0.14-alpha.5":{"name":"import-remote","version":"0.0.14-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"5dc0f89f4bac937dffd943fa278679fb94249706","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-QfH3TauRF/8aQirZbrkk88T+Auy7H4VRFiR7qi041p3VU36ku3JAC0YEhR8pkHTwi57AzMjhLNdP0KzTGkeaiA==","shasum":"9d8a6f99dbc7af179a6306ec65559b63492b5f6c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14-alpha.5.tgz","fileCount":42,"unpackedSize":929338,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgF+tjCRA9TVsSAnZWagAARtEQAIdlddKcrWp4d9ynsfgB\nEBJT00lB6JTi5q+r9Z+4QQZdJJ3pSiLiaNoQ8HQlamyJtwiLbx/O+h7IU4dq\nwxRVqd3ydelaSpvCNN59e9FfcYlOtRzjocGznWfkDfJA7EC3j0KM503Z9yEH\nE6CkbK1jKNYkM2PgEJbythoMDqKPSxnpLaV6s4XuTu+DDyUieq7fya7FQufb\n30ooPo+/kj5viN5QCS8jwaH4X1ufz/yG8VdoJZAq5adpw3X5OjFEzkL4376V\nNVC9XxIoX/3lLxwTDpvyaDeQKb7XC1zCtWiKdbV473Gy87kiPZOwBKo0jynh\nfUeKO24nZ0/m+gP/UoyhpZekVOrNbvVfprJVvGP2sIYakuXVb+F6CxeQdhZn\nuTI3Nvtiqgdr12JoJqM8YuN9KyviofaF+qCIZ2zCbClsTO/qj3EucknHp0oY\nS1v3/1GZAcH4IWndZhthITeHmZ8P+7xSA+mvxDZ9sDmlHwT5lDucf71F0VTY\nB+5he662Ts9nTBweob3c+om8yhGdefEyIoetf1ZaaChAVrGCIxM9RxYlDMiD\n7j/JhnR+BxXzXRyjcHvaJZT+35ZGkCa+VZpuTG/7nRjx8pRqsu24cLWULkzE\nyqbz+oNa8FNUMpC83yguqeUgxREtQX9SvbS5VWw0cxNifxg2DjTUu2sqOuG6\nMGBs\r\n=/cfz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBxioeXpwVPSmDjTOKG+gl5k182VCkuypsCIb+IN37xhAiAaNAYcb/nPIvemRaBpFyMB1e9j5L9ZD5XVYwaNXhrCrg=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14-alpha.5_1612180322583_0.2838588267234119"},"_hasShrinkwrap":false},"0.0.14":{"name":"import-remote","version":"0.0.14","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"0d5ea8e391662a8ef98feeb46f97b45f1919cd11","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.14","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-MuTJdR7HNFvIxRPaoR/pZZfSJklOVubQncO+YQ9LaurMSFJnRTf6WO1TDK2J6Gaujatm/w0HzksSNjRt1OiedA==","shasum":"0d712f956fa0473376b39d2fd6b4a77ae777753b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.14.tgz","fileCount":42,"unpackedSize":929526,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgM1DkCRA9TVsSAnZWagAAGIQP/AxbPN9ZxYLa+dVfGPoi\nhOdNl9XwzxiHYUCH2fFx2cIvmy8Yfwd3b2hCvnida521fC5O5Rsh1BNMCS6D\ndkxu412hTtNJ+aqV3Dwzlra5Us0YXap3Mqp0mILIgitwWpanfpBxOgi4Miy4\nMjvNO9aDQnEhxRUAnCuge7OCtgcfV2MN/VvizZC9hjP4UV+qiM9JHFEqxX4U\n0a4VsTE2MUIgBlKT1WsKvb3i/SHAiozYrGR/sRCLFLiITLPuzEokyvs/jYae\nI3kk9YmdiYzNFNXLr7nGvzi3Jx+Lqu53S1pMXsfdVHRavjmHUkL9Oqlfjg7t\nMVZCmaGUT/vX7p8GUDMSJpYSHgTULqRjFCfpRpm9i3l0KA2uw26JGI0ZGHHf\ntkJCRQ8P1SA/2C2Fgd4dYxzNjA/2YiPiMNBhxX6cIEIlMm/VCJwrv5k8lfHp\nuT3b4aC3w7VEA7QPo7VRHvBs8tO6zr7gaIRikZlS5uvgxJyVSv3f2Vz4YgSZ\nywEsY2Zupnf750VxqyRkZQ5qfXEa2Ox/p/2rE4y5s0vnSKKMylD3ezeB60PG\n7GXDJR0Fl2WiW2pCk6cb0Z8XV95oomI+7GZL0Eor4M7Zh+Ux8puMAHI8CAWA\nJH7oudYv+4HqYktyXVgFiM3MqRUCSRH2M51rgPmmVke+pJbfB+ZWQmL8VsIg\nLRDC\r\n=+EkX\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCWH0/MNhUX9vTqXGik8d022AFVEjGIq8IE2qVF5ZS7LQIgE1iWoQMxRU+9R4x0IQ/wxY2CQ4dC33hZbxnMfBQsnos="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.14_1613975779707_0.473927364549813"},"_hasShrinkwrap":false},"0.0.15-alpha.0":{"name":"import-remote","version":"0.0.15-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"a7601d524ca5b3a0d0b78b0d9b9fceda4aaa7859","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-q928WnO9CXAfBTTf7m4ViIdbiMuk0sfikHBNhDvKzAg+iqVI9qY0mq5nTcwyGuMhdrjDmagchXbsWfNELn9pPw==","shasum":"e50c440cb4ce72f4d26bbc8c245ceb55f4819811","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.0.tgz","fileCount":42,"unpackedSize":929891,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgYtNXCRA9TVsSAnZWagAA2QoP/21dhaVreVqNcDhwIEM6\nVXXUlAqz8xp6fzGvXTFUb3QuI11YqW05hggOVIFYW7tuWCSjbR6snQjsV1Kw\nInMeHf1LckRo1uw5GpB8tmoGlyrCbeHXIo0nnUMPZ1Oco5/zu4VCihwXDXJo\ndt+WyH3GYEb0O2PvpxG415bGBOxXliDTjSbZVz+MMaVsLNpTOAJCVUDdmx4Y\nZF+8GQ+NblKh87ak2g4nYNdBoINbXiYgZLlBJhe+8DAW2mKJO0BjScyW7upv\nFYAVJUWI/iAc1eAQ63DFSPgh4fCfRAfhDJbFeds3LYw/bB6ReZ7k/7JCBhkL\nrbvwlyOeNksVFuw6UPXrYzDbmgXTF6yrMSQVgForBnqOEig0FbqvuRcFNlV4\nMqScl6MflNie9/P+M4bAFnf8Sy8qtJiJ500ESAIYJuyx93DbsU+u1R+qJjX2\nNmxNG9z+1T0kESaE45Gf6Q2bMmPNbdeoHhQqN5b2MR/TuD9zfzsqajKDHArs\niCSZeYNkOBUuYNdv0y+pkDFey8ZKVTNRa++XUwUcOETH93y7UT4bXmceRHeP\nH5mm1D5TUfBvn5lkVpCJEGMhK6FV32KSvdUnjerM1h+/uqfUi32MqKIPjoA9\nJIGbyhQHXhX2E3AEeOJXTU683CVESNsS4eDHvloDA3Oz0+Q94cIACCMbyPb1\nf7tb\r\n=7oMk\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDqFkThOCQYyzEWVrTXfKpsKs9drrrVrSMpvbtkWoZmgwIhAOrNTqkrpshgcLDLAhhtzDIZnewnQK5yhKWAerZEAD+J"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.0_1617089367392_0.7966716062598036"},"_hasShrinkwrap":false},"0.0.15-alpha.1":{"name":"import-remote","version":"0.0.15-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"9aec087ba8a976d2dd56a4e0af041c2ff4e1c72f","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-qk8UvDeUOqAD/j4dG+mYiA1NJ7IIdumDCqDgTo4eqHi7zEG+PSJMi53ulrdGm5y+ZYvqNSIVQG4SXh+q34XpcA==","shasum":"05794018ced212f75b2c6741d5cf29f4344a5658","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.1.tgz","fileCount":42,"unpackedSize":929912,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgYug6CRA9TVsSAnZWagAAB6IP/iKJShkGbbmEYMHYYKYX\na4mgxZ4I36OWR5ti5EEhyfRGzN1D9hOm6cAu3JhF/SKRKbrYVcZZHGkQlP96\nqUgRLdtGL6hRlEslSHSShMetd+s68NXh48MaHaDX58ZveLgzbnAwJTeWZAKW\n1bikxligogxcHEIotcuxlLUV0H8/P/dwlI5NnAzwH3ZH53xXfOuI1R6H0tPQ\np3v0ZvaPp6FkgscNOxWJl9Wwj1obI6yiz0pdh6VT87lyKehObczOg9iPXYLQ\nky/aO69WbFAlRn/E1yJO94xdMdHlv4i/bjgVXgDT693Exw49WDbrpC0WtE9H\nlSu4L0LVjcrGv8zKKvd5xcjS9IDG37yQOiwXg3eMC/ZXMBAjU8wC+li3gWCD\n9gr/PQaUXMeKpaNOeY9r54TrUZSkSpK1jhtbFxWAOlkSoxVaD3wIWxicqo/9\nGwjIhExywAzK9wiWxnB8qBnQ1LT4fEGQl6vq/JSmE0O2c2c6OD8o78xVxwMJ\nu7tej27qf5ABhWpPPZffgSmAwSBhqW4nLGThnUsjZf6duPsjd8x8Wtl+Vwjv\nTSR4EuWAbKhjGT/8Ip2v505tBMGyK++2LzoE47wvwsvW6KUITk837Bm2mnCk\nL4YMTyC0T7hSUINjQcMpfUm1ckJ8VddLJlR5rD5PceWZJGPjU0Z9BobeyV2C\nOxFi\r\n=2glc\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICSaBUnwpunwE/1nP3zOpbBf7/FxTYYu+rpI5mUgOcrKAiEAzKrcdI6QEQulTXDFYj7eX1vGmbarx+A0Ix0Ue+f/MOo="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.1_1617094713590_0.41492269213824384"},"_hasShrinkwrap":false},"0.0.15-alpha.2":{"name":"import-remote","version":"0.0.15-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"d7b73feff3aa1ff50fbd1c2e915e31445f016adb","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-FiNZBkGQ2M9P48R9ius0TFDjP1amHtA/wTle0sPIr70TS99Vi4WjlSGvPydizZx+ZG6ffh1bNB36RonMu5gQ9w==","shasum":"162eb534ed13b2c2cbf09da5b6c8b3710a4b158f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.2.tgz","fileCount":42,"unpackedSize":930249,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgYvpNCRA9TVsSAnZWagAAQHoP/3HPm7d394Kynt+fDJTs\ntxKPXhLHX+q7coy3CTt9wCyCIYigG552fZFxvtyXRCWZ1MpY4VJEyDMUaDvN\nZucTW3Uj/HfNFLZMxjrtsOT8jJmXdU9OlCFqCLAFYjVmM+Xod1hTnzP5hcHx\nYNNbC/gH+5/GFh3XcJ0OgS95jhs8LWEmlttkMRYJZaB5KUiWSnm8wTRKoA/B\nLY1cGCFTRJazWFVp03LeiKUbSz9Rvuo2x/wmtPafysBLuGa+BsaPYypoUsUi\nmBUM2KBuwx3Pwt6b2RgQEccQ4dWkbAzelV63XAyfjOwMll9wM4W4pvWUPtLp\nhhAKZzDgpzdM9iKhmgk0N0rM2Ld9+8CUV1ha3bYLh3uBf0/P8fBqN4Nm3GaG\nW47CIMNUYHINH+Pzcw6lWP7vLOkt9OJ7rpdKH0O18XbHi+J1DwGkKmo9eXmF\nGAnsrwy7bP/9aR6PRdk+h2xHkwqAnx8159MJrstnvJK6VCfWKzdJv5Lsjh0W\ndadJO+SeZIUR8e3WQ51sjJwJ2mI3/GY2w7o45AQNkbTJKlP3A8ATYZO70Bea\nsiGEoS0rw7k+31XkxqkplaUpeKVQz36hG7lNXMBgFeYYes/xGRK1Bi7yxXQR\nmMFIsfCeZfCiohxXSXV6FxtN380zptJgTXbxVwlMYI65flhonQbVvLrdCJ+5\n2PrN\r\n=CiT+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGGVxQvXgLQqQvAdJ6/W0qC01uX3pfGCsMCui4HHZ8/3AiEAgBWEsEYVLz3Tl+dFWV4lZb+2j3xmKEyr7gqH1URUFlI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.2_1617099340413_0.5127290119527235"},"_hasShrinkwrap":false},"0.0.15-alpha.3":{"name":"import-remote","version":"0.0.15-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"50efe54861866a30507a1fa909625a1475e83d52","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-yHKJo5aYHqT/OFK9v8XSllSyAiRVdloJS4NXiOs1EdgE2FktcYL3/iV88cSi01dGYFCszrg+q3klKDuMPj4jgA==","shasum":"21f8e2e5477ef8e63dff359e6259fbb276ac45a0","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.3.tgz","fileCount":42,"unpackedSize":930387,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgZEg5CRA9TVsSAnZWagAANQQP/0gCGMTfgHCa7QkQXy0Z\nHeS9M1krqxv8xkyv2sONvtusBUOLY+R9Fqj3U+A4ma4YgKD2b5UJv/4YjAgh\nbRT4yjFMT7nKs4CDoglH12H/m56BzzlQvYLsdCydq6/eEfSA+D99jbsqo5Y3\nLhD3ZTNfw3idPBl0QG5Zey/AdwaeWASu6Y134/y9LgAM35TknXl8y0advjrW\nFVF5G/pEAOXWa88nnROtf5GoqdBEn7dcD9DdSQafH/fAe2OFM3LqiA1Tuo9A\njKM0ciLJzJWo+b/rMultCp6pTIQ96yU/bWHDoeFJMfwjDk2aw4sOAGQYbC1Z\n8W8PtaKW1iT/BlSxW98Mf+UxUVNNWuOrkIy3TMdVL3i67EOedzn3+BPIh22o\n/VFeylDVLyP2B9zsxJIwWStI78K5Ga+EvKNZIB5mqs9y0Q1hnPw/fByDXiul\n8CNS7RTUhCIfwbbyVLZqKQS8hh+PIH/jR2TvohJlwkfMSr1l1qTVkW3MF5Y5\nibIv16W4ERG+L4sVjqF37CtXCDS3feuEL4ybBZwJRby2laZwlZodp0c/Om5I\n+AHx3wCiVKazqxTbu1rkZHEVWsQjEZwQkx4V28nB90A9KO1AXE4n47Ar1f52\nQx4M81CvzCriu3uXsPylueC+lortCCnQvLzVvRkTROdiWhRONd6jsowA6lqU\nnkk1\r\n=lda2\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDbA3oistAsbdsN60GUsRBJZbXMRPEtX3A3cL6k2cp8UAIgD3bU2GmF2UPxPrc5IJja45TzqRtpchn8ly+eXUL0Wlo="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.3_1617184824529_0.3144249680523672"},"_hasShrinkwrap":false},"0.0.15-alpha.4":{"name":"import-remote","version":"0.0.15-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"d5b8ab7f6a8094a265c90e447305ab0aea004587","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-1k2khx+l/h5wx42TNVFgnq0y1swnilG4IHtDqPKPk/qvoP+XE+RToROl6iFtnStZNRn3TFsj7xq8bd0cGlzS6A==","shasum":"872eb4b9a1467d06d17b47fa496df69674bbbae0","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.4.tgz","fileCount":42,"unpackedSize":931010,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd+Z0CRA9TVsSAnZWagAABAsP/jYIEDXZOVeH4+VSzP4r\nTnwmduKFR3WYMqkdMFYWy4RhUcC+WmXns/9763x8gXUoOsL8D66ewGSGmNld\neAPVtOqTHXxCWzYPJ/FoMCPQxXAEsv9Lr3BEUjcmzzvpF7QRA3Ts9ecBkBMu\nk+ZM0J1IHuEKJVYOgNC6c8ZRqr1FN5v1vR19PZBFcuYijKJXcXrDkjyun25s\nXmMnWU6b2uP8NiTeliC4nyQICOR2KL7iTDlYv3inyyfBEaKNCcaxhcIn7L5+\n4tyuhDH2Wam0AVVzqIhN4wfVohBC+rWNy0V/QyDc6blDWGycCLbFsQNgts0p\nDrNADIoKbGisBsgJMu460RZNjrQEAm88VELRcS/TwOibXnlT2yGWaYflzqN/\ncWyOnrd9eXiUTrVZNZhJ8uQKM1EbUWo+UzT55mSejy0ksOUK9Z3DrDVicFdj\n8V7p6PWfUCcG7RSVRI+KjN4FZyptMTeFdY0Z9p1X9VrVJ4aK1JYhu5Lyh2cy\nNr5vPAq5G40msnxSRIrc+0vl5Xq2Woe+PyDjADYeBI89QPHlEY3ESWBatp8s\nlyClgpe9r77m1DLNQA6OLv1+41rwPC0IY/ZrDE5iwPym7nqAeANUvcmzNmQ0\nop58buo9LGztcbbUU3UXpfbbVMM10sYyyyY2r6ZMl2FvMIUPh1KhY+6nBuJE\nOOhM\r\n=hrZM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCLW41ijdUwEO0wdF/IKvsSpRlEIRywnW7uRSHqwE9lNAIhANtHfCMWBrYKkmXyriV4SqxpwwgnwYJ8abe3ZgbM8Hhf"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.4_1618470516360_0.7828370040406252"},"_hasShrinkwrap":false},"0.0.15-alpha.5":{"name":"import-remote","version":"0.0.15-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"d5b8ab7f6a8094a265c90e447305ab0aea004587","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-0Ta4y/Uuw/Go5MZLwAXEogUzqrLIVryKW/RhO/1UyYqfGm583iATVP4kATvVtUEewO29633cKzh1O8mFHavZTg==","shasum":"911cb7f04ae31da0ea1cde4d26027bf894900398","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.5.tgz","fileCount":42,"unpackedSize":930803,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgd+p1CRA9TVsSAnZWagAAq4AQAKObtqMcQXIiA8ujbuPf\nj3FJ+9AAmjm2yfZjQaI5HFC31ncwvivDUm5w8+o7lLYIOOd8mlHvvA8GF/f0\n7WK5GQNDZh5IJxBfFzAUHL7dMinhq7vhhWk5UeMAzJn5gcLUz/x0GU3HEKlX\nP2TInWkI4TmqcP5kMj3qsCI+sIRpBpGsQtNf6SuB5Jcaq3+/21RYMHO6xDQj\nGkv7OzsAOXzrcFgZgHreWj2qQ4oo/8R+jyeNmAZSk1SGMm2pHEZbGws69vt5\nBqvPBeDseU7tmayDN7wPmWmmasth9BHVVZ5HfY4TS8kmRizoT4jqax711d3k\nbOcEa8fSL2bOlgHKHVEOUavp1/LuHMJcn6kOmbnmJRVKOAFEPXeOWgzL+88H\n3kSWzL7WWqO6k8YTrCrqu62JUy6FF7x99xNvpycJ+uEgLKN0vnN1+IqEEZt9\nHfmZqMjhqB4YKE+1L8y8wdGJT5AhtLiKoCFgQxe9zl9TkK9AcCpPjjTWuaYG\naDS9iolop0NvQ/UyhTq5o9musy/FXdqSOrkmevCaugLw/zVqaEa5ePRhv1LN\noj+0KqnFYlE3qwB+6IYPmwoUEkM8mlIZnhqqvr8DxNun6vBtEQING/fWacFH\nqxmQljFxshpfPAUavzQRWPqhKmzmpdBFkKjbjTRAAOOkt/Mo0CK7VtbLfln2\nhBZo\r\n=7nTt\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDPv1HupkffiTnl66Z9UsJax9LmUZ4mmVLU6dx1YoRLZAIhALPGU+g7i6IMBlB0bVPzvplVUuAUn1pdCR/Ici+aISHP"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.5_1618471541027_0.054856617632319526"},"_hasShrinkwrap":false},"0.0.15-alpha.6":{"name":"import-remote","version":"0.0.15-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"036a1e97614f9dac0868b95a8953b99010c73bb2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-eoxo94mXkS5NH3osykYmKBZZJGqTU/WhSRFORuOZKPSHA3aCjQ9T/SDmUpcHFF51Q6+XxoaGnxhOa/zJZN37bQ==","shasum":"76c01ce7064980b928b1653593605507dd1319e8","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.6.tgz","fileCount":42,"unpackedSize":931968,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgeS5gCRA9TVsSAnZWagAADwYP/A+N+wYB4oks04Tkr3Oi\niSDGnGg+Z4Apt1TXREOtknjUj47IlqAIxebv7tjLwRJoC36PBkdgMHfhCXyW\naRS5Uib9e/l86UB27kVitCxdxKafwKTAEpq76jglU5/TaxzWlPEIltYuKC+E\nqEDWDkOVHAkxYI3zid+jU1b7sQr5wH16hDV2X9i6fw/FFENhYVVNkUrlX4Ij\nRsavz6FIBW5tLpd23VueSVBocEASakW5YWTgicK3GpK/3F5oodL8aHYgWSzb\npkGRTVDPJYSzZhDaBfsni8tSaDuBtAEI4rfATD1mzGBhuUMMnwJFZpMQqXqX\nPzwlEcryeE251KTAgee6dfGfA+TFwAkBq9jwPbKJqJOY0NLpgPlSLDlZYrvR\nW11TrRZ3WLsVikzNt1aH7vvl1Chs37qZ6LzQOVIL86EEk0RupoJt5hMMxDKu\nO0XmdszNF3s2J8aZsiWDJZjrCL52PtS3NbRkXy0HMjzbwcP4aQwoiQSW846/\nlVzfFsmn0uv5k48kjpXGFUw7gNjl9szKOKdM34cwqySU7r7KwzcAkXsWiOKX\nSDTJbhbNmg36/k63FjvLCwfh14aI0gehCWxMRd20Z7hiNRGLNaTTXyyDyU7B\nRPIorXgEa3iyYIXCjnd8pqlPOs3i7DIzvKqBMQpcAmAWyIvrhfIgl/jekpXl\n/9LH\r\n=wXXZ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBYSsx/kjE2846Pij9XWCsfK3FxKJxKg0eiBFtSUurhAAiBmK8wtTZiDlna4mGyBCCnu7qXL0YjmIbAh3MlKh12YIQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.6_1618554464245_0.47614127276656815"},"_hasShrinkwrap":false},"0.0.15-alpha.7":{"name":"import-remote","version":"0.0.15-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"8b73885b0a25ea4807bab89e264cafb214675fa4","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-SQ5wLxTBZW3anlSp1nZUPLSrvX4E/OTHTlyLf0AGSAyNZ5Dvahp5Y40BX/v9OQrssmUTXbEbUbPd1RNP4jIIjg==","shasum":"9edf626e7900a44aca7a0b13ad50cf580e93ae77","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.7.tgz","fileCount":42,"unpackedSize":928748,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJggOTfCRA9TVsSAnZWagAAz0gP/3uvrRiMusgQvUoo5etr\n+tOh5N/ndIrIsX0a/FMsLqzMXq4Du9WC2lSbSNtqHEAcDFKkQ9eWfXQyt+M+\n0kUZXmy+16mUZI0tUoNw1J6nVVKiUOwTEipVSFiX3xL9OEFE4fg0P7p0ZghO\nT6497kjg9K+XXYl9owTkPm56YwsmvgynXchBYpgSn9arNkCA94GnSzo+yrOo\nxOICEK8Wq3mffwdk3CNoy2rezrMfltjWtassJRbyqcbKRdbSKbnCwtKL//3D\n2NujIFd2ICDfMIJ+wz45j+BWvJeWMIKfDMHj0/aAo6gvISw1+6bA+nig01Tb\nnrasfrFvfxlQ9x7fqE7QpqZYtfRAyeMLvYfSlzkEt/kLXrRk0ouAX6ffLSej\nJUkSsiYBY+7tU7DyFNVmQ2nuUZAv1zsLpCCCzkRvpbHLcZP9dmzVbohPp0Pn\nIdHoYCH61WV/5uash7cHTRVvZAdaN8iOID7tDIQGoHk0ewyxMtOX0g/NLtfx\nvQSY72l/Wuotl/EGHQHOzWEbukQ2KcuAjov1Hui7Rzrpu22Sy7oYJ1VPME9V\na+PZn7AuYH6ZZ8eo8itViXEW6ox+d3zC4mwTt4Vrk+qdBvGJDnCRAM+m4B1F\nFKyOEeStclfCt92LrQt5cr/4e13LHnnri2x6R4PH5bv5SfWxwKWl0k2TBMeC\nwCXm\r\n=H45r\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF8Cb6wdQTJEImHeND0MZW0DcPBOGDe8K8+/qdmf9+mCAiABKjEDCfybwbtXoHUtCB+63OCcuBxa6yS6rlykrYhtIw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.7_1619059934554_0.46402745255711775"},"_hasShrinkwrap":false},"0.0.15-alpha.8":{"name":"import-remote","version":"0.0.15-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"8b73885b0a25ea4807bab89e264cafb214675fa4","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-h7nEBPatdblFXKncVqVjpaErKmL76BkvaFvAb0PjEeB/xx7hdABNUwwqxH5QECkNhZr+ZrNjIHRLtpaKiSjLQw==","shasum":"f75ea0df8d28dd7cec11157f3fe75a928fe80e10","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15-alpha.8.tgz","fileCount":42,"unpackedSize":932141,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJggQ5JCRA9TVsSAnZWagAAm2QP/1rNvBPpogOXSf85qiAc\nFiokQZnP70OwralhhzXWBq3gInBpdCIERp88+CFy5E6kKFIpgq9sqWR9aLeU\nXnyyA6/cpvOVIlC4dauaFtU7vlMWENVTt3H466kpYrHLpf0ukEowtka//WJn\ncDGlmN/+pHGK7ylm65KKN0sV3cw/N/e6qW2SpzUQhhP1nuNX2DVy73MrLW1Y\n5fw6vU/0yy2S4FMux+PC5KiBfJTJIGuH3ILmKH5W53ty6qh/PDuAF+WLnHrp\n1j1dCIJzGbWTIUxY8TsEEAKg2JUOkoXst6HP7Ta68S2QipTxTfmuaQqTnqAa\nEvAQCFZMey4rrPZtB0KGNcDvEJkBLlDbMJrveRaaUE1q0+O0sOlfOCwh05jv\n9oOYaQGw/77l3ulMOsuGEmNIxpVdv1BXchQYsZcfrsmocgnx7mGG2bQWeOf6\nmNsIJ7LlfiDGFA3vcFXGfMIWmt7pBxYNfVHPm8aRnEItPUMiP6ebo7GG/jBJ\nSPwhyPi9uE11rWv3UpHRjTBwchZVpzXa2iPJXpewSXgyJz15utbOJjoPgcVo\nZNf8W4GgfTjtagOpylBbjjZJFEcDYcmUlzqw8gngSKyeV8sLFXF5unOUMZ0t\nFuG3mU99v5dYTMipJpWi2yXSBbEGtN93fqrxVFF/Fq1UgI1ymUl62qvmhWxK\n/qbC\r\n=l5Ro\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBE26smfL3bx6m1t5nOHEm3U1mySv/uwS2LoKG0ctwB7AiEAkWx0UDW1sKrtyr16T3bCtpBf5q4LNQaLIAd89NcXHsE="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15-alpha.8_1619070536461_0.942485002381527"},"_hasShrinkwrap":false},"0.0.15":{"name":"import-remote","version":"0.0.15","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"170437beeec044a4f2cb2bb8286ffae885e28250","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.15","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-0ViMLvJDMbD7Mno7zotWAAHJSlrvbvOF2LhbPxwuTH6xGSCsK6/BGW5a7b0qYRBOHaqXKcNZVXwOvX5bUSRusg==","shasum":"bd945ffceef8209e01c39eca7af11096cb431a87","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.15.tgz","fileCount":42,"unpackedSize":933165,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJghMjxCRA9TVsSAnZWagAA8JoP/0rJq5ruDtUVPFlm6ZsZ\nzu0xpHZcxJlt14uYFbyAKv2wiih5kzqEOWV5hsP5zbEhpeg1VTMKN8YltT/G\n/z5BvJ904/b+0Z9JQsSuRz+WLXuCoNftaMgpXx4HV9B5fCCET/FZsgGYG5kj\nskaJfU+TNhr8wIwNn0pqQJHVN2XAEW/pc9fdWKNET4B4A/VOUKBk8oaJH+ow\nBfbrJXkt7isNWCb/yZV1+InEj16Pise2niB/MkCKqAxmLXm8Hp4iUO9y2LF/\nRPbfGihxYkW+F3nXDkeoq6dpRUsjc92l9fYwAoHqoTUihVqwUzMg6AwpccAs\nU2/6PXSl4DOBI1DfF/edO8bhxD5e0zITMD5YjUMN2b65AW6w+5l5IdbepVNw\nE0S1Dqnn7bv8asa7gB8nQ5OWtjWU5+rZsdtBUkAf4a+KXl61flASmVONocEQ\n2deCPYZ7LU17rP1Do2JuyoFYc/WC94K0bILvMsA5sLfhcY0FPBWdVoPQ0P1K\nZsSRAT1Dzgkbr7BiNaHjrn8fswGJLNLQoxAyK1Rhu1Rl7M9b1M+/kSM/9SH5\nJUILWX9pjST2dRjxcDiUNivkTM9mXd5tSe1TQCYwxj2dqZ0Eya4ySdC59uGM\nTxMNkuYZVJd0CaFL+KdnmT6sx6YcnpAXgsHLWmxfM1XdW44/FJM1NraS/Mem\nt69E\r\n=R3LU\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEDPJQPN9A+RHCmThTFNdA6okNzKnd9B0TNVKZ1/OZXAAiEAty0v6mDsPCQsy5mDlK8ke1GE2K0Chh1KoBxYsPhGGzk="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.15_1619314928500_0.08317656966447928"},"_hasShrinkwrap":false},"0.0.16-alpha.0":{"name":"import-remote","version":"0.0.16-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"55384056f7c2128e5eb116f3eb8627c6c7e6fa6e","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.16-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-vRNJ78SCXBrBWNcVrSimBxQGNHhsG59AtGRBm6mMeEyts2E2LYwAKDs6OzceYGe7R71b0UC9VaYZBLDZy9FzYw==","shasum":"df3ca529b07e7bc371fd729d52a6994fdb72a07c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.16-alpha.0.tgz","fileCount":42,"unpackedSize":933066,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgpQpJCRA9TVsSAnZWagAAYvsQAIFhBwMsuC2wyfedDwXm\nbbsQ0SUPpP4fSkJ5KBp/wsXtwiZAAeLm2nDu3w4cikrv0eTjf+c5CH0tlbVx\ncY07KAWU/AOUt/Q1sWXlOE18Frn4VyKsbaCLofEeYRudfpeHIWkq6VaDD4PA\nLIyhhjFIx4EjWBqwiDxuyBu2XleI804nrjM5XvqG1fkrlj/o2HJ2c9cXpjq9\nM7cd8SKH3fpuIUXFOFEkzO1PhX2VPrlYVrsi0BaX4pCTmb6A7AoJtgVq3t0J\n0iUyHcbGYk2qCldBKufDRnX9wXLkzGF+BDjJDRBxfgt6ouUNv6NxQUi5t4Pg\nxm4bjODdhvBACMicL8ZBeaQvkOGjpdy6WnMF9J/HJilNxul4xEmjb139fnm2\n8Baev5PkrwivozYVdZzuogecxnLN4EkV8vAkJYp1L8jWEkNythgag/2CWWgC\nw2phpQnNsK3LvVLibyj8Jb3J3ixZET4keq1V6VsIx2dIanjPcBI1yoWh+e3Y\nsV+YlomSqKPSyNWwSEovryAp2TVReyi1yxaijoycenTBla5Tv+uYq/VN5Egm\nS0U9ioU6eK7WjRJKHln2mPSmU9ciO/QvgKzoFR9gAv9RZPj0Hck00Mu9CVqa\nqdZwpUmwTT6qsnj1V3CWVdYjV7Q+dp612igojfRySOvUAzof4wgV+UMJIae7\nDyvG\r\n=NWxi\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDz0D/qGyXTGh3aEauZFk/Hi4s2l6mK4uSkw7RnjMpPogIhAL1JmL38fMXFfKsEh6V0EOpVlat/5vwCQ2FXKjaKOAEW"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.16-alpha.0_1621428809071_0.12212714258148383"},"_hasShrinkwrap":false},"0.0.16-alpha.1":{"name":"import-remote","version":"0.0.16-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.19","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"55384056f7c2128e5eb116f3eb8627c6c7e6fa6e","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.16-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-fSGOdCq9sxQJnvjpnMzhjAHhNIEl4cI2mMgJNnvdj2A97ZRgVdFHRYI+xVFubLhtXK52dDGbrQau16hmn/CJ+Q==","shasum":"6200181dfd089c168750d98dd9bd0f91e878aad7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.16-alpha.1.tgz","fileCount":42,"unpackedSize":933897,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgpbbaCRA9TVsSAnZWagAACZEQAJSfq7nf8rVe0sQJQBfB\nHUmZheFwiplEf6x/XnCqO61yRJ66I530EqxNdU/2FxcMZyua+s2/9YhF5cCY\nw8NOoAS33dlfN6umFyIK1QyMSZE+YCetJY0YEKQe1eeADySFU0hnPHq/BNwm\n+pMtj9XhOvMIEfzBxaMzwH7bI84+WhZc3qs2AcPgPDHjExtWZDnI693kdmsh\nGZpJwDFyLb/Up54BgYpybisNqKPPxxaBfdjaXqCiZ2ZGeQp2mS62BOEX0mzv\n77AsaQfHIce/CxKOZjv1Eu79rCul1isuSuetBsqPMcR+atQdJl6f2s0xXwuN\nwbuBf2h7/Mm7P/XnF5J/d0nyZLcwiMkYXTMHDKoPyONj6Owqf19X7u07ahXH\n0lC1HRXy1FbQgBnwfWuOp1Hzb25st/IzcJ01VT5yUv/7NFEVB152j8ku7u7X\nu38Osd6/SXi/PPHAgqtmlbO9Cwi/4KmyPJ1g6bvXjJeMxe2akvZwqnd1H/XH\nVX3F8khLrPkL8Ucuf9/AW7+uSjSQciYXjGjn0hN9EJF7K1J7GbNumzc9PNe9\nuIz0SgmMYuBAryTM7SugumVdSfIs4W+Vb62gSIofKeEZcRJRtzmJ5CdRqqvG\nN/LPlaPEnKVGdgXCts7xrP1zPXAfduoDzG6GizjnNd2VHricOAJj5DhvFxmE\nmLSM\r\n=ZziO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEZLGO2cQEAD8Rb7AZfGQVbKI/cVHP8qMTrH1frH395vAiBSduWJ7BeT1p7BGAbXL+eNjyh+5+Yz+Tisdl0U7O0nNQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.16-alpha.1_1621472986403_0.044202573712535775"},"_hasShrinkwrap":false},"0.0.16":{"name":"import-remote","version":"0.0.16","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"gitHead":"445105bf4b500e5737b6ee662d1e443a2458d64a","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.16","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-GVALnUSIUBaBjtrbE06jvFWECe4Xng3aLCyPUtT5C9JEkIPfDmecWDnZ6Qty31Wao4RTTxDRsoj7TTGS1UHLVw==","shasum":"a0db1c24f41f14fbb551f0d4bce410a8ceeac4a5","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.16.tgz","fileCount":42,"unpackedSize":933875,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgrKwFCRA9TVsSAnZWagAA7XIP/1HmUMjXgXHbTvl4rLLD\nU6ilMGhnazhmrgUo3SCbAusTkuFqesaFUGVsEQ3+bA/9pPvYGMVnDbDlLtUF\nfgxqrma/A1ucmkPiloJ8VwdqpxUjWaU4Tb5OfOhOEUfNeAShtdFp3oahUVMm\nq/UM6AeNGstCFjBQgLRCmoAavQ8wU4YKBHgKWVDz57vBa0caiLZkV9pxcimm\nykl+WtSxTsQNoy/4z5z6VoM0z366JPFK6nwF3UsITxiukTj+uyyfxtwNRFKb\nEfLoNje02Eo5sTy0mETwgp42OHmY3MD2JcrkaVNI6Qb52l5c4GQxPCeq8ExH\nvCraceoOefHWjDQv1f92Q6VK3Nz2jFQw+50V0E3B2FGXUv5X4iziEgTa5s6Q\n5A5cPJQSSQOiNj1fT+f0YfPaXHiRdyDggfMkuTSm+lwMc4QBxn0PMb+90tMC\nMnNs89UqW0qN+F5qGpIzsjM42wS6jOpy5lJs5UJ4OIG3rzL9QUkC+WjBFkU6\ndBtdy2Mrrd420P/gtdQ39z5OPGbi6FV44w10T7Rf/GlEePbcrTKQkKSJiOvY\n2Gjj9ogbEwxAmJLaRM/WHIId6dxZqDCDM+ttxwStsO59rgJDLc7Zm1QM59B+\n497s9dx8GJH3yEtwrTEnr2r+rNCZJ2BCxl+EmFLXIeXVNfhOhNgZb0iYEihc\n8gms\r\n=4uu8\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIG2NTx5l5dtIp6m0Z89tEWQ1kWMM9NX6bsnZZTViNz0OAiEA4Ln7h67fv/5oOJZ3+cGC2LD95eSTB7FSmbwHLdMtusQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.16_1621928964892_0.8697461142340306"},"_hasShrinkwrap":false},"0.0.17-alpha.0":{"name":"import-remote","version":"0.0.17-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"905a335af39009e69e2939db1fdec60018fa01ba","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-H7WN93949HWcZXiNr5Yg7dioOM2b0JK01QuK7ThjYKeVbk37dveHzooORKot+qbwypiwhpgRVCsfWV76eD5+uA==","shasum":"d914e89ce74d61ae1c2dbfefceabb0f201174a1f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.0.tgz","fileCount":42,"unpackedSize":937208,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgub7XCRA9TVsSAnZWagAAZK4P/2k7V73a90b8z5LnRFkE\n7X4+Ap8yQudBfqd94hMj3V//K6v5YlvK8Urf65X3hDZ4rzw0r0Re11C/TI1c\nidlWzJSH+djUXyGlJ1j5WeeNZULGa1ZuDHkAjLDkn1xOW8TncsOoq30jdOmP\nsG5JQcMPiItb6RUJIY56zujkUodmdTitq48tHNUH7MsprPGLZkiuG8CGcli8\n0VwObm9T9kEb3P482aQYhxqiAc8iN23kldip0g8MWUdhPffYo+0XsjeI5qVG\nuy2EegyHCBieBVvGBIGqEs4miuBSdkFm0XWmyhdI7SlXKX/Pl1VEgX9PNiNf\nSTmM8LZztmfwz/9R/UnPjMoHd+Rx2U8uan2SuxLlBnPUqVxqKkSPZRdDyqym\nIIsBT/bsYxDNgjezBIcGtdNpTumm8AzqUGXU0Glyb+hwxVjOm+BRnB+llfjL\nG9rDSMPf+hRoaxItoSFxy4ymwzsqBgDusWHEuWxOr83/tUGrqqFCYMbVEmRe\n47MA3bGERFDbqwnt0YnlOKNyb1n1K2PFSNlU2xVSbIHoVaPKOINsMLfvTJFN\nVDJ5nGBgUMaeBq0fV8pH+e26eghJwEH8krwdlHXS4aiwDR0gpuKUPahqQoqP\nUlf3JBLC3fYqaewla4aWJXF5u69e/hXKjZBce24/XCtb58SAf2AW/if7J2sJ\nZl1s\r\n=8UM5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAz+nltWwaiTIEKLjVToB0KZitBLFkQUdhCQ62U0uy5VAiBfIwM+5/FILYPEktY77ryvmuP8MH+U+Y4Ik/l7F4dXaQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.0_1622785751081_0.6287433700422176"},"_hasShrinkwrap":false},"0.0.17-alpha.1":{"name":"import-remote","version":"0.0.17-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"b462a94fb4992c8848d5808acb0c1406f6d443f2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-k/aKd6+WJUWaYa2aXOGDc+oIG732sgySSX37kVTEdM5NYxM0QXBSibxeZx2gybk8PKGvXzHkJb+PFTZGdvRqdA==","shasum":"cd6b249d79dc3ab0544ae7e2437024b72ce12f44","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.1.tgz","fileCount":42,"unpackedSize":937209,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgucQACRA9TVsSAnZWagAA06oP/iwR3yf+u16ACIdbrDoQ\nWAu/Km0n1Mhv+2avRXhWBytVHTl2E3BiqPIDJYgHeN/CoEwfosZ0DIuOjz0b\naOF2SWtt9t0gptzRebcH6jwPdGbK+NRIMxJeD4P1gIak+ihUjOTOVCN8yKQU\nfhxTeZnCLOlCQn5X3t5cn9q1QY0ymR3GXmn8ABxoSCdUQqAVxkVvWYmJlOwr\naUk/rZsRIKNGS5bxeaNqUwp3ei4uwx7MI/2D7BemlCYm+QxUrm+72HXFlox4\nqvTkL8BBfJRAg7MEsZ79+YlSSnbcMyHsOAM0/mJYxZbQpMNGtU6g+WNQAN57\npo6vL8OsszA36J3v2Ma/SM9GEYTNHdbjU+OZrOn0Kr4hsjxbmuaotimnyqTG\nx8WFe8PpyDUn8DWsku8hp5KlfPAunNgB9SUE5zg1P1DE5QnMM7Yi7VPPRNla\n+gWJDj85z/ApsGg3b9h0ZX5t+rHN4IxBTnxCRw6ANNOAATQiVyaQkZOPP6Xg\nBwO59t7e3fdEWvP3PD199pki6M8qY2tw50hRrFeyKQSyD5r/vPWJGP6LgIoK\nSoCJmuDT/s6N4pbP3HE4LRzvLJYZcxgSuODXRFPRbG8IbcTVVk/caimBnmsU\ng91jGi93MiLdrbVf+q+I/LT30VFZt67BQ8p/U2GWA4FS0rE7Hr5W9/qognWp\ndmzx\r\n=9vJY\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBZKyyGZFUEhq5bESI1lBjQgsm0clnytrV5VULcoQj61AiEA3UvyZclCMQKBLDm+I/FCj8FGfCRQBbqSqYF32p0ivwY="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.1_1622787072425_0.7271443746059891"},"_hasShrinkwrap":false},"0.0.17-alpha.2":{"name":"import-remote","version":"0.0.17-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"c9ec208cff4242437be2f49cfaae666bf9223625","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-JvgN9i5HIFLKCGSv7P6/JuERcaa/xHRTA4G8S/KH73fk96EqCsZnUoqdXyMhaUIBYswM1S9RV2jOdudahUyc7A==","shasum":"d927cd9a3b43df410a99771eaa22f4a291aa268f","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.2.tgz","fileCount":42,"unpackedSize":937703,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJguczHCRA9TVsSAnZWagAA0GUQAIVjB1tzbYMPB9I6f9La\nPh5pA+Wiw7aEIaBdjbQcKr9uC8TC0oBSbsrNoVDarboYhX6Iey7rY6Wg1Lky\nafF67m+PJyRS0kHl06SKcXygVGcnGyER+tFIrTF2YnezVS34MIHUklg2tH6v\n1Cpe2URKDiC2x9LIEgHe6+ugQRuIX7PETeedFgeH33gjH0Loz/nsLDETnHI2\n73/cZGbUMigIX0Spsr1S4j0Rdgz/Es/V2IiRaUQ9rkmQlx7Sxkmndf5F3FXA\nJ+dxmeG+x6VL7ucMc4hmyYtx2tn7bYTOfxvjbrJMfE/pNCnfvyIEZTlUeijJ\n7EV0LTOIKsv9RvPVvooGS3gz/5HZ+LgoZwdA0oW8RKRW3gYmnMKAKGzi3KTu\n48L/RjRTo9cQQP4mM4EDgSccmEPcqrzjBjPhPEqnSHurfbmmAcSZq1rRwter\nnja/PKi8vY29Kto1NdYeZJ711wJJ9vd2vUG3MqAg0MHYe+MnWzMcz+H43pxh\nk3E4cx5kTf1BQp/asrWFTYUVwW/ufR1Yg1cTKNJFphnB9gYmI4/FXRoR17+Q\nncCZXKn4VEQSooOe78zkAfc8BsTBCBHNPLJW+iTrW9h363x1b7131mxen+mz\nkgHs6fRPYJFGK9ZfW2tGwIfW9Ux/Jj8iL7QbqWSaW6H7QDf0Z2bgwhqDIgk0\nYdQF\r\n=Zoj4\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD6JybgAmdFssFoCgeiIFOl1OHWMnzpDFCJqlDQkzk/XwIgatZf8sryoav432UQamSaGm5l2j0LBlkRyzKZdZE8juI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.2_1622789319673_0.19037490822216552"},"_hasShrinkwrap":false},"0.0.17-alpha.4":{"name":"import-remote","version":"0.0.17-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"c8218598d11eaebb4457b462d337aa734bebdcbe","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-QqixpkKPw4domU2f2g1Gc8n8rzoqag15DI+hst8I32Uy+RDFn38Pxgi83DfMp8j+AaMttJmrq169fPQSvZJ2Nw==","shasum":"bbefd9bc211a549584441dd5c2f85b999e9454ca","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.4.tgz","fileCount":42,"unpackedSize":937574,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgudTSCRA9TVsSAnZWagAAthoP/jad8uVN5FLxGfx10GOk\nOU/LMt10AytW/avRhiOw1c8XdwD9eVs94L8jvEaU7BlxfpTSGnv0jI3O9i/u\nrkWaW0zHNjEhWxEdEvg5TKGGlU4O2P2Su4zVgU0aMSJJI739fnW0D1cStCjX\nc8VqwESHxCzvqFmXnpG4v4ozsa5M+MGaXItD1mHOvNVcNDD+lOmQMFBWvNUg\nogkbhCj1+Tzx+HEFrHoxbqUrJLz6ZkK+bcvjG6w+wu8As1aVdXCZbfle7Lj+\nwOHEtunylH2erEeEUfu4yhEwZQ4ZU2DlJ8KmrAJVl1MQuATlWTht7IS7tss4\nphQId++wN1XbUOwB4dJDsng50q+U0SP64zWh7rUyLwlSOe6pFT4+zqdp+FI2\nN1Aq/MI28ut4lAx1bdMceCSxD5KIw9OWPY9U4BMZiOPrCXFMwirrifG3Vkha\nsMuirttW7U/wrKDsGCJd7W5W0R2L32ULWGxrEkOmgzV4vgVmnl/lxVgcL7AT\nE9EGsRpoSIjNpiXJFZXHx2bbgnRlntz2L5227q4dV4Gz3J5xYqTn245TKLQL\nOU5zvdP0aO9x/ge31T5rzq7xoG5SVWJk07AsmpqwWopoBZQKTyB3dTOBfAQE\nzhAEl7jj2fAZ1znidtJOoP1G+A0YjLAt8oT0QRRCQh2HrxHatqEdjU2MipVL\n2oSv\r\n=hoBP\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDjSxeJWr27s1vZXFYTvYXygdbeIrQWZezx59aBtCjqxgIhAJyiotLeznPiPwl/KGKMAkjSvNq45hvqZXz9zv+D7SRX"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.4_1622791378335_0.2513884160039901"},"_hasShrinkwrap":false},"0.0.17-alpha.5":{"name":"import-remote","version":"0.0.17-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/cli":"^7.2.3","@babel/core":"^7.11.4","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"ea7177ec0e59ae60c8a4c70d8e33d9e5f76b4052","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-b3c7SrwR1A6D5lPc5DBSZ5PtIKSx0fOJVkiNbZPgg2dPqfjyu4RpDnkrtYJUjI21mEORLF6ZFaMNXQAKynUwZg==","shasum":"5d2de8bb646cb0e9883a98232b0e5b49937a6bfa","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.5.tgz","fileCount":42,"unpackedSize":941996,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgvwWECRA9TVsSAnZWagAAWsgP/jtzHSQmzR+8TXJypa5u\nbHhQXjJnoQx6xx1WtohD4hDCzIAmr579AVilsUo6YKG2yAB6Ha6AMKEv1WVJ\nH5l6PRyjEHn/67a+M+HtRLcfLwHEiT8yMw9Hw6qpJpUQnJYBa2yU/hT0GcpO\ngfydrkSKKNN9k+jyp6z0cIeyfzdGm6Fyc3h7wqs4GBUaXmJ80X2qYv3vc12A\nMrCdMo8TlyH1Zidf/+jEgA0OIcPq1q9DHGIH4Am3bGPel3fwkIZPR9Lry7PY\ndYk0ZwEpvmj9IeCFUn/MQLlxgOC7rLrXXENPOJnERYmu2tFHZX2wd8PwtEvK\n9WyLtCx8lMl4YQxeZxA5V1YYhzbGEHO2eDuUAf/n47dRMVJhg7RXR8xFAY/1\nXQ9YIEPXHKoELAZrnRX/6lGOKzXIk30vfAxWN/0qd8NuJWMcG0OF0dMB+4yr\n2Y3LBKEFr3DYWew+3WMU9dryPk7bDc3fmiEUHR56vy3Y8p87Gt6Hf5wsNLyE\nnAe3MP6oBKYRB0jjCiF1zbj2VQO/9yZ10X7ljt7D0sE9nHtZpeBtNzf5sTbG\npNpTaXw5SnYOyD+iwcENLGy2wcshptk2lYJrFNWpTHzfjBAnHDnlojAVnEfz\nNppT9FbS5TDwE/ELli6TV7GC7T875+kGX/bDO8IL7k3j2wokbMqj81xdN8+E\nDK3B\r\n=29DF\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDu6nF48jNP7Dorqb1FijG6/Tbd3MjKroBOdUDOotXfxAIgeTC/KZRF+qV/Hck02WwGXMU9p5cr7dbNwxscWp0wfTk="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.5_1623131524451_0.6341323492654762"},"_hasShrinkwrap":false},"0.0.17-alpha.6":{"name":"import-remote","version":"0.0.17-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"edb362e7bd34c046c3a3e2dc2607a4c9cabafe6c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-dHmk7nvmxnofGizze7bgCOB7FbM69GRCB6j3M4caXXSynsAAQIg2Vu4gH6dcGYbtARU0fdBnJLOGR+Hs8bhq9g==","shasum":"8d90267f711189832060422c9f31ca1d6e54ff40","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.6.tgz","fileCount":42,"unpackedSize":943199,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg7jmCCRA9TVsSAnZWagAA7CsQAIMazGUZ3cSYU9N3Df5M\nNGqXrVJVRK9Zlv8jrWY04OEq8tUcEsafLVFATm3iNiz0qJS3yz/vjkz1btH/\n9GTk4DMe8iMD2Mq+rXMpcCndcjtYQ2OaeWkg0MXFOQaAH1Me+I3kuqu2dlmS\n1RsGQiHEdjWBmz0iVfrzKeIOgqz1YELae8egk7FuCnOKA+IO3YkcfsTT837w\nyHsV8zHR9OHe7fWK3YXoJINCIST2oxDUHy1ExALDL2W65PDmEvXy/MWpZ197\n55rUrZ5CtGVTl7e+hx281TAFdaQsajNStPVXRIQaauHIHT6OW53aNHFXGM+L\nPvhua7EK5LDh9AfjJIu/f8VL1mJW6RQ+MwfzVGc8AMLl+mQhalpAmJ3EpGyn\nDNCYRI+c4x/yeQaTJMRuBRfphd9YH2ptK2Gv0qu6pcgnexQhUvhC/9T604bO\nmNr4nfUt5li8QxgMpZWmG5aPVR7VdAsGiqqPXpFKv82laF0tuopgyYlRThjU\neFI/TiVZeAykEVJXGK5TFdWMYZh1OESS//1TV4FuxGvoS7tVSbJgku+yC5u+\nkRP+qwU1C4AbDtYopaiwVZ8ap0bPay0GM+OF/KPmnK2dAoVoFdpsY5BCyd9p\nEKqZjJGo8PRqabDH9XLIoTvVEISky44xufNQrWQhxYCytPuGPmC4OTtsBx/W\nSJsd\r\n=a9nK\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCkyOwyEa6OiwIiWWqvpGjN6HAdqbhocizFEIiSe4DHwwIhAM6HG+odXB+fehjWHkLusKQ2afHqzpKVQzBcPtLczcHq"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.6_1626225026196_0.05518548217318031"},"_hasShrinkwrap":false},"0.0.17-alpha.7":{"name":"import-remote","version":"0.0.17-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"b0013ec6aa67b926e66cc5cc292581f75f2ebb3a","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-gijfVLpDFSG36cH8m0ugozGXqSy1eC0hwLm7RRm7dsII8DOT8vZTXilTXXIubtvrxQi7A0wLLE5/yGt31mXmpw==","shasum":"c749c2655a06b0e8b5ca8cd4164c6679aec11f68","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.7.tgz","fileCount":42,"unpackedSize":943199,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg7n4aCRA9TVsSAnZWagAAR70P/iR+Dw+IJiMpae3RUFG1\nZYssdOT2qrShHr1YpPsyCmCg8NX/ceogzS7Ya60lFcCY5Aw4Kl2DuW7KJSdD\n+3fK2TkpgHmPjgwyQ3X+kFfpz0ZYZEHjlvyxwBuHNTe8V0GwLDardt4dkzPZ\nm2ibrwwHYgMafY79rRh/bB5cmg48HAhWiFIHdEzpFwADG1kOBzZxiDP2QDH1\nTdboxs+zcxoigujRbxFdVIy9LUdObovSTcayGLA8ERGHutfkGQfeEBCnyCqd\nWYTh5xdF/12iVhQXXKrENMWf+45b29rA3vS+94WT2VkPUmRxh+4MR+F7fcSE\ndZAB2qi7qStdUoyFxkdGoXQcVWPuBQdbYN1U1XMIRutbN581OrvLDdgcSn9D\naRCVhTMkmI5zolNBCVGUNFWUJVnkABHZonuYxfIvrMc82apkEgwnv1PWEi4F\nLvGWpe8GmOvPIIJm3e/8JfZRdVfTHwzmbb+kpuY7wDGoVmSX/P7b0+OwO8GZ\n+WbM6MQwMIVPAE6+aoo/1FYfNi3/1qebiXl0N0sr3wKNILmnMqCzqa1nZPOv\npLTbzYyjeDJwUgfwUR9U7vFhgRgaEaQvKiZkShaK5nOkQfuhf2xX86gEnUtS\nfbFOarg3KUQ2J0ba6FT3/qyvUThQjArkyGnmdGrNVfjXoW6PFXw8Kc+VsuHW\nS6rh\r\n=D8C6\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC984DqyrCnQelQC+EdWM5gXcKqkE3O80wC7ikNdN86VwIhAJKTPYevahKodod8QGeKAA+zOq1ywXAZLTiJvCx1nYnn"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.7_1626242585418_0.9059850352088663"},"_hasShrinkwrap":false},"0.0.17-alpha.8":{"name":"import-remote","version":"0.0.17-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"143a2c0804b5ac6daf82832c8d7c14a46ee8883a","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-7QKedZZhwgZx9GgEG0kqTqXE4RssEziZDe3RFb8zNcF8lUZg0pKNdMiHiplfoH2kGIaHJiC/2XpH3cpymeAtKg==","shasum":"17aa907cbdc70ad1095bf63fe8ccee6073f4c330","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17-alpha.8.tgz","fileCount":42,"unpackedSize":943608,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg76HuCRA9TVsSAnZWagAACRYP/RHlmoo/BmFQC3Irlmcn\nKa26iIokoqsZfktNu2QxHL068HZbgE78EidNfCk8pYfgSxWb2gP+uusi8wfl\nwi3N7HoUyBYm0bJOhA6/FY6q0yPv/3WLm2D/a86P9SM2/V1uFKpDKGI/D8/Y\nPSHDn9AtisqdKmiPuNx43GL2m1X7Qzub9+1TWdHVMSq6i2wnOIsq8IXzMjpY\nZxrcfWbcGt0JSbUp19uygKyCd7W+jq4BHEMldlOLRMwAknKtn9SKJYN2xJoz\nkVU9OqWd/tSMpDZr4nBMbDI4Lx1oQgtvQq2LJIeM2gcf8kO05J+gEe9mXNZh\n3nHLIlsenBNjnVBAAEF3ZPHwNyj1VPQkWU77OnUgG7x/JNNWASrMvJfHi//K\nXbRi36ZpgjaPry7yNJuEZO+HaGIlKXytoD9bilJYLBWQ8ZZAQnOd/bGYa/AE\n+IWM79OVYMKe6iHsh2MI4o94P7YcTDTZRTktFas87GgDrxWIiadRJBkitWrK\nJwTHraiAWYGnzM1ovZAb+g2fr2w9+kfcXgjBGhNa1eEnc99J38sRL5p6Blv/\nqVZ1dZwcPgnicBSFzgA4nK2BsGFWklDoAKBGX0iL2rFXQMylPJUyiMMSYs3O\nVUdE6pNZWfXdQdxwnIsv4CWEQ1lydA0QIKgHNiaXFCnx0EsDtC2dC2ps+o+9\nvsvl\r\n=rj2r\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDzVhF3EezkapWSsHwuHvMfwkHyJA3XbxPpgy1M3vcN6AiBa7kL8KN6GchKjXAeq8rB91O4Q5Tz/b80cHmV6y+UTsw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17-alpha.8_1626317293576_0.6538474571614856"},"_hasShrinkwrap":false},"0.0.17":{"name":"import-remote","version":"0.0.17","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"gitHead":"5d23d1c565bfc4c267e1f40eb225ed3ca85bb102","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.17","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-IJ1SAgeWRGEZy8rXiqzN4aAiCUejCtlSFgepr+6MyYGT+drRUvCbBDdfzKc8OVBOAy1hVt5y0l9NMJXax46VDQ==","shasum":"0c8ecdbf422ec9317be562105053a817c8bdf4a1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.17.tgz","fileCount":42,"unpackedSize":943575,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg8NleCRA9TVsSAnZWagAAQLoP/iVoYWifanVNPASp3xzX\n6PyMIouWR+MSZXqqOvxZRU/R33anSBeAbcI+aySSUrNMwS0xhqlUHb8JUj//\nfLTe+5PqV8Fhqu1M263Cvd8rATAsQ4794d5+Z6PDhLiGbZ5LMqHjev/mqElQ\nEfF/hqSP0Piv5mVlGT/LpgakKIse8N2cBGdfIPvqcsCK0j+wuc0D+/ziACYP\nxR/xRnbRXl45uB4MpV6s89c+vjF70nrAG8SsuzlgEY0xPPoSCKL7LiM/Nvk6\naZxIXTbTqIlIr02lWiZI9atKaSIru/eK7M2C91LfjPugPHTt/XIf9IIeGIxe\nie9fBuC/8iSnyWrmJSnoeu0DYEUUxL2rBqpE7fAAgV9XKJawW4ntIWQHF6Ie\nVYC9GcmL4nbKfAs6b4yairh1fa1GdzCJ3OY0XBSifr06mCzYMwY/+XFD8x70\nSgScrqZMikWcQyTQhDxE8N67QkunJf6AY/eUJNZPGBIJ6b7Ui87gqpmcdcnI\nD51tEjvuOJpzl5ylWhh3SdhYBv6Y9mnNrbPw0JW5/O5iBcRHLqIfW79/Kpj4\nOTNm6dUho06SJekXjmF0A+pL6H1TwOsYMV9Dtzj/Bc4w7KNvr7BvNmo4cX9C\nSRkOB1WfFHsH0FHl9uLUwVJM2FSMmSmZV+G7XRmBVp7fUBsfnKwU/XR4axa4\nbNwo\r\n=rM2L\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCjVvuC5gUXGlh+27q1l1nAhsOUGQ49sArcnxULwD1wsgIhAN97+e4wSzunbah6BSglK+KLYqpC9JIe9e/8oUEOr4F+"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.17_1626397021814_0.4813635271106529"},"_hasShrinkwrap":false},"0.0.18-alpha.0":{"name":"import-remote","version":"0.0.18-alpha.0","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"6124086f23c22e6bd20f2077e59a3ba78c505990","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.0","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-4J2hVy4Q0czvn37cfqIphYjt2jC4tVyvpJyn9KqcVNeltlOK0oOmPyGr7I3o67w4JmnIJSFHxHYD0w/SqxWdww==","shasum":"709e2fb4273181730e7b33cbe39957ed1042e6c7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.0.tgz","fileCount":42,"unpackedSize":944211,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhJMp1CRA9TVsSAnZWagAAslkP/0UVPMQPf5sm0rdkNby9\n4TW3ySH5V6+RLx/0mjoDRiO6wO3riAhuOrrV7VeBft7klg5qt1Pq7VuAd5NS\nRP5Th1sjHb5/EKMaDKGQMPBheXiTpemxegUP+Okf/Uh9wld6emb/NS205YLZ\nwQtbC6d78nUJc/44CJBM56T+x5+83Uv7SO6Jh8UISgBOO03UNbwXKUpXrFBv\npviVutmA7tJ8JIRSlXcXlytqqIn1lZTtm1+DTIEHQ3utlw/GJqmQVbaVoPhM\nOWNjd3UT24Fg96Rx+aN/y+R4KbPYpiEFBloR64NI0lKvneNtcZ2avu1NP8vd\nf/tc9rxvYts2/td857oNmkeW0p9NsR/tMOJUiljj4ELio0U1hMnZPuvQtDCg\nfsbcNfeH1muxA4HNRvzhY2RfB++zr31e9k/7z/GZoq+PaoaYhy49bSZ2UZTn\nMIcSBQWvzz53kuwnn1bgGB0em68+ydrNKZgZlfB7nMXkQO+s45moScn6v/YR\n0JQsWW7E+fdq86/zmrs4bKYTZ4xJD6ZprZXuOiFKLniK65XospabY0XVMjgO\nYZVaQiVcvmgLKS0d53vbPrUANQd9EsNLhqn/CYnxe4M2N9ZVTugGopAxF+7t\nsLfKzcNaGcFFB7JlcScGNt5+mcJ/iuCivtNMkmXJG09CsRHntn3Sw6it9dbH\nwGww\r\n=oAOB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCeq/6ar5bHCZgzuB4R/sa1EC2NcFqkY7cA7eeBpbcWPgIgd9eNKJa9+4+80z0U19dy6bppUG0VvRDmtcn2Ma0tnvQ="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.0_1629801076862_0.7627951820538807"},"_hasShrinkwrap":false},"0.0.18-alpha.1":{"name":"import-remote","version":"0.0.18-alpha.1","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"256fbe9b1aa1acb297c2e98e12d59f182e9a2f55","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.1","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-6j9EDFzwST9AAfnLaOVIbvO78TjZ/eHqqf6bAvOZMI/NAu0SpMh1XVn6cMYhdMoFUd801pfnuPXUwAvfCDp5YA==","shasum":"acdf4ce46f033d417b655f96433d3b1d22a6e840","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.1.tgz","fileCount":42,"unpackedSize":946206,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhPvw+CRA9TVsSAnZWagAABC4P/AjrezlKH8dLm2Vi4hy9\nLj6eRiGFEZvrf1B76KMbgBbRDvDM1LZXk/7rFlmjhCagYULw7dvNFSiqOwkN\nlcBD7Z+S5q3ZcGCDhmO/10C3W2b1p+Z5gV43nM+WBP4Ax8xL01g4reL4e4zn\n3vFCRdKaqfE8zc47oqYMAzl8eLaYdJC15qOPXIeu60e/dxp9ek2iG1ctyl2X\nw2DTnF/joOd20WIpnHj4sCVlex3fLgR7cCdszzSgaXqIh62qPhSvMb3Nfy1+\nwrUO+hLiBOu6btawv20kPwd02dRj/Ue5qxbshRuTYxWseKhA8r/E77UO7A2D\nAkJi5p4vWMMiX0ZzZBlUPdnPDspMWX7wR8xo9lX5VcLgFC/P4JsHYJRrBThX\nzEv6bqSwsr95iYGg1urwDufVybKSru/QSxRoDHAqquPCgRuJyznsFKCvhzgh\n0zdibOd2ZdND3DVk4JCeXB+SAkgZ3uZU98dqw0WcbmZ2gjxl063SXEwTkGHH\nRrSoaGXaYUA2531pmf+SKImtpCUpXpAiITgXSGzbM6BbTQ9h3TdpsnCeatEw\nWdyXaYoCmUCLjBSA1u8r3w73VnplVb0rWr04h10vrrAybl4ZbWoEWv8VMHJ9\n453xMiP0dPgQAbwc8h4G97KXtMD+NaiGAC6DJ4yoGugwASGy25M08R1VJ4xY\npkVZ\r\n=YwAJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDEmhHXJXLDxaLnzxhNDt0d7fcqySXqBwqJ5iNFb5jacgIhAKfD4N/IWwF/yHLdA+PXyWhfSZmRhzTCV8aP1aiEfNYK"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.1_1631517758375_0.6429072725809204"},"_hasShrinkwrap":false},"0.0.18-alpha.2":{"name":"import-remote","version":"0.0.18-alpha.2","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`也为true时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"71cfc16da27d799f2d57d5cf5a1859efad36e466","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.2","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-eXd5NFMLS3t8eGyvxlXfXsVuonqscLg5HwJzgzYyQcoHo3faYbQsmruGE8YaAbll+7Gt+6VTyIrqDvlBZjfjgw==","shasum":"310c0912df35afd39f125d2fd0cf6f34ea9c2ff3","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.2.tgz","fileCount":42,"unpackedSize":946754,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhQAfHCRA9TVsSAnZWagAAj4EP/0jJrS6olPYaYVWTexh9\n5ye1iG7qkR2/9tonp0oH+CbFkJfaSp17LiYnctvcK2qiaswf+dyKip/Z7PaY\nN0YkBLq3tDLdwVN9r3R+zMAOFFAhQaFBz76YcarR1Okkdu1VeBxZsnt/qIok\n9YCWXlwKePbL5jRS4Je5YqvDadvpoiMdddh0PYCpAw7t3nJDAzu8bj7pXx06\nYWXF68u7maWWlwCRmFeUobgvfZ1vUFsMlBrBx+LYL3UGlE9PNeQblW4K+PDZ\nW7qHZFEF3xSCnpwz66ICJRgY7mkmrDBOFpjhY5umsdi+xMWYll6cJSq9N20q\naWAQB6+xPecu8suTInMIXwViVMQpfD1HRILkBCqSxU1vCm6q+ueAsEPMJyyx\nhNpJKJ0o0bPfYHPJOlPUZiOE5y5Q43ZiEr9+oXpj0qcgtr8ZagfhHuaG3Nzd\n2CM3H1YD8W3W02gcDLGc0Cib43xin4mMuTyN2xKwzR4CSlhxiEtaLiSXIPFL\n3EOqJognkWzJWwAu98FOC2sr5QbtdXcj3nyehybdRgxPzfTHyGmj7UboqS1o\nuno0irZe0HI64l1wx6ekMxI+auX6Ns8EPOicAenSyx5e5D36D8afR4a5JgxY\nXUwvVBlc98yR/V+xTIVesBGP/DRswiuyd6YuVXvOWHukwqKp/6IKRx7lJkzB\niuki\r\n=CbKE\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFJVM2AejAcJFfG4o+5HRySYBZtp3x5Zw9KBje2vyt1DAiEAshRUEJDNM5rYaST7sHNp15NxTqjo40jqsqGBKfCp4xI="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.2_1631586247510_0.7525969131049766"},"_hasShrinkwrap":false},"0.0.18-alpha.3":{"name":"import-remote","version":"0.0.18-alpha.3","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"2bb02b92e040fc1d45c71420100c1be9027d5b68","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.3","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-wTULNvXiX0V7pRibr3/iKvMHRHd0NG7nS7u7otqkBC5ZuxJgn5QBhPSSk/9hfXVo7cONQL+OX0BwWmM0ZPNFjg==","shasum":"4979f309c00048ae86952c5109a326be5a490d54","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.3.tgz","fileCount":42,"unpackedSize":947848,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhQVPaCRA9TVsSAnZWagAAMY4P/A/IoeAllnufFsbioRjQ\nBI9bMcJ5ne6Dan7k0GxwKDJuB3DNSsxqELYHjCEvRF8w3/ZIAZxEyGwJAW8Y\nIDd7s9yY6iZWyiX37KyFuRQvzD5Wwxbd8SjbXL8C72W++I9wAXIEeJ+Z5Kvo\nJwVwrZmF0k6CaXYHHcEHhKyqygfATmjceycnEsjbQLTYnKAqS5F6tvzHlW1d\nd+pVgT5NSdNc+261YS9iBcXuqwE/726W+n/cQ79ocr9ngVfzdpZHAhQR4Jft\nSyoYTaRSZY8jOpNbDO7x8apNIKJ54a10hyECJxyUwE0xmN7V2GEj3Byl7ZyV\n4QdEYlLHAcLjtlmzdaIAe+e01CdbEZ6/Cr3//FlG+tFawbkKEY6bd8qp6woB\nRpvNrEI7zq3VSGY1QgMhzHdk380BOD7TcsgfvGHGdkavttFZYzwVXBom2YsL\ni/fV7VuQsC+7EKwxurGX9xPk1qDbDDD0VpqIaHjWuwCCpgJRcvZbp51qz5ZR\n6UBzfiRaTdEwNCjpymlNUGEQxPwK/YPBqE5CNEFT3E4NKpgyM5sCKl4IB5HI\n0pLU90RmA52eKfrU7vWsqLmF8X2xioAVlUO/Fxbhi6oecIjxAYJvraT3OdIW\nKlcy8qEh31GQ39JqR2cmHGIx5b+PtonoyC6OTfLcVQ2iS/sQQgv+aJlR8WrG\ns4lg\r\n=wFEs\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDurCqO0oP+jSTfsyPyf+I3Cxu1zUVVCOqzNUWucGoIZgIgNOI614lPxBAAEA6hsY301RLWiypxtxUDwUqH7uwko40="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.3_1631671258061_0.6779760247646318"},"_hasShrinkwrap":false},"0.0.18-alpha.4":{"name":"import-remote","version":"0.0.18-alpha.4","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"c6d6245c4236c328e1bdbde273050189bb3490ec","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.4","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-vBHMk6lBadrq3eOVbKOw8/iqhjX5kppMgwjeUMpgFQelXUgZxdcy9go+tvJiUqazJShaL8ZGbeZ/Hj4PBhUVZQ==","shasum":"a2f54b218c8e15619dc2eba6779d5603ef959840","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.4.tgz","fileCount":42,"unpackedSize":950023,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhm0nCCRA9TVsSAnZWagAAwGgP/Rgb5cHtfoZZh6d++ZK7\nBFzcunhFfvP6OlauZNOvwlAZjhYRUI2D2iga50EhAJwo3qdzrJEqyDUz+2/L\nGrTuqc0Ryi5YU7VahDhM+rx91VU7dawVscsnsmVr8wvlvB1xoqlqxRsjFm3N\n6GxSlUftY9bBH9whavlldY8qrF30y9EelB+8a0SHgg3L6sfkXG8dArfqsLAR\n1F2anXQYvFeFoLx+fITlixa4qEgN5nkPy6iSkPoja6dyNeTJ2RWUE3ukPkoe\nur44tHYtc+mCnexkLbDfW9hKpKjyrYEJFORFVexB8CCo9GFRPHrJiVgkTj2U\nt6QcC7OW8lk/zM4Qb5F2SFM8D0sfDdF0uh9Tt+0H4ZDXHlAPFMmq07KzNzVp\nUOO8pm6rouEUaWiMrJVsiFMXvfciL6lLRXK+mVM7aUqgR8k0cnzP0V81+xRr\nlt/oQ4wMRobhInAT6usYhj+Zgvk4Y/xZE44G7tLz5E+ISX+7/2c1xho5tkFa\n4JaPePP1yQWLZusUQL/rg0wWChnCtSRig7A8K4MLPyzLZ1qmapv5TVsqf0pl\nWHfNp85NTLGjdVfwrgwAMWGVo23jZDdLyUC1qbKkYuZkw63mv2dQ6AdRWIT0\n3xt1yaiPKvEEN1sjIa+71xEmTU4ini9zWjFWx7qZRwMmjtZ6AzmeqHWsTQmu\nFPnD\r\n=u40W\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCk+N4xYDuPlE4f1FbuiZKu9LYs/8/kEqrr/Ld1GQBNOgIhAI/LTogCwIhg0MdOZpiMdnFW9BF1lhWMc+QRv4wY1mT9"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.4_1637566914244_0.18789052343828594"},"_hasShrinkwrap":false},"0.0.18-alpha.5":{"name":"import-remote","version":"0.0.18-alpha.5","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"e5eaaf9261e83695fba3ca4485e1d1fdbcd2c14b","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.5","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-EDCBUdoxkonmU478a7PyelmSEenH2lsJPVre+Z5wC5bbhdXbHKt7o0uhcmB8uI2utVCikZXXDZJl/e/Lad+xFQ==","shasum":"8740b8fca50ad195889d1fd3036941f4b5aca754","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.5.tgz","fileCount":42,"unpackedSize":961410,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhnbBKCRA9TVsSAnZWagAAs9UP/i4w0qSBky0yeQHlfdqH\nwkspTz/h3xCMp+KhcYmkNxdlfOD6zJ7Re81B2VDgCPVg/iU4esehhJPLU/1b\nNfTPehQRVOehv1p5XbsNRk0o18aWCGTg/1bqspgNYJW99ZD0wcAHoEGPUC4U\noMQGYRjVp04lkBYk0Xs7E4JmiQf/8Sn/GGzKXjzL56V0rB24pKJ8cjqGrvIA\nAAj4+hupnve8IQatYTvXBvLuuSer7o0R+gbA8EegaeE1jg7NPKEHK6iytsl/\nIedREbfgvw0vB90otXv+z0AUMfgkoy2WuxgXQQxLKGa9/3jNuqtd4zExYUz7\nhd4JVKq6G/m6E6cL76QUghnR6gf+B/kur2U5/VR2LYH3hxiXslAvARTnuTiU\n7EbDOc/Yg9AQq8zLzLg52lvmFoiC7kXh3B2kdKA2q3s17XHxJgKpajpJ8Ysw\n8TX+6hBzSG0LWmRna8AF54oRSIauUrcwgEip9ggVqyCvOnvB7yySX3G1iXYp\nU5Ci840IYrCLp2CJAuEYs+hLENPiJqmUbukcKSONrZirUaeDNbcCY+7SVzYp\n9XHYFcLLaAnTkw4hPyqosZ0QtTH37eDGMsOho9SeOslsMEpwEqcVBFfz26Up\nURbiCp/VJfCuUUaXr3kDRRVy765TElSHdrcfkyeWciDb7jpbk9unKO3PrBt4\n3NeF\r\n=AoOz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCLkPXw72hYY6p6gA37OXjSd/A2GT/Z7EzKfmYTpztDgwIhAOerfeDT/gVnpGBFV6Zd6sfJDtZdHOV2Yd9E5MLvjPAH"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.5_1637724234084_0.9287050052028554"},"_hasShrinkwrap":false},"0.0.18-alpha.6":{"name":"import-remote","version":"0.0.18-alpha.6","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"e0832f76fd6a18dcd5784fe0ad1ed461e92bc687","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.6","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-OTiK0VQcfm9C77LF2Qt1jZS+OcufiMJG499nT6e6oKVp9TgjmAccOKvsKl3O/BDBI+g7GDdoA+sFSFikFjUzcQ==","shasum":"91d555f065cf576d195a2a0a29dbdf9354efeaa7","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.6.tgz","fileCount":42,"unpackedSize":958952,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhpL8aCRA9TVsSAnZWagAAGzIP/04xUQnJ+msNxtVsrMJ9\nAUqD1Y3BJKAnJcKgFIP0Cf+UXRBoFLvtT9mQgg7h6GGSI5vjhlbONlfOlaoB\nD45TATm2jrC2hHt1YYE8QxQ5mAjqqjzq2Z6R+NTWfVnk6iO++ipv1QwfuddF\nyj3JVMA8ICtrv3kWrkUbqRjh8VV3FELn+nccXlj8kAXweyaUl1ZVnuYCsIsd\n4uMASBCclOCMOwrBFSB7Bf7GHXq7fW7F9eYJKbtIcs+ozRfA+ij2qlGDnfvV\nB2diyY4ssodcRlIbePMj09avrMKD4ftV7/Pg0P5eaAtcFgvTurshmzFwUUtv\nzqKKnQJ/q49DxeosNGRrXVF+dTPG3GuDb/5kR3GzlGSsTPzSKgISscgChLOS\nn3PaegWJ1AIS+2nvc2uSeYa5WfUa+5cj5ORRjaK85sXxjTYD+noAoJLXberL\nlUhAAXCLXt9YU5wcUcFcUcm6Jp/+R6D75Saap/WaQxqcNnkhk2+oraaE6OkC\noA159jA7xAxBlG+oytELeHd7swdafC15s1TDNsH+M4lVtfognhWjVjX/Hxd5\nJ/NifGy7ZTYfII+XukKLTrppc24dEdM5Gv8vDhC/71iPgogHwzSeL+3nwam5\nuk24QKuMwRY5vo+OLLJZ9MBqwCz+OoLvk4OaeUwhSWx5QKdyNmzD8jXAD5tC\nU3lb\r\n=aDQM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC5RJCxwHQObCOtDIN4HZgcfGlOxb0MgO9rwm/xAfocQwIhAJ5qeIZupxl9CAVhQLcym+14cKWcJgxtkC+77ELjraff"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.6_1638186777905_0.7317572204739526"},"_hasShrinkwrap":false},"0.0.18-alpha.7":{"name":"import-remote","version":"0.0.18-alpha.7","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"6851dfbad1b7193ac7b302e5367013165d500a8d","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.7","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-xGrsr/HnR8d2az8Lwe8Cyy/XOUq7ABBIdlgKeTuyQeioN+7B5VY3DKxzD5scm6JtHt6J9wQCUca9kRs4C/RsVQ==","shasum":"d2d402f367f5f2f4dfb56aa0a5ade00616fbdcd0","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.7.tgz","fileCount":42,"unpackedSize":958966,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhqF/QCRA9TVsSAnZWagAAXCEP+gNsg36wcFMgHwb9wfRK\nzzzzKOhXe2FpCFCimmh+NtSaZc3DTnyT94B9NtWtuXot45UD+co/HB9K6lJK\n3orT617+dNPL5FM/dcgblU7gjjSAxUorhu1ei35zAO0Z2HUssn1zKE5iUUWp\nF5hVyXSCwNOH6TGIkKZz5TL7DqEi8mmxyh5J/XfsGFpeJZ3k3v3H5tinfz1y\nz4rzF+QG330O2a6/upLBfFnw47m1fRUvqdAWzug522BVhsaS6siPhaOzbVbD\nf0tmseFWEAlM8oryMxVfpJ4oIVUeUlYD9ny0XQwO6TnK8ibZNkvquS0nSCWQ\n83uhpTXOzqZ+VhmmIwaxHazfQv3OWuRWuQ++jEBEePEMrx4b0PNQrzl+c6V5\nSWmOSWdWlu+kcUdAktPKp9cjLsNh2BywTFPsXnc+koFHzZIWMLurw5jrNdZ7\nEWG2m6N2pN+TFQ/bxSSJgg46QWXdK98oSqsic2g+O/+O1dt5LKQ7KZxrFd0L\n2cafQYsGHMFeDKLJTMvPGMwtaoHCcL2TSWOcK0OM8Ov0rw5bAQep2ZARJlrj\nG0Mtzz4Sdt6b56CkGVSRo/Lo1sqtuUYq/pSh/qYw59/TsdA1aKyGM7G3gD0Z\nEtj8ILI6JpTYpiml32QZGRN17gpU0vGGpxQ3oEc8VcM0tX8NOUDYOYfTJ538\nYkmJ\r\n=mVe9\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD/VKzFFRQ1STXy87uePgjanWtChQDA+N9+zBpty25HzQIhALMmhGUW5p3DHumtBOlQ7iv5qodi8n09OmbIarWx4t73"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.7_1638424528171_0.1497025870537525"},"_hasShrinkwrap":false},"0.0.18-alpha.8":{"name":"import-remote","version":"0.0.18-alpha.8","main":"esm/index.js","module":"esm/index.js","types":"typings.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"720b3cb1927830daffb1ebadfb6fc83b25557173","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.8","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-G6IeW72tJnzfxvShePN5EGrATpfIKeu/k+Cnxg69Isnn//NYgTmYXeoSdhDWJT0pZN8lFPTOTw073HxmpbUNyg==","shasum":"848f1dd63cf7743e11f41c708fff2d2b28e46ee2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.8.tgz","fileCount":42,"unpackedSize":959125,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhwtjiCRA9TVsSAnZWagAAUwQP/i5/m6f2H3xy3RsnT1RI\n5cHpMWqZ/ErsUwJcVfiRpB/RjvQlfC/f4i7YWhpPjibgzPy1m/EQfnWS4Hr9\nYfPFWi3s0m2s5FuET2DIHT+x0gNOFfn0lyiE6etuU0p40nCx/brXDYhSpyMU\nCZgBzANTGHPB/Mo36zSEphP1KydFZctZYVrHrdoV/Oslb6MX3eq+dDC1FtFE\nOz4llLu5ENq+BS3c3aT8njTEN2Z49FgYiITI4yqEs2WhOqWzCQ5PmJBHIfLX\nPBsZRUaA2kEXSmxEIJdqOasAzeidnh/YqmXBNk6xShbDW26LHL8KJ8V0PmoM\ne1A8XcGc40iWisU6UOdCz82u/5Jj/x9BHM/M+A6hXTt7jUymzrsL7aou86RC\nKaRptgKuiavXTWrHiq0rVnEBxNdkPXisXmMNMigU5FalYaTAfjH1K7O0xxc9\ntOEzJNKigTdaWQhcyrlyhlb3WKAs3cmxpB7Cqp/Cx//sHkeSLpdZZSfchPaQ\nuh3Oh+ObFZxcqPwl+07GShiGVSmT1mGNL88BZiiny+McGqCv9CfRoTpTlIxz\nywDDM8pOZvsnMNNKp/lYdUzhNpZ8E2mcTCENnrNrKzHqwMlkwLaCLoedvDLC\nudwn7TOTjyJZ2mue93FQvInnJWa+FoaVB2271EJIjvrceixfbvV+45rI02YK\njbQp\r\n=NqMH\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDxwFliw3WAqTDRIcoxSayAq13TfIRp5DnTXXIjT+niHAiEA+btJYsocl/juDar5zEy3cTewfAMxIXfeyOiiIMQAl3w="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.8_1640159458271_0.8913480396913855"},"_hasShrinkwrap":false},"0.0.18-alpha.9":{"name":"import-remote","version":"0.0.18-alpha.9","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"1ca089855aea82126256dc30562dd4a6b80c15b2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.9","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-/bpSYHescy5PxsXcO/IjFTq0vJUEGSnssTcntJ2ZzWawe/jBBTmrRZ/v9tnxdIbK9cF6Ozbi0oWqZ2c7l2uK3Q==","shasum":"93abe49eca8ac36a91c34f9cdd2e33e2d8fee7a4","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.9.tgz","fileCount":42,"unpackedSize":961725,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh29+TCRA9TVsSAnZWagAAJAAP/isA5UdCikcufa4UQTnz\np/svYl4YoqVfi9dOWYzfOzbfu6SBKXpsy4JBNsCOfFnnJwlS2Q1UPACeP1J9\nkWH47jMXdS8JzbtuRzWqnhQCZGFZUNKGOyXBXLtuuUqhMZYxEHCKQtn4RwqD\n4ECljyN4JLSAbHqnomkQXkifQZUsYRbFFBq+/k0348u4f66W6LFneNLiPB7/\nD1nMS2pjTWkhp9rEFR6LpCDUJQ86bQ95PKdHvYpPeQLS7yvLuJHEuvnDVexi\nKqNIdN6v/99pj/ZC23HG/eHiG34lSHLRtNR3ulAwmCdq05bipPjlp3DMjuFZ\nFF5OsTyDJsPU6DkTl7LmiiiNlS9tlbFqKZmtvZBzjAOTagM4C0QP2Ij45vTp\nhjoS/F2Be+R47HrkfSSLD6BgW6iOIS80W5BuR2pSRoz6f0wvhOXhZfQ40xRk\nncE+teoV7DLsEVxU3d4UCOcjGvwIofCVAe7yAHil4JCXudeTAWpXQ3sF/Xmv\nF87tVHh6pnuYO8urCt787KR2MLfB6uuclVJ+XTauvZly91ci6yCuVLxy9GRN\ncKbRrkuH7Lwk0hGwgHRPnL3AlEoMc350k54E+gIrMahl7NBkOivVbw452x9P\n38dchsIgZvvZbkFETq82J6TY2ql3myigO4OsUmJ2BSjf5xGp0pdwo6i5VOLQ\nCuIp\r\n=D7iB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIB91Hi5lWVCSoHhxCQ2nz4OYmFRN8jqiWZnZA+KHyEKqAiEAxmyKDFBcy2yWqNaUIosbvmj0Bv4HcfaXv81gM3wBCOU="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.9_1641799571205_0.6438367939152001"},"_hasShrinkwrap":false},"0.0.18-alpha.10":{"name":"import-remote","version":"0.0.18-alpha.10","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"1ca089855aea82126256dc30562dd4a6b80c15b2","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.10","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-aAIJ1iiHhICPukINPDHUb319DFu8/MkkRBe/VPOcCCSzarRbfTpLcD0txQJbNf3aY4gvQzhz220AaTXqZdReOg==","shasum":"a42db46095aceb0ffe04acceb473f263d0e9e5b9","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.10.tgz","fileCount":57,"unpackedSize":975384,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2+OzCRA9TVsSAnZWagAAP4kP/0UJbkYOvYeytJWli4qK\nC3j7XiX22SA4NskR7/wU1aa+CkNNzNzVEBrKnyBA535P3S8t7UzwKSv603Ua\nj10bBwO4OuECFu2n0COnC2cS0d8zQX631i2y7cVzNktDrxC1D7/hV9z271Ur\nkFeNegS24jwWZUOfxTeTcxQMh99ELnGRBUMpq6cWkmqgibWNO5SoRTgc3Dha\nmJq1Pm9iJoOSVG/cxnl+g/3HB5gcMzAoFUA/7I8VmOO8iNfHlU9rkVQb+94y\nIZDo/JZO+ey9mWx+9ntB0GXaU+WPexInPZwazuq+VU2Xbe0j4H3g2G3TKnyo\nhdP3XXrx5Oi39aAA1D13YGVBJiGDaYDAGN7tcsE6tNjwnmkQTWfvufFX3AY/\nyJ8jbgEaQvnVGoWjsB4sCCwIJOZuhIPRZSyaY/ywpNDTV8cxHMNLDjQ1r0MJ\n/AhYgSvrNmwUA888jUYAuFTVJ1X5NLHbqi8Q9CGyjLK0bjcKjCM2jK2b+smd\n7CmpJl9LJcEZj5niv0RXevld8t4Td9DEkMRQ7ZWeNWChlWKs8YN0j5V7ubQB\nPSwwspmiGTdKzm7GBPWQ6E2g8nb/Hhg4d9E1Md+vgHgZcQgMjY8V+8I6Ltni\nw8xX4W5IeWMc3ABehwqeYmUCHEvZB7KqlrxzvcV/abjxvNjcKzWZTzNsmekz\nH4Cp\r\n=JSLV\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIH6zI5e2HVlTN+i20gX3Tb1eh3BHp6Rz9HH2CnQbN6OSAiBUOVKvFlUvYcD+I88jjEr6KSuzLmQLH/pDRFiPSfFmHw=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.10_1641800627079_0.5825230271191513"},"_hasShrinkwrap":false},"0.0.18-alpha.11":{"name":"import-remote","version":"0.0.18-alpha.11","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"30fd16828cb161202f38a56fb44fda859c151c8d","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.11","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Fp9TcDjCNYx5EiBHWR7c4fchvZkE5G1LiPkUplSPKNfizHw0G48A6f0lMkdf1p6fd+LWvnN1sjJdFOes2kPpXQ==","shasum":"c8d156e2b6f51ae5d75ec2ce0411ae99a37dd084","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.11.tgz","fileCount":57,"unpackedSize":975390,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2+YrCRA9TVsSAnZWagAAXrAP/jLtsE3/X2xTG89bX/XS\n1ftT/tOmvdnx5YO5p+frbx7/Z488p33Mo1FJKCxucai2UXGHFE25uwSZGffp\nP2fNX0D8ky8ArLIOJXP4Cp+D6NfbD7/hL7lGTZ44gn7yplC2EmU/yjN5RnB2\nJjVcfwpp95e8/6Z8z4N5oy67P5ytmb/yQPZqzB1t8/R0/5jqBkETblIzrUif\njWBoXxMK6UP1GaZt34O53Nay28CMlYViJRtzOe3gY3WIehIGdpExbvh4rHvv\nAx7NUhHTXHLwRHqpx+eSzOXQQESBoYlwUHjSVSkdutdtR3k6Eky+9LqAhz7T\npfda2SJMsnRCoULbOctxl/IvzbJlz6h2Ulzv/CBr/DQnK18oFKe1ke9cZQsN\nBscDz0kv/kUMvMbKJ7ofzGf859n4wQLt+xHgmjGGtTAHLiNg8LG1xL0pnURa\nDftA6+M8gKSJm8Op0LKJy8oWfyJqLnv6VlACLJEkizidm4C49HELgqES7yyl\nv3JPNq4c/H9TFO+wGk9ynqBAzaMk59vcji/UC5vRaLfGbYCu0G8Ng5zZEjFe\nVDGO9hGQ1Ukl2VJ10OAw5q+7EH8mSKNzec49afBVn62XEAJ4M+YQuoBXu2bj\nlIrrITar1IglujikrkTX8zl2W8PDDtEnLWtoRT/DN+bXt+bdLvvTj00Pzpsf\nqW6D\r\n=u9SB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCePScfEhdqFRNJ5n/ZM2rRDjJW3rpR4ZruFyG/3/WBmgIhAItb2B07S2lqjCeMr5OkM9vkdu0wZ/lMLnH5LJcb4kup"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.11_1641801259570_0.7019287456261163"},"_hasShrinkwrap":false},"0.0.18-alpha.12":{"name":"import-remote","version":"0.0.18-alpha.12","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"b7c8831d2ff3d07efb0dba71c8435544a508f06c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.12","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-1TaLb0c/lYnNz+/wwazgn1JEYicsGWZTyVCMK4LsIRLbMCjzHMJ0srgOISFVnnjnPMrgfa/pl0r8uFbTfYtKqQ==","shasum":"1e4f939e18c6f1feea2ecae3ac88072a612cbb79","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.12.tgz","fileCount":57,"unpackedSize":979896,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh3+5TCRA9TVsSAnZWagAA++IP/RZUEo3clw0gzztfOjse\npZjkgR+FY0XanevWp6hmoQKkBtZGNJ9Js0gp+KJd+iJ02sUzwzv9BGEVtYI7\n/5RRxdLSmze+M2CXa122KbN/ndg0x7zIaIYEtFPlW90ezoiGFQ6vCdGXgWV/\nMH930mD3CwDw7134DZOu69efw11ZY4tbJT/trqB1UL1gY0hOIwT1huDZRfrt\nXRCWtzRhFjcEhLgxCgH9JumWhhnWteJwkZHgoG2eHvz2OAX0NXVCcpsun79n\nx/+YVcPwZsKdvJ2CQGYcBSWnUUSKbhHVRDdz0Y9F5cPus1Zi/CapBuaSl6X8\n5McIYbbCZDUf0eqvmx8t6i+aQJuAcpgJYPqpw8TSMeZmWplky8f3VtWx32W7\nom+REsvSkxOOnc6OUj9pLn3gEtdNF7J4Uvx31mL16Hc1+JKeUPrcfu15jChw\nRkseon3g0yA20pMayU+hNs27OihGRH47rb46fAbPaDtdLJnTfSWhStjFDQCx\nl6+4QuI7hh+XGaNl6rasrqSI+CyFIP8C8hJihG5Uuu85XJjNlxJxa/l9JHar\n7o9+/JfJW7aGKmqw1h0ipPU1oi/pOFwDsmaRSxjew0CP8FUXcG9oSmEx5X++\nUIRahEil3XP1NDjmWTCpWuxfQ2p4+1k7wK9l9sjLZXOSTxhULC2vG71YQUcv\nVAxV\r\n=lxQq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBI4FJdOgLfaqcoZpnh2ha6AQiXATWwGt1sV89W70UzDAiAVqjHs/M7y1bRWAExAlqoP4KmBaboVLR7Q+urrnupcWA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.12_1642065491532_0.5973917529605999"},"_hasShrinkwrap":false},"0.0.18-alpha.13":{"name":"import-remote","version":"0.0.18-alpha.13","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n","readmeFilename":"README.md","gitHead":"53d6edca8eca86a44abedb9b816187988ed1997e","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.13","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-Gbc0iV2vj7wXf8cgCg9ZG9IKkSWQ7idRLevA7r13oT3/03qat7361ooUrGH0hwv1JGYiGKNLi3uDR4F9NqsZuw==","shasum":"1859bb993b566eada388c47301720fe6994079f1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.13.tgz","fileCount":57,"unpackedSize":981542,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJiB4DUCRA9TVsSAnZWagAAtosP/1+veRRxOoAEFhAvHgNn\nY3qVft4YmuT9hvNp4n1w0J4GY3nlaIXbO8D3r1EczM0UIAla275GgcSy+Zfe\n8s2foW9Pbb/Ug7ABgsDVBfyqFRtIbu/sBnlmsb5IRyGxJnnCG2/LXu6OvWOt\ngS+x9egDGivVFb7bMHYlWUifa48PuPB5mRV1HEK+J0V8RCDKQonNMxryWO7l\nZmmO7tvNa6nRgIMl8KvbeoS5Rf+iVWKDFjXJDc+g463bf5aoc3KOorjX/F5U\noXMf8BtqO59mxhU0bHdIa1tQXUOGT1/0Ta8dcWYgUdX8VFNLdn0TYcQDWSfj\nyD80K0nPnP54rKLDfJ4aakoE25kvySCY2K7sRSngiYElb/3fXTg7dbhNpxjW\nnHCblUZ3yQBjTgtdQ+Pht4bLk2viq7Ntt0cr4qEMvZMeDddwNuEcO6E0QhM+\n1fFO+qmk08pflqF2xuYG/Glxo/5iUM50DkdqvzUgekoLINYc7rb4Zgpa09SH\nxoLw0eGmCHLlBz+RmkqLXpzPB42dU7VFLdIwkfzybJCgrQualicTKzb1V3qA\n/Mt3OuPZd1NvozBdZuD2V/yNgYQ9LgNyiqVzbsJLJVfkJ22LiWIjux/Df1wY\nKQIZdOrtxW5wZ1RXokq2Bummxx/Xm7Tvd4z7Zk2d2BQUUBgRuO+3OFaoVOcV\n9ZMw\r\n=QXh/\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDiP54gcvnALBSnhfxqoh8kr56DQN1seLb/ykiFcNGysQIhAIDeDeVs6ndvWUEAY6SxwFS9R8VgNdUDgqZPf+p1yYya"}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.13_1644658900156_0.32158041267081683"},"_hasShrinkwrap":false},"0.0.18-alpha.14":{"name":"import-remote","version":"0.0.18-alpha.14","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名，调用\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"246ca83ca5ed28937e3ef32ebf7600faf27dbf8c","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.14","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-WU4WHKi823N1t13L5vergwxN2qFyTQvGMU8fB866vsOp5qsV0v8XA3Ve1e5BWKalhvFXFyEnI1mu4MZ/xUDqeQ==","shasum":"6cc3c97db64a4726acd3a62a67ab820c007235ed","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.14.tgz","fileCount":57,"unpackedSize":987961,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiDeVcACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq9fQ/+PJWdu0AklWinpochwcYMmzReJtlGfqcAxNu+Mqy+PyQrBC11\r\ne3UmrEJ7kje+fucDalsIxK2nSdINhUZwvPwq8ZzSC6Py+xnfxcZTzXAOpH2v\r\nefXj2DMy8+9fNz0WiVoJ30ZqYAggg0dCpDKzT5nVvzBB4Vup4wbu66FZ4WUZ\r\nt51nz1geK4JfaOYYoUbKkYhEFR1sQqLIASazBgAREP87MtI3NSmSSSVv7U33\r\nb3NRlxvyxl/lCvmH7ZpAWOeIDbcHCURvzXlY+HO60SKKQgeAUoBGd4PzPkP/\r\nvdUwAJIYOHzoSeKL/9kiWtjyIEMz10NcfqShg2hB7OEDhoSyY7InZtmNqs3S\r\nq36VpptbXeczALcC4ILetBVdC0RDWHbFkTZQ7+3uO/atsOtULr0Eq3R8eWG6\r\n+3/9AdAftV3CQNpeVvvqnS3EK+ZFWo7Qyb06l/syjzYNvz2loK4AQLVx6X29\r\n2cKYQBfrmKEM8GGbciInRl3Wdm/sljtWZNf2xAnj4yuNP367AZL9fNZ8JQn5\r\nRdOnVLUn3cWgLhOKnpQnFxWmInkA081EK59PdT4Bse5rg1+qoOU4evCz16H1\r\nD61wqaRSoNJ5kWrQJ6BPVjHcS8JYXMuU2z3SUSoeVmPhgJT/b0GzLgJdH5F9\r\n4UgmdwhuCIS7h0T8cN/c1A3fsNRIyzFbXJU=\r\n=Eiy4\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDBdh75fIa3FZTlo6WwxieKSLAvSR5MS4w+FgyoDILz4AiBmuQ1t2ALwukVS4SS8cpuiArwln6kIR92BiMJF9RMbtA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.14_1645077852555_0.7618152456430933"},"_hasShrinkwrap":false},"0.0.18-alpha.15":{"name":"import-remote","version":"0.0.18-alpha.15","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"f01e8119b4cf7e053b3815312adef0cdaafb5fd0","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.15","_nodeVersion":"12.16.2","_npmVersion":"6.14.4","dist":{"integrity":"sha512-PNRJaWYIantti4HkmmXOGZ3GJ4qO0FUTbVYUaSQl2zmvMYZPtJYwiFUpWnlBFdHoKEJ1QlMPR5LPlU8WTfp3FQ==","shasum":"b6989644ae033b67b6df872ed5a52704b81efd53","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.15.tgz","fileCount":57,"unpackedSize":993845,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiKVctACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqKfQ/5ANGe5cyizPZiaqC+tsV7y1p+P3GwK5/DlHl9e/Uwyk+D8Ooe\r\nOeTZ1O+ZhVBFJycU4N82H4fXQYnnsMxJYMVJHiarrtO9dvkclwqf2ABgApQC\r\nDwlwf6Pa9o/Llq3/JDbbXpICdzI2jwibFVAGXi3mC51yQFmW1w0HsK53GW4s\r\nTdnJUbUD8UcT78PSmKf1HMcmQlnSWMxZ5V6OOI3Vmaap206kMW3oYscxwS+4\r\nwKvE6AkYK9DAdmnTgo8a0UJrfWWpgyYeFU3jrEOUqCRf3LYoac+D75+U25ja\r\neXeAEe1vlx5xiR4hUTt6nxonqjlJ2sw8b7Fzjv6g6kgiORp+2vOJRfNtZ8L1\r\nPBMo+pHA1m2/OjB1nT8SgFib1S951A3dsuooTgfRTx2A4q4o5nb5JaCIzX2a\r\njTTsdyoGhELbv5KhSOQBy4wZjyLUINvMdb+jvunfMiJ7Na8weU58v0rFTCMB\r\nmB4MJJCLlRx8bkNPsClddpXWdjXFR5MnZawsNdh/2KquSXMLvjJFplD8zY2e\r\n0Wm0vDGyzN3yui6Rsu/J+dGqirIYRhqc9yph/nBdunAY+Ib+dAadWW+OL7oV\r\n4Gm24siRpf0vAHd1inBcf0YtivHG1patA1xGuR/3SnWGuSiwRY0mznKh0FUR\r\nZ8BIHCgPSgoSrSkf6yLTFq9GaxXuNVGF5Io=\r\n=5PSU\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFGp34op84TpkmwAUPBUS/AhmR1f6dQ0Dik6gGilziYrAiEA+CmcEM2JApK8iOlOSc9KF+GE3em3C2kGytXlMOT0uwA="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.15_1646876461143_0.8011867911502017"},"_hasShrinkwrap":false},"0.0.18-alpha.16":{"name":"import-remote","version":"0.0.18-alpha.16","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"0533f430b563b4c9e9402c6175b113fb079f91a6","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.16","_nodeVersion":"14.19.0","_npmVersion":"6.14.16","dist":{"integrity":"sha512-KKwXtDnUu8dVw16T2j6YPSz0O8kQpPfvivvB1bKv3zp99zrz7WX5vtUz651ojfF2FOy0HzqC28JgFCts9m7ozA==","shasum":"a0ff4940840088a302b1511141de55c5e85e3918","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.16.tgz","fileCount":57,"unpackedSize":1003640,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiOGN9ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqbzQ//Yy4Dfkrqnea4lUqo9Vl1yOAviKoj++NW21OXGp8pYamRXtLb\r\nOjQusrVjoFOjKLvsxuFJfC0zgnjMFk7vKNYsSPWKVWWFRFvo7ITrYaThjVQ0\r\ne3OUl8KBQtCGzlf8JcATlNsBNJqqLc2eZc7goWatKXTVdMusYiajzUUvh85L\r\nSXF3zSOusCpgFnM97SyUl/46Pyh4rJ5S2nztzFgJMaFFG87jaN7LT6InD8J0\r\nVxGKHppxLjajZe5D/+BUEUE1djnv479Lm0cylij4xVOT8K69JDBxh0+Do6lO\r\nRBjT/15mkWU1HNQ+nSv3ItQ/OmEq/+n/tC2Dwe/HsyJ2xjvN5ZcJKdXZEOMA\r\n5roIorvwQA/AwTH5KcOGbH982ZmKG75o5wnrKVjOgt1QmdTL1LuUs2e376zq\r\na87c9q1NA7YAl3bQiXZ/zC51i3wOsYlGQWU6t9yZdQnH7GihluXA7RqRIIEi\r\nVMo7iBz9+Cz/ROPjc9Yfd9SRbnVy7rTDhzZtXFe267vPk/tOo/FmNoi8lUBR\r\nO5/sga4BIOaw1ndYfc5O3X7c0hJpz6qzSIme4F1lIifClq1+2yuU5WMN8dn8\r\n/Z4IfX4RmnHT0//Ha7ZU6Wo3d/DzLJQItOJNIQZPx/zAsdX/r/LEOZzgL8p4\r\nhwrnZH/BXqirRvA2dulRQ6xpeb8EBmOL8VU=\r\n=W0gz\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGW+LsudseLrQMxeJ49VNbd+3YHAfRlutod+sqp++3hvAiAvq0+UmHwF1pjywqRx770C5GT7BY+jjtfioqjIm5d0GQ=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.16_1647862653317_0.9243484121221073"},"_hasShrinkwrap":false},"0.0.18-alpha.17":{"name":"import-remote","version":"0.0.18-alpha.17","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"60ada9b4498bd0c9f91e4314513d723c37875ac0","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.17","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-keTjfaB4IILWOdAfwmAcD66bo+rRGguUJfVIqpJaFzzIDqGSQ6FEjXo58qvHWyt2C2w8Yx62wmpNkxBMkmbpKA==","shasum":"779e01a3b89dd9c25413170cfb7e62201458e635","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.17.tgz","fileCount":52,"unpackedSize":987414,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiQtBKACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmpwog/+LdfesBQli2gCROB3EQiMRnY9HkIQ+05uHdBp7Jpk9XxLnNCf\r\nSRuJqxp0i4u3bsaSOwtGm3qGT03WBuK3+/hcO4n8LE4T4PIObEj1fb5T09DB\r\nyP+jyeNqORmy9KV3hP4xxBwrXh6NoM57/n+G+9UfKza0OUjwwzrZtlvyVonc\r\n/75An+qMJkRI6upfvRdi1haphMCUE4+tttoIxHCuKZ3FuKd19EwMEMB5xeZT\r\nq3T1kVSdyzRWlNIg05vaPjVz4locI4eMHjOow+I4MQt1wzodrvJzZ7o940vx\r\nfsNw3TySsVNXvYFRX2JqXNSF4JULEl8aLhLvqwS5RzpbriASwLNrxSHaef94\r\nH29MhupM1usKZmuI0qVXvXD8ciygxtr2MGOgYymYnCRQn/nDKld1x5qekBWq\r\nt1/BCAis8bMyg+DB0/qKZ/KG9nnDqrAUqHRFuSaACESO418aFDRP3nDq82l3\r\noa9yDSOMkalH45DlC1JqPG5KAwi2bmQj1TFNqtlhXkpODxpx+xCy+womcNa5\r\nj66qpFvXN6CF38fVB8hwji9JQOggeI7Ek0aHabfYShX/zkETd1oU1o/Iwz40\r\n/EHrW4k1Pjp9sAkkWsfLIMpQMNzgzdGQwAtrBLPOm0IM3vcFcl94XvsAnlOn\r\n3zVPjaVENDPjgBiN7b0cyALjK5Dmo83mZ7s=\r\n=sxxg\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCyb42FVXiIkBvH4nKlx1fmScRdFgz0oOaTXtYOgfHqawIgLLlbx+X3sWOvKnqoskSgktcLghSypDhegGZmOM0BH68="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.17_1648545866317_0.15686442680768797"},"_hasShrinkwrap":false},"0.0.18-alpha.18":{"name":"import-remote","version":"0.0.18-alpha.18","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"7b315cf0cc443c8f64771f1e4d9cfee70ef43761","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.18","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-RvH2aPflFua1BbSmwq+cxizrrW+Q0n/lmclumid9e8VxNQdmJSnoX5S3ONOqvMDaBGpLYFzQOVAVBPKK0Tl+SQ==","shasum":"abe554ae884697bc571170ed0d1ecd9f894fa70a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.18.tgz","fileCount":52,"unpackedSize":990799,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCDRIpoDEn3y/wS7ZOVPTU67dl+oPJrgeOWArGFwBzt2AIgL9KH4di6jbn1M/6ByH0EwD/7lexcz1CKiA0Fg7n14Wg="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiRX2FACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmo9KA/8CrjHi9WX8gmPKqHTyKy/DO7xHOJvdvFtd6SqEdvNR0ZTPXVc\r\nnNeaTfRqffl6U+LJhzW/q9ahaX/AlRlLoai9GoyBK9UTW7T/LaeK2w9+MP4J\r\nhzI0cMGf0GUVnwEu48LNVeLhRxsDOSvuy888lUGLju+F50Lcph/yBhKD4osU\r\nTLXXpanPGK/Jja54W8MMo8CAtOagrPyKyHcVofe4JcLUKNWpKFj7vbJd3y0R\r\nhw/rvf3H/9IcWjHdGH+j7DUI9rSRvKDwBSNWLnV8qL8FmEt4b5QdK4weOYbZ\r\nbkf6SGJaVm33ye/yIEStFGHG6xx4jBZJVGGkG2ie9gd7qwyQL9wsTEri6SmO\r\na/Q556WScClTA0FhKC8ZdPkMX3C80/6zF8oxqi2FF49gKHqjAJEl02ET8Kkr\r\nDctTy6WomsmfNiUTUbT5JjYvfr9yhkv0ww7YL6NlN0ZtTYFJoXzU441AVsFI\r\nt+ePXHEqFEtoPZFin95TP2OCvV7IOY45A+mousGPz0xu/JdHb5gfYJl7zGbJ\r\nopvGdenyHJ1ZTNrKyIIskcoRjhxpY5KkX5mjEGX+T75to7BkTtEJ/vz5R5YK\r\ngRVm3eyPvDZWAAA0iPQEbK9EXkO0yh2OQGx9FIe+qvWZQ8hCXiosMPm9iXEh\r\nevOJAq7Wf78TeGspcOPzoX8r+Mw4liwHF4M=\r\n=uo4H\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.18_1648721285058_0.13181715626699742"},"_hasShrinkwrap":false},"0.0.18-alpha.19":{"name":"import-remote","version":"0.0.18-alpha.19","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"0c33de5468550b5a39abb44f427a7aa8c07c2e08","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.19","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-gmDT6QTYKiA2usL7hjKmG1u7E8X/Xjz+++gsjTJ8boGsBPmqeJUrEj/iWHvmQ85QgCkDJ6lbrB+j1TBcpPrM+w==","shasum":"a2dad9d9f7f778acaf9d9cc43b3df868456ff577","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.19.tgz","fileCount":52,"unpackedSize":995205,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC6uDXdnqG/oWO/ssAWJQgaTJqnmIUVw/kJMEU+zrFhiAIhAMyLh+9nx6igEDIKR+a/nrIsnei/+CRdFCkn+V9FbhTw"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiTPMDACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrnrg/+PEtgwEmSXAb4Ghw7gJIZoIf9kMS07nvQ14k24WWRZrYeQd0e\r\nrgZERcZTzg8bP2HpUygctqUpjkubYTqpSm+FU4Uo5cgTWPstYQiHKX+sjSx8\r\nbeRnLqggWB7kdJdZwgNkpL3tIpKc/21pjBYJQzHwVp+HIbZh9kcaeiK3vpq9\r\nqQXQfOE4wNqZRo12tAsh4jTia971vJWezz1rxywr3HBmHXKx1X/ZIetlymww\r\nzRxP3YUCt/og4beM/xf4az/t0AwFxUED7Q08cYLgssagBXC3id7XviApAIl9\r\ndE2P3NmpNe7Das3av04EAkHRov3ohLhV2bUgK66V5gpMGuvuoBVPAtsptp73\r\nOn+8ygsSc/UTuTEdiafvvX8v/ElzMxOzELTu8Q+YTOmBDlSJBMkBQI4SdYZp\r\nPe1ilIsZLrBS//RzO3axjxB57CIypDCYU25qCjPRCtjqiJLsWUZeerY9tdCG\r\nBhRHuwgBbitusQXiQlh+TCvsqyPKO8V22xE9V5YJDsqKVSFn4up7PFY9tgqx\r\nZILdyq2yTMwUFovuw4HF8r1coJaFq2HJAE6XI/AJCMlUIgpNQ+OZOuj9cHvU\r\nslEdrz1rZFrvwt/zfjMNQ5gao3V/lEJz+JoPqDOHPb3mA7MOxfG8lht3s976\r\n34n8rCNHjcYF2Bh9gdeeYQIioG7487PInPU=\r\n=q1Yn\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.19_1649210114831_0.22324934573392086"},"_hasShrinkwrap":false},"0.0.18-alpha.20":{"name":"import-remote","version":"0.0.18-alpha.20","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"71f12891dac6e3ca95bc568994309009b41b9bc1","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.20","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-S3Wlwi1l4QmvMaonNhiAj2Sfgf3ToecN/Um1Z0DQuwq0nqBYMVXwRg0oUPmpb3x4KzpUbvDkdb6rDUwWj+Cd0Q==","shasum":"81ea92ae2e4b8c5a9b08503fbd104aff94095dbb","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.20.tgz","fileCount":52,"unpackedSize":999034,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCM9dtlQ9MvlJ7aLb9WNH44ehBzYwU6RaTaXgXUgnuPhgIgStdeyGMDjoUDtC52PferQRnGDVFHCQ+LY+S07SiE9iI="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiU8vzACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqaJw//b3bAmyTb4W/x5LZGrJFAXgR2RlXqNHro1ShRMeF6fMeaygvz\r\n9MIltuvLXlbvMXhkuneRuohWg215kEAwxBTdonKX1MxgKiwzLPHdPXKOb36S\r\nfYyPOfFDHo9vLRDg2UnA7p3fsNA8SvZhqYZ+gFReMhLf+KOJCDKCyp9bN2VW\r\nfXzDfOj7MmBBIse71YMt2pOwWYZXXVD6LmePyWUn0sA7WrOlkpTmObK8nlJ0\r\nWcio/FDdZ7dTZxOSEsDB9OvuOgunk0vSgd6bcEkuXIWyv7cP6tcTQdjsWsxO\r\nraq/iAT2omuVXnFtwXWZZAInQZUP6a/qDs4wW6kVLCXZxBHWlDCbcHdRsnQ3\r\nXO4f1gGckX7hS42bmtM60i9GCzK1CPxLscy4bFmrzo1/q1UA5vPVM37jzbV0\r\nKCvl6i8hF7dDwcCN7D6/Da4roJnCcugRnZYgOfmZqQ7vQUB38rKnqOSl2Uiv\r\nTZ0bxNxojezrEnjB0eP8eaoZdv/v0r460tx2E++xKJYCieiG069y+9JEuQRv\r\noFTRUA4bacOzBi1rxWt/t1tGC+mNN1sv5RbAX9AIpGX9WgY0+07p+kl0yInO\r\njE4mTQQ9VKV/mhgefk96osBiCyY/0Mq4wTGYHuQ0XaKCb9D8A6I/SQHT9tHm\r\nlQohs2seHQfsOaTu1nnYf1Kv0dw7z0Jb0Bc=\r\n=zu6r\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.20_1649658866995_0.78034981227768"},"_hasShrinkwrap":false},"0.0.18-alpha.21":{"name":"import-remote","version":"0.0.18-alpha.21","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n\n**index.js**\n```js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  axios: () => import(/* webpackChunkName: 'axios' */ 'axios'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  moment: () => import(/* webpackChunkName: 'moment' */ 'moment'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        { name: 'a-common-modules', url: 'http://localhost:3000/common/index.js' }\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"26238031c14f93b305605546af11663fcf3b6b83","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18-alpha.21","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-zlO29KajGJv8HDHAf8oeN8QW7z2092bTdbfH4kXNQwHcD8fggq8do7DRTWeuCWswz3XmapodutQURTuD3iapUg==","shasum":"573a6923c7b3e71d0dabdf38927a1194e1bd3fc1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18-alpha.21.tgz","fileCount":52,"unpackedSize":999103,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDg6pqTlXHBgqWFmwayq5gDC7yFpqfD47vb3rx0ksxN0AiEArMXjhJ+eT7p7+eEi7X4+p8n6c1Vj454lq9NV48Hil9A="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiV/lvACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmryKQ/+OIdtCbQk18VgiQQbhjgXQqLppg965EUbSiDdZTVAG90Kotpm\r\nmwrgajpH+11fvjfVe8ICAwY+oZUiroYPULUoba/dsPFGeZHJz6XmNMhACJoe\r\nQ7tZoIbjudVeX52gytCAj1H4XkG8FV1Qy3ED9feAzUA4Ay5R25NSKVS+LyQH\r\nPVI6tBKe8IQt9fjb4dQIlVvGhULNX/b884u876tliURwJfwCOx+GyC7MpEsc\r\naTAgxFtLWMx2thuoJiACul5ffsq2NumgfZB+crtychzMCN0iyMHDGwoRe+E8\r\njUjhOVIVoqf1+ttf46VByz0Rk4/MmFb+Ftx3Sc+MLN1rrqRJmm7GW8z2kJc+\r\nSyPT6yFaNkMkiFOpUo5s0Tf5o9XhC9A/LM+8BNyRs7H0j1rqHq6gVvUabnac\r\n4wyKmGb6dKFEGxNbErSuvfsqi6RnDERg8kL7CM3SoPDbyeBIv8emfypq6ngi\r\nrH4ofDzsd6zOW2ElhH5lk2BQSQga8tqUmHQM/Hc0c1nxdLrJtKrAairLFN89\r\nNddJ0wvz8kyV0GXQP3qB+3Ii2mpt9xZZTxCysZJ/rSPNZAOwnKrJtOWwXRSY\r\n9VmMHD25eGBtNB5nKNRM69Z4dBM/i4dLgEhsmMI5xC90+gzCv6jJOuzWZ0/R\r\n6BfXfcsaGwZWuyR06VwmDI4ijiSEh9u9fU8=\r\n=n6Xc\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18-alpha.21_1649932654896_0.5585245645640737"},"_hasShrinkwrap":false},"0.0.18":{"name":"import-remote","version":"0.0.18","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"gitHead":"f06bb1e962275895a51ce906dc6c77cb1622f6c7","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.18","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-nvYcdjwo2dly92SHP47qe/hmo5Fg2qI5HSqV8A+tBdRdEJOWKyNSNWmMTcqtBOfD//n0zGqI4eLzTG8Jlxqtgg==","shasum":"6d5cea6090878a638f4f6f1c53f1edf2ffb0c916","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.18.tgz","fileCount":52,"unpackedSize":1000412,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEICICJvm3IvQzPYYWGvT66UqLL+FZjj8xTyCwYhtPvB1AGZAh4blOj/UnMeoUKwAWrNRZ//KU52V9wAXmnWLeqU1Uw="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJia04rACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoKWg//T45/Kd2fUj+TQrJuDCI1n0lUYAF+DUbAhiSzpA9Mws17Tm0w\r\nsD3cMgulH/dHrVAq6iUnoIBYg51SLA79RlbZtTH9lDrLzJvvg3ebcBPrJmNN\r\nTIOXHxu1cmc7dyEscozJn/P6Az6gSiruNx3CMjKqkM8Q9+0NgYiU2NXARz14\r\nV1jp5p6PPJHqtHkbmCIg8YMIHX4TGq1/Ws3NnyocSnYswrXdtp0YraR7Kemr\r\niNzDmxv4cPzJA3WIKngJegaIchxPAsIesNmZLApOzFEqaCbajEWtBi4zm3eD\r\nH26eWZBripmEl8p3CO6UieVKuhInNxAHlh4Vo08dHiI97yWkM20GoCVrZgEQ\r\n4gvR49SuUDnBcMcKzP74eICSTU/4sn/QKxiaJ/7pbxPqDwWqRAAD9pKZwSew\r\n9dVYmTbIzMF2Jk12+0LRsD+VgqzadsWk8FFz0XMDNHNhnvkGNauYc7A4oVjp\r\n4BLPFNhobry/W2mZnuTOjolhc12+Kx30b9T5DbK3mK2FLt3NkXG/SM/Lmn8L\r\nDrB4qo/F+FIA9EpmP2QlF4KiGW4JHoVvZ7pvI671wIvzMn3BpkqVuVRUqz6Z\r\nRI6vYi5/c52TxyF/m5p5QfrqO2i7b3MoqhrLyMwh3f+hhme5312mwm5RTJEk\r\nOwO8qw8aJvYIbdbHEHDOA+p68hnbgBLTRjE=\r\n=Gapp\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.18_1651199530952_0.15933870523434202"},"_hasShrinkwrap":false},"0.0.19-alpha.1":{"name":"import-remote","version":"0.0.19-alpha.1","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n一个远程加载JS模块的库。支持webpack4/5。\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/axios/index.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/common/index.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"a5304b8eb5c86a142d97cfe714f043871ecd46e4","description":"[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote) [![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)","_id":"import-remote@0.0.19-alpha.1","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-LWPJmBNSFTFgNy7CmwryZrxToCKE1fozy5NhQycUiwLAtgyooE47Qtyh/XLiNfRXkTtFqWBK5rX+OP/59cbwaQ==","shasum":"c10a04e9564d54b58499f2596d0a578578e28cbc","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.1.tgz","fileCount":52,"unpackedSize":1004729,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEtseHfPkZbiJxv2L88W/VVYyGrSNATxArGwfBTECeyrAiAbJu8PwE1H4X26Fjzs5bl6TTsg8H5M0+e/j+ptuK92IQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJixUawACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoNyg/+Ogj6TUisN7wcMu5wDIamqlZ3k3sQlXgpFsxdzEFwW9XoQGYP\r\npFXWPUM+ciZZyGnwjfjsDBJCVu4S6HM+zMhfPQFzBEYeE3FTXb6YGnZs8rFu\r\noxWSKtoTp+zvklTbFvkzQUQTHNACmxpWPFwXGv9Xa1IiPy0egngVncW3YNqY\r\nwoOsgJOk18W/UWXKBIuDvEi5ZtOe0CG3AwY8QPmLnJwr/cGWNQzgQoNCdCTI\r\nd70A+WNBgIUkYc1Hvhte59CzWDCi6FvMoZ1a3K0IIwj+0fFejXjB2TgTqzaL\r\n7sOfpGPiga8YDQf+rdZGVVuhPEGKAtvbEgv7DaebZh95EI+g3cjtUmxkXizg\r\nhP9fwA4yWQp95ekMSI0JIBcK6Krccohxu8IDl6we9mm/nW4gs97LLd3OK0ye\r\nAI0WWCrmtnnrGseulG5hfknAFdr98Qo2Qpr2QmVJ3/wveIJzHMkeOhVBNnRt\r\nl8QboZEHfdB11a2Y1DUs4i+sH9bUD+35tJ4ElUra5pDLlkRY7ZPvAMzlBN9b\r\nNkUFrXr4f98NtZBiXuwfyYuqU/EflPaT30qXd4HNbi1qUJc94LsiRCGPMO4C\r\nAhmFc8M7Qp8J5G5skaSQaN9PlMl3qAU9gw3ovlPTjeZQdVKn5GtoJonncgwT\r\nUEs0FbKHIsMkSI2h/LA+aOIRkgaYcHRe/no=\r\n=66jE\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.1_1657095855962_0.34261701322943017"},"_hasShrinkwrap":false},"0.0.19-alpha.2":{"name":"import-remote","version":"0.0.19-alpha.2","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"36436ec7bfd6faac335147841e7499b7fdf27f9f","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19-alpha.2","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-PIRD5SBUxrPC4c7LIi5pXdhOozRUa4w1wIotWKmipvrZDID313tZNzGuNXwJpMXhjpp7BaXvCA7LlR0WhMkKtg==","shasum":"5b0075cc09a218825b9788ec83d93905ff4d0307","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.2.tgz","fileCount":52,"unpackedSize":1007310,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCtc/fCnyAcXWX1zOu1hl9qEHcxbRgF4TBQKxAOiCDwKQIhAPycHWzLCA8PVwlgM/pmnlw977hwL1xfE4kKbILcMoU2"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJix6jIACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmok/A//WqeXIx3ZENi14+nOqzqXjUDSpOVN8rPbddPCRIfyUWZc/vDw\r\n5DP58JpSQuRqF0kQsW1gBLvZR1vmEosDtSmCo/tHyBOD0gyCUruFuqk8h5hz\r\nKaynSC7LaZBz6wncAhdcNeGQSPsx6cdgijZb/uRdZuqRrrA5bFQV1UQ88k9h\r\n/6tp/98WvNVdwvS5Qf2heDqM1inxEgLLeGtd0bGLL/Aq8+Ho3xGdEHU73zOq\r\n9oQwc9m+RW5W9seWUKiklyWVimQkPLgfLUHcUtn8qtSuvjFQlcX2L1bFsdtB\r\nSRz5vXOS5xre6xGcZf4LTBZBFgKTcVc5NNikrewVAanhwsBknVWVP6yFEQli\r\ngFldiugbwWZaVAkjYbeOh1i4pmHsJhLH9f3+A+LPyZUHylSlwhu1HU/Yo6cu\r\nXLiKciioi8oP4CJsLUQ/hb/sbBHBnXZPh1Jf+RaGgdrjtqiCGyC1VmmF6DsY\r\nB1vq7CuQkaCgnTORuj9JfL7hybe2GQIZYvyu/gWJoINve0+qOQ7pzhZ4SjqK\r\nxW/GfoPNlAEg8TmUKZn00SvaE2REzuLLuP41rfIcorCuLD8AEYAOL65Mqn9U\r\nXSbE0ezT91XpzHKfFsqW+ZTmwscTReEeFi38ouQngdM+gQ6stWKe6EctJcz6\r\nbAqaECCAEasXlxVsCuQYk8dfG2xYC2l7AY4=\r\n=OMm0\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.2_1657252039975_0.39879222878224585"},"_hasShrinkwrap":false},"0.0.19-alpha.3":{"name":"import-remote","version":"0.0.19-alpha.3","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"60b0f83570a4641dffd0b5ea17fb11f2469a1f56","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19-alpha.3","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-6FjjMf504S2bacyXvRq8ZlnI+coVUCGq0Rqbumz79XSVUJRnSBvTgMn0CJIR9UkGgWCOXLuDrFUzRn0AuburLA==","shasum":"063f4bdb6d2fb22b0322dd71a67f3d17cb5f975c","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.3.tgz","fileCount":54,"unpackedSize":1045671,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCh3f6oRSTTwONXdIHNWwria+eiWCipgtkFUi+KDFBROwIhAMlDt7eQcpKQD/VP3R2qYyMRlGCpAI6lp794C/gm+Yp7"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi/dzVACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrBuA/9Fr2KnsZS9PcuNwtg5mKPQYG8R1OAPMeARfHPWLpTSb8r/Ln7\r\nyWOcgVMSMxYALa2DlvWlnJvGZip+cSIO0JD1hPTI0n/6yqa/3DAmgrMA9yjA\r\nBNcadjqHwRJvVxVlDuuMylhuydq7l8MoDy/uqQBQwwPwuH9KPUaQms9pOaK/\r\nqWbt5NLub773vHDDrEA+MMHwYd4mDF250AMT5V35rsFJCpeNZbaGbp3eyU68\r\n91lz1akH2ZARSnMkS8Ndj6idsacZxGhAyBWV9HU4F3nMiY80kn3P3/zpYBd7\r\nIvL2dijpDhaw01/QObwVRmc5Wb7hp8JQ3Dzpjhyr3hv7G+Yu3mU+olmIQh8y\r\nQqv6wi8G/Qv965E+e4bLoiKgobG2kxvUtHzek9h8v3Krjm4QYNzbBqppbHu3\r\nGbTRgGEuZtutoP28e1oPXASKmtn0nyJSgZJq/FKABEvkJnbNNo+q7AVhWT1w\r\nAGtTIr+qXpE8TPa6Y4WCWpo3Iux2+TLuzHhcXI2NvwmCf6eh0GMfacTwX7vW\r\nXNUGDrCZgD2rBzfqTaECoZJ7D0eng8yfiLBV/2JOnrcd2QX7Q8xydPRNkbll\r\nHbHQO4bcNYqgfF2R9LTr0LjfM/JsokGN3/UeR+eD/ZHaMLcrpHuEAN1La8kM\r\ndELoWLQzAev7+fAHD1Xc8wb978bUOWuL+Ho=\r\n=EnYe\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.3_1660804308915_0.09334035604374225"},"_hasShrinkwrap":false},"0.0.19-alpha.4":{"name":"import-remote","version":"0.0.19-alpha.4","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"e6e3fb50f94c0898bbefe5c6052d6b20b9b6fc1d","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19-alpha.4","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-L3LT6q70MbcGKmU1o1NVfeFqshi3soZwwJ8UVkb5nodxXUpIbt3Xr7LIxXDH7uxRp7P8/8iOJDydW0IIIGoyIQ==","shasum":"95a2bc9cef4006fcef0b66bc66b1e49ac38325da","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.4.tgz","fileCount":56,"unpackedSize":1066424,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHXAH4Am3hWPUSftmppQeyhK5SFSOOXxfyOEvD0/ubezAiBSieybFy/afyW6b1oIMd43elz+s4poTE/72ObaD1Z7xQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjEWVBACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmr2Rw/6AoTOsTByiklMuB/ft452h0xgJxpPu8RiWwc2VmECuEhZfREs\r\n0a2N+dpR06dauo+VQ6bFSwxDT+QRlyed0P772oS+xkTz/WLAoSw8UERO6v8z\r\nhUWGC9X5fNGP5aRrvW99X8LRce6+5UCRImX9z7L32I1462AzX61x9OrZOjLs\r\n6rs3MnzDuMiDEYuEPydF2vbzqY34ofPC77WfkO1F+VTbKs4hVzkjLTvjeHns\r\nRr1ZUW5OvesgSZj9eDlVrVnK+55RZIqv9xFob54Nbfv/wa8f7GgQNcj2XWXW\r\nK0no74AvMiQQEXzr17tioI/qXgoajRnev5l+/5G7sg94OS1mB2Z0rMnZu3Bi\r\nB8XG3vnEHxJWxay2SK6d2gG33D4q48/roWDzDB7NDFtrCgfzUeLktVjYzfQa\r\nvequw7fhbWe1k5B+GOHKRbGZWXA3xqEAnMALYh96HecNeO8Kv0jMNE20KUAS\r\n8H8L74aARyc8o746chb9v4cZ2sg+ITclv4sYRChT8rk0zmyML6MKUndEDLHX\r\nT6Cf95wT8BuslUzptplJ460fJLvvFiOaKPhd90jjPW8fOKvAji9A2VXpjqDG\r\n7Kb3MUlaHWv3JHX7AbBRVFNOuAA5wHpDkMINDWLgKjU5UmrS3HtYfLKv/h8M\r\nKQzWkQhzVQTZZ++XMhpr83aB07906Oy95JA=\r\n=62Fp\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.4_1662084417190_0.5100949511155908"},"_hasShrinkwrap":false},"0.0.19-alpha.5":{"name":"import-remote","version":"0.0.19-alpha.5","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"fefb66222dd84e6adb2e8bf4a6540f038024c7ea","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19-alpha.5","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-2SVzPIgFpkdUOZm5WupduX0HjBofuzzXTHVGhUXDTkktaR9tNfA9SOXbPP/Mqk/skxckbhAFKOPvL6TDXhb2sw==","shasum":"58e4e0c7ddb4c2888b7d47b8cd4e58c4d5084981","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.5.tgz","fileCount":56,"unpackedSize":1068927,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDWX4N+V+j6ffsnrD85b/kIGn2DP9ez4c6alQC5Tiu4RgIgaW3B0TThzPZkdf0hn9tX+OzM43piSTDFFeTsZsPUAeU="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjFxRqACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq9eA/6AsOjiDOvtRo9RQFUlzz7y789Gg4owMKWWPLiA2lJFVE8hqzy\r\nkiwNPSVga6DBBbRION5PIYnntVRm0y5iLosbLH/EKdV0q7EgPflopxZxl7j5\r\nNexJCiRtoCt0vkSXCxpPKKas4oyoso9A9BeZtutfJT3pL5RKCtP+oqK1X8eE\r\nuZlrx9ASTQQRcya5ZF9IDrvrDtSIrUlxqLHVzGOJcCmU0CTdP+Ml0gygiZVu\r\noTJCuPy9GhRd3Z2NXfNH24b+AmGr5mkR58gD+fGBdhjsTn1h31R79MQnKCoa\r\nGJnLAa5Kji9jFGK6XzVBOdpI5aHI1idyZbyPOkRJkGGkONsP2AGjz3zzMB0d\r\nQHV6POAfS60HnXYhCqt4ZYE8GW/mYBP8n+u1svDx6y6A40snAcm8Oizz91ZC\r\nWdvHGMON+ewtrAXpyA07KzdNikKghwtWrIDvwuAzqPZ2NXBrzXX+fiyKqdWa\r\npvgoz4DCuyZG43EpK0QZD/Xvq2D/rlr7llKwPVZeABoUM5H/ovGTGo57J3yb\r\nE5iM3Ur+WqKO/saoxHfmXlZce3AktObd885eY5hjA5hiOpbS0gUi8rbTYUh8\r\nIa117hUSebd0VTuF27+0jDh+fOu+x3Lob5XOEhPcJ4RX3WaT2X4S1uhJSNC1\r\nbwN3cVvYzcRQf7LDpTiItdsCbU4DEFhu0G4=\r\n=f3+v\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.5_1662456938069_0.505353703505822"},"_hasShrinkwrap":false},"0.0.19-alpha.6":{"name":"import-remote","version":"0.0.19-alpha.6","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`__windowProxy__.globals.app`、`__windowProxy__.globals.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"fefb66222dd84e6adb2e8bf4a6540f038024c7ea","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19-alpha.6","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-lJebtHmM9iVj8MHusdJ0AMKRa+8vlb1PAuir/n1W0er94IKpASs5kEvrQ/EmlO9+YQIgnQLEuEqWI70ye/+weg==","shasum":"6327f0174ae5ef4eea1eaf1e0daf8540509006a2","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19-alpha.6.tgz","fileCount":57,"unpackedSize":1069162,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGykpESS5/7euldlh5zYtuBaDnSvV1P5gKo/hsBzoHpmAiEA/8VGxsZunXDhWZNIl7pnRzy3SxB1mcMdVvVVvdcH/XI="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjNULaACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoC/g/8Dh8ljiEkPiPqYjF20GofwyTCvqnedB4Twzwo62kyiEageE9w\r\nDYLFz4OS/0OZLCjrMzmdp01kX3DB2J0xMCJg89DEdNneqCGLuF4NOgF3m8b3\r\nv59O1SazZhz3Wale9UAaHvW//ReFRAVWs5JZaI1Pdf/R/rQKas4dWBEGLfAR\r\n+CZtnvTyQ6zMczffz+y4Iu2LiyIz9PbpcG86MvspGJGv/rgM3ZYMeC9/0Fu4\r\nHOc3pcXE4dRj4meNgXwpN+94grVKlkrOJ2EADVjsDZlXIoimbK/bPhBJxmQJ\r\nqIJyFEgJvbtSdSFpP+v2hl4YtgZNhqEPNpE9i7tHfkVBKV2KRexkpvjCU3Rt\r\nKqrLbxM+d7r4H+Ho7ZslbfWxVNcYOSruWVXrPnaUHdnYLtDBx0Z2evCn09tx\r\n0ir5/oVATl8Xx97PDxNZaTQ8cs3JvOSwDjcjYoP4IckjcyHivPfDPiUJmiVL\r\nRpfOXJDcUkVw7e83YUXHzR45j4fzLu+Wvx7xalya5lNYxwhM1M39c1v53FOO\r\ndmZ+obMM+dR+ht1H886Q+zXTLlJP+jFpHQLSux8M8YSnFam6bNbNrRTOjbF9\r\nI+ahPlzbhsQdNPExi98vQGOz8Wnpgzy2WH6HH8L0JXrEw94wbEhJb9ZQEMg5\r\nptFAc9ZYISImscsF6E0W13HxzpLAyAcqUek=\r\n=9jsE\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19-alpha.6_1664434906352_0.6378809862799986"},"_hasShrinkwrap":false},"0.0.19":{"name":"import-remote","version":"0.0.19","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"babel src -d esm","build-view":"babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.11.4","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.2.3","@babel/plugin-proposal-class-properties":"^7.5.0","@babel/plugin-proposal-decorators":"^7.4.4","@babel/plugin-proposal-export-default-from":"^7.2.0","@babel/plugin-proposal-export-namespace-from":"^7.2.0","@babel/plugin-proposal-json-strings":"^7.2.0","@babel/plugin-proposal-object-rest-spread":"^7.5.4","@babel/plugin-syntax-dynamic-import":"^7.2.0","@babel/plugin-syntax-import-meta":"^7.2.0","@babel/plugin-syntax-jsx":"^7.2.0","@babel/plugin-transform-arrow-functions":"^7.2.0","@babel/plugin-transform-destructuring":"^7.2.0","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/plugin-transform-parameters":"^7.2.0","@babel/plugin-transform-runtime":"^7.5.5","@babel/plugin-transform-spread":"^7.2.2","@babel/plugin-transform-strict-mode":"^7.2.0","@babel/preset-env":"7.4.5","@babel/preset-react":"^7.0.0","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-eslint":"7.2.3","babel-loader":"^8.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^6.0.1","eslint-config-airbnb-base":"^13.2.0","eslint-config-react-app":"^5.0.1","eslint-import-resolver-webpack":"^0.11.1","eslint-loader":"^2.2.1","eslint-plugin-flowtype":"^4.2.0","eslint-plugin-import":"^2.18.0","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-react":"^7.14.3","eslint-plugin-react-hooks":"^1.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"0.6.0","typescript":"3.5.2","webpack":"5.4.0","webpack-bundle-analyzer":"^4.2.0","webpack-cli":"3.3.12"},"peerDependencies":{"webpack":">= 4"},"gitHead":"a4cbd9184fa717d35fb96b2296f43cc561d33290","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.0.19","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-FtOPkKZd5U9fQ8+lcmsSceyMZw8+0bP7092NmVwFqiJwfq4iT3zyROudi3F3TWRiGyXXuuiTPMTTz0tby2DOFg==","shasum":"dade1f9679c435ae99cbf7e28083f96596c6b7a1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.0.19.tgz","fileCount":64,"unpackedSize":1072506,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDw/QzabNxknWtapb6F6vSESEObCShy/g8J5/w0hKB/jwIgZa8TmT/i6ueD36dNUndS5yqTpXbECOD4JymSLt+PH9I="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjabFUACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoAUA/+NbDmSqmNRG5s33ae88ZgtbYWXvAtJVAa9Qj2p0eHB7SzEn1U\r\nxA0m3Scv6+nsO0xwR8sDHGOch/PTUL7L5EQr9v90Ypdm+KfeaWLaMnZOeQdM\r\nonTQxMJypbxzRBi9savU8dIqcNXccWQZvdh22y9JhQTlXQpK17sTO80qAp9p\r\n1PUXZGVzWrohfi13t3EOO1LoRAAQzJMuUIY1jlkI7qDBxQ+IcRnIzhUaGWsV\r\nEMoN1rNxJHm5L5VVs7zT46cm8mxwnkSwScof3aEYdHIxmjOw3XBC0P9YNbJ3\r\nzQ4EnwRhc5n7qWMv6IOBlAsdpnQE+5AY9K00g2nSSg2pce4AeHh63MNbYNqh\r\nr3QkjkWNMR773vH5KJTkcRBF8JoUU5cO8HDr3EQsvQOpZjGAzdi3K22MOzMc\r\n5xXaYKLfNxaxwxu5llSIaCrqBkX2PWOwluENS5NhFYTEWgbikWvF6W/TkLUQ\r\nQhEfQEY6Y1CwdSC/QJzBhfVTzAUp98bH5CP39peMXf4sPcNL2n1BvAapMIjy\r\n0TtAMJSJ1NcWUTwhol5asaT3EuTPMHS+zvrf5HI1d92Sc8XYSgQ0UvTj5yMn\r\n3LaNz1CuF0RBmudG2Hon+xMYr6Wo0qQt70uEGUznEfFjEicmBA540hs48VlT\r\nMCCkJAq2Qg8L8bAIwzFxjXWGbyJGSE6K0vg=\r\n=beo6\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.0.19_1667871060592_0.48612127746112144"},"_hasShrinkwrap":false},"0.1.0-alpha.1":{"name":"import-remote","version":"0.1.0-alpha.1","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.2.3","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"a4cbd9184fa717d35fb96b2296f43cc561d33290","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.1","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-pwyUsELb78Lfgrjx09hTZEgVzd0VicITqNKIveTRnUG6IKl3AZuZuOYB/biKxljuYEwLY1BxRoRfjqHJ9Dk5vw==","shasum":"5ff399dd11595a3fd02c72aa8a8bdab0d96f5cba","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.1.tgz","fileCount":64,"unpackedSize":1052724,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDwPzSbz5HuS3gw2u5+eeinL7Mnu5xwkxgPhWxSur/UnAiAXudbcQPV7VPf2V2q7HQHo6o6Lq0nYHF10BfQ1M7z+HQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjfZH8ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqmAQ/9FrJ4HUPIvGcuKjnaS7NIRe4bcaetfFnRx+nBKb3n4tpZLnTg\r\nTuj7XTn21AT20/O39ZjWMB6b71e7oq1TYSX6Af7xBzk1tTLnnRDFSMNqlR5x\r\nec4DamVkowqBk6BOl1v6ekvx6o/iU8qu9hKjAUgRGMBp0oEtZHpQkefkMHvE\r\npowv/HhVATxhonlHQltcq/u9vKVJA5LZMspLqk6/Oiy8tKtw0AE5J9UP2IQT\r\n3VpTj8KhoaFlOSCNhfx5xnCkArl0d70QLWEQrjBMZrGTZbYjX6XcjJiS5yb1\r\nU4DAGaNW4o9/OTIbPeG3YUYHXzeTMjheFI0NEU4lqovYs3tI5difuKATRUn/\r\nESbbNbAQ7cHrZG5LxXVBlZddw4NbE4AVCnNSea4QtVY7fQCgCNoJmYLZSHQJ\r\nuP4t8h6K6BYgPqjO5fo9ZRdNT/MKzZUtcBtSkbcK78YvvSyk8zXwDFozNQiE\r\nUe+dlRTCso3GoCoJUJzBRbnwiCgVvci8qMs6CdV06su8KaZy2mcmYb0wQPb2\r\n2pLR8G+XvFgXt4PunqCJTmTkOmD4S/jvl3SGFHboaVC7CbRrCprM4imEEPeF\r\nND/CvN1xgeSXAMHUNWotGDWRvdasC/e6dRoQcYY4+TVOUFaw+m6PxmznU+J+\r\nfs5lbXA4OmahTYbgX4kn40CGKOZjlT2Us2c=\r\n=QpQL\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.1_1669173756631_0.8359377080304098"},"_hasShrinkwrap":false},"0.1.0-alpha.2":{"name":"import-remote","version":"0.1.0-alpha.2","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.4.1","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"494de8ea3f7d8352b361758919352751897530af","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.2","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-Xhh0u/NeLwji4qf9f9YFRCIWYPfimhA9NGGQxh66XJwiRU5hTrV30QcPLxqNupjzacwS5Zo/IWw6lT4COPckEA==","shasum":"7e6604ac257b583cb8165f525f0ee30ac1c753b1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.2.tgz","fileCount":64,"unpackedSize":1053877,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFH1LTRL3gLI/6DRaoGNd/2YsEBQOvBj07gXgl8JNoJ0AiASKoMTO2/jLiXv5rXnU9bu4rUEMGi2MArU9obHmX419Q=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjfbi9ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp2GA//SG4UfvyVOq1Ez/Kk1lvLtGgnhKk8RHr3Ofat3GVn446XyQky\r\ny0vAw2vm5hInIbw+uMNj1ouuWOvi6kfpX9zfX9mer/vb89r4ghCHMyNG0KPg\r\nb8Duxrvnb/hL9IDVX+rsRDSfNQZOOfrwpWSjjAYoXigt/XPi7rCVmZnsXrZs\r\nQQ3/3sArJcwi3Rt6k9Cw2xgDO4FjZDREpYYdovzFIdKNDr5DBRwZFxfidHwq\r\n613tKfhGnEo53ZYepMxZ4S42aiDfFU2YrH0DiBW9S4ns5Be3UtWCNumDCkz+\r\n04UesnR9lRu2vKBd6sd+OsrCltYOFfEL2ySbKXooVC7Lzz+JwEzUzr1F4x09\r\nJVYp3FaOxKnMa8F9gZB+oiajJ0X1sJhFfhoaNBC6tbJCUhOYMcVIQPlTKa+v\r\nRBDG+CZI47VNNta8TCN6uUdCCLOz4ZVXbP7fbzzzs7XRjpp2Xze5phIqkGTU\r\nBef++d37Q+y0I0P5S4XSV8jV03sGq6P9NbgtTiV91ik/CYTeV65MlH4uJxxK\r\nv7bPlX7n//FUz7Vis9JDW1aaR6vW1b8Pe502Hr6ug81mffThvMLi+rB6z3H1\r\nimdtv35LOz6J+BMGG+8/rYed5Br8AI6eSolBhvKO7xffDeeSYeq7eGUXKgyH\r\nR+zbx/evq9w9tqhppHQ7Z1+9Rw7RO6U6hag=\r\n=wxdC\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.2_1669183677416_0.33801162610392677"},"_hasShrinkwrap":false},"0.1.0-alpha.3":{"name":"import-remote","version":"0.1.0-alpha.3","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.4.1","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"65790729f73f153e7fedbccf101b5c7f3c9b4233","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.3","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-OXHNx6IlbjzXeDAGISmlfQQnbv/Ko1F9BNFzgydjYkZh6P9qR09IPawifzEp0ygKnEsiR60aylEuqYKBpB0PhQ==","shasum":"8d713f68396f1b318bba0c423d10bfc6331cf56a","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.3.tgz","fileCount":64,"unpackedSize":1054378,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDcoJoe1RpKSsp32f0tS8e4IvytPPgdpCWjny8cbRIcUAIgRhUgtaqk9fuq4sBpESj+6eIdOrZpXD/siGFx3CCLBGU="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjfcc4ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrjmg/7BDxkfH3W1Y9Ll6xOUcau4sjDy/NA6N235F6+NBTLRRt0QN9D\r\nUsMFUqpZcE1Dlon2vyWAIKKh8KlcWX/Uy844QQVJz2EG2G+XrmyYs8UjHq09\r\n2RHlwiFyRF3WJO4od6TSdDUY8Dd7im4DR39eXL6BG4GdSzH6ZvYwPQ4Nuicw\r\n/uR/uFUaPUMRV539vqthRymGtO0ITep0e4vGjI8RYDPZHp/LVWuphBGtW+Tz\r\nYWmRqNEkAL/un1oJUlCfUqIyFlkEbf/1EoBAALItIavCsgYX6ChcnZOoTIdZ\r\n76XG6YOYpGo+SRsWCY6I2vdYUDoapvVhxYPEzyEQDCuur1CdEx4dVrrA+OPy\r\nN7vVdSxvZ1gRnkIO4uwVY6gz0fCLAj7baWVFSIRVuD25dVqgBX02yRMBb2sZ\r\nPSEIjcPEd/zBy3baWLKULUNUnnuTI1Z6Adl3iNDO7iA7baxaxnJ4TPyQkHWM\r\nYRwC8/gxxBRbfIhtk9QLAFsbLcfYAF7Kxw6tweig50XPPu/k9Wk2MIiCnIK+\r\nZsdd9O5+ULJmZZMe/twcy7cjFx5ncXnyWDFz9RGLTw5H4nP/3eiZbRROv4lF\r\nyQjZlke8jQiJ8yJ1521oOjGaafqZDtDOmLa04KxD2Y0tK5evn/pvbD4+CzEX\r\nvPJrkmZHOMB7aOmu9jiEQItkwRjijPNn6+I=\r\n=PNjZ\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.3_1669187384203_0.7940915084096503"},"_hasShrinkwrap":false},"0.1.0-alpha.4":{"name":"import-remote","version":"0.1.0-alpha.4","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.4.1","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.3","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"gitHead":"c08fe3d0de03b4ca887b24bb275d150ad851ad52","readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.4","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-XvvGCz79T3PKNgNv9ej9MKNi5qayXrSYPod7gkpyaaWewtvNTWgDQRoyy4t/f3MOrtgr83crwPxMGZBvqoamYA==","shasum":"2c0ab0199ce80162b485b935b3f833f182a551fb","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.4.tgz","fileCount":64,"unpackedSize":1054596,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCc16nr4bRR1YwflP9umnm2gIfsS8USSkSfDTrB/Om89QIhAOUMnQX8w48Qz/gzWzBQn4Ltnyp9RIewcHLbyp7EYdn9"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjfz2jACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqSnQ//dUfloEpexZmd7U37plQqsYYBxdmEWlqS7LW8LQ5QhvN87HL1\r\n+RikhcZAgxm65yUpiGoId85rOlElvlHIG1mE6GsCDOHBBdUF+rZ3N+Tvk/sd\r\nHS2bMBMNwUa4oEr5LJuSkaNAnEqqM6FxQ4YxGEzRbZbeEN6j4xGC9y6qTOlR\r\nIm088VZ/2Twgj/FQ/bWLofffQGG2bzaBsohojh8kF4bvRauqjYMc95bPfD5i\r\nPt6rv9FiN5owynCPsQQGgMX+qpC6e53mLp4+In4fJGf5NmPWuIXicCIt/pWU\r\nMdtATRxt7IH2sTyq5kElE8UwEyROBm/gvXHEPnyjfQ6SmKNuCro0FLUB4Cj2\r\n4P9VPrg8YLPOE73YgwWh3cMWZvQOlHEsrQAeWgImhDyH0peMibtxbfkbDeO3\r\n6klCVwCy3aPXWghKBz6dM7rPcY056GxRPTDL4Yn+MpRUK4T3mpV5ROZZXW8Q\r\nUvaVrKhK7PGD9Rks6Cy6xZXdGKSBj1o8UVCxLlwKb/nFhX9a0jNoWcLddj+O\r\n6QfLqr0SXQdt/J9bZ3tmVLg9BEzuXi/ONIkjQGLBueitgHolZvsSlUEzZqqx\r\nooz3ri5nqjZu80H/fOMip47o65bOHgZFwC90Wk3wNZ4ysFJJZ9wuXNE6gcbq\r\nU7O6z1F1ZzGXhnsMJyA65KrZMLOukYYjjGg=\r\n=0RJS\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.4_1669283235252_0.39939129521964034"},"_hasShrinkwrap":false},"0.1.0-alpha.5":{"name":"import-remote","version":"0.1.0-alpha.5","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^1.4.1","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"2d11be3632c8406ab840fbf46c043015ccf28f85","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.5","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-hJFxu/XSUomzogwi1A9MC5NSQWqK7nD6NeTz7YLRPN4s/wsLCj3NPHxCK29cqADHV8R5xOG39XoqgCu2eC+dmA==","shasum":"837df5d1f1dabc4131df5b00c9c56ec4d9c63073","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.5.tgz","fileCount":65,"unpackedSize":1058031,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIF2HiWYe2HSNNWNzpknCnL5zGLIorDJqA/fT3MDn6mxzAiBsKrbbED0Ng6QJpUeYgJSFDGYGYx7eQGMkxP4pVo7WbQ=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjhCoRACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqppA//QZQ9j4sGQ34jrmGuxZxeSJyloF5DlVeaJqhXhx0Q3E74DumD\r\n/EdhJwMnY0hvemN08QR7WcihRGIvAm+joDoFSL0sZvysX+CGQ9yvGGOxlgSK\r\nGXpDsOf5vGsycu03lc0peYIABHKfn0BIutDwGo1ztOKxG3xMBi3XpNg6PAxQ\r\nQiVDFQrldjwKx8mFvZ6T2/JR16OEvFiJxAWnlznTaPuxeW0fL28vhEhuonUa\r\n327A4qyC2aylrZj8u3CD6+9f2WTXNWjCSAAAsxYT6JbZL59+LhhwHLsqFYx9\r\nL7D+6v//jdjV2aJqnRgOa2iLjPUrYIEg+HOb6KKaEU4ASJwWpmie7k4nWJen\r\nRLVLUpouo3MsSQZ/BgLNB2G3dlbgvy2V4enuZYyfSIfnfe6RftOaWJEeQCkk\r\nLlQKzjwQbqKozRvVUjJtmT/iB41kPHH6M2xtHWDB870bZNscXwYXjtKcNycK\r\n61n3CiU0XBMwFeGHLdiFefsnJ9J64DhMUAOzzHbdmtD6xNj5pJfaHb+k1HPp\r\nUC44JN+MoSlGnF4Jn9miXragbKh7sCz4FCe8qnD0QU8B2G0x4yBBl/Gd/D+o\r\n9NH5+DzNyFfji+RD52kcYGsCZC2/f9RLuG8A58A3Wf4/E1SO/LJ207yD32Xd\r\nvHVXJNmy0jrUG4b7eDSgvtugZ9GFCTiP/h0=\r\n=myfd\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.5_1669605904991_0.49673614476680106"},"_hasShrinkwrap":false},"0.1.0-alpha.6":{"name":"import-remote","version":"0.1.0-alpha.6","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"74177fcdfbed8480fcbc1fedb816625df9bf4077","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.6","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-j2RkFYUztXqoPyrOXFpdW1vTKJf4MR+6Glq/DPZTsQgCOTF+4EQKP+c/lK7Cnpp21F+GeiMNgarqvH4g+ah5iQ==","shasum":"c3afc30c21393c7cb2a97827705f97fab5bbd142","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.6.tgz","fileCount":65,"unpackedSize":1059622,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCoCLbDbPo22VxK+DxEeo12PglyOEbrfq+di8kKnFrhOwIgMXegfu+Cz3e1Sz4+8YelbsUizjUcy2gjAaiRpq3gsD8="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjkUp4ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqXHBAAivT3nK8LzBku4u9TBKbF8XzBtkr8sxEor8RxqHRXzEvNkFtR\r\nBu99vCk4dWFsAUBsFnDFQtBN6N551QmhapwP+Ok4xv9DtgvhBmRAu/Ldzd37\r\n2YuFG/HA3vdI2je7oTAtyFoilmMdNxLVTuFGAQ/EsRtrgJ0buEuzL/4vY4fo\r\n0nrxmCK5pqyfpXYmG5/P2XU8CfQlNFYmdoABUip7NqUHxZNCSaYM3KLw6xGt\r\nrpzzCXCNZNSM7Dz7ejyKwHCYrvETrKL6iLfKdYsAjUctO6KdksklAAOGv6VL\r\n3SQOZqnch1Ug1hyN1T30dHT0FitEtzKvnu+pZU+2lfQbBVhxMxuYrIPLRTd8\r\ngTNR0fjLVMhtrx9ZdYe5k1l0LdsSVi0xtd22Vt9Nyi194JNcWwx9KFHjqEY9\r\nsun0bzFT8+il+/seK0GLReuH9xdW7ilslAe7eX9+IvuX+A/1WEV+S92V53AW\r\noN+aOplOgIRi/PzG2mIAOosKUneplpjTM5/QddH91oHLt/ElIwCtnekJtTCV\r\ni6kPZijMBWjcXslvMScXrie0mv9TSJZ5iAqydBllQVtUWweBhsXMueVQrwvv\r\nmAumIIalltaJiouZMpT3RmixtETZUZzLbhIOE/9yRAAwhFTlSzQzDsKXWxf6\r\njBLsmX47zaeWd9nlHxb5pwQkBzCPl8BGZ/o=\r\n=zwqK\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.6_1670466168536_0.45614258273360075"},"_hasShrinkwrap":false},"0.1.0-alpha.7":{"name":"import-remote","version":"0.1.0-alpha.7","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^2.0.0","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"74177fcdfbed8480fcbc1fedb816625df9bf4077","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.7","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-vU7/CcRnXtTZPYEkljxzEi0Ttc9bfyaeK2ShdLtQEGA600MRoX9CwzWBIzENmQs6hhmw1lkPJoFAH20ZEs3VJw==","shasum":"134900cb5f1b456fdb7a8143850a4219aa38961b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.7.tgz","fileCount":65,"unpackedSize":1060548,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC5lRBw7oroo6MNgKmui987mHLcmedEUo9LUnr8N83lVAiEA0xanZXk+D7whwJLHc1DWpdfw+ll1ZPXyHPJLaGezh1k="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjkZDgACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoZpg/+LS2j49yCdaS6nc4U80TPVi15qgWukWLjauAQaeF4VssM3rBr\r\n2FQaKUqLJ8gR1YGZ33qt6B/aZ9BbP1moi1f5YeXVuA63H2QjOHwl27co2JaZ\r\nL3Br+TdjTFtCLJ38gZ/kn//B5EN3sumO1VgvFl/vOOPJgL9oR3cbpf3LAYG7\r\nXLgrHHaHoN9py8JSinGlIP5cH50VjW4ZjyFm6hEwE5FSv3t0sgoieQiMqO/e\r\nGTFVfmo5/oYMHvVdiPsCJ5GH2lKP3nW2sKkLF+MMOfRpICE62XU1UqGDX3ty\r\nIxi2t4wU92mBKiR5GFSzm5blR3n6dCcnWnqvPfkVp2qwxuSvjV6HJR1US7+t\r\ndCr3WGORf0ocYXJObpndbgkEAA9eN23LhHlse5jXg0YOB/Tv3435Oz9FAx7q\r\nwP6qrV5Af1C7siFbSay3Fsfo7/k0VAcUns33BkrOa5YHbYRDj48NDgOS0L9+\r\nnyd/K+0kJd/DwHU4Wf5S2UTtFDYrXyvFLXaJP4oslH8UrwVsxACJRwf1xRzI\r\nHPTQPto+/3/BjuLXWgabWPynFcAiB8gNCI8e39cYkIj/YxAG/lKCk6VNuEi2\r\npdHU+xhMc6yc03GG8unJPP9HkwgKFJM4xucVdZ3xi++XCtkXfH7XY272EDOL\r\nE3Wq/RqMTncS3zDCA3BXVdbIJIpJf8SCBrs=\r\n=vKgR\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.7_1670484192490_0.9130484846900533"},"_hasShrinkwrap":false},"0.1.0-alpha.8":{"name":"import-remote","version":"0.1.0-alpha.8","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"36ee43a578589aaf251f35f158ae1bed5c0056e2","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.8","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-10DeQK59t/LuFwNRcw+ZhUcHNugNqb4v012RrcATL1+zDjd5FYVHlkkh0WC3/lTcMvCwku1Qz9a+4McIgJr3bA==","shasum":"e871c0c48c757485d2b6e68e59259671afcac7aa","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.8.tgz","fileCount":65,"unpackedSize":1060587,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDHDQEZGAVfVopGYDwNLrTRFSOO0EGYGOPd44jyjTOPGQIhAJLVhgrihrCWmi8a/nvpCpM7EaSSm9Re8nscGlyS1Hbe"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjmBvDACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoNUQ//dsRA1KS9eztiB4JNqime9mckNqBZPdqk5zMkAHHtfHn/Uj7S\r\nhNx+0frJPlqZHiNJsFF7FSSanb3l7xUkddTmgvf+bQY618v/68ZZlSS1ZrFO\r\nKdpcmlyP/anWW7jXE3AH4t0oc/NYrIS035MhmdmDEPiheny1XTuR64573sHc\r\nwRZXZyM7HchdZ56Bhw4UQNWBuaCTSDwu25COUW2z2vWt+p/aMeSBsDZGdR9t\r\nb5F/P/qiJPdNXxm8ikEi/ZYIos0Unj5bRCyNg14sO9PEujy7kVggy0it5xBj\r\nV4Qq2KL+eL6E/0ZOK+HgAGykAd5dUAA3OZkxBWY4ENK5iIg0nhjmvN9kCUpC\r\nJJ9Yav1n5kWXmRw9ky91TN6rbdxZQr2gMT5HswpJDS88SPUTth4B7xzwXBYG\r\n/8n6nMN6Sln7vaL+yw++rYRqcnSXbmAiDIdtnr3dZFi8qptQp+jDfoMgLhpF\r\n9hwi+g8LOOK+5dq6faV6vij9lo9fdrFUOUVbWAMcvOsLCSETYRuJ6h9D4EBT\r\n58gn7l0mInn9CI6T1iD3qwNxHHElaQDSsH2PApjg2RUeFcOgz/v02/KINzjr\r\nVGonWFXYLKJX1DqikrPok1i4t6+qojZxyHSU0aMEIefTPuE0AF8cuFK9jb/Y\r\nNYudvtNRKWy2x/XZ4injacWegKVtIeo9WoA=\r\n=sJh1\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.8_1670912963119_0.7215555038334902"},"_hasShrinkwrap":false},"0.1.0-alpha.9":{"name":"import-remote","version":"0.1.0-alpha.9","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"36ee43a578589aaf251f35f158ae1bed5c0056e2","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.9","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-zOsHEFM5DAFdvmm9nuDe9uVhafI5rEzIq5LjpVmQ0RlZqN6N6Y54QkWdIhwxpWPl7P8dCpcq+V5qQWbSXNXx7A==","shasum":"0044d048272116676094b58550c956764516eafd","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.9.tgz","fileCount":65,"unpackedSize":1064040,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBgEb3Wu+I1IloHcsAs1JeM2MXLt5aUz/erV6Jyz62XBAiEAh2TRKRxdauDbvsKYBAzQkHRjHJIOAdTY2xau5W7pSt0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj7K9CACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpAsw/+M6nX0iBWVlvlv7MYq0VMdrDSEXaMIqKrAFO/RpNvpsNr7rV+\r\ny/urAUWZC9WM8BWPxIHrV8KZmj5I79byZmL4WLl5TJHawDkyAPCp6DsMkxE+\r\nnMObKCX8QUKOkjW+5vbToEokB0Wje1YTJ/87ZVJbs9VZWDzqKoqKyxpp8C9t\r\nLN/WgF2Zc6UayXusof8KUytrhhNbHxYnET08FdxSoh0761miu+DdT9B4q9lb\r\nfeR6OJnnJcKvc9mLnSA1hK+Hf+ko51SF61wMnOVe94g5LEp3tFJmDX+gct/G\r\nqV+/wCBvD4Xb72oQbMBcCDxJjk1e5FsKbzWdvXpIFqqZHfFwoiXhNHqEaW6z\r\numFSouQCvdFKUcg3CzaKewpMDKzIwT3RCJrXMqWZa/4MBhENL/wAjnGOlpM+\r\nFCp02Ldp0W6H3CZC+FMjzBDApPzuL4/hMa7EoLxo/GTrrDeCN+OLfH93DBXq\r\nqejjDiIKle4UdarzdLJgxJnJV1Lb4dGEpf95l6FlOnJpEzZ+7f/JtC9fw3rm\r\nk6t/iGDCVhft7tP5gMbdAQb6BvlF57WRakYPELWTip6aMTFJqM9cc7DHFagb\r\n8z5n7AFY8pFVh6wQ2hAKxVrtewKYOIkHDDXi7kQBB3TcspNtqf5rl9zTJRV7\r\n/1lyZ7tIIJvIeLAdktGzQF8lbJdhhfOPOvc=\r\n=pX26\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.9_1676455746398_0.10056092498196012"},"_hasShrinkwrap":false},"0.1.0-alpha.10":{"name":"import-remote","version":"0.1.0-alpha.10","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`(gulp、vite等)也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"4146d8a240d7d587a3391318877f0b90f38d1804","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0-alpha.10","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-XVNmVnEXSNk77YzwQ9zIzWASeAtUGTop1+rQrdNCilN255yoztIwx8EbL1wHNm0HoUH+cWqu/L74Qp80MiCezw==","shasum":"10aa8e9401dac5a1448c58d08d32072632a8a9c3","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0-alpha.10.tgz","fileCount":65,"unpackedSize":1064188,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBYzlTNIATieHF5YfA8w1KSYzuH3zgavSsjqQUF/qVNxAiB52mtF5BTRuC7426vdvcIcmZ9scFWlROnBiXyYobZStg=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkGmyHACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrbmw//YjsnDRFvYyDERyyyLSvG6FtDBEE/bhuIcUkKD4t/f416Q8je\r\nvW/urSxNuJ6kOj1HHuclYyIt4p+Dg//FJc+/LGpBP6AkGom2TLbdE5xUl8f6\r\nQJOSVs4LTCm1WqNj+yvJ4Y700W5Rff7q8tnA31MyXcfRE2NhEuGIpBCGCf7Y\r\nnf6KH6wmnOdAMEefHrv0rzZPYQwrBSdL4AwaMcM+ZkkUB7umUilfbCnvT8DJ\r\nQIWXJdlECoDnxmnOyhMVc92mroCCTEuG5JxveBABDMCyx9i4s8fd36obEenN\r\nGxFHYsgNhZetpparDaFgoTM+6UApCNwVYpl5fZmtqg3NFv8TDpLnT2oB2NaG\r\nJWsbY2V2sx6AcQsAWxqEV8QsP07ivV3vyM+JACAR2aAL8MbbOaGl+x1UA1da\r\nwV4ZV8lISA7Ut2Y002x78nOTki08lJ8i3aYGC1yyHDSJBdRc+4HD3fmtDddb\r\nVhZ6o+n7JgtCQgMT3Gi0sfk6DZq8h1T7zFZyW0xBrCapeGIreM+IKg/IUQt8\r\nXrSlcHLEQVJyJyJq3IYDXe1VC0dwXtUQmksakRu4WXdDIf3EAsoNg5lZ1K2j\r\n6lKvDnqY2nLk1tmNiNAE1HHtuyFhngjTjxOh8KYIh7iHQAOAd7dw21y0SMbb\r\ntSOz265DoE4VCLaTTZJgOK2DvbhlDQqFMgY=\r\n=sJ1/\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0-alpha.10_1679453319626_0.8905625903945635"},"_hasShrinkwrap":false},"0.1.0":{"name":"import-remote","version":"0.1.0","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"gitHead":"600d41507c2f7a647a3431e45802cdf6040d2e08","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.0","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-6ZnIUW+A+t665CiTaH/DqCYa3bTylAeXNgCcpmqvOPZkPRtARWQc87YcrP/Az7kmiWv9Q9yOjBoqR84O5L4hWQ==","shasum":"7dec45c0481b4056b677df3b6525adea3ed3fae1","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.0.tgz","fileCount":65,"unpackedSize":1064151,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEz+U3C54b9QIwiC8rz/Vob7H/gq1TAUkrUjIJL3lRZnAiEAo0MQ5CrE/krvpv5od5Mo0SkNX3L42iA1bh8ci6xrWH8="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.0_1686618114999_0.29300453849285324"},"_hasShrinkwrap":false},"0.1.1-alpha.0":{"name":"import-remote","version":"0.1.1-alpha.0","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`(gulp、vite等)也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"7b1597109d01163a73ff2f77350e3b07040743c3","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.1-alpha.0","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-C3SyZsf0jJjoudtDZ348aZ9J5XwhvWYVZRvGA/I1sa+/V3sipnRLzsireF2OklsEbwUgRCpMiWq24cODucVwJA==","shasum":"80caf842a215f5676da5796b64b81635fdf3ade6","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.1-alpha.0.tgz","fileCount":65,"unpackedSize":1064252,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGELCBac1L5AUTGx1gIzY0CTHwTXb5abWBLJcFpy2FPpAiEAjqma3XMk4Y2Sh7GQT1oZjg3EJTDc8MfU8ooSMUKaqec="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.1-alpha.0_1687659501779_0.08024515391735765"},"_hasShrinkwrap":false},"0.1.1-alpha.1":{"name":"import-remote","version":"0.1.1-alpha.1","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`(gulp、vite等)也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"f92b365dfc7872671bb50bc54e872dfeed0c5045","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.1-alpha.1","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-RJC/68dDT30VTQpbdYoe/bmhn/TsmH78iuo07XtyWdbmWwyaPSEF7DDcQYYeErxj05UZFKgQyEFJhiiOU1CouA==","shasum":"bb9c976494f4c3eaee4e85b8654c895b1261f51b","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.1-alpha.1.tgz","fileCount":65,"unpackedSize":1064328,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIClY6CAaoVnTHXooa/cgsp31AI9dUP0sWYTaNTbeStKfAiAes60Gt7rhX5tgQYA2ffux5ntZPhmwxBbsqTqqX5oDcA=="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.1-alpha.1_1692096803864_0.24307402017123803"},"_hasShrinkwrap":false},"0.1.1-alpha.2":{"name":"import-remote","version":"0.1.1-alpha.2","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`(gulp、vite等)也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","gitHead":"65f9310aa55c5456820bef462ee84765b2ca1267","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.1-alpha.2","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-iPTWfGy+68RLzENTRWJ4QEm/dZtapydl1mjxNCT/wpOYbHUtZ+nE1HykorKCaZ+b5pkVmLZPr9EFemlglZGQcg==","shasum":"341fccd2a2712c0ec57a134382e92084c97e8951","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.1-alpha.2.tgz","fileCount":65,"unpackedSize":1065442,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICKNeGM+qEW2YxvJ7pWfc+ZT8YgsG9w5nnk6B/nXRoJJAiEAiVjOzLMjwUgNfqHYSqrabsqtYYHYByo78c2oNAMNllA="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.1-alpha.2_1703036681225_0.3181673025906704"},"_hasShrinkwrap":false},"0.1.1":{"name":"import-remote","version":"0.1.1","main":"esm/index.js","module":"esm/index.js","typings":"types/index.d.ts","keywords":["import","remote","module","import-remote","micro-frontend"],"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"homepage":"https://github.com/gxlmyacc/import-remote","repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"engines":{"node":">=8.9"},"scripts":{"webpack":"webpack --config ./build/webpack.config.js --progress","build-src":"cross-env NODE_ENV=production LIBRARY_MODE=true babel src -d esm","build-view":"cross-env NODE_ENV=production LIBRARY_MODE=true babel view/src -d view/esm","build":"npm run webpack && npm run build-src && npm run build-view","prepare":"npm run build"},"author":{"name":"gxlmyacc"},"dependencies":{"@babel/core":"^7.20.2","base-64":"^1.0.0","core-js":"^2","escape-string-regexp":"^1.0.5","find-up":"^4.1.0","hash-sum":"^2.0.0","indexed-db-async":"^0.0.2","loader-utils":"^2.0.4","lodash":"^4.17.21","pretty-error":"^2.1.1","tapable":"^1.1.3","util.promisify":"1.0.0"},"devDependencies":{"@babel/cli":"^7.19.3","@babel/eslint-parser":"^7.19.1","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-decorators":"^7.20.2","@babel/plugin-syntax-dynamic-import":"^7.8.3","@babel/plugin-syntax-jsx":"^7.18.6","@babel/plugin-transform-runtime":"^7.19.6","@babel/preset-env":"7.20.2","@babel/preset-react":"^7.18.6","@types/loader-utils":"1.1.6","@types/node":"11.13.9","@types/tapable":"1.0.4","babel-loader":"^9.1.0","babel-plugin-define-variables":"^0.0.3","cross-env":"^5.2.0","dir-compare":"1.7.2","eslint":"^8.28.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-react-app":"^7.0.1","eslint-import-resolver-webpack":"^0.13.2","eslint-loader":"^4.0.2","eslint-plugin-flowtype":"^8.0.3","eslint-plugin-import":"^2.26.0","eslint-plugin-jsx-a11y":"^6.6.1","eslint-plugin-react":"^7.31.11","eslint-plugin-react-hooks":"^4.6.0","file-loader":"^6.2.0","mini-css-extract-plugin":"2.7.0","typescript":"4.9.3","webpack":"^5.75.0","webpack-bundle-analyzer":"^4.7.0","webpack-cli":"^5.0.0"},"peerDependencies":{"webpack":">= 4"},"gitHead":"0a629388bea0b0408a609d4d64e5266a30161cd7","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。","_id":"import-remote@0.1.1","_nodeVersion":"16.14.1","_npmVersion":"8.5.0","dist":{"integrity":"sha512-FMb0cZgExgS6V/erhKnlo3OaF/lzUlTSkpE9DAcmrB95leclCneoCroGDwtsWYwsGMiuNlfB0P7bgnjg6op8Mg==","shasum":"7dcc29abc1a6e22e904dee1516dbc24f8dfbb2fd","tarball":"https://registry.npmjs.org/import-remote/-/import-remote-0.1.1.tgz","fileCount":65,"unpackedSize":1065884,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFqDoVgLm+Esd9U4MoU1sdLWte+zS7i8PyWr1i/l9cCDAiEArMgQZuhImt/hfyFrSB3tv+vArnMmMyu3HYpgJn/R/n4="}]},"_npmUser":{"name":"gxlmyacc","email":"gxl.mymail@163.com"},"directories":{},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-remote_0.1.1_1710924767659_0.6216601049861077"},"_hasShrinkwrap":false}},"time":{"created":"2020-06-02T09:20:56.319Z","0.0.1":"2020-06-02T09:20:56.482Z","modified":"2024-03-20T08:52:48.784Z","0.0.2":"2020-06-03T11:08:02.286Z","0.0.3":"2020-06-06T15:39:13.590Z","0.0.4-alpha.0":"2020-06-07T06:05:30.138Z","0.0.4-alpha.1":"2020-06-07T11:03:51.954Z","0.0.4-alpha.2":"2020-06-08T01:45:29.290Z","0.0.4-alpha.3":"2020-06-11T07:20:46.784Z","0.0.4-alpha.4":"2020-06-11T09:10:48.070Z","0.0.4-alpha.5":"2020-06-14T09:18:02.323Z","0.0.4":"2020-06-17T06:00:49.467Z","0.0.5":"2020-06-19T08:50:06.115Z","0.0.6":"2020-06-20T04:49:05.810Z","0.0.7-alpha.0":"2020-06-20T04:51:51.665Z","0.0.7-alpha.1":"2020-06-28T03:22:22.045Z","0.0.7-alpha.2":"2020-06-28T05:51:09.648Z","0.0.7-alpha.3":"2020-06-29T06:10:52.001Z","0.0.7-alpha.4":"2020-07-02T01:11:56.604Z","0.0.7-alpha.5":"2020-07-03T01:21:01.683Z","0.0.7":"2020-07-13T07:55:59.150Z","0.0.8-alpha.0":"2020-07-17T02:25:37.987Z","0.0.8-alpha.1":"2020-07-23T10:57:12.065Z","0.0.9-alpha.0":"2020-08-13T02:49:15.259Z","0.0.9-alpha.1":"2020-08-24T07:29:52.502Z","0.0.9-alpha.2":"2020-09-01T08:44:10.553Z","0.0.9-alpha.3":"2020-09-01T08:51:36.743Z","0.0.9-alpha.4":"2020-09-10T07:15:21.771Z","0.0.9-alpha.5":"2020-09-10T12:08:50.531Z","0.0.9-alpha.6":"2020-09-15T08:23:15.366Z","0.0.9-alpha.7":"2020-09-16T07:09:00.769Z","0.0.9-alpha.8":"2020-10-21T10:06:14.961Z","0.0.9-alpha.9":"2020-10-22T06:52:03.902Z","0.0.9-alpha.10":"2020-10-22T11:45:30.349Z","0.0.9-alpha.11":"2020-10-27T09:01:02.456Z","0.0.9-alpha.12":"2020-10-28T01:46:02.705Z","0.0.9-alpha.13":"2020-10-29T02:48:39.547Z","0.0.9-alpha.14":"2020-10-29T08:53:55.681Z","0.0.9":"2020-10-30T07:39:00.653Z","0.0.10-alpha.0":"2020-11-04T05:52:53.244Z","0.0.10-alpha.1":"2020-11-05T01:41:14.269Z","0.0.10-alpha.2":"2020-11-06T08:26:58.232Z","0.0.10-alpha.3":"2020-11-06T08:55:57.851Z","0.0.10-alpha.4":"2020-11-09T02:58:37.438Z","0.0.10-alpha.5":"2020-11-09T07:51:25.283Z","0.0.10-alpha.6":"2020-11-10T07:44:52.586Z","0.0.10-alpha.7":"2020-11-10T10:09:00.299Z","0.0.10-alpha.8":"2020-11-12T07:36:30.392Z","0.0.10-alpha.9":"2020-11-13T04:11:50.377Z","0.0.10-alpha.10":"2020-11-17T02:21:09.054Z","0.0.10-alpha.11":"2020-11-17T02:47:18.105Z","0.0.10-alpha.12":"2020-11-20T08:15:38.119Z","0.0.10-alpha.13":"2020-11-20T09:26:56.108Z","0.0.10-alpha.14":"2020-11-23T00:58:23.546Z","0.0.10-alpha.15":"2020-11-23T05:52:31.120Z","0.0.10-alpha.16":"2020-11-23T05:55:34.447Z","0.0.10-alpha.17":"2020-11-23T07:52:15.068Z","0.0.10-alpha.18":"2020-11-23T08:33:08.929Z","0.0.10-alpha.19":"2020-11-26T02:08:38.363Z","0.0.10-alpha.20":"2020-11-30T02:14:28.143Z","0.0.10-alpha.21":"2020-11-30T07:23:27.435Z","0.0.10-alpha.22":"2020-11-30T09:00:24.946Z","0.0.10-alpha.23":"2020-12-01T10:33:08.551Z","0.0.10-alpha.24":"2020-12-02T01:10:40.830Z","0.0.10-alpha.25":"2020-12-02T05:43:39.378Z","0.0.10-alpha.26":"2020-12-02T06:25:02.612Z","0.0.10-alpha.27":"2020-12-03T07:31:44.593Z","0.0.10-alpha.28":"2020-12-04T01:29:43.898Z","0.0.10-alpha.29":"2020-12-07T07:53:50.187Z","0.0.10-alpha.30":"2020-12-07T08:48:52.308Z","0.0.10-alpha.31":"2020-12-09T06:37:48.128Z","0.0.10-alpha.32":"2020-12-10T09:12:53.137Z","0.0.10-alpha.33":"2020-12-11T02:47:42.343Z","0.0.10":"2020-12-11T06:16:02.789Z","0.0.11-alpha.0":"2020-12-14T00:57:06.909Z","0.0.11-alpha.1":"2020-12-16T01:22:51.374Z","0.0.11-alpha.2":"2020-12-16T02:06:47.773Z","0.0.11-alpha.3":"2020-12-16T07:57:01.738Z","0.0.11-alpha.4":"2020-12-17T08:56:01.247Z","0.0.11-alpha.5":"2020-12-18T09:29:29.291Z","0.0.11-alpha.6":"2020-12-21T01:22:37.564Z","0.0.11-alpha.7":"2020-12-21T08:54:04.051Z","0.0.11-alpha.8":"2020-12-22T02:36:31.751Z","0.0.11-alpha.9":"2020-12-22T02:43:59.475Z","0.0.11-alpha.10":"2020-12-22T10:34:44.225Z","0.0.11-alpha.11":"2020-12-23T06:33:25.542Z","0.0.11-alpha.12":"2020-12-24T02:53:07.850Z","0.0.11-alpha.13":"2020-12-25T07:12:13.664Z","0.0.11-alpha.14":"2020-12-25T08:33:55.031Z","0.0.11-alpha.15":"2020-12-25T09:19:05.919Z","0.0.11-alpha.16":"2020-12-29T02:37:59.889Z","0.0.11-alpha.17":"2020-12-30T02:27:14.186Z","0.0.11-alpha.18":"2021-01-04T05:44:21.293Z","0.0.11":"2021-01-11T07:15:17.170Z","0.0.12":"2021-01-13T07:59:44.396Z","0.0.13":"2021-01-14T06:13:18.996Z","0.0.14-alpha.0":"2021-01-14T12:47:10.130Z","0.0.14-alpha.1":"2021-01-19T10:09:14.734Z","0.0.14-alpha.2":"2021-01-21T06:48:20.891Z","0.0.14-alpha.3":"2021-01-22T05:52:29.526Z","0.0.14-alpha.4":"2021-01-27T07:39:13.005Z","0.0.14-alpha.5":"2021-02-01T11:52:02.971Z","0.0.14":"2021-02-22T06:36:19.917Z","0.0.15-alpha.0":"2021-03-30T07:29:27.663Z","0.0.15-alpha.1":"2021-03-30T08:58:33.840Z","0.0.15-alpha.2":"2021-03-30T10:15:40.827Z","0.0.15-alpha.3":"2021-03-31T10:00:24.803Z","0.0.15-alpha.4":"2021-04-15T07:08:36.521Z","0.0.15-alpha.5":"2021-04-15T07:25:41.195Z","0.0.15-alpha.6":"2021-04-16T06:27:44.365Z","0.0.15-alpha.7":"2021-04-22T02:52:14.776Z","0.0.15-alpha.8":"2021-04-22T05:48:56.687Z","0.0.15":"2021-04-25T01:42:08.700Z","0.0.16-alpha.0":"2021-05-19T12:53:29.331Z","0.0.16-alpha.1":"2021-05-20T01:09:46.609Z","0.0.16":"2021-05-25T07:49:25.090Z","0.0.17-alpha.0":"2021-06-04T05:49:11.223Z","0.0.17-alpha.1":"2021-06-04T06:11:12.690Z","0.0.17-alpha.2":"2021-06-04T06:48:39.889Z","0.0.17-alpha.4":"2021-06-04T07:22:58.518Z","0.0.17-alpha.5":"2021-06-08T05:52:04.649Z","0.0.17-alpha.6":"2021-07-14T01:10:26.411Z","0.0.17-alpha.7":"2021-07-14T06:03:05.623Z","0.0.17-alpha.8":"2021-07-15T02:48:13.896Z","0.0.17":"2021-07-16T00:57:02.049Z","0.0.18-alpha.0":"2021-08-24T10:31:17.083Z","0.0.18-alpha.1":"2021-09-13T07:22:38.649Z","0.0.18-alpha.2":"2021-09-14T02:24:07.655Z","0.0.18-alpha.3":"2021-09-15T02:00:58.212Z","0.0.18-alpha.4":"2021-11-22T07:41:54.848Z","0.0.18-alpha.5":"2021-11-24T03:23:54.305Z","0.0.18-alpha.6":"2021-11-29T11:52:58.101Z","0.0.18-alpha.7":"2021-12-02T05:55:28.388Z","0.0.18-alpha.8":"2021-12-22T07:50:58.517Z","0.0.18-alpha.9":"2022-01-10T07:26:11.401Z","0.0.18-alpha.10":"2022-01-10T07:43:47.276Z","0.0.18-alpha.11":"2022-01-10T07:54:19.747Z","0.0.18-alpha.12":"2022-01-13T09:18:11.705Z","0.0.18-alpha.13":"2022-02-12T09:41:40.405Z","0.0.18-alpha.14":"2022-02-17T06:04:12.800Z","0.0.18-alpha.15":"2022-03-10T01:41:01.625Z","0.0.18-alpha.16":"2022-03-21T11:37:33.527Z","0.0.18-alpha.17":"2022-03-29T09:24:26.549Z","0.0.18-alpha.18":"2022-03-31T10:08:05.343Z","0.0.18-alpha.19":"2022-04-06T01:55:15.049Z","0.0.18-alpha.20":"2022-04-11T06:34:27.175Z","0.0.18-alpha.21":"2022-04-14T10:37:35.123Z","0.0.18":"2022-04-29T02:32:11.184Z","0.0.19-alpha.1":"2022-07-06T08:24:16.429Z","0.0.19-alpha.2":"2022-07-08T03:47:20.286Z","0.0.19-alpha.3":"2022-08-18T06:31:49.334Z","0.0.19-alpha.4":"2022-09-02T02:06:57.455Z","0.0.19-alpha.5":"2022-09-06T09:35:38.293Z","0.0.19-alpha.6":"2022-09-29T07:01:46.584Z","0.0.19":"2022-11-08T01:31:00.814Z","0.1.0-alpha.1":"2022-11-23T03:22:36.835Z","0.1.0-alpha.2":"2022-11-23T06:07:57.626Z","0.1.0-alpha.3":"2022-11-23T07:09:44.440Z","0.1.0-alpha.4":"2022-11-24T09:47:15.399Z","0.1.0-alpha.5":"2022-11-28T03:25:05.193Z","0.1.0-alpha.6":"2022-12-08T02:22:48.768Z","0.1.0-alpha.7":"2022-12-08T07:23:12.767Z","0.1.0-alpha.8":"2022-12-13T06:29:23.320Z","0.1.0-alpha.9":"2023-02-15T10:09:06.613Z","0.1.0-alpha.10":"2023-03-22T02:48:39.825Z","0.1.0":"2023-06-13T01:01:55.231Z","0.1.1-alpha.0":"2023-06-25T02:18:22.016Z","0.1.1-alpha.1":"2023-08-15T10:53:24.073Z","0.1.1-alpha.2":"2023-12-20T01:44:41.461Z","0.1.1":"2024-03-20T08:52:47.861Z"},"maintainers":[{"name":"gxlmyacc","email":"gxl.mymail@163.com"}],"homepage":"https://github.com/gxlmyacc/import-remote","keywords":["import","remote","module","import-remote","micro-frontend"],"repository":{"type":"git","url":"git+https://github.com/gxlmyacc/import-remote.git"},"author":{"name":"gxlmyacc"},"bugs":{"url":"https://github.com/gxlmyacc/import-remote/issues"},"readme":"# import-remote\n\n一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。\n\n[![NPM version](https://img.shields.io/npm/v/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n[![NPM downloads](https://img.shields.io/npm/dm/import-remote.svg?style=flat)](https://npmjs.com/package/import-remote)\n\n## 说明\n\n它的初衷是：\n\n  - 希望像`html-webpack-plugin`插件生成HTML入口文件那样生成一个JS入口文件；\n\n  - 插件的配置参数基本和`html-webpack-plugin`保持一致，以便只需将`html-webpack-plugin`插件替换为`import-remote/plugin`插件即可在`html`和`JS`入口间切换；\n\n  - 解决JS模块依赖传递的问题：不再是通过`window`变量来传递依赖；\n\n  - 支持自动从公共包中加载依赖；\n\n  - 利于`webpack`的懒加载功能实现公共包中的导出组件按需加载；\n\n  - 提供`global.xxx`、`window.xxx`这些全局变量私有化作用域的功能，避免污染`window`；\n\n  - 为`JS/CSS`动态添加`publicPath`，方便一份打包成果多处部署；\n\n  - `JS/CSS`资源异步获取，避免通过`script`、`link`加载资源导致的同步加载；\n\n  - 支持多入口文件，实现多模块代码/作用域共享;\n\n### 和`html-webpack-plugin`的区别\n\n  - `html-webpack-plugin`生成的是HTML入口；`import-remote/plugin`生成的JS入口；\n\n  - HTML入口通过浏览器或`iframe`标签加载；JS入口通过`import-remote`提供的加载方法(`remote`)加载；\n\n### 和`webpack library`打包的区别\n\n  - `webpack library`的入口JS可以直接通过`script`标签加载，支持资源跨域；`import-remote`的入口JS需要专门提供的加载方法(`remote`)加载，该方法通过`ajax`请求加载资源，所以资源间的跨域问题需要使用者自行解决；\n\n  - `webpack library`的模块依赖需要通过从`window`变量获取；`import-remote`的模块依赖只需在加载方法(`remote`)中传递进去即可；\n\n  - `webpack library`包必须配置`publicPath`；`import-remote`无需配置`publicPath`或只需配置为`/`，加载方法(`remote`)在加载资源时会自动配置JS的`publicPath，并且查找CSS源代码中的相对路径，将其替换为绝对路径；\n\n### 和`webpack Module Fedetation`打包的区别\n\n  - `webpack Module Fedetation` 需要应用和宿主都是`webpack5`，`import-remote`没有这个要求，`webpack4/5`都可以，甚至宿主的打包环境使用的不是`webpack`(gulp、vite等)也没问题；\n\n### 实现原理\n\n#### 构建期\n\n参考`html-webpack-plugin`生成的js入口里，实际上是该入口依赖的mainfest信息，包含入口JS/CSS文件、external等信息；\n\n#### 加载期\n  \n`import-remote`的加载方法调用时：\n  1. 加载入口JS文件；\n  2. 根据入口JS里的信息，通过ajax请求下载入口JS/CSS资源;\n  3. 通过内置的重写`webpack运行时`加载相关资源;\n  4. 根据`external`信息，替换调加载模块中的这些外部模块；\n  5. 调用入口模块，获取它的`modlule.exports`;\n  6. 返回获取到的`modlule.exports`；\n\n![说明](./images/principle.png)\n\n## 安装\n\n```bash\n  npm install --save import-remote\n```\n\n或者：\n\n```bash\n  yarn add import-remote\n```\n\n## 插件使用说明\n\n通过`import-remote/plugin`插件可以为你的`webpack`包生成一个JS入口文件。下面是一个`webpack`配置示例:\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nconst entryFiles = ['index', 'foo', 'bar'];\n\nmodule.exports = {\n  // 配置入口，可以配置多个入口\n  entry: {\n    ...(entryFiles.reduce((p, file) => {\n      p[file] = `${file}.js`;\n      return p;\n    }, {}))\n  }\n  // 配置输出目录\n  output: {\n    path: __dirname + '/dist',\n    filename: 'assets/[name]-[chunkhash:5].js',\n    chunkFilename: 'assets/[name]-[chunkhash:5]-chunk.js',\n  },\n  // 配置外部依赖\n  externals: [\n    'react',\n    'react-dom',\n    'lodash'\n  ],\n  plugins: [\n    // 装载插件\n    ...entryFiles.map(file => new ImportRemotePlugin({\n      template: `auto`,\n      // 生成的入口文件名\n      filename: `${file}.js`,\n      // 包含的入口chunk\n      chunks: [file],\n    }))\n    \n  ]\n}\n```\n\n入口文件示例：\n\n**src/index.js**\n\n```js\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport _ from 'lodash',\n\nconst test = {\n  dosomething() {\n    console.log('dosomething', React, ReactDOM, _);\n  }\n}\n\n// 导出你要导出的内容\nmodule.exports = test;\n```\n\n**宿主端使用示例**\n\n```js\nimport remote from 'import-remote';\n\nasync function run() {\n  // 通过await等待模块加载完毕\n  const testIndex = await remote('http://localhost:3000/test/index.js', {\n    // 配置模块需要依赖\n    externals: { \n      react: require('react'), \n      'react-dom': require('react-dom'),\n      'lodash': require('lodash'),\n    }\n  });\n  // 拿到模块输出对象后，即可做你想做的任何事了\n  testIndex.dosomething();\n\n  // 也可以将依赖配置成全局依赖，这样importRemote时就不必再传依赖了\n  Object.assign(remote.externals, { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  });\n  const testOther = await remote('http://localhost:3000/test/other.js');\n  testOther.dosomething();\n}\n\nrun();\n```\n\n`import-remote`还提供了远程模块(`RemoteModule`)类，方便管理同一组模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n\n```\n\n### remote/RemoteModule的options\n\n- `scopeName: string` - 作用域名，不配置时，会使用远程模块项目的项目名(`package.json->name`)；\n\n- `timeout:number` - 资源加载的请求时间，为0表示不会超时；\n\n- `externals: Record<string, any>` - 传递给远程模块的依赖模块(webpack配置的externals)；\n\n- `getManifestCallback: (manifest: RemoteManifest) => any|Promise<any>` - 当加载到`manifest`后的回调函数；\n\n- `afterCreateRuntime: (webpack_require: any, ctx: RemoteModuleRuntime) => void` - 模块的runtime创建好后的回调函数；\n\n- `host: string` - 远程模块的`host`，相同`host`的远程模块会认为来自同一个项目，它们会共享同一个runtime运行时。当不传时，host将从url中去取(去掉url中最后一个`/`前的部分会认为是`host`);\n\n- `sync: boolean` - 使用使用`ajax`的同步请求来加载资源；\n\n- `cacheDB: boolean` - 是否开启`cacheDB`，当开启后，每次加载成功的远程资源会保存在一个名叫`import-remote-global-db`的`indexedDB`数据库中，当遇到某个资源ajax请求失败，`xhr.status`为`0`时，将会改为尝试从该数据库加载该资源。该数据每次启动时会清楚一个月之前的缓存，以避免缓存资源膨胀。\n  \n  `注`：你也可以通过`import-remote/cache`导出的`enableCacheDB`方法来为所有模块开启`cacheDB`；\n```js\nimport { enableCacheDB } from 'import-remote/cache';\n\nenableCacheDB();\n```\n\n- `useEsModuleDefault: boolean` - 是否在加载到远程模块后直接返回入口模块的`default`导出，而不是返回整个该入口模块；\n\n### `import-remote/plugin`插件基本配置选项\n\n`import-remote/plugin`的基本配置选项基本和`html-webpack-plugin`一致，这样目的是方便两个插件间切换。\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`filename`**|`{String}`|`'index.js'`|生成的入口文件名。默认为`index.js`. 你也可以在这里指的子目录 (比如: `entrys/index.js`)|\n|**`hash`**|`{Boolean}`|`false`|如果值为true，就添加一个唯一的webpack compilation hash给所有JS和CSS文件。这对缓存清除（cache busting）十分有用|\n|**`cache`**|`{Boolean}`|`true`|如果为true (默认)，只要文件被更改了就生成文件|\n|**`showErrors`**|`{Boolean}`|`true`|如果为true (默认)，详细的错误信息将被写入到JS文件中|\n|**`chunks`**|`{?}`|`?`|允许你只添加某些chunks(e.g only the unit-test chunk)|\n|**[`chunksSortMode`](#plugins)**|`{String\\|Function}`|`auto`|在chunks被include到html文件中以前，允许你控制chunks 应当如何被排序。允许的值: `none`|`auto`|`dependency`|{Function} - 默认值: `auto`|\n|**`excludeChunks`**|`{Array.<string>}`|``|允许你跳过某些chunks (e.g don't add the unit-test chunk)|\n|**`entriesManifest`**|`{Boolean|String}`|`''`|当该参数为true时，将为项目生成一个所有入口的说明文件(默认文件名`import-remote-entries.js`),你可以通过通过`RemoteModule`的`requireEntries`方法加载该文件来获取该项目目前存在的所有入口文件信息|\n|**`libraryFileName`**|`{Boolean|String}`|`''`|当该参数为true时，将会将本包打包的`import-remote.min.js`文件复制到打包目标目录中，你可以通过`import AsyncRemoteModule from \"import-remote/esm/fetch\";`，先通过它远程加载`import-remote.min.js`后再加载实际的远程入口，已避免`import-remote`升级后需要宿主也升级`import-remote`的问题|\n\n#### 生成多输出入口文件\n\n要生成多个输出入口文件，只需要在你的插件列表中配置多个`ImportRemotePlugin`插件：\n\n```js\n{\n  entry: 'index.js',\n  output: {\n    path: __dirname + '/dist',\n    filename: 'index_bundle.js'\n  },\n  plugins: [\n    new ImportRemotePlugin(), // 生成默认的index.js\n    new ImportRemotePlugin({  // 同时生成一个test.js\n      filename: 'test.js',\n    })\n  ]\n}\n```\n\n#### 过滤Chunks\n\n要只打包特定的chunk包，你可以通过`chunks`属性来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    chunks: ['app']\n  })\n]\n```\n\n你也可以配置要排除特定的包，通过`excludeChunks`选项来控制：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    excludeChunks: [ 'dev-helper' ]\n  })\n]\n```\n\n#### 长期缓存\n\n要支持长期缓存，你可以通过为`filename`添加`contenthash/templatehash`：\n\n```js\nplugins: [\n  new ImportRemotePlugin({\n    filename: 'index.[contenthash].js'\n  })\n]\n```\n\n`contenthash/templatehash` 是输出文件内容的hash。\n\n另外, 你也可以这样配置hash： `[<hashType>:contenthash:<digestType>:<length>]`\n\n* `hashType` - `sha1`、`md5`、`sha256`、`sha512`之一，或者任意一种node.js支持的hash类型\n* `digestType` - `hex`、`base26`、`base32`、`base36`、`base49`、`base52`、`base58`、 `base62`、`base64`之一\n* `maxlength` - 生成的hash字符的最大程度\n\n**默认为:** `[md5:contenthash:hex:9999]`\n\n\n### `import-remote/plugin`插件额外配置选项\n\n除了从`html-webpack-plugin`继承的配置选项，`import-remote/plugin`也提供了一些自己的配置选项：\n\n|属性名|类型|默认值|说明|\n|:--:|:--:|:-----:|:----------|\n|**`commonModules`**|`[{ name?: string, url: string, scoped?: boolean }]`|`[]`|依赖的公共模块，如果配置了公共模块，则模块的依赖将尝试从这些公共模块中加载依赖。其中`name`是可选参数，为某个外部依赖模块名，如果配置了`name`，表示该公共包只是该名称的模块，则只有该模块依赖该名称的外部依赖模块时才会加载该模块包。该功能依赖于webpack的externals|\n|**`shareModules`**|`[string or { name: string, var: string }]`|`[]`|共享模块列表，如果配置了共享模块，则模块的这些依赖将优先使用宿主应用externals参数中传递的依赖，如果未在宿主应用中找到，才使用自己的该模块。该功能不依赖webpack的externals。\n|**`globalToScopes`**|`string[]`|`[]`|需要局部化的全局变量，该数组中声明的名称，在加载资源时将会从源代码中将其替换为从某个私有变量中读取这些值|\n\n\n#### 公共模块包\n\n提供公共模块包的目的是方便公共模块的共享，同时利用`webpack`的懒加载功能实现组件的按需加载。\n\n这是一个公共模块包的示例：\n****\n```js\n// index.js\nimport { createRequireFactory } from 'import-remote';\n\nexport default createRequireFactory({\n  'import-remote': () => require('import-remote'),\n  react: () => import(/* webpackChunkName: 'react' */ 'react'),\n  'react-dom': () => import(/* webpackChunkName: 'react-dom' */ 'react-dom'),\n  'prop-types': () => import(/* webpackChunkName: 'prop-types' */ 'prop-types'),\n  mobx: () => import(/* webpackChunkName: 'mobx' */ 'mobx'),\n  'mobx-react': () => import(/* webpackChunkName: 'mobx-react' */ 'mobx-react'),\n  qs: () => import(/* webpackChunkName: 'qs' */ 'qs'),\n});\n\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: [\n    {\n      'import-remote': {\n        root: 'importRemote',\n        amd: 'import-remote',\n        commonjs2: 'import-remote',\n        commonjs: 'import-remote',\n      },\n    },\n  ],\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n这是一个公共lib模块包的示例：\n****\n```js\n// index.js\nimport axios from 'axios';\n\nexport default axios;\n```\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin()\n  ]\n}\n```\n\n一个依赖公共包的webpack配置如下：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  externals: {\n    react: {\n      root: 'React',\n      amd: 'react',\n      commonjs2: 'react',\n      commonjs: 'react',\n    },\n    'react-dom': {\n      root: 'ReactDOM',\n      amd: 'react-dom',\n      commonjs2: 'react-dom',\n      commonjs: 'react-dom',\n    },\n    mobx: {\n      root: 'mobx',\n      commonjs: 'mobx',\n      commonjs2: 'mobx',\n    },\n    'mobx-react': {\n      root: 'mobxReact',\n      commonjs: 'mobx-react',\n      commonjs2: 'mobx-react',\n    },\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      commonModules: [\n        // 当指定了name时，只有该name在externals中存在时，才会加载该公共包\n        { name: 'axios', url: 'http://localhost:3000/lib/axios.min.js' },\n        // 未指定name事，只要externals不为空，就会加载该公共包\n        { url: 'http://localhost:3000/lib/commonLibs.js' },\n      ]\n    })\n  ]\n}\n```\n\n则这个模块的依赖：\n\n  1. 首先从宿主使用加载方法`remote`中配置的`externals`中寻找依赖；\n\n  2. 第一步没找到依赖，将会从模块中寻找依赖；\n\n#### 共享模块\n\n通过共享模块，使多个远程应用间使用共同的依赖。\n\n这是一个共享模块包的配置示例：\n\n**webpack.config.js**\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      shareModules: [\n        { name: 'react', var: 'react' },\n        { name: 'react-dom', var: 'ReactDOM' }\n      ]\n    })\n  ]\n}\n```\n\n宿主传递共享模块的方式：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 共享模块也可以通过externals来传递\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n  }\n});\n```\n\n#### 全局变量私有化\n\n通过`globalToScopes`选项可以将你的源代码中一些`window.xxx`、`global.xxx`的变量替换为一个同组模块共享的私有变量中。这在相互集成时全局变量冲突时的一种解决办法；\n\n如果你的webpack配置是这样的：\n\n```js\nconst ImportRemotePlugin = require('import-remote/plugin')\n\nmodule.exports = {\n  entry: {\n    index: 'index.js'\n  }\n  output: {\n    filename: '[name]-[chunkhash:5].js'\n  },\n  plugins: [\n    new ImportRemotePlugin({\n      globalToScopes: ['app', 'someVar']\n    })\n  ]\n}\n```\n\n则宿主在加载你的模块时，将会把你源代码中的`global.app`、`window.app`、`global.someVar`、`window.someVar`替换成`_wp_.g.app`、`_wp_.g.someVar`。\n\n注：全局变量私有化可能有风险，需要您仔细斟酌是否使用。毕竟最好的方案就是没有全局变量。该选项只是用于解决您目前无法或没时间去掉全局变量时的一种托底解决办法。\n\n## 运行期包使用说明\n\n### 远程加载方法\n\n函数签名：\n\n```ts\n/**\n * @param {string} url 远程模块的url地址\n * @param {object} options 加载选项\n * @param {object} options.externals 远程模块的依赖模块\n * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n * @returns {Promise<any>} 远程模块的导出内容\n **/\nfunction remote(url: string, options: { externals: { [key]: any } }): Promise<any>; \n```\n\n使用方法：\n\n```js\nimport remote from 'import-remote';\n\n// 通过await等待模块加载完毕\nconst testIndex = await remote('http://localhost:3000/test/index.js', {\n  // 配置模块需要依赖\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n// 拿到模块输出对象后，即可做你想做的任何事了\ntestIndex.dosomething();\n```\n\n### 远程模块\n\n通过远程模块可以用于管理一个项目生成的所有模块：\n\n```js\nimport { RemoteModule } from 'import-remote';\n\n// 配置RemoteModule的host和依赖\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\n// 具体的加载路径为RemoteModule的`${host}${requireName}.js`\nconst testIndex = await testModule.require('index');\nconst testOther = await testModule.require('other');\n\ntestIndex.dosomething();\ntestOther.dosomething();\n```\n\n`RemoteModule`的的方法有：\n```ts\n{\n  /**\n   * @param {string} host 该项目生成的模块的基地址(host)\n   * @param {object} options 加载选项\n   * @param {object} options.externals 远程模块的依赖模块\n   * @param {boolean} options.useEsModuleDefault 当远程模块导出的是ES模块时，是否只返回它的default导出部分\n   * @returns {void}\n   **/\n  constructor(host: string, options: { externals: { [key]: any } }): void; \n  \n  /**\n   * 根据模块名生成该模块的入口地址\n   * @param {string} moduleName 模块名\n   * @returns {string} 该模块的入口地址\n   **/\n  resolveModuleUrl(moduleName: string = 'index'): string; \n\n  /**\n   * 发送HEAD请求判断`moduleName`是否存在，当存在时返回请求的响应头信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项\n   * @returns {Promise<object|null>} HEAD请求头信息或为空\n   **/\n  exist(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} entryName 入口模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 所有入口的描述文件\n   **/\n  requireEntries(entryName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 获取`moduleName`的meta信息\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 模块的meta信息\n   **/\n  requireMeta(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和requireMeta相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireMetaSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  require(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和require相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  requireSync(moduleName: string = 'index', options: {}): any; \n\n  /**\n   * 异步加载方法,功能和require相同，只是在导出模块为es模块时，只导出它的default部分\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {Promise<any>} 远程模块的导出内容\n   **/\n  import(moduleName: string = 'index', options: {}): Promise<any>; \n\n  /**\n   * 功能和import相同，只是将加载资源时的ajax设置为同步请求\n   * @param {string} moduleName 模块名，调用\n   * @param {object} options 加载选项，和remote方法的option一致\n   * @returns {any} 远程模块的导出内容\n   **/\n  importSync(moduleName: string = 'index', options: {}): any;  \n}\n\n```\n\n### RemoteView\n\n`RemoteView`是一个`React`组件，模拟`iframe`，内部创建3个元素模拟`html`、`head`、`body`，通过`src`属性加载远程`React组件`, 通过该组件加载的资源会有自己独立的全局空间，移除该组件时，也将移除加载该远程模块时的所有资源。\n\n示例:\n```js\nimport React from 'react';\nimport RemoteView from 'import-remote/view';\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      src=\"http://localhost:3000/test.js\" \n      props={{\n        aa: 1,\n        bb: 2\n      }}\n      externals={{\n        'react': require('react'),\n        'react-dom': require('react-dom')\n      }}\n    />\n  </div>;\n}\n```\n\n或者使用`RemoteModule`来加载:\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport RemoteView from 'import-remote/view';\n\nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteView \n      module={testModule}\n      moduleName=\"index\"\n      props={{\n        aa: 1,\n        bb: 2\n      }}\n    />\n  </div>;\n}\n```\n\n`RemoteView`除了支持远程模块导出的`React`组件，也支持以下格式的组件导出接口：\n\n```js\n{\n  namespace?: string,\n  bootstrap: (props, children) => void,\n  mounted: (el, props) => primise|void,\n  update: (el, props, prevProps) => void,\n  unmount: (el) => void,\n}\n```\n\n和\n\n```js\n{\n  namespace?: string,\n  init: (props, options) => primise|void,\n  render: (el, props) => void,\n  destroy: (el) => void,\n}\n```\n\n注：如果`namespace`不为空，这将会将其添加到`body`模拟元素的`className`中。\n\n如果想将这样的对象转换成`React`组件，可以通过下面的办法：\n\n1. 直接将一个导出对象转换成`React`组件:\n\n```js\nimport { createAppView } from 'import-remote/view';\nimport testObject from './test-object';\n\nconst Test = createAppView(testObject);\n```\n\n\n2. 远程加载一个导出对象，并直接将其转换成`React`组件:\n\n```js\nimport { requireApp } from 'import-remote/view';\n\nconst Test = await requireApp('http://localhost:3000/test.js');\n```\n\n\n#### RemoteView的props\n\n- `scopeStyle: boolean` - 当为true，并且`shadow`为false时，将为引用的样式添加一个`hash`作用域，避免样式影响到外部。是一种在不支持`shadow DOM`的情况下解决样式冲突的一种解决办法\n\n- `scopePrefix: string = 'v-'` - 为引用样式创建的作用域名的前缀\n\n- `classPrefix: string = 'import-remote-'` - 创建的`html`、`head`、`body`模拟元素的类名前缀\n\n- `tag: string = 'div'` - 创建的`html`、`head`、`body`模拟元素的元素类型\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `props: object` - 传递给远程模块导出组件的`props`\n  \n- `externals: object` - 远程组件的外部依赖\n\n- `shadow: boolean` - 是否将`html`、`head`、`body`创建在`shadow DOM`中\n   \n- `onViewLoading: (loading) => void` - 远程模块加载中的回调事件，分别会在加载前后调用\n\n- `onViewError: (error) => void` - 远程模块加载失败的回调事件\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n### RemoteApp\n\n`RemoteApp`是一个即插即用的React组件，当远程模块返回的结果本身就是React组件时，可以使用该组件加载对应的模块。它和`RemoteView`的区别是：`RemoteView`会局部化远程模块的作用域空间，而`RemoteApp`不会：\n\n```js\nimport React from 'react';\nimport { RemoteModule } from 'import-remote';\nimport { RemoteApp } from 'import-remote/view';\n// or \nimport { RemoteApp } from 'import-remote/view/app';\n \nconst testModule = new RemoteModule('http://localhost:3000/test', {\n  externals: { \n    react: require('react'), \n    'react-dom': require('react-dom'),\n    'lodash': require('lodash'),\n  }\n});\n\nfunction Test(props) {\n  return <div>\n    <RemoteApp\n      module={testModule}\n      moduleName=\"index\"\n      aa=\"1\"\n      bb=\"2\"\n    />\n  </div>;\n}\n```\n\n#### RemoteApp的props\n\n- `src: string` - 远程模块的入口文件地址\n\n- `module: RemoteModule` - 远程模块对象，和`moduleName`配合使用，与`src`属性互斥\n\n- `moduleName: string` - 远程模块名称，和`module`配合使用，与`src`属性互斥\n\n- `exportName: string` - 当远程模块的结果为一个`esModule`时，取`esModule`的导出`key`名，默认为`default`\n\n- `hoc: Function` - 对导出组件的高阶封装，参数格式：`(Component: React.ComponentType | React.ForwardRefExoticComponent, error?: any) => React.ComponentType | React.ForwardRefExoticComponent`\n\n- `clearWhenError: boolean` - 当加载远程组件失败时是否清除当前显示的组件，即上次加载的组件。默认为`true`。\n","readmeFilename":"README.md","description":"一个远程加载JS模块的库。支持webpack4/5，可以通过使你的项目支持微前端架构。"}