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

Sco*_*y H 6 javascript build require brunch

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

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

我最终得到了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
  }

};
Run Code Online (Sandbox Code Playgroud)

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

<script type="text/javascript" src="js/vendor.js" defer></script>
<script type="text/javascript" src="js/app.js" defer></script>
Run Code Online (Sandbox Code Playgroud)

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

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

这是我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

如果我能提供其他任何有用的信息,请告诉我们.谢谢你的帮助.

UPDATE

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

node_modules
source
|---resources
|---scripts
|---styles
|---views
Run Code Online (Sandbox Code Playgroud)

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

assets
css
|---core.css
js
|---app.js
|---app.js.map
|---vendor.js
|---vendor.js.map
home.html
Run Code Online (Sandbox Code Playgroud)

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

  1. 克隆此示例存储库
  2. npm install
  3. brunch build(确保全局安装npm install brunch -g)
  4. 比较的大小app.jsvendor.jspublic/js.它们应分别为744 KB和737 KB.检查app.js的内容并记下库的内容.我files.javascripts.joinTo['js/app.js']用正则表达式包括这个怎么样/^source\//

Joh*_*ter 6

该问题通过的混合物引起的joinToentryPoints.我假设使用您的配置,您首先将代码拆分app.js,vendor.js然后app.js由输出覆盖entryPoints.

为了解决这个问题,您必须选择以下选项之一:

选项1

删除entryPoints声明.这将只是沿着提供的RegEx拆分您的代码.

选项2

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

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

结论

在这种情况下,两个选项的输出是相同的.但是entryPoints对代码进行了分析,只需要捆绑所需的模块.因为没有任何不必要的模块,所以大小相同.有关更多信息,请参阅此问题.