├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | es3ify-webpack-plugin 2 | ===================== 3 | 4 | [es3ify-loader](https://github.com/sorrycc/es3ify-loader) is better, [For detail.](https://github.com/BryceHQ/es3ify-webpack-plugin/issues/5) You should use it. 5 | 6 | ------------- 7 | A simple webpack plugin to es3ify your code for old versions of ie, such as ie8. 8 | 9 | I am new to webpack and babel. And I need to support ie8, but I didn't find a solution to handle it. [es3ify-loader](https://github.com/sorrycc/es3ify-loader) helped, but it couldn't transform the module in node_modules. So I write the simple plugin to resolve it. Hope it can help you. 10 | 11 | ### Usage 12 | 13 | ``` 14 | npm install es3ify-webpack-plugin --save-dev 15 | ``` 16 | Then in webpack.config.js 17 | 18 | ``` 19 | var es3ifyPlugin = require('es3ify-webpack-plugin'); 20 | 21 | plugins: [ 22 | new es3ifyPlugin() 23 | ] 24 | ``` 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var SourceMapConsumer = require("source-map").SourceMapConsumer; 2 | var SourceMapSource = require("webpack-sources").SourceMapSource; 3 | var RawSource = require("webpack-sources").RawSource; 4 | var ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers"); 5 | const name = "es3ify"; 6 | var transform = require(name).transform; 7 | 8 | 9 | function Es3ifyPlugin(options) { 10 | if (typeof options !== "object") options = {}; 11 | if (typeof options.compressor !== "undefined") { 12 | options.compress = options.compressor; 13 | } 14 | this.options = options; 15 | } 16 | module.exports = Es3ifyPlugin; 17 | 18 | Es3ifyPlugin.prototype.apply = function (compiler) { 19 | var options = this.options; 20 | options.test = options.test || /\.js($|\?)/i; 21 | 22 | if (compiler.hooks) { 23 | compiler.hooks.compilation.tap(name, function (compilation) { 24 | if (options.sourceMap !== false) { 25 | compilation.hooks.buildModule.tap(name, buildModuleHook); 26 | } 27 | compilation.hooks.optimizeChunkAssets.tapAsync(name, optimizeChunkAssetsHook(compilation)); 28 | }); 29 | } else { 30 | compiler.plugin("compilation", function (compilation) { 31 | if (options.sourceMap !== false) { 32 | compilation.plugin("build-module", buildModuleHook); 33 | } 34 | compilation.plugin("optimize-chunk-assets", optimizeChunkAssetsHook(compilation)); 35 | }); 36 | } 37 | 38 | function buildModuleHook(module) { 39 | // to get detailed location info about errors 40 | module.useSourceMap = true; 41 | } 42 | 43 | function optimizeChunkAssetsHook(compilation) { 44 | return function (chunks, callback) { 45 | var files = []; 46 | chunks.forEach(function (chunk) { 47 | chunk.files.forEach(function (file) { 48 | files.push(file); 49 | }); 50 | }); 51 | compilation.additionalChunkAssets.forEach(function (file) { 52 | files.push(file); 53 | }); 54 | files = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)); 55 | files.forEach(function (file) { 56 | try { 57 | var asset = compilation.assets[file]; 58 | var inputSourceMap, input; 59 | if (options.sourceMap !== false) { 60 | if (asset.sourceAndMap) { 61 | var sourceAndMap = asset.sourceAndMap(); 62 | inputSourceMap = sourceAndMap.map; 63 | input = sourceAndMap.source; 64 | } else { 65 | inputSourceMap = asset.map(); 66 | input = asset.source(); 67 | } 68 | var sourceMap = new SourceMapConsumer(inputSourceMap); 69 | } else { 70 | input = asset.source(); 71 | } 72 | var map; 73 | if (options.sourceMap !== false) { 74 | map = inputSourceMap; 75 | } 76 | var stream = transform(input); 77 | compilation.assets[file] = (map ? 78 | new SourceMapSource(stream, file, map, input, inputSourceMap) : 79 | new RawSource(stream)); 80 | } catch (err) { 81 | if (err.line) { 82 | compilation.errors.push(new Error(file + " from es3ify\n" + err.message + " [" + file + ":" + err.line + "," + err.col + "]")); 83 | } else if (err.msg) { 84 | compilation.errors.push(new Error(file + " from es3ify\n" + err.msg)); 85 | } else 86 | compilation.errors.push(new Error(file + " from es3ify\n" + err.stack)); 87 | } 88 | }); 89 | callback(); 90 | } 91 | } 92 | }; 93 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es3ify-webpack-plugin", 3 | "version": "0.0.2", 4 | "description": "a webpack plugin to es3ify your code for old versions of ie, such as ie8.", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/BryceHQ/es3ify-webpack-plugin.git" 9 | }, 10 | "scripts": {}, 11 | "keywords": [ 12 | "webpack", 13 | "plugin", 14 | "es3ify", 15 | "ie8" 16 | ], 17 | "author": "bryce (https://github.com/brycehq)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/BryceHQ/es3ify-webpack-plugin/issues" 21 | }, 22 | "homepage": "https://github.com/BryceHQ/es3ify-webpack-plugin", 23 | "dependencies": { 24 | "es3ify": "^0.2.2", 25 | "source-map": "^0.5.6", 26 | "webpack-sources": "^0.1.2" 27 | }, 28 | "devDependencies": { 29 | "webpack": "^1.13.1" 30 | } 31 | } 32 | --------------------------------------------------------------------------------