Git Product home page Git Product logo

egg-webpack's Introduction

egg-webpack

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Webpack dev server plugin for egg, support read file in memory and hot reload. More Detail

Version

  • egg-webpack ^5.x.x > webpack 5.x.x
  • egg-webpack ^4.x.x > webpack 4.x.x
  • egg-webpack ^3.x.x > webpack 3.x.x

Install

npm i egg-webpack --save

Usage

// {app_root}/config/plugin.js
exports.webpack = {
  enable: true,
  package: 'egg-webpack',
};

Configuration

support native webpack config and easywebpack webpack config

// {app_root}/config/config.default.js
exports.webpack = {
  // port: 9000,  

  // webpackConfigList: [],
};
  • port: {Number}, default 9000。webpack dev server port, default 9000, when hava multile webpack config, the port incremented。
  • offsetPort: {Boolean}, default false。when mutil webpack target web config, need set true。
  • browser: {Boolean | String} if it is boolean type, whether to open the browser automatically, defualt true; if it is string。 That is url address, will automatically open the browser's url address。
  • proxy: {Boolean | Object}. webpack compiled in a separate service inside, default webpack service is http://127.0.0.1:9000, you can set koa-proxy options access static resources by koa-proxy。the default options:
config.webpack = {
  proxy: {
    host: 'http://127.0.0.1:9000', // target host that matched path will be proxy to
    match: /^\/public\//, // proxy url path pattern.
  },
}
  • webpackConfigList: {Array}, optional, default []. native webpack config.
  • webpackConfigFile: {String}, optional, you must set when you easywebpack config file is not in the project root directory。

webpack native configuration

  • if you write one native webpack config ${app_root}/build/webpack.config.js, you can use like this:
// {app_root}/config/config.default.js
exports.webpack = {
  webpackConfigList: [require('../build/webpack.config.js')]
};
  • if you use easywebpack solution, you can use like this:

default read webpack.config.js file under the project root directory.

const EasyWebpack = require('easywebpack-vue');
// {app_root}/config/config.default.js
exports.webpack = {
  webpackConfigList: EasyWebpack.getWebpackConfig()
};
  • if you use easywebpack solution, the easywebpack config file in ${app_root}/build/webpack.config.js, you can use like this:
const EasyWebpack = require('easywebpack-vue');
// {app_root}/config/config.default.js
exports.webpack = {
  webpackConfigList: EasyWebpack.getWebpackConfig('build/webpack.config.js')
};

easywebpack configuration

The default read webpack.config.js file under the project root directory.

// {app_root}/config/config.default.js
exports.webpack = {
  webpackConfigFile: 'build/webpack.config.js', // easywebpack config file path
};

see config/config.default.js for more detail.

Customize

  • mount app.webpack.fileSystem to app, you can customize the webpack memory file read logic
// read webpack browser build mode memory file content
app.webpack.fileSystem.readWebpackMemoryFile(filePath).then(fileContent =>{

})
  • render vue from webpack memory
'usestrict';
const path = require('path');
const egg = require('egg');
const vueServerRenderer = require('vue-server-renderer');
module.exports = class IndexController extends egg.Controller {
  async index(ctx) {
    const { app } = ctx;
    const filepath = path.join(app.config.view.root[0], 'app.js');
    // server render mode, the webpack config target:node
    const strJSBundle = await app.webpack.fileSystem.readWebpackMemoryFile(filepath);
    ctx.body = await vueServerRenderer.createBundleRenderer(strJSBundle).renderToString({});
  }
};

see lib/server.js for more detail.

  • monitor webpack build state
app.messenger.on(app.webpack.Constant.EVENT_WEBPACK_BUILD_STATE, data => {
  if (data.state) {
    const filepath = path.join(app.baseDir, 'config/manifest.json');
    const promise = app.webpack.fileSystem.readWebpackMemoryFile(filepath);
    promise.then(content => {
      fs.writeFileSync(filepath, content, 'utf8');
    });
  }
});

