【问题标题】:Brunch: separating vendor and app javascript早午餐:分离供应商和应用程序 javascript
【发布时间】:2017-09-29 07:25:45
【问题描述】:

我从我们的项目供应商和应用程序中制作了两个 javascript 包。我按照documentation 建议的方式执行此操作,如我的 brunch-config.js 中的这个 sn-p 所示:

files: {
  javascripts: {
    joinTo: {
      'js/vendor.js': /^(?!source\/)/,
      'js/app.js': /^source\//
    },
    entryPoints: {
      'source/scripts/app.jsx': 'js/app.js'
    }
  }
}

我最终得到了一个 vendor.js 和一个 app.js。但请查看文件大小:

注意 app.js 比 vendor.js 大!这种大尺寸使观看速度比需要的慢。在检查 app.js 的内容后,它似乎包含 lodash、React 和其他库,我希望它来自 vendor.js。 vendor.js 似乎包含相同的库,这是我所期望的。

我的问题:为什么 app.js 中存在这些库?为什么 app.js 不从 vendor.js 中引用它们?

我可能遗漏了一些配置。这是我完整的 brunch-config.js 供您检查:

module.exports = {

  files: {
    javascripts: {
      joinTo: {
        'js/vendor.js': /^(?!source\/)/,
        'js/app.js': /^source\//
      },
      entryPoints: {
        'source/scripts/app.jsx': 'js/app.js'
      }
    },
    stylesheets: {joinTo: 'css/core.css'},
  },

  paths: {
    watched: ['source']
  },

  modules: {
    autoRequire: {
      'js/app.js': ['source/scripts/app']
    }
  },

  plugins: {
    babel: {presets: ['latest', 'react']},
    assetsmanager: {
      copyTo: {
        'assets': ['source/resources/*']
      }
    },
    static: {
      processors: [
        require('html-brunch-static')({
          processors: [
            require('pug-brunch-static')({
              fileMatch: 'source/views/home.pug',
              fileTransform: (filename) => {
                filename = filename.replace(/\.pug$/, '.html');
                filename = filename.replace('views/', '');
                return filename;
              }
            })
          ]
        })
      ]
    }
  },

  server: {
    run: true,
    port: 9005
  }

};

在 HTML 中我需要这样的文件:

<script type="text/javascript" src="js/vendor.js" defer></script>
<script type="text/javascript" src="js/app.js" defer></script>

我尝试设置订单对象,但无济于事:

files:
  javascripts: {
    joinTo: {
      'js/vendor.js': /^(?!source\/)/,
      'js/app.js': /^source\//
    },
    entryPoints: {
      'source/scripts/app.jsx': 'js/app.js'
    },
    order: {
      before: /^(?!source)/,
      after: /^source\//
    }
  }
}

这是我的 package.json:

{
  "version": "0.0.1",
  "devDependencies": {
    "assetsmanager-brunch": "^1.8.1",
    "babel-brunch": "^6.1.1",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-rewire": "^1.0.0-rc-5",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.10.3",
    "babel-plugin-transform-object-rest-spread": "^6.8.0",
    "babel-preset-react": "^6.3.13",
    "babel-register": "^6.11.6",
    "browser-sync-brunch": "^0.0.9",
    "brunch": "^2.10.9",
    "brunch-static": "^1.2.1",
    "chai": "^3.5.0",
    "es6-promise": "^3.2.1",
    "eslint-plugin-react": "^5.1.1",
    "expect": "^1.20.2",
    "html-brunch-static": "^1.3.2",
    "jquery": "~2.1.4",
    "jquery-mousewheel": "^3.1.13",
    "mocha": "^3.0.0",
    "nib": "^1.1.0",
    "nock": "^8.0.0",
    "oboe": "~2.1.2",
    "paper": "0.9.25",
    "path": "^0.12.7",
    "pug": "^2.0.0-beta10",
    "pug-brunch-static": "^2.0.1",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "react-redux": "^4.4.5",
    "redux": "^3.5.2",
    "redux-logger": "^2.6.1",
    "redux-mock-store": "^1.1.2",
    "redux-promise": "^0.5.3",
    "redux-thunk": "^2.1.0",
    "reselect": "^2.5.3",
    "spectrum-colorpicker": "~1.8.0",
    "stylus-brunch": "^2.10.0",
    "uglify-js-brunch": "^2.10.0",
    "unibabel": "~2.1.0",
    "when": "~3.4.5"
  },
  "dependencies": {
    "jwt-decode": "^2.1.0",
    "lodash": "^4.17.4",
    "postal": "^2.0.5",
    "rc-tree": "^1.3.9"
  },
  "scripts": {
    "test": "mocha --compilers js:babel-register"
  }
}

另一个想法,这可能与使用 require 而不是 import 有关吗?

如果我可以提供任何其他有用的信息,请告诉我。感谢您的帮助。

更新

这是我的文件夹结构,简化:

node_modules
source
|---resources
|---scripts
|---styles
|---views

这是brunch build产生的输出结构:

assets
css
|---core.css
js
|---app.js
|---app.js.map
|---vendor.js
|---vendor.js.map
home.html

自己调试! MVCE 可用。请遵循以下说明:

  1. 克隆这个example repository
  2. npm install
  3. brunch build(确保使用npm install brunch -g全局安装)
  4. 比较public/jsapp.jsvendor.js 的大小。它们应分别为 744 KB 和 737 KB。检查 app.js 的内容并注意库的内容。我的files.javascripts.joinTo['js/app.js'] 是如何包含正则表达式/^source\// 的?

【问题讨论】:

  • 你的文件夹结构是什么?也许您必须调整 conventions 以忽略您的库:brunch.io/docs/config#-conventions-
  • @JohannesFilter 感谢您的链接。我用我的文件夹结构更新了问题。 conventions 应该忽略我在 brunch source 中看到的 node_modules 文件夹。我将在 MVCE 上工作。
  • @JohannesFilter 我包含了一个指向 MVCE 存储库的链接。谢谢!

标签: javascript build require brunch


【解决方案1】:

问题是由joinToentryPoints 混合引起的。我假设使用您的配置,您首先将代码拆分为app.jsvendor.js,但随后app.js 会被entryPoints 的输出覆盖。

为了解决它,您必须选择以下选项之一:

选项 1

删除entryPoints 声明。这只会沿着提供的 RegEx 拆分您的代码。

选项 2

删除joinTo 声明并将entryPoints 更改为:

    entryPoints: {
      'source/scripts/app.jsx': {
        'js/vendor.js': /^(?!source\/)/,
        'js/app.js': /^source\//
      },
    }

结论

在这种情况下,两个选项的输出是相同的。但是使用entryPoints,代码得到了分析,并且只捆绑了需要的模块。因为没有任何不必要的模块,所以大小是一样的。请参阅this issue 了解更多信息。

【讨论】:

  • 谢谢!从documentation 看来,我并不清楚entryPointsjoinTo 在实践中是相互排斥的,但似乎是这样?
  • 是的,我还发现文档有点误导。我会尝试更新文档。
  • 我最终选择了一个配置,将 joinTo 用于库,entryPoints 用于我自己的代码(以便在加载时执行)。我将 joinTo 用于库,以便使用 browser-sync-brunch 可以工作,否则它不会被拾取。请参阅我对this question 的回答中的代码示例。
猜你喜欢
  • 2013-09-26
  • 2023-03-27
  • 2015-07-07
  • 2016-11-19
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2014-05-14
  • 2018-08-27
相关资源
最近更新 更多