https://webpack.docschina.org/api/node/

  1. webpack-demo
  2. ├── src
  3. └── App.vue
  4. ├── webpack-node.js
  5. ├── webpack.config.js
  6. └── package.json

package.json

  1. {
  2. "name": "vue-server-renderer-demo",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1",
  8. "build:server": "cross-env BASE_BUILD=server npx webpack",
  9. "build:client": "cross-env BASE_BUILD=client npx webpack",
  10. "dev": "node ./server.js"
  11. },
  12. "author": "",
  13. "license": "ISC",
  14. "dependencies": {
  15. "express": "^4.17.1",
  16. "vue": "^2.6.14",
  17. "vue-router": "^3.5.2",
  18. "vue-server-renderer": "^2.6.14"
  19. },
  20. "devDependencies": {
  21. "vue-loader": "^15.9.8",
  22. "vue-loader-plugin": "^1.3.0",
  23. "vue-template-compiler": "^2.6.14",
  24. "webpack": "^5.52.1",
  25. "webpack-cli": "^4.8.0"
  26. }
  27. }

webpack.config.js

  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const VueLoaderPlugin = require('vue-loader/lib/plugin')
  4. module.exports = {
  5. mode: 'development',
  6. devtool: 'source-map',
  7. entry: {
  8. app: './src/App.vue'
  9. },
  10. output: {
  11. path: path.join(__dirname, "./dist/"), //文件输出目录
  12. filename: '[name].js',
  13. chunkFilename: '[name].common.js',
  14. },
  15. module: {
  16. rules: [
  17. {
  18. test: /\.vue$/,
  19. use: [
  20. 'vue-loader',
  21. ]
  22. }
  23. ]
  24. },
  25. externals: {
  26. vue: 'Vue',
  27. },
  28. plugins: [
  29. new webpack.DefinePlugin({
  30. 'process.env.BASE_BUILD': `'${process.env.BASE_BUILD}'`
  31. }),
  32. new VueLoaderPlugin()
  33. ]
  34. }

App.vue

  1. <script>
  2. export default {
  3. template: `<div id="root"><router-view></router-view></div>`,
  4. }
  5. </script>

webpack-node.js

webpack 1

  1. const webpack = require('webpack');
  2. const config = require('./webpack.config.js')
  3. webpack(config, (err, stats) => { // [Stats Object](#stats-object)
  4. if (err || stats.hasErrors()) {
  5. // [在这里处理错误](#error-handling)
  6. console.log(err)
  7. return
  8. }
  9. // 处理完成
  10. console.log('webpack build 成功')
  11. });

webpack 2

  1. // Compiler 实例
  2. // 如果你掉用 webpack(config),不传入回调函数,会返回一个Compiler实例
  3. const webpack = require('webpack');
  4. const config = require('./webpack.config.js')
  5. const compiler = webpack(config)
  6. compiler.run((err, stats) => { // [Stats Object](#stats-object)
  7. if (err || stats.hasErrors()) {
  8. // [在这里处理错误](#error-handling)
  9. console.log(err)
  10. return
  11. }
  12. // 处理完成
  13. console.log('webpack build 成功')
  14. compiler.close((closeErr) => {
  15. // 不要忘记关闭编译器,这样低优先级的工作(比如持久缓存)就有机会完成。
  16. });
  17. })

webpack 3

  1. /**
  2. * 调用 watch 方法会触发 webpack 执行,但之后会监听变更(很像 CLI 命令: webpack --watch),
  3. * 一旦 webpack 检测到文件变更,就会重新执行编译。 该方法返回一个 Watching 实例。
  4. */
  5. const compiler = webpack(config);
  6. const watching = compiler.watch({
  7. // [watchOptions](/configuration/watch/#watchoptions) 示例
  8. aggregateTimeout: 300,
  9. poll: undefined
  10. }, (err, stats) => { // [Stats Object](#stats-object)
  11. // 这里打印 watch/build 结果...
  12. console.log(stats);
  13. });
  14. // 关闭 Watching
  15. watching.close((closeErr) => {
  16. console.log('Watching Ended.');
  17. });
  18. // 使用 watching.invalidate
  19. // 你可以手动使当前编译循环(compiling round)无效, 而不会停止监听进程(process):
  20. watching.invalidate();