see lib/constant.js for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

egg-webpack's People

Contributors

brickyang avatar hubcarl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

egg-webpack's Issues

将egg-webpack升级到5.0.1后出现如下报错

报错信息
`
context.logger = context.compiler.getInfrastructureLogger('webpack-dev-middleware');

^
TypeError: context.compiler.getInfrastructureLogger is not a function
`

相关的依赖版本
`
"egg-bin": "^4.11.0",

"egg-ci": "^1.8.0",

"egg-mock": "^5.9.1",

"egg-ts-helper": "^1.25.9",

"egg-view-react-ssr": "^3.1.0",

"egg-webpack": "^5.0.1",

"egg-webpack-react": "^3.0.0",

"webpack-cli": "^5.0.1"

`

目前还没有找到原因为什么报这个错误

前端页面热更新无效!

单独使用egg-webpack,修改前端页面文件,webpack自动编译了,但是没有热更新,前端页面没有自动更新修改。
情况一:只使用koa-webpack-dev-middleware和koa-webpack-hot-middleware,修改前端页面文件,webpack自动编译了,而且自动热更新前端页面。热更新有效!
egg里的app.js内容如下:

const webpack = require('webpack')
const webpackDevMiddleware = require('koa-webpack-dev-middleware')
const webpacHotMiddleware = require('koa-webpack-hot-middleware')
const IS_PRODUCTION = process.env.NODE_ENV == 'production'
const devConfig = require('../config/webpack/webpack.dev.js')
const prodConfig = require('../config/webpack/webpack.prod.js')
const config = IS_PRODUCTION ? prodConfig : devConfig
const compiler = webpack(config)

module.exports = app => {
  app.beforeStart(async () => {
    app.use(webpackDevMiddleware(compiler, {
      publicPath: config.output.publicPath
    }))
    app.use(webpacHotMiddleware(compiler))
  })
}

情况二:删除上面的app.js,只启用egg-webpack(按照README.md里的说明),修改了前端页面文件,webpack自动编译了,但没有热更新前端页面。热更新无效!
{app_root}/config/config.default.js

exports.webpack = {
  webpackConfigList: [require('../../config/webpack/webpack.dev.js')]
}

lib/utils.js L84: spell error

version: v4.5.1
lib/utils.js L84

exports.getFramework = (root, option = {}) => {
  const vuePkgName = 'easywebpack-vue';
  const reactPkgName = 'easywebpack-vue';
  ...
}

it should be:

const reactPkgName = 'easywebpack-react';

js 请求404,如何设置 proxy

我看源码中已经把 proxy 配置关闭了,现在如何使用 proxy 功能呢?

agent.js

// webpack-tool not need proxy again
    const pluginConfig = Object.assign({}, config, { proxy: false });
    if (utils.isUseMultProcess(agent.baseDir, config)) {
      new MultProcessWebpackServer(agent, pluginConfig).start();
    } else {
      const port = utils.getPort(config.port);
      pluginConfig.port = port;
      new WebpackServer(agent, pluginConfig).start();
    }

koa-compose 4.2.0更新导致proxy中间件不生效

egg-webpack 是如下图将proxy插入到中间件数组中的,操作的是原数组对象
image
koa-compose 的4.2.0更新,flatten生成了一个新的中间件数组,proxy 是插入到原数组对象的,如果是在compose之后插入的话会不生效
image

需要自己实现render拦截,然后读取memoryFile吗

按照文档来配置,发现webpack服务起来以后,ctx.render还是读取的磁盘的html,没有插入打包后的script标签。现在我是自己写的一层中间件把返回的ctx.body用readWebpackMemoryFile('/game/entry.html')暴力替换掉。

正常的解决方案是自己实现一个render方法吗?还是我没用对这个插件?

谢谢

please set [egg-webpack] plugin config.module

请问 egg-webpack 一定需要 easywebpack-${framework} 来配合使用吗?单独使用 egg-webpack
webpackConfigList 已正确传入,但是报错 please set [egg-webpack] plugin config.module

