├── .gitignore ├── README.md ├── V8LazyParsedFunctionModulePlugin.js ├── V8LazyParsedFunctionModuleTemplatePlugin.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | *.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # V8LazyParseWebpackPlugin 2 | 3 | This is a webpack plugin designed to exploit the V8/Chakra engines treatment of functions with parens wrapped around them. This lazy loads the parsing decreasing initial load time. This is experimental. 4 | 5 | ## Install 6 | 7 | 8 | To install run the following npm install command: 9 | ```shell 10 | npm install v8-lazy-parse-webpack-plugin --save-dev 11 | ``` 12 | 13 | ## Usage 14 | 15 | **webpack.config.js** 16 | 17 | ```javascript 18 | const V8LazyParseWebpackPlugin = require('v8-lazy-parse-webpack-plugin'); 19 | 20 | module.exports = config; 21 | 22 | let config = { 23 | /*...*/ 24 | plugins: [ 25 | new V8LazyParseWebpackPlugin(), 26 | ] 27 | }; 28 | ``` 29 | 30 | ## Disclaimer 31 | 32 | This plugin is desgined specifically for the V8 engine, so don't expect to have the same effect on all browsers or JS engines. 33 | 34 | ## Report your findings 35 | I'm very curious what the performance increase is for this for larger applications. If you find success from this, please submit an issue with before and after photos of your chrome dev tool timelines, I'll include it somehwere in the readme etc. 36 | 37 | ## UglifyJSPlugin 38 | 39 | If you are using UglifyJsPlugin, you must set compress option `negate_iife` to false if you are going to minimize your code. 40 | 41 | ```javascript 42 | new webpack.optimize.UglifyJsPlugin({ 43 | output: { 44 | comments: false 45 | }, 46 | compress: { 47 | warnings: false, 48 | conditionals: true, 49 | unused: true, 50 | comparisons: true, 51 | sequences: true, 52 | dead_code: true, 53 | evaluate: true, 54 | if_return: true, 55 | join_vars: true, 56 | negate_iife: false 57 | } 58 | }) 59 | 60 | ``` 61 | 62 | ## Further Reading 63 | 64 | - [Initial discussion in Rollup Repository](https://github.com/rollup/rollup/pull/774) 65 | -------------------------------------------------------------------------------- /V8LazyParsedFunctionModulePlugin.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Authors: Sean Larkin @thelarkinn, Tobias Koppers @sokra 4 | */ 5 | var V8LazyParsedFunctionModuleTemplatePlugin = require("./V8LazyParsedFunctionModuleTemplatePlugin"); 6 | var RequestShortener = require("webpack/lib/RequestShortener"); 7 | 8 | function V8LazyParsedFunctionModulePlugin(options, requestShortener) { 9 | this.options = options; 10 | this.requestShortener = requestShortener; 11 | } 12 | module.exports = V8LazyParsedFunctionModulePlugin; 13 | V8LazyParsedFunctionModulePlugin.prototype.apply = function(compiler) { 14 | compiler.plugin("this-compilation", function(compilation) { 15 | compilation.moduleTemplate.requestShortener = this.requestShortener || new RequestShortener(compiler.context); 16 | compilation.moduleTemplate.apply(new V8LazyParsedFunctionModuleTemplatePlugin()); 17 | }.bind(this)); 18 | }; 19 | -------------------------------------------------------------------------------- /V8LazyParsedFunctionModuleTemplatePlugin.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Authors: Sean Larkin @thelarkinn, Tobias Koppers @sokra 4 | */ 5 | var ConcatSource = require("webpack-sources").ConcatSource; 6 | var PrefixSource = require("webpack-sources").PrefixSource; 7 | 8 | function V8LazyParsedFunctionModuleTemplatePlugin() {} 9 | module.exports = V8LazyParsedFunctionModuleTemplatePlugin; 10 | 11 | V8LazyParsedFunctionModuleTemplatePlugin.prototype.apply = function(moduleTemplate) { 12 | moduleTemplate.plugin("render", function(moduleSource, module) { 13 | var source = new ConcatSource(); 14 | var defaultArguments = ["module", "exports"]; 15 | if((module.arguments && module.arguments.length !== 0) || module.hasDependencies()) { 16 | defaultArguments.push("__webpack_require__"); 17 | } 18 | source.add("/***/ (function(" + defaultArguments.concat(module.arguments || []).join(", ") + ") {\n\n"); 19 | if(module.strict) source.add("\"use strict\";\n"); 20 | source.add(moduleSource); 21 | source.add("\n\n/***/ })"); 22 | return source; 23 | }); 24 | moduleTemplate.plugin("package", function(moduleSource, module) { 25 | if (moduleSource.children.length === 4) { 26 | moduleSource.children = [moduleSource.children[2]]; 27 | } else if (moduleSource.children.length === 3) { 28 | moduleSource.children = [moduleSource.children[1]]; 29 | } 30 | 31 | if(this.outputOptions.pathinfo) { 32 | var source = new ConcatSource(); 33 | var req = module.readableIdentifier(this.requestShortener); 34 | if(Array.isArray(module.providedExports)) 35 | source.add("/* exports provided: " + module.providedExports.join(", ") + " */\n"); 36 | else if(module.providedExports) 37 | source.add("/* unknown exports provided */\n"); 38 | if(Array.isArray(module.usedExports)) 39 | source.add("/* exports used: " + module.usedExports.join(", ") + " */\n"); 40 | else if(module.usedExports) 41 | source.add("/* all exports used */\n"); 42 | source.add("/*!****" + req.replace(/./g, "*") + "****!*\\\n"); 43 | source.add(" !*** " + req.replace(/\*\//g, "*_/") + " ***!\n"); 44 | source.add(" \\****" + req.replace(/./g, "*") + "****/\n"); 45 | source.add(moduleSource); 46 | return source; 47 | } 48 | return moduleSource; 49 | }); 50 | moduleTemplate.plugin("hash", function(hash) { 51 | hash.update("V8LazyParsedFunctionModuleTemplatePlugin"); 52 | hash.update("2"); 53 | }); 54 | }; 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var plugin = require('./V8LazyParsedFunctionModulePlugin'); 2 | 3 | module.exports = plugin; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "v8-lazy-parse-webpack-plugin", 3 | "version": "0.3.0", 4 | "description": "This is a webpack plugin designed to exploit the V8 engines treatment of functions with parens wrapped around them. This lazy loads the parsing decreasing initial load time. — Edit", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Sorry no tests yet\"", 8 | "preversion": "npm test", 9 | "version": "git add .", 10 | "postversion": "git push && git push --tags && npm publish ./" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/TheLarkInn/V8LazyParseWebpackPlugin.git" 15 | }, 16 | "keywords": [ 17 | "webpack", 18 | "plugin", 19 | "webpack-plugin", 20 | "lazy-parse", 21 | "parse", 22 | "webpack-plugin" 23 | ], 24 | "author": "Sean Larkin ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/TheLarkInn/V8LazyParseWebpackPlugin/issues" 28 | }, 29 | "homepage": "https://github.com/TheLarkInn/V8LazyParseWebpackPlugin#readme", 30 | "devDependencies": { 31 | "webpack": "^2.1.0-beta.22" 32 | }, 33 | "dependencies": { 34 | "webpack-sources": "^0.1.2" 35 | } 36 | } 37 | --------------------------------------------------------------------------------