From aeb5584cac6f3f6173e2d04653f1719569503bd0 Mon Sep 17 00:00:00 2001 From: sunhao Date: Wed, 21 Aug 2019 12:49:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20[=E6=9E=84=E5=BB=BA]=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20=E6=8E=92=E9=99=A4=20=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=8C=85=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- @types/index.d.ts | 19 ++++++ doc/build.config.ts | 4 ++ doc/build/webpack.base.conf.babel.js | 12 +++- doc/build/webpack.client.conf.babel.js | 6 +- doc/build/webpack.server.conf.babel.js | 2 +- doc/package.json | 2 + doc/src/App.vue | 15 +++++ doc/src/views/index.vue | 21 +++++++ doc/yarn.lock | 84 +++++++++++++++++++++++++- src/config/webpack.client.config.ts | 5 +- src/config/webpack.server.config.ts | 13 ++-- src/utils/plugins.webpack.ts | 10 +++ 12 files changed, 183 insertions(+), 10 deletions(-) diff --git a/@types/index.d.ts b/@types/index.d.ts index 6c5e8e4..60a39da 100644 --- a/@types/index.d.ts +++ b/@types/index.d.ts @@ -316,6 +316,11 @@ declare namespace build { */ webpack?: options.webpack + /** + * exclude 排除 第三方包 + */ + exclude?: options.exclude + /** * 服务器端 插件 配置 * * 中间件 @@ -355,6 +360,20 @@ declare namespace build { server?: serverConfig } + /** + * exclude 排除 第三方包 + */ + interface exclude { + /** + * 排除 客户端 第三方包 + */ + client?: string[] + /** + * 排除 服务器端 第三方包 + */ + server?: string[] + } + interface serverConfig extends Configuration { nodeExternalsWhitelist: any[] webpack: Configuration diff --git a/doc/build.config.ts b/doc/build.config.ts index b573601..264a93e 100644 --- a/doc/build.config.ts +++ b/doc/build.config.ts @@ -23,6 +23,10 @@ export default function(inject: ConfigOptions.getOptionsInject): ConfigOptions.o return { env: ['SERVER_ENV', 'ENV', 'NODE_ENV'], port, + exclude: { + client: ['globals'], + server: ['@bestminr/fastblur'], + }, babelrc: { presets: [ [ diff --git a/doc/build/webpack.base.conf.babel.js b/doc/build/webpack.base.conf.babel.js index 0db3148..eb1572a 100644 --- a/doc/build/webpack.base.conf.babel.js +++ b/doc/build/webpack.base.conf.babel.js @@ -18,7 +18,17 @@ export default function(config, { resolve }) { loader: 'worker-loader', options: { inline: true }, }, + { + test: /.*\.wasm$/, + // This is needed to make webpack NOT process wasm files. + // See https://github.com/webpack/webpack/issues/6725 + type: 'javascript/auto', + loader: 'file-loader', + options: { + name: '[name].[hash:5].[ext]', + }, + }, ], - }, + } } } diff --git a/doc/build/webpack.client.conf.babel.js b/doc/build/webpack.client.conf.babel.js index 2344e70..3383f41 100644 --- a/doc/build/webpack.client.conf.babel.js +++ b/doc/build/webpack.client.conf.babel.js @@ -1,3 +1,4 @@ +import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' export default function(config, { resolve }) { return { entry: { @@ -5,6 +6,9 @@ export default function(config, { resolve }) { }, output: { globalObject: 'this', - } + }, + plugins: [ + // new BundleAnalyzerPlugin() + ] } } diff --git a/doc/build/webpack.server.conf.babel.js b/doc/build/webpack.server.conf.babel.js index 1355ac0..91ac704 100644 --- a/doc/build/webpack.server.conf.babel.js +++ b/doc/build/webpack.server.conf.babel.js @@ -1,5 +1,5 @@ export default function(config, { resolve }) { return { - entry: './src/entry-server.ts', + entry: './src/entry-server.ts' } } diff --git a/doc/package.json b/doc/package.json index 34a80ed..058c960 100644 --- a/doc/package.json +++ b/doc/package.json @@ -48,6 +48,7 @@ "devDependencies": { "@babel/plugin-syntax-jsx": "^7.2.0", "@babel/runtime": "^7.1.5", + "@bestminr/fastblur": "^0.1.1", "@types/babel-core": "^6.25.5", "@types/bluebird": "3.5.18", "@types/express": "^4.16.0", @@ -74,6 +75,7 @@ "ts-jest": "^24.0.0", "ts-node-dev": "^1.0.0-pre.39", "vue-jest": "^3.0.4", + "webpack-bundle-analyzer": "^3.4.1", "worker-loader": "^2.0.0" } } diff --git a/doc/src/App.vue b/doc/src/App.vue index 7b5e267..6dc4bce 100644 --- a/doc/src/App.vue +++ b/doc/src/App.vue @@ -13,10 +13,25 @@ import Component from 'vue-class-component' import { State } from 'vuex-class' import { Watch, Prop } from 'vue-property-decorator' +import axios from 'axios' +import { isServer, injectGlobal } from 'src/envs' + +function get(url: string) { + if (isServer) url = `${injectGlobal.__INJECT_CONTEXT__.SERVER_HOST}${url}` + console.log('axios 发起请求:', url) + axios.get(url).then(({ data }) => { + console.log('axios 获取当前版本号:', data.version.join('.')) + }) +} + @Component({ components: {}, }) export default class App extends Vue { + created() { + get('/private/version') + } + mounted() { console.log('init APP') } diff --git a/doc/src/views/index.vue b/doc/src/views/index.vue index def959f..8415941 100644 --- a/doc/src/views/index.vue +++ b/doc/src/views/index.vue @@ -15,6 +15,26 @@ import { Trouter } from '@types' import { commit, getState } from 'src/store' import { isServer } from 'src/envs' +import fastblur from '@bestminr/fastblur' + +let fastblurWasmModulePromise: Promise +function getFastBlurModule(): Promise { + return import('@bestminr/fastblur').then((m) => { + if (fastblurWasmModulePromise) { + return fastblurWasmModulePromise + } + const wasmPath = require('@bestminr/fastblur/fastblur_bg.wasm') // 使用 file-loader 处理的时候是酱紫的 + console.log('got module', m, wasmPath) + // wasm-bindgen 生成的 js 和 d.ts 不太对... 只好这样强行调 init 函数了 + const initPromise = (m as any).default(wasmPath) + fastblurWasmModulePromise = initPromise.then(() => { + return m + }) + return fastblurWasmModulePromise + }).catch((err) => { + console.log('err', err) + }) as any +} @Component({ components: {}, @@ -43,6 +63,7 @@ export default class APP extends Vue { mounted() { console.log('init Views') + getFastBlurModule() } addTestHotLoadingVuex() { diff --git a/doc/yarn.lock b/doc/yarn.lock index 78eeeea..f5c9ece 100644 --- a/doc/yarn.lock +++ b/doc/yarn.lock @@ -696,6 +696,10 @@ webpack-node-externals "^1.7.2" webpackbar "^3.1.3" +"@bestminr/fastblur@^0.1.1": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@bestminr/fastblur/-/fastblur-0.1.1.tgz#ed89586624ed668cdd15a6f9e25446019a91db6a" + "@cnakazawa/watch@^1.0.3": version "1.0.3" resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.3.tgz#099139eaec7ebf07a27c1786a3ff64f39464d2ef" @@ -1288,6 +1292,10 @@ acorn-walk@^6.0.1: version "6.1.1" resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-6.1.1.tgz#d363b66f5fac5f018ff9c3a1e7b6f8e310cc3913" +acorn-walk@^6.1.1: + version "6.2.0" + resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-6.2.0.tgz#123cb8f3b84c2171f1f7fb252615b1c78a6b1a8c" + acorn@^3.0.4: version "3.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" @@ -1300,6 +1308,10 @@ acorn@^6.0.1, acorn@^6.0.5: version "6.1.1" resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.1.1.tgz#7d25ae05bb8ad1f9b699108e1094ecd7884adc1f" +acorn@^6.0.7: + version "6.3.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-6.3.0.tgz#0087509119ffa4fc0a0041d1e93a417e68cb856e" + ajv-errors@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d" @@ -1774,6 +1786,15 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" +bfj@^6.1.1: + version "6.1.2" + resolved "https://registry.yarnpkg.com/bfj/-/bfj-6.1.2.tgz#325c861a822bcb358a41c78a33b8e6e2086dde7f" + dependencies: + bluebird "^3.5.5" + check-types "^8.0.3" + hoopy "^0.1.4" + tryer "^1.0.1" + big.js@^3.1.3: version "3.2.0" resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e" @@ -2156,6 +2177,10 @@ chardet@^0.4.0: version "0.4.2" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2" +check-types@^8.0.3: + version "8.0.3" + resolved "https://registry.yarnpkg.com/check-types/-/check-types-8.0.3.tgz#3356cca19c889544f2d7a95ed49ce508a0ecf552" + chokidar@^2.0.2, chokidar@^2.0.4: version "2.1.6" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.6.tgz#b6cad653a929e244ce8a834244164d241fa954c5" @@ -2329,7 +2354,7 @@ commander@2.17.x: version "2.17.1" resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf" -commander@^2.12.1, commander@^2.19.0, commander@~2.20.0: +commander@^2.12.1, commander@^2.18.0, commander@^2.19.0, commander@~2.20.0: version "2.20.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422" @@ -3017,6 +3042,10 @@ dot-prop@^4.1.1: dependencies: is-obj "^1.0.0" +duplexer@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1" + duplexify@^3.4.2, duplexify@^3.6.0: version "3.7.1" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309" @@ -3052,6 +3081,10 @@ ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" +ejs@^2.6.1: + version "2.6.2" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.6.2.tgz#3a32c63d1cd16d11266cd4703b14fec4e74ab4f6" + electron-to-chromium@^1.3.164: version "1.3.165" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.165.tgz#51864c9e3c9bd9e1c020b9493fddcc0f49888e3a" @@ -3363,7 +3396,7 @@ expect@^24.8.0: jest-message-util "^24.8.0" jest-regex-util "^24.3.0" -express@^4.17.1: +express@^4.16.3, express@^4.17.1: version "4.17.1" resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134" dependencies: @@ -3533,6 +3566,10 @@ filename-regex@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" +filesize@^3.6.1: + version "3.6.1" + resolved "https://registry.yarnpkg.com/filesize/-/filesize-3.6.1.tgz#090bb3ee01b6f801a8a8be99d31710b3422bb317" + filewatcher@~3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/filewatcher/-/filewatcher-3.0.1.tgz#f4a1957355ddaf443ccd78a895f3d55e23c8a034" @@ -3933,6 +3970,13 @@ growly@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081" +gzip-size@^5.0.0: + version "5.1.1" + resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-5.1.1.tgz#cb9bee692f87c0612b232840a873904e4c135274" + dependencies: + duplexer "^0.1.1" + pify "^4.0.1" + handlebars@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.1.2.tgz#b6b37c1ced0306b221e094fc7aca3ec23b131b67" @@ -4054,6 +4098,10 @@ homedir-polyfill@^1.0.1: dependencies: parse-passwd "^1.0.0" +hoopy@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.4.tgz#609207d661100033a9a9402ad3dea677381c1b1d" + hosted-git-info@^2.1.4: version "2.7.1" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047" @@ -6031,6 +6079,10 @@ onetime@^2.0.0: dependencies: mimic-fn "^1.0.0" +opener@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/opener/-/opener-1.5.1.tgz#6d2f0e77f1a0af0032aca716c2c1fbb8e7e8abed" + optimist@^0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" @@ -8181,6 +8233,10 @@ trough@^1.0.0: dependencies: glob "^7.1.2" +tryer@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.1.tgz#f2c85406800b9b0f74c9f7465b81eaad241252f8" + ts-jest@^24.0.0: version "24.0.2" resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.0.2.tgz#8dde6cece97c31c03e80e474c749753ffd27194d" @@ -8734,6 +8790,24 @@ webidl-conversions@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad" +webpack-bundle-analyzer@^3.4.1: + version "3.4.1" + resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-3.4.1.tgz#430544c7ba1631baccf673475ca8300cb74a3c47" + dependencies: + acorn "^6.0.7" + acorn-walk "^6.1.1" + bfj "^6.1.1" + chalk "^2.4.1" + commander "^2.18.0" + ejs "^2.6.1" + express "^4.16.3" + filesize "^3.6.1" + gzip-size "^5.0.0" + lodash "^4.17.15" + mkdirp "^0.5.1" + opener "^1.5.1" + ws "^6.0.0" + webpack-cli@^3.1.2: version "3.3.4" resolved "https://registry.yarnpkg.com/webpack-cli/-/webpack-cli-3.3.4.tgz#de27e281c48a897b8c219cb093e261d5f6afe44a" @@ -8953,6 +9027,12 @@ ws@^5.2.0: dependencies: async-limiter "~1.0.0" +ws@^6.0.0: + version "6.2.1" + resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb" + dependencies: + async-limiter "~1.0.0" + x-is-string@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/x-is-string/-/x-is-string-0.1.0.tgz#474b50865af3a49a9c4657f05acd145458f77d82" diff --git a/src/config/webpack.client.config.ts b/src/config/webpack.client.config.ts index 755b06a..a1d290f 100644 --- a/src/config/webpack.client.config.ts +++ b/src/config/webpack.client.config.ts @@ -6,10 +6,12 @@ import webpack from 'webpack' import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin' import { ConfigOptions } from '@types' -import { getClientDllPlugin } from 'src/utils/plugins.webpack' +import { getClientDllPlugin, getExternals } from 'src/utils/plugins.webpack' export async function getClientConfig(options: ConfigOptions.options) { const client = options.webpack ? options.webpack.client || {} : {} + const externals = getExternals(options, 'client') + return (merge as any)( getBaseConfig(options), { @@ -17,6 +19,7 @@ export async function getClientConfig(options: ConfigOptions.options) { entry: { app: './src/entry-client.js' }, + externals, output: { globalObject: 'this' }, diff --git a/src/config/webpack.server.config.ts b/src/config/webpack.server.config.ts index 4532180..33a15fe 100644 --- a/src/config/webpack.server.config.ts +++ b/src/config/webpack.server.config.ts @@ -7,6 +7,7 @@ import VueSSRServerPlugin from 'vue-server-renderer/server-plugin' import { getStyle } from 'src/utils/style.webpack' import { ConfigOptions } from '@types' +import { getExternals } from 'src/utils/plugins.webpack' export function getServerConfig(options: ConfigOptions.options) { if (!(options.webpack && options.webpack.mode)) { @@ -20,6 +21,7 @@ export function getServerConfig(options: ConfigOptions.options) { const whitelist = [/\.css$/, /\?vue&type=style/].concat(options.webpack.server.nodeExternalsWhitelist || []) const mode = options.webpack.mode || 'production' const isProd = mode === 'production' + const externals = getExternals(options, 'server') return (merge as any)( getBaseConfig(options), { @@ -29,10 +31,13 @@ export function getServerConfig(options: ConfigOptions.options) { output: { libraryTarget: 'commonjs2' }, - externals: nodeExternals({ - // whitelist: /\.css$/ - whitelist - }), + externals: [ + nodeExternals({ + // whitelist: /\.css$/ + whitelist + }), + ...externals + ], performance: { maxEntrypointSize: 1024 * 1024 * 6, maxAssetSize: 1024 * 1024 * 3, diff --git a/src/utils/plugins.webpack.ts b/src/utils/plugins.webpack.ts index 0dbbb52..fd9016f 100644 --- a/src/utils/plugins.webpack.ts +++ b/src/utils/plugins.webpack.ts @@ -8,6 +8,7 @@ import { existsSync, readFileSync } from 'fs' import requireFromString from 'require-from-string' import consola from 'consola' import { compilerDll } from 'src/utils/compiler.webpack' +import { getValue } from 'src/utils/get' export function getDllPlugin({ path, @@ -76,3 +77,12 @@ export async function getClientDllPlugin(options: ConfigOptions.options) { plugins } } + +/** + * 获取 alias + * - 主要是对 只存在于server端或者client端 插件 支持 + * @param options build 通用 webpack 配置 + */ +export function getExternals(options: ConfigOptions.options, type: 'client' | 'server') { + return getValue(options, 'exclude', type) || [] +} \ No newline at end of file