proxy中间件会因为自定义的auth插件导致鉴权失败无法进行代理跳转

在新版本(easy-webpack4.0)后,egg-webpack 中将 proxy 中间件从原来插入到中间件首位的方式更换为插入到中间件后方。
https://github.com/easy-team/egg-webpack/blob/master/app.js?1570629460961#L37

因此会导致应用在自定义了 auth 等鉴权插件时并不能顺利地进行proxy跳转导致鉴权报错。目前处理方式只能在 auth中间件中进行过滤判断。请问这样的改动是为了解决什么问题,而我遇到的问题有更好的解决方式么?

how to use style-resources-loader

// webpack.config.js module: { rules: [{ test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader', { loader: 'style-resources-loader', options: { patterns: [ path.resolve(__dirname, 'path/to/scss/variables/*.scss'), path.resolve(__dirname, 'path/to/scss/mixins/*.scss'), ] } }] }] },

image

能否不强依赖 'easywebpack-react'

能否不强依赖 'easywebpack-react'.
安装完毕, 跑以下命令
npm run tsc:w & node index.js
提示

ERROR 30743 nodejs.unhandledExceptionError: Cannot find module 'easywebpack-react'
    at Function.Module._resolveFilename (module.js:542:15)
    at Function.Module._load (module.js:472:25)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)

can't fetch html page

在使用html-webpack-plugin更改html输出路径后,webpack-tool起的服务无法正常返回html

完善文档和一个潜在的 bug

  1. 仅使用普通的 ejs,jade 等作为模板引擎,该怎么使用你这个插件呢?能加个小例子吗?

  2. egg-webpack/lib/index.js 下面这个 hot middleware 是不是写错了?

exports['koa-webpack-hot-middleware'] = require('koa-webpack-dev-middleware');
exports['koa-webpack-dev-middleware'] = require('koa-webpack-dev-middleware');

请问如何开启Gzip压缩?

版本:weback 3

我在webpack.config.devServer 和app.config.webpack都尝试增加了 commress 配置,依然不起作用。

无法启动多个webpack,报错 ERROR 10316 nodejs.unhandledExceptionError: listen EADDRINUSE

我的config.default.js
config.webpack = {
port: 8000,
webpackConfigList: [require('../edesign/scripts/webpack.dev1.config.js'),require('../edesign/scripts/webpack.dev2.config.js') ],
}
错误
ERROR 10316 nodejs.unhandledExceptionError: listen EADDRINUSE

初步排查 当我如上配置两个 程序还是 进入了utils/server.js 而不是 utils/mult-process-server.js
getPort(target = 'web', offset = 0) {
const EASY_ENV_DEV_PORT = EASY_ENV_DEV_PORT_${this.pkgInfo.name};
const port = this.config.port || Number(process.env[EASY_ENV_DEV_PORT]) || 9000;
if (target === 'web') {
return port;
}
return port + offset;
}
而我的程序运行target 为web 默认返回了同个端口 所以端口冲突了

请问下这个插件只支持Egg1.x么?另外 视图(html、tpl)文件怎么调用内存中编译后的js文件?

根据Egg官网教程, egg-init egg-example --type=simple 生成 Egg2.x的脚手架;
配上webpack 3.x, 使用上这个插件发现修改了文件 命令窗口提示 编译成功, 然后 编译后的js文件没变化。 通过webpack-dev-server 教程了解到编译成功后的文件并没写入磁盘而是放在内存中。
所以有两个疑问:

1.插件只支持Egg1.x么?
看到egg-webpack/package.json 中:

"egg": "^1.0.0",

2.怎么调用编译成功后放在内存中的 js ?
README.md 中自定义中部分: 下述代码在Egg工程中哪个文件写的?

app.webpack.fileSystem.readWebpackMemoryFile(filePath).then(fileContent =>{

})

顺便问下,可以设置成 编译后文件不放内存 而是 写入磁盘么?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.