{"id":630,"date":"2020-03-11T11:42:55","date_gmt":"2020-03-11T11:42:55","guid":{"rendered":"https:\/\/www.haoyuan.info\/?p=630"},"modified":"2020-03-11T11:42:55","modified_gmt":"2020-03-11T11:42:55","slug":"%e4%b8%bavuepress%e9%a1%b9%e7%9b%ae%e5%b0%9d%e8%af%95%e5%8a%a0%e5%85%a5cdn%e7%9a%84%e5%b0%9d%e8%af%95","status":"publish","type":"post","link":"https:\/\/haoyuan.info\/?p=630","title":{"rendered":"\u4e3aVuePress\u9879\u76ee\u5c1d\u8bd5\u52a0\u5165CDN\u7684\u5c1d\u8bd5"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u8c08\u5230\u5982\u4f55\u4fee\u6539<code>vue.config.js<\/code>\u4e3aVue-CLI\u9879\u76ee\u6dfb\u52a0CDN\uff0c\u5b9e\u73b0\u4e3a\u9759\u6001\u8d44\u6e90\u52a0\u901f\u7684\u76ee\u6807\u3002<\/p>\n<p>\u8003\u8651\u5230VuePress\u9759\u6001\u8d44\u6e90\u4e3a\u6570\u4e5f\u4e0d\u5c11\uff0c\u56e0\u6b64\u4e5f\u5c1d\u8bd5\u4e3a\u5176\u6253\u5305\u7684\u7ed3\u679c\u505a\u4e00\u4e0bCDN\u3002<\/p>\n<h2>\u76ee\u6807<\/h2>\n<p>\u5c3d\u53ef\u80fd\u5c06\u9759\u6001\u8d44\u6e90\u8bf7\u6c42\u5230CDN\u4e0a<\/p>\n<h2>\u6b63\u6587<\/h2>\n<p>\u901a\u8fc7\u7814\u7a76VuePress\u7684\u6587\u6863\uff0c\u5b98\u65b9\u5e76\u6ca1\u6709\u5411Vue-CLI\u90a3\u6837\u63d0\u4f9b\u4e00\u4e2apath\u4f7f\u5f97\u9759\u6001\u8d44\u6e90\u91cd\u5b9a\u5411\u5230\u76ee\u6807\u7684URL\u3002<\/p>\n<p>\u4e4b\u524d\u5728\u7f51\u4e0a\u4e5f\u6709issue\u63d0\u5230\u5173\u4e8eCDN\u52a0\u901f\u7684\u95ee\u9898\uff0c\u5b98\u65b9\u4e5f\u6ca1\u6709\u7ed9\u51fa\u76f8\u5173\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u4e8e\u662f\u8003\u8651\u8bbe\u8ba1\u4e00\u5957workaround\u65b9\u6848\uff0c\u7814\u7a76\u751f\u6210\u7684\u76ee\u6807\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4e0d\u96be\u53d1\u73b0\uff0c\u5728<code>docs\/.vuepress\/dist\/assets\/js\/app.*.js<\/code>\u6587\u4ef6\u4e2d\uff0c\u5bf9\u8c61<code>a<\/code>\u4e0b\u7684\u5c5e\u6027<code>p<\/code>(\u5373<code>a.p<\/code>)\u7ed9\u5b9a\u4e86\u6240\u6709js\u6587\u4ef6\u7684prefix\u3002<\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u8003\u8651\u8bbe\u8ba1\u811a\u672c\uff0c\u5728\u7f16\u8bd1\u5b8c\u6210\u540e\u6279\u91cf\u66ff\u6362\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6587\u6863\u4e2d\u4f1a\u6d89\u53ca\u5230\u8bb8\u591a\u7684html\u6587\u4ef6\u4e2d\u7684\u9759\u6001\u8d44\u6e90\u7684\u66ff\u6362\u3002\u56e0\u6b64\u53ef\u4ee5\u8003\u8651\u5c06html\u6587\u4ef6\u4f7f\u7528cheerio\u8bfb\u5165DOM\uff0c\u6279\u91cf\u66ff\u6362link\u3002<\/p>\n<p>\u4ee5<strong>CUP Online Judge Docs<\/strong>\u4e3a\u4f8b<\/p>\n<pre><code class=\"language-shell \">#!\/bin\/bash\nWEBPATH=\"https:\/\/cdn.jsdelivr.net\/gh\/CUP-ACM-Programming-Club\/CUPACM-Docs-CDN@v$(cat VERSION)\/\"\nif [[ \"$OSTYPE\" == \"darwin\"* ]]; then\nsed -i \"\" \"s|a.p=\\\"\/\\\"|a.p=\\\"$WEBPATH\\\"|g\" docs\/.vuepress\/dist\/assets\/js\/app.*.js\nsed -i \"\" \"s|\/assets\/img\/|$WEBPATH\/assets\/img\/|g\" docs\/.vuepress\/dist\/assets\/css\/*.css\nelse\nsed -i  \"s|a.p=\\\"\/\\\"|a.p=\\\"$WEBPATH\\\"|g\" docs\/.vuepress\/dist\/assets\/js\/app.*.js\nsed -i  \"s|\/assets\/img\/|$WEBPATH\/assets\/img\/|g\" docs\/.vuepress\/dist\/assets\/css\/*.css\nfi\nnode .github\/shell\/build_cdn_environment.js\n<\/code><\/pre>\n<pre><code class=\"language-javascript \">const glob = require(\"glob\");\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst cheerio = require(\"cheerio\");\nconst version = fs.readFileSync(\"VERSION\");\nconst webPath = `https:\/\/cdn.jsdelivr.net\/gh\/CUP-ACM-Programming-Club\/CUPACM-Docs-CDN@v${version}`;\n\nfunction rewriteSrc (element, $, attr) {\n    element.each(function () {\n        const src = $(this).attr(attr);\n        if (src.indexOf(\"http\") !== -1) {\n            return;\n        }\n        $(this).attr(attr, webPath + src);\n    })\n}\n\nfunction writeSrc (element, $, dir) {\n    element.each(function () {\n        const src = $(this).attr(\"src\");\n        if (src.indexOf(\"http\") !== -1) {\n            return;\n        }\n        $(this).attr(\"src\", webPath + path.join(dir, src));\n    })\n}\n\nfunction loadFile() {\n    const htmlFileList = glob.sync(`docs\/.vuepress\/dist\/**`)\n        .filter(name =&gt; {\n            return fs.lstatSync(name).isFile()\n        })\n        .filter(name =&gt; {\n            return name.substring(name.length - 5) === '.html';\n        });\n    htmlFileList.forEach(filePath =&gt; {\n        const content = fs.readFileSync(filePath, \"utf-8\");\n        const $ = cheerio.load(content);\n        const script = $(\"script\");\n        const css = $(\"link\");\n        rewriteSrc(script, $, \"src\");\n        rewriteSrc(css, $, \"href\");\n        writeSrc($(\"img\"), $, path.dirname(filePath).replace(\"docs\/.vuepress\/dist\", \"\"));\n        fs.writeFileSync(filePath, $.html());\n    });\n}\n\nloadFile();\n<\/code><\/pre>\n<p>\u5173\u4e8e\u53d1\u5e03CDN\u7684\u8fc7\u7a0b\uff0c\u53ef\u53c2\u8003\u4e0a\u4e00\u7bc7\u6587\u7ae0\uff0c\u8fd9\u91cc\u7565\u8fc7\u4e0d\u63d0\u3002<\/p>\n<h3>\u8bbe\u8ba1\u4e00\u4e2aGithub Actions\u7684step<\/h3>\n<pre><code class=\"language-yml \">      - name: Build distribution\n        run: |\n          npm i\n          npm run docs:build\n          .\/.github\/shell\/after_build.sh\n<\/code><\/pre>\n<p>\u5373\u53ef\u5b8c\u6210\u4e0a\u9762\u7684\u5de5\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5373\u5b8c\u6210\u4e86CDN\u9759\u6001\u8d44\u6e90\u7684\u5de5\u4f5c\u3002\u7136\u800c\uff0c\u4ecd\u6709\u5c11\u6570\u8d44\u6e90\u6ca1\u6709\u5b8c\u6210\u5168\u90e8\u7684\u66ff\u6362\uff0c\u53ef\u6839\u636e<code>build_cdn_environment.js<\/code>\u7684\u683c\u5f0f\uff0c\u7f16\u5199\u66ff\u6362\u89c4\u5219\u3002<\/p>\n<h2>\u5b58\u5728\u7684\u95ee\u9898<\/h2>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u5927\u90e8\u5206\u8d44\u6e90\u8d70CDN\uff0c\u4f46\u662fhtml\u6587\u4ef6\u5fc5\u987b\u76f4\u63a5\u8bbf\u95ee\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5bf9\u4e8e\u670d\u52a1\u5668\u901f\u5ea6\u672c\u8eab\u5c31\u6bd4\u8f83\u6162\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u8bf4Github Pages\uff0c\u53ef\u8003\u8651\u4f7f\u7528DNS\u505a\u5883\u5185\u5883\u5916\u4e0d\u540c\u7684CNAME\uff0c\u5883\u5185\u4f7f\u7528Coding Pages\uff0c\u5883\u5916\u4f7f\u7528Github Pages\uff0c\u7b80\u5355\u8bbe\u7f6eActions\uff0c\u5c06dist\u63a8\u5230\u4e24\u4e2a\u4ed3\u5e93\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u8c08\u5230\u5982\u4f55\u4fee\u6539vue.config.js\u4e3aVue-CLI\u9879\u76ee\u6dfb\u52a0CDN\uff0c\u5b9e\u73b0\u4e3a\u9759\u6001\u8d44\u6e90\u52a0\u901f\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[65,37,67],"tags":[9,8,68],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-git","category-node-js","category-vuepress","tag-github","tag-javascript","tag-vuepress"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8UC2c-aa","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=630"}],"version-history":[{"count":0,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/630\/revisions"}],"wp:attachment":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}