Zan Proxy:解决前端跨域、服务端预览本地文件、

Zan Proxy 官网链接 https://youzan.github.io/zan-proxy/
Mac 需要 安装证书 https://youzan.github.io/zan-proxy/book/quick-start/cert.html

一、Zan Proxy 是什么?

官方介绍:Zan Proxy 是一个用Node.js编写的HTTP代理服务器,可用于修改请求地址和模拟响应数据。

可以通过这个代理、转发线上**获取资源的请求**,拿取本地**实时的静态代码**【css,js,html】
这样就不用每次本地调试完,打包、部署服务器查看了。本地就可以直接预览部署之后的效果。
解决跨域的问题。

二、安装Zan Proxy

方式一【推荐】、也可以直接 下载客户端:传送门
方式二、可以通过官方的 npm 或是 yarn 来安装。

通过Yarn安装

yarn global add zan-proxy 

通过NPM安装

npm i -g zan-proxy

安装检验

zan-proxy --version

三、安装 Chrome 插件 Proxy SwitchyOmega

一、官方下载地址: https://proxy-switchyomega.com/download/

一、github下载:https://github.com/FelisCatus/SwitchyOmega/releases

四、配置 Zan Proxy

第一步新建
第二步启用转发
第三步前往编辑新的转发规则集
在这里插入图片描述

第四步 新增
在这里插入图片描述

第五步 创建规则集在这里插入图片描述

第六步 新增转发变量 和 新增规则
在这里插入图片描述
在这里插入图片描述

第七步 通过线上地址查看静态资源的url
在这里插入图片描述

第八步、 输入对应的url特征:
例如:
1.

  • xxxx.xxxx.cn/mall-cloud/xxx/(js|css)/(.+).(.+).(js|css)
  • ${ 转发变量名 }/$1/$2.$1
  • xxxx.xxxx.cn/mall-cloud/xxx/(js|css)/(.+).(js|css)
  • ${ss}/$1/$2.$1
    在这里插入图片描述

第九步、选择使用的规则集
在这里插入图片描述在这里插入图片描述

第十步、打开服务地址
使用插件选择 proxy 即可

在这里插入图片描述

proxy 配置:
在这里插入图片描述

五(可选)、本地项目启动 npm run watch

增加文件:
在这里插入图片描述

watch.js

const ora = require('ora');
const fs = require('fs-extra');
const chalk = require('chalk');
const webpack = require('webpack');
const config = require('../config');
const webpackConfig = require('./webpack.watch.conf');
const spinner = ora('building for watch...');
spinner.start();

process.env.NODE_ENV = 'development';

fs.removeSync(config.build.pages);
fs.removeSync(config.build.output);

const compiler = webpack(webpackConfig);
compiler.watch({
  aggregateTimeout: 300,
  poll: undefined
}, (err, stats) => { // Stats Object
  if (err) throw err;
  process.stdout.write(
    `${stats.toString({
      colors: true,
      modules: false,
      children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build.
      chunks: false,
      chunkModules: false
    })}\n\n`
  );

  if (stats.hasErrors()) {
    // console.log(stats.toJson());
    console.log(chalk.red('  Build failed with errors.\n'));
    process.exit(1);
  }
});

webpack.watch.conf.js

const path = require('path');
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const {getAppName} = require('../../utils');

process.env.NODE_ENV = 'production';

const webpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({
      sourceMap: false,
      extract: true
    })
  },
  devtool: false,
  output: {
    path: config.build.output,
    publicPath: `https://file.yzcdn.cn/mall-cloud/${getAppName()}/`,
    filename: 'js/[name].js',
    chunkFilename: 'js/[id].js'
  },
  plugins: [
    // new webpack.ProgressPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),
    // new UglifyJsPlugin({
    //   uglifyOptions: {
    //     compress: {
    //       warnings: false
    //     }
    //   },
    //   sourceMap: false,
    //   parallel: true
    // }),
    // extract css into its own file
    new ExtractTextPlugin({
      filename: 'css/[name].css',
      allChunks: true,
    }),
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: { safe: true }
    }),
    // keep module.id stable when vendor modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks (module) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        );
      }
    }),
  ].concat(utils.htmlPlugin())
});

// if (config.build.bundleAnalyzerReport) {
//   const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
//   webpackConfig.plugins.push(new BundleAnalyzerPlugin());
// }

module.exports = webpackConfig;

package.json

"scripts": {
   "dev": "node webpack/dev.js",
   "watch": "node webpack/watch.js",
   "build": "node webpack/build.js"
 },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值