简体   繁体   English

早午餐:分离供应商和应用程序javascript

[英]Brunch: separating vendor and app javascript

I have made two bundles of javascript from our project- vendor and app. 我从我们的项目供应商和应用程序中制作了两个javascript包。 I do this in the manner suggested by the documentation , as seen in this snippet from my brunch-config.js: 我按照文档建议的方式执行此操作,如我的brunch-config.js的此片段中所示:

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

And I end up with a vendor.js and an app.js. 我最终得到了vendor.js和app.js. But check out the file sizes: 但请查看文件大小: 文件视图

Note how app.js is larger than vendor.js! 注意app.js如何比vendor.js大! This large size makes watching slower than it needs to be. 这种大尺寸使得观看速度比需要的慢。 Upon inspecting the contents of app.js, it seemed to contain lodash, React, and other libraries, which I expected it to get from vendor.js. 在检查app.js的内容时,它似乎包含了lodash,React和其他库,我希望它可以从vendor.js获得。 And vendor.js seems to contain the same libraries, which I do expect. 并且vendor.js似乎包含相同的库,我期望它们。

My question: Why are the libraries present in app.js? 我的问题: 为什么app.js中存在库? Why does app.js not reference them from vendor.js? 为什么app.js不会从vendor.js引用它们?

It is possible I missing some piece of configuration. 我可能错过了一些配置。 Here is my full brunch-config.js for your examination: 这是我的完整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
  }

};

and in HTML I require these files like this: 在HTML中我需要这样的文件:

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

I tried setting the order object, but to no avail: 我尝试设置订单对象,但无济于事:

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

Here's my package.json: 这是我的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"
  }
}

Another thought, could this have to do with using require instead of import ? 另一个想法,这可能与使用require而不是import吗?

If there's any other information I can provide that would be helpful please let me know. 如果我能提供其他任何有用的信息,请告诉我们。 Thanks for your help. 谢谢你的帮助。

UPDATE UPDATE

Here's my folder structure, simplified: 这是我的文件夹结构,简化:

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

Here's the output structure produced by brunch build : 这是brunch build产生的输出结构:

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

Debug it for yourself! 自己调试一下! MVCE available. MVCE可用。 Follow these instructions: 请遵循以下说明:

  1. Clone this example repository 克隆此示例存储库
  2. npm install
  3. brunch build (make sure it is installed globally with npm install brunch -g ) brunch build (确保它使用npm install brunch -g全局npm install brunch -g
  4. Compare the sizes of app.js and vendor.js in public/js . 比较public/jsapp.jsvendor.js的大小。 They should be 744 KB and 737 KB respectively. 它们应分别为744 KB和737 KB。 Examine the contents of app.js and note the library stuff. 检查app.js的内容并记下库的内容。 How is my files.javascripts.joinTo['js/app.js'] including this with regex /^source\\// ? 我的files.javascripts.joinTo['js/app.js']如何用regex /^source\\//包括这个?

The problem is caused by the mixture of joinTo and entryPoints . 问题是由joinToentryPoints的混合引起的。 I assume that with your config, you first split your code in app.js and vendor.js but then the app.js gets overridden by the output of the entryPoints . 我假设使用您的配置,您首先在app.jsvendor.js拆分代码,但然后app.jsentryPoints的输出覆盖。

In order to solve it, you have to choose one of the options: 为了解决这个问题,您必须选择以下选项之一:

Option 1 选项1

Remove the entryPoints declaration. 删除entryPoints声明。 This will just split your code along the provided RegEx. 这将只是沿着提供的RegEx拆分您的代码。

Option 2 选项2

Remove the joinTo declaration and change the entryPoints to: 删除joinTo声明并将entryPoints更改为:

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

Conclusion 结论

In this very case, the output of both options is the same. 在这种情况下,两个选项的输出是相同的。 But with entryPoints the code get's analyzed and only needed modules get bundled. 但是使用entryPoints可以分析代码,只需要捆绑所需的模块。 Because there aren't any unnecessary modules, the size is the same. 因为没有任何不必要的模块,所以大小是相同的。 See this issue for more information. 有关更多信息,请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM