├── .babelrc ├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGES.md ├── LICENSE ├── README.md ├── creating-presets.md ├── package.json ├── src └── index.js └── test ├── .babelrc ├── .eslintrc ├── add-export └── general │ ├── as-default │ ├── actual.js │ └── expected.js │ ├── normal │ ├── actual.js │ └── expected.js │ ├── only-named │ ├── actual.js │ └── expected.js │ ├── options.json │ ├── re-exporting │ ├── actual.js │ └── expected.js │ └── with-named │ ├── actual.js │ └── expected.js ├── fixtures ├── auxiliary-comment │ ├── options.json │ └── overview │ │ ├── actual.js │ │ └── expected.js ├── interop │ ├── exports-default-non-function │ │ ├── actual.js │ │ └── expected.js │ ├── exports-default │ │ ├── actual.js │ │ └── expected.js │ ├── exports-from │ │ ├── actual.js │ │ └── expected.js │ ├── exports-named │ │ ├── actual.js │ │ └── expected.js │ ├── exports-variable │ │ ├── actual.js │ │ └── expected.js │ ├── hoist-function-exports │ │ ├── actual.js │ │ └── expected.js │ ├── illegal-export-esmodule-2 │ │ ├── actual.js │ │ └── options.json │ ├── illegal-export-esmodule │ │ ├── actual.js │ │ └── options.json │ ├── imports-default │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ ├── imports-glob │ │ ├── actual.js │ │ └── expected.js │ ├── imports-hoisting │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ ├── imports-mixing │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ ├── imports-named │ │ ├── actual.js │ │ └── expected.js │ ├── imports │ │ ├── actual.js │ │ └── expected.js │ ├── module-shadow │ │ ├── actual.js │ │ └── expected.js │ ├── options.json │ ├── overview │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ └── remap │ │ ├── actual.js │ │ └── expected.js ├── options.json ├── regression │ ├── T7160 │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ ├── T7165 │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ ├── T7199 │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json │ └── es3-compatibility │ │ ├── actual.js │ │ ├── expected.js │ │ └── options.json ├── source-map │ ├── exec.js │ └── options.json └── strict │ ├── export-1 │ ├── actual.js │ └── expected.js │ ├── export-2 │ ├── actual.js │ └── expected.js │ ├── export-all │ ├── actual.js │ ├── expected.js │ └── options.json │ ├── export │ ├── actual.js │ └── expected.js │ ├── import │ ├── actual.js │ ├── expected.js │ └── source-mappings.json │ └── options.json ├── index.js ├── mocha.opts └── no-mangle ├── auxiliary-comment ├── options.json └── overview │ ├── actual.js │ └── expected.js ├── interop ├── exports-default-non-function │ ├── actual.js │ └── expected.js ├── exports-default │ ├── actual.js │ └── expected.js ├── exports-from │ ├── actual.js │ └── expected.js ├── exports-named │ ├── actual.js │ └── expected.js ├── exports-variable │ ├── actual.js │ └── expected.js ├── hoist-function-exports │ ├── actual.js │ └── expected.js ├── illegal-export-esmodule-2 │ ├── actual.js │ └── options.json ├── illegal-export-esmodule │ ├── actual.js │ └── options.json ├── imports-default │ ├── actual.js │ └── expected.js ├── imports-glob │ ├── actual.js │ └── expected.js ├── imports-hoisting │ ├── actual.js │ ├── expected.js │ └── options.json ├── imports-mixing │ ├── actual.js │ └── expected.js ├── imports-multiple-default │ ├── actual.js │ └── expected.js ├── imports-named │ ├── actual.js │ └── expected.js ├── imports │ ├── actual.js │ └── expected.js ├── module-shadow │ ├── actual.js │ └── expected.js ├── options.json ├── overview │ ├── actual.js │ └── expected.js └── remap │ ├── actual.js │ └── expected.js ├── options.json ├── regression └── T7165 │ ├── actual.js │ ├── expected.js │ └── options.json └── strict ├── export-1 ├── actual.js └── expected.js ├── export-2 ├── actual.js └── expected.js ├── export-all ├── actual.js ├── expected.js └── options.json ├── export ├── actual.js └── expected.js ├── import ├── actual.js └── expected.js └── options.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": ["transform-flow-strip-types"] 4 | } -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "env": { 4 | "node": true 5 | }, 6 | rules: { 7 | // general 8 | 9 | "strict": [2, "global"], 10 | "one-var": [2, "never"], 11 | "max-len": [1, 150, 4, {"ignoreComments": true, "ignoreUrls": true}], 12 | 13 | // code errors 14 | "vars-on-top": 0, 15 | "consistent-return": 0, 16 | "no-with": 2, 17 | "no-undef": 2, 18 | "no-new-object": 2, 19 | "no-array-constructor": 2, 20 | "no-shadow-restricted-names": 1, 21 | "no-redeclare": 2, 22 | "eqeqeq": 2, 23 | "no-octal": 2, 24 | "eol-last": 2, 25 | "no-dupe-args": 2, 26 | "no-unexpected-multiline": 2, 27 | "valid-typeof": 2, 28 | "valid-jsdoc": 2, 29 | "no-sparse-arrays": 2, 30 | "block-scoped-var": 2, 31 | "new-parens": 2, 32 | "no-eval": 2, 33 | "no-unneeded-ternary": 2, 34 | "no-new-wrappers": 2, 35 | "no-fallthrough": 2, 36 | "no-lone-blocks": 2, 37 | "no-cond-assign": 2, 38 | 39 | // code warnings 40 | 41 | "no-nested-ternary": 1, 42 | "consistent-this": [1, "that"], 43 | "radix": 1, 44 | "camelcase": 1, 45 | "new-cap": 1, 46 | "dot-location": [1, "property"], 47 | 48 | "no-unused-vars": 1 // could be reasons such as "catch" and placeholders 49 | }, 50 | "ecmaFeatures": { 51 | "modules": true 52 | } 53 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | lib/ 3 | node_modules/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | test 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '5.0' 4 | - '4.0' 5 | notifications: 6 | email: false 7 | sudo: false -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | ##### changelog 2 | 3 | ### 6.6.5 4 | 5 | * Sync version number with babel 6 | * Make `noMangle` an option 7 | * Refactor tests to keep originals as-is for testing with option disabled 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2015 Sebastian McKenzie 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-transform-es2015-modules-commonjs-simple 2 | 3 | Use ES6 Module Format While Preserving Imported Symbol Names 4 | 5 | [![NPM version](http://img.shields.io/npm/v/babel-plugin-transform-es2015-modules-commonjs-simple.svg?style=flat-square)](https://www.npmjs.org/package/babel-plugin-transform-es2015-modules-commonjs-simple) 6 | [![Travis build status](http://img.shields.io/travis/jamietre/babel-plugin-transform-es2015-modules-commonjs-simple/master.svg?style=flat-square)](https://travis-ci.org/jamietre/babel-plugin-transform-es2015-modules-commonjs-simple) 7 | 8 | The regular *babel-plugin-transform-es2015-modules-commonjs* module mangles symbol names in order to implement the ES6 modules feature of exporting bindings rather than references or values. 9 | 10 | However, JavaScript source maps don't currently support mapping symbol names. So when debugging using source maps, any imports will not be available under their original name, which can be a frustrating experience. This module ensures that all symbol names are preserved. 11 | 12 | This module adds a `noMangle` option that, when true, prevents variable names from being mangled. 13 | 14 | This module also adds an `addExport` option to allow modules with a single default export to be interoperable with CommonJS as they were in babel <6. 15 | 16 | ## Installation 17 | 18 | From [npm](https://www.npmjs.com/package/babel-plugin-transform-es2015-modules-commonjs-simple): 19 | 20 | ```sh 21 | $ npm install --save-dev babel-plugin-transform-es2015-modules-commonjs-simple 22 | ``` 23 | 24 | ## Usage 25 | 26 | ##### Without Presets 27 | 28 | If you are not using a babel preset, just include this module as a plugin instead of `transform-es2015-modules-commonjs` and add the option `noMangle: true`: 29 | 30 | .babelrc 31 | 32 | { 33 | "plugins": [ 34 | "transform-es2015-arrow-functions", 35 | "transform-es2015-template-literals", 36 | ... 37 | ["transform-es2015-modules-commonjs-simple", { 38 | "noMangle": true 39 | }] 40 | ], 41 | "sourceMaps": true 42 | } 43 | 44 | ##### With Babel Native Presets 45 | 46 | If you are using a preset, it probably already includes `babel-plugin-transform-es2015-commonjs`. While you might be able to just add the `plugin` to your `.babelrc`, it doesn't appear to work reliably in all circumstances, so it's not recommended. [See this discussion for details.](https://github.com/babel/babel-loader/issues/217). 47 | 48 | I wrote up some basic instructions on [creating your own preset using this module transformer](./creating-presets.md) based on the native `es2015` preset. 49 | 50 | ##### With ES2015 preset 51 | 52 | If you're just using the generic ES2015 preset, then someone's been gracious enough to create a package for the ES2015 babel preset that has everything *except* `babel-plugin-transform-es2015-commonjs`, [here.](https://github.com/gajus/babel-preset-es2015-webpack) 53 | 54 | So, with that installed: 55 | 56 | .babelrc 57 | 58 | { 59 | "presets": [ 60 | "es2015-webpack" 61 | ], 62 | "plugins": [ 63 | ["transform-es2015-modules-commonjs-simple", { 64 | "noMangle": true 65 | }] 66 | ] 67 | "sourceMaps": true 68 | } 69 | 70 | 71 | ##### Any other clever way to override babel presets? 72 | 73 | There's a package called [modify-babel-preset](https://github.com/developit/modify-babel-preset) which ought to let you override any preset with a lot less code. It's a good idea, and it would be nice to not have to keep your own preset "fork" in sync with the current default one. But it didn't seem to work for me; seems to need some updates for the latest version of babel. 74 | 75 | ## Options 76 | 77 | ##### noMangle 78 | 79 | When true, will prevent variable names from being mangled. 80 | 81 | ##### addExports 82 | 83 | when true, will enable ES6 modules with a single default export to be used interoperably with CommonJS `require`. This matches the functionality of Babel <6, and basically adds this to the end of modules with a single default export: 84 | 85 | module.exports = exports['default']; 86 | 87 | This allows you to use those modules as: 88 | 89 | var foo = require('foo') 90 | 91 | instead of 92 | 93 | var foo = require('foo').default 94 | 95 | 96 | ## Building 97 | 98 | #### To compile: 99 | 100 | npm install 101 | 102 | #### To run tests: 103 | 104 | All the tests from the native babel transform are under `fixtures`. Tests that validate the new features are under `nomangle` and `addexport` 105 | 106 | npm test 107 | 108 | You can run individual tests using a special convention, see `test/index.js` 109 | 110 | ## Versioning 111 | 112 | Starting with 6.6.5, the versions of this package will track the versions of the official `babel-plugin-transform-es2015-modules-commonjs` for simplicity. Since babel is monorepo, it's not possible to just fork the module itself, so I am tracking upstream changes in a fork [babel](https://github.com/jamietre/babel). 113 | 114 | ## Details of what's really going on here 115 | 116 | There's a reason the native babel transform mangles variable names, and you might need this. The ES6 module format specifies that bindings, and not objects, are exported. This means if an exported entity mutates within the module, consumers that reference the exported entity will refer to the actual current value of the symbol, not the reference or value that was originally exported. 117 | 118 | #### How does Babel enable dynamic bindings? 119 | 120 | In order to implement the complete ES2015 feature set, when compiling ES2105 modules to CommonJS format, Babel changes references to imported symbols so they always made to refer a child property, e.g. 121 | 122 | import foo from 'bar'; 123 | console.log(foo.text) 124 | 125 | is transformed to 126 | 127 | var _foo = require('bar'); 128 | var _foo2 = defaultExportInterop(_foo); 129 | 130 | console.log(_foo2["default"].text) 131 | 132 | Since the parent object never changes, any changes in values will always be current, as they are always resolved when evaluated. 133 | 134 | #### Why is this a problem? 135 | 136 | This is a problem because source maps don't currently support mapping of symbol names. This makes degugging difficult if the names aren't the same as the source code. If you are writing good, modular code, you probably import a ton of symbols. If you are using source maps, you have probably been frustrated because none of your symbols are defined when debugging. 137 | 138 | This plugin was created to give ES6 developers the option to sacrifice a feature that they may never use for the sake of preserving symbol names in the compiled code. This code with `babel-plugin-transform-es2015-modules-commonjs-simple` becomes: 139 | 140 | var _foo = require('bar'); 141 | var foo = defaultExportInterop(_foo).default; 142 | 143 | console.log(foo.text) 144 | 145 | #### What exactly am I sacrificing here? 146 | 147 | This feature isn't especially well-known. Most articles that explain ES6 modules don't mention it at all. [Dr.Rauschmayer of course does](http://www.2ality.com/2015/07/es6-module-exports.html), since he knows all. That article does a good job of explaining why it could be useful. 148 | 149 | However, this feature didn't exist with CommonJS modules. It's also unlikely that anything you consume from npm will use it today, since most everything on npm is expected to be a CommonJS module that runs in node, which doesn't know what an ES6 module is. So this is probably not happening anytime soon. If a module author today wants to export a binding, he's probably exporting an object and telling people to refer to properties of the object. 150 | 151 | So if you don't plan to mutate your exports or create circular references in your own code, you should be quite safe to use this. 152 | 153 | #### Couldn't I just use "require" instead? 154 | 155 | Sure! If you use `require` to import modules instead of `import`, then symbol names won't be mangled. 156 | 157 | Personally, though, I would much rather write my code with a forward-compatible module syntax, and not use a feature, then write non-forward-compatible CommonJS modules that also don't use a feature that doesn't exist with CommonJS modules anyway. 158 | 159 | #### ... and what happens to all my code when source maps start supporting symbols mapping? 160 | 161 | Nothing at all. Using this module makes no demands on your source code, it just changes the compiled output. You can turn off the `noMangle` option to restore the native babel behavior, or swap in the offical plugin with no code changes required. 162 | 163 | ## Relationship to Official Babel Transform 164 | 165 | When the `noMangle` option is false, this is code-identical to the native babel [source babel-plugin-transform-es2015-commonjs](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-modules-commonjs) transform. This plugin tracks changes to the official transform, but extends it with the options described here. 166 | 167 | 168 | -------------------------------------------------------------------------------- /creating-presets.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-transform-es2015-modules-commonjs-simple 2 | 3 | Presets in babel are implemented as any other module. You can also just define them directly in your project, e.g. 4 | 5 | my-project/ 6 | babel-presets/ 7 | my-es2015-preset 8 | index.js 9 | 10 | You can start by just copying `index.js` from the default [babel-preset-es2015](https://github.com/babel/babel/blob/master/packages/babel-preset-es2015/index.js) module, and change the module transformer. As of version 6.6.0 it might look like this: 11 | 12 | module.exports = { 13 | plugins: [ 14 | require("babel-plugin-transform-es2015-template-literals"), 15 | require("babel-plugin-transform-es2015-literals"), 16 | require("babel-plugin-transform-es2015-function-name"), 17 | require("babel-plugin-transform-es2015-arrow-functions"), 18 | require("babel-plugin-transform-es2015-block-scoped-functions"), 19 | require("babel-plugin-transform-es2015-classes"), 20 | require("babel-plugin-transform-es2015-object-super"), 21 | require("babel-plugin-transform-es2015-shorthand-properties"), 22 | require("babel-plugin-transform-es2015-duplicate-keys"), 23 | require("babel-plugin-transform-es2015-computed-properties"), 24 | require("babel-plugin-transform-es2015-for-of"), 25 | require("babel-plugin-transform-es2015-sticky-regex"), 26 | require("babel-plugin-transform-es2015-unicode-regex"), 27 | require("babel-plugin-check-es2015-constants"), 28 | require("babel-plugin-transform-es2015-spread"), 29 | require("babel-plugin-transform-es2015-parameters"), 30 | require("babel-plugin-transform-es2015-destructuring"), 31 | require("babel-plugin-transform-es2015-block-scoping"), 32 | require("babel-plugin-transform-es2015-typeof-symbol"), 33 | 34 | // replaces babel-plugin-transform-es2015-modules-commonjs 35 | // note the "default" after the require -- for some reason you can't actually compile babel modules without them being exported 36 | // as "default" with the regular babeb commonjs transform, yet the official babel modules themselves don't export "default". 37 | // This one does -- so you need to refer to ".default" when requiring it. 38 | 39 | [require("babel-plugin-transform-es2015-modules-commonjs-simple").default, { 40 | noMangle: true, 41 | addExports: true 42 | }], 43 | [require("babel-plugin-transform-regenerator"), { async: false, asyncGenerators: false }], 44 | ] 45 | }; 46 | 47 | Then in your `.babelrc`: 48 | 49 | { 50 | "presets": ["./babel-presets/my-es2015-preset"], 51 | "noMangle": true, 52 | "sourceMaps": true 53 | } 54 | 55 | Make sure you include `babel-preset-es2015` as a dev dependency of your project, too, so all the babel plugins above are installed. 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-transform-es2015-modules-commonjs-simple", 3 | "version": "6.7.4", 4 | "description": "", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/jamietre/babel-plugin-transform-es2015-modules-commonjs-simple.git" 8 | }, 9 | "license": "MIT", 10 | "main": "lib/index.js", 11 | "scripts": { 12 | "prepublish": "npm run build", 13 | "_postinstall": "node -e \"require('fs').readdir('src',function(e) { require('child_process').exec('npm run prepublish')})\"", 14 | "build": "babel src --out-dir lib", 15 | "lint": "eslint src", 16 | "pretest": "npm run lint", 17 | "test": "mocha --opts test/mocha.opts test/index.js" 18 | }, 19 | "dependencies": { 20 | "babel-types": "^6.7.0", 21 | "babel-runtime": "^5.0.0", 22 | "babel-template": "^6.7.0", 23 | "babel-plugin-transform-strict-mode": "^6.6.5" 24 | }, 25 | "keywords": [ 26 | "babel-plugin", 27 | "commonjs", 28 | "sourcemaps" 29 | ], 30 | "devDependencies": { 31 | "app-root-path": "^1.0.0", 32 | "babel-cli": "^6.6.5", 33 | "babel-code-frame": "^6.6.5", 34 | "babel-core": "^6.7.4", 35 | "babel-eslint": "^6.0.0", 36 | "babel-helper-plugin-test-runner": "^6.5.0", 37 | "babel-plugin-external-helpers": "^6.5.0", 38 | "babel-plugin-transform-es2015-block-scoping": "^6.6.5", 39 | "babel-plugin-transform-es2015-destructuring": "^6.6.5", 40 | "babel-plugin-transform-es2015-template-literals": "^6.6.5", 41 | "babel-plugin-transform-es3-member-expression-literals": "^6.5.0", 42 | "babel-plugin-transform-es3-property-literals": "^6.5.0", 43 | "babel-plugin-transform-flow-strip-types": "^6.6.5", 44 | "babel-plugin-transform-runtime": "^6.6.0", 45 | "babel-preset-es2015": "^6.6.0", 46 | "chai": "^3.5.0", 47 | "eslint": "^2.3.0", 48 | "estraverse-fb": "^1.3.1", 49 | "lodash": "^3.10.1", 50 | "mocha": "^2.4.5", 51 | "yargs": "^3.32.0" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint max-len: 0 */ 2 | /* global Symbol */ 3 | 4 | import { basename, extname } from "path"; 5 | import template from "babel-template"; 6 | import * as t from "babel-types"; 7 | 8 | let buildRequire = template(` 9 | require($0); 10 | `); 11 | 12 | let buildExportsModuleDeclaration = template(` 13 | Object.defineProperty(exports, "__esModule", { 14 | value: true 15 | }); 16 | `); 17 | 18 | let buildExportsFrom = template(` 19 | Object.defineProperty(exports, $0, { 20 | enumerable: true, 21 | get: function () { 22 | return $1; 23 | } 24 | }); 25 | `); 26 | 27 | let buildLooseExportsModuleDeclaration = template(` 28 | exports.__esModule = true; 29 | `); 30 | 31 | let buildExportsAssignment = template(` 32 | exports.$0 = $1; 33 | `); 34 | 35 | let buildExportAll = template(` 36 | Object.keys(OBJECT).forEach(function (key) { 37 | if (key === "default") return; 38 | Object.defineProperty(exports, key, { 39 | enumerable: true, 40 | get: function () { 41 | return OBJECT[key]; 42 | } 43 | }); 44 | }); 45 | `); 46 | 47 | const THIS_BREAK_KEYS = ["FunctionExpression", "FunctionDeclaration", "ClassProperty", "ClassMethod", "ObjectMethod"]; 48 | 49 | export default function () { 50 | let REASSIGN_REMAP_SKIP = Symbol(); 51 | 52 | let reassignmentVisitor = { 53 | ReferencedIdentifier(path) { 54 | let name = path.node.name; 55 | let remap = this.remaps[name]; 56 | if (!remap) return; 57 | 58 | // redeclared in this scope 59 | if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return; 60 | 61 | if (path.parentPath.isCallExpression({ callee: path.node })) { 62 | path.replaceWith(t.sequenceExpression([t.numericLiteral(0), remap])); 63 | } else { 64 | path.replaceWith(remap); 65 | } 66 | this.requeueInParent(path); 67 | }, 68 | 69 | AssignmentExpression(path) { 70 | let node = path.node; 71 | if (node[REASSIGN_REMAP_SKIP]) return; 72 | 73 | let left = path.get("left"); 74 | if (!left.isIdentifier()) return; 75 | 76 | let name = left.node.name; 77 | let exports = this.exports[name]; 78 | if (!exports) return; 79 | 80 | // redeclared in this scope 81 | if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return; 82 | 83 | node[REASSIGN_REMAP_SKIP] = true; 84 | 85 | for (let reid of exports) { 86 | node = buildExportsAssignment(reid, node).expression; 87 | } 88 | 89 | path.replaceWith(node); 90 | this.requeueInParent(path); 91 | }, 92 | 93 | UpdateExpression(path) { 94 | let arg = path.get("argument"); 95 | if (!arg.isIdentifier()) return; 96 | 97 | let name = arg.node.name; 98 | let exports = this.exports[name]; 99 | if (!exports) return; 100 | 101 | // redeclared in this scope 102 | if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return; 103 | 104 | let node = t.assignmentExpression(path.node.operator[0] + "=", arg.node, t.numericLiteral(1)); 105 | 106 | if ((path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) || path.node.prefix) { 107 | path.replaceWith(node); 108 | this.requeueInParent(path); 109 | return; 110 | } 111 | 112 | let nodes = []; 113 | nodes.push(node); 114 | 115 | let operator; 116 | if (path.node.operator === "--") { 117 | operator = "+"; 118 | } else { // "++" 119 | operator = "-"; 120 | } 121 | nodes.push(t.binaryExpression(operator, arg.node, t.numericLiteral(1))); 122 | 123 | let newPaths = path.replaceWithMultiple(t.sequenceExpression(nodes)); 124 | for (const newPath of newPaths) this.requeueInParent(newPath); 125 | } 126 | }; 127 | 128 | return { 129 | inherits: require("babel-plugin-transform-strict-mode"), 130 | 131 | visitor: { 132 | ThisExpression(path, state) { 133 | // If other plugins run after this plugin's Program#exit handler, we allow them to 134 | // insert top-level `this` values. This allows the AMD and UMD plugins to 135 | // function properly. 136 | if (this.ranCommonJS) return; 137 | 138 | if ( 139 | state.opts.allowTopLevelThis !== true && 140 | !path.findParent((path) => !path.is("shadow") && 141 | THIS_BREAK_KEYS.indexOf(path.type) >= 0) 142 | ) { 143 | path.replaceWith(t.identifier("undefined")); 144 | } 145 | }, 146 | 147 | Program: { 148 | exit(path) { 149 | this.ranCommonJS = true; 150 | 151 | let strict = !!this.opts.strict; 152 | let noMangle = !!this.opts.noMangle; 153 | 154 | let { scope } = path; 155 | 156 | // rename these commonjs variables if they're declared in the file 157 | scope.rename("module"); 158 | scope.rename("exports"); 159 | scope.rename("require"); 160 | 161 | let hasExports = false; 162 | let hasDefaultExport = false; 163 | let hasNamedExport = false; 164 | let hasImports = false; 165 | 166 | let body: Array = path.get("body"); 167 | let imports = Object.create(null); 168 | let exports = Object.create(null); 169 | 170 | let nonHoistedExportNames = Object.create(null); 171 | 172 | let topNodes = []; 173 | let remaps = Object.create(null); 174 | 175 | let requires = Object.create(null); 176 | 177 | function getIdentifier(name) { 178 | return { 179 | name: name, 180 | type: "Identifier" 181 | }; 182 | } 183 | 184 | function checkExportType(exportName) { 185 | if (exportName === 'default') { 186 | hasDefaultExport = true; 187 | } else { 188 | hasNamedExport = true; 189 | } 190 | } 191 | 192 | function addRequire(source, blockHoist) { 193 | let cached = requires[source]; 194 | if (cached) return cached; 195 | 196 | let ref = path.scope.generateUidIdentifier(basename(source, extname(source))); 197 | 198 | let varDecl = t.variableDeclaration("var", [ 199 | t.variableDeclarator(ref, buildRequire( 200 | t.stringLiteral(source) 201 | ).expression) 202 | ]); 203 | 204 | // Copy location from the original import statement for sourcemap 205 | // generation. 206 | if (imports[source]) { 207 | varDecl.loc = imports[source].loc; 208 | } 209 | 210 | if (typeof blockHoist === "number" && blockHoist > 0) { 211 | varDecl._blockHoist = blockHoist; 212 | } 213 | 214 | topNodes.push(varDecl); 215 | 216 | return requires[source] = ref; 217 | } 218 | 219 | function addTo(obj, key, arr) { 220 | let existing = obj[key] || []; 221 | obj[key] = existing.concat(arr); 222 | } 223 | 224 | for (let path of body) { 225 | if (path.isExportDeclaration()) { 226 | hasExports = true; 227 | 228 | let specifiers = [].concat(path.get("declaration"), path.get("specifiers")); 229 | for (let specifier of specifiers) { 230 | let ids = specifier.getBindingIdentifiers(); 231 | if (ids.__esModule) { 232 | throw specifier.buildCodeFrameError("Illegal export \"__esModule\""); 233 | } 234 | } 235 | } 236 | 237 | if (path.isImportDeclaration()) { 238 | hasImports = true; 239 | 240 | let key = path.node.source.value; 241 | let importsEntry = imports[key] || { 242 | specifiers: [], 243 | maxBlockHoist: 0, 244 | loc: path.node.loc, 245 | }; 246 | 247 | importsEntry.specifiers.push(...path.node.specifiers); 248 | 249 | if (typeof path.node._blockHoist === "number") { 250 | importsEntry.maxBlockHoist = Math.max( 251 | path.node._blockHoist, 252 | importsEntry.maxBlockHoist 253 | ); 254 | } 255 | 256 | imports[key] = importsEntry; 257 | 258 | path.remove(); 259 | } else if (path.isExportDefaultDeclaration()) { 260 | hasDefaultExport = true; 261 | let declaration = path.get("declaration"); 262 | if (declaration.isFunctionDeclaration()) { 263 | let id = declaration.node.id; 264 | let defNode = t.identifier("default"); 265 | if (id) { 266 | addTo(exports, id.name, defNode); 267 | topNodes.push(buildExportsAssignment(defNode, id)); 268 | path.replaceWith(declaration.node); 269 | } else { 270 | topNodes.push(buildExportsAssignment(defNode, t.toExpression(declaration.node))); 271 | path.remove(); 272 | } 273 | } else if (declaration.isClassDeclaration()) { 274 | let id = declaration.node.id; 275 | let defNode = t.identifier("default"); 276 | if (id) { 277 | addTo(exports, id.name, defNode); 278 | path.replaceWithMultiple([ 279 | declaration.node, 280 | buildExportsAssignment(defNode, id) 281 | ]); 282 | } else { 283 | path.replaceWith(buildExportsAssignment(defNode, t.toExpression(declaration.node))); 284 | } 285 | } else { 286 | path.replaceWith(buildExportsAssignment(t.identifier("default"), declaration.node)); 287 | 288 | // Manualy re-queue `export default foo;` expressions so that the ES3 transform 289 | // has an opportunity to convert them. Ideally this would happen automatically from the 290 | // replaceWith above. See T7166 for more info. 291 | path.parentPath.requeue(path.get("expression.left")); 292 | } 293 | } else if (path.isExportNamedDeclaration()) { 294 | let declaration = path.get("declaration"); 295 | if (declaration.node) { 296 | if (declaration.isFunctionDeclaration()) { 297 | let id = declaration.node.id; 298 | checkExportType(id.name); 299 | addTo(exports, id.name, id); 300 | topNodes.push(buildExportsAssignment(id, id)); 301 | path.replaceWith(declaration.node); 302 | } else if (declaration.isClassDeclaration()) { 303 | let id = declaration.node.id; 304 | checkExportType(id.name); 305 | addTo(exports, id.name, id); 306 | path.replaceWithMultiple([ 307 | declaration.node, 308 | buildExportsAssignment(id, id) 309 | ]); 310 | nonHoistedExportNames[id.name] = true; 311 | } else if (declaration.isVariableDeclaration()) { 312 | let declarators = declaration.get("declarations"); 313 | for (let decl of declarators) { 314 | let id = decl.get("id"); 315 | 316 | let init = decl.get("init"); 317 | if (!init.node) init.replaceWith(t.identifier("undefined")); 318 | 319 | hasNamedExport = true; // "default" is not a valid symbol name 320 | 321 | if (id.isIdentifier()) { 322 | addTo(exports, id.node.name, id.node); 323 | init.replaceWith(buildExportsAssignment(id.node, init.node).expression); 324 | nonHoistedExportNames[id.node.name] = true; 325 | } else { 326 | // todo 327 | } 328 | } 329 | path.replaceWith(declaration.node); 330 | } 331 | continue; 332 | } 333 | 334 | let specifiers = path.get("specifiers"); 335 | if (specifiers.length) { 336 | let nodes = []; 337 | let source = path.node.source; 338 | if (source) { 339 | let ref = addRequire(source.value, path.node._blockHoist); 340 | 341 | for (let specifier of specifiers) { 342 | if (specifier.isExportNamespaceSpecifier()) { 343 | // todo 344 | } else if (specifier.isExportDefaultSpecifier()) { 345 | // todo 346 | } else if (specifier.isExportSpecifier()) { 347 | checkExportType(specifier.node.exported.name); 348 | 349 | if (specifier.node.local.name === "default") { 350 | topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name), t.memberExpression(t.callExpression(this.addHelper("interopRequireDefault"), [ref]), specifier.node.local))); 351 | } else { 352 | topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name), t.memberExpression(ref, specifier.node.local))); 353 | } 354 | nonHoistedExportNames[specifier.node.exported.name] = true; 355 | } 356 | } 357 | } else { 358 | for (let specifier of specifiers) { 359 | if (specifier.isExportSpecifier()) { 360 | checkExportType(specifier.node.exported.name); 361 | addTo(exports, specifier.node.local.name, specifier.node.exported); 362 | nonHoistedExportNames[specifier.node.exported.name] = true; 363 | nodes.push(buildExportsAssignment(specifier.node.exported, specifier.node.local)); 364 | } 365 | } 366 | } 367 | path.replaceWithMultiple(nodes); 368 | } 369 | } else if (path.isExportAllDeclaration()) { 370 | hasNamedExport = true; 371 | let exportNode = buildExportAll({ 372 | OBJECT: addRequire(path.node.source.value, path.node._blockHoist) 373 | }); 374 | exportNode.loc = path.node.loc; 375 | topNodes.push(exportNode); 376 | path.remove(); 377 | } 378 | } 379 | 380 | for (let source in imports) { 381 | let {specifiers, maxBlockHoist} = imports[source]; 382 | if (specifiers.length) { 383 | let uid = addRequire(source, maxBlockHoist); 384 | 385 | let wildcard; 386 | 387 | for (let i = 0; i < specifiers.length; i++) { 388 | let specifier = specifiers[i]; 389 | if (t.isImportNamespaceSpecifier(specifier)) { 390 | if (strict) { 391 | remaps[specifier.local.name] = uid; 392 | } else { 393 | const varDecl = t.variableDeclaration("var", [ 394 | t.variableDeclarator( 395 | specifier.local, 396 | t.callExpression( 397 | this.addHelper("interopRequireWildcard"), 398 | [uid] 399 | ) 400 | ) 401 | ]); 402 | 403 | if (maxBlockHoist > 0) { 404 | varDecl._blockHoist = maxBlockHoist; 405 | } 406 | 407 | topNodes.push(varDecl); 408 | } 409 | wildcard = specifier.local; 410 | } else if (t.isImportDefaultSpecifier(specifier)) { 411 | specifiers[i] = t.importSpecifier(specifier.local, t.identifier("default")); 412 | } 413 | } 414 | 415 | for (let specifier of specifiers) { 416 | if (t.isImportSpecifier(specifier)) { 417 | let target = uid; 418 | if (specifier.imported.name === "default") { 419 | if (noMangle) { 420 | target = specifier.local; 421 | 422 | const requireDefault = this.addHelper("interopRequireDefault"); 423 | const callExpression = t.callExpression(requireDefault, [uid]); 424 | const declaration = t.memberExpression(callExpression, getIdentifier("default")); 425 | const varDecl = t.variableDeclaration("var", [ 426 | t.variableDeclarator(target, declaration) 427 | ]); 428 | 429 | if (maxBlockHoist > 0) { 430 | varDecl._blockHoist = maxBlockHoist; 431 | } 432 | 433 | topNodes.push(varDecl); 434 | } else { 435 | if (wildcard) { 436 | target = wildcard; 437 | } else { 438 | target = wildcard = path.scope.generateUidIdentifier(uid.name); 439 | const varDecl = t.variableDeclaration("var", [ 440 | t.variableDeclarator( 441 | target, 442 | t.callExpression( 443 | this.addHelper("interopRequireDefault"), 444 | [uid] 445 | ) 446 | ) 447 | ]); 448 | 449 | if (maxBlockHoist > 0) { 450 | varDecl._blockHoist = maxBlockHoist; 451 | } 452 | 453 | topNodes.push(varDecl); 454 | } 455 | } 456 | } else if (noMangle) { 457 | // is a named import 458 | target = specifier.local; 459 | 460 | const varDecl = t.variableDeclaration("var", [ 461 | t.variableDeclarator( 462 | target, 463 | t.memberExpression(uid, specifier.imported) 464 | ) 465 | ]); 466 | 467 | if (maxBlockHoist > 0) { 468 | varDecl._blockHoist = maxBlockHoist; 469 | } 470 | 471 | topNodes.push(varDecl); 472 | } 473 | 474 | if (specifier.local.name !== target.name) { 475 | remaps[specifier.local.name] = t.memberExpression(target, t.cloneWithoutLoc(specifier.imported)); 476 | } 477 | } 478 | } 479 | } else { 480 | // bare import 481 | let requireNode = buildRequire(t.stringLiteral(source)); 482 | requireNode.loc = imports[source].loc; 483 | topNodes.push(requireNode); 484 | } 485 | } 486 | 487 | if (hasImports && Object.keys(nonHoistedExportNames).length) { 488 | let hoistedExportsNode = t.identifier("undefined"); 489 | 490 | for (let name in nonHoistedExportNames) { 491 | hoistedExportsNode = buildExportsAssignment(t.identifier(name), hoistedExportsNode).expression; 492 | } 493 | 494 | const node = t.expressionStatement(hoistedExportsNode); 495 | node._blockHoist = 3; 496 | 497 | topNodes.unshift(node); 498 | } 499 | 500 | // add __esModule declaration if this file has any exports 501 | if (hasExports && !strict) { 502 | let buildTemplate = buildExportsModuleDeclaration; 503 | if (this.opts.loose) buildTemplate = buildLooseExportsModuleDeclaration; 504 | 505 | const declar = buildTemplate(); 506 | declar._blockHoist = 3; 507 | 508 | topNodes.unshift(declar); 509 | } 510 | 511 | path.unshiftContainer("body", topNodes); 512 | 513 | if (this.opts.addExports && hasDefaultExport && !hasNamedExport) { 514 | path.pushContainer("body", template("module.exports = exports['default']")()); 515 | } 516 | 517 | path.traverse(reassignmentVisitor, { 518 | remaps, 519 | scope, 520 | exports, 521 | requeueInParent: (newPath) => path.requeue(newPath), 522 | }); 523 | 524 | } 525 | } 526 | } 527 | }; 528 | } 529 | -------------------------------------------------------------------------------- /test/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | 3 | } -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | extends: "../.eslintrc", 3 | "env": { 4 | "node": true, 5 | "mocha": true 6 | } 7 | } -------------------------------------------------------------------------------- /test/add-export/general/as-default/actual.js: -------------------------------------------------------------------------------- 1 | let foo = "foo"; 2 | export { foo as default } 3 | -------------------------------------------------------------------------------- /test/add-export/general/as-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | let foo = "foo"; 7 | exports.default = foo; 8 | module.exports = exports['default']; -------------------------------------------------------------------------------- /test/add-export/general/normal/actual.js: -------------------------------------------------------------------------------- 1 | function foo() {} 2 | export default foo 3 | -------------------------------------------------------------------------------- /test/add-export/general/normal/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | function foo() {} 7 | exports.default = foo; 8 | module.exports = exports['default']; -------------------------------------------------------------------------------- /test/add-export/general/only-named/actual.js: -------------------------------------------------------------------------------- 1 | function foo() {} 2 | export { foo } 3 | -------------------------------------------------------------------------------- /test/add-export/general/only-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | function foo() {} 7 | exports.foo = foo; -------------------------------------------------------------------------------- /test/add-export/general/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", ["transform-es2015-modules-commonjs",{ "addExports": true }]] 3 | } 4 | -------------------------------------------------------------------------------- /test/add-export/general/re-exporting/actual.js: -------------------------------------------------------------------------------- 1 | export { default } from "foo"; 2 | -------------------------------------------------------------------------------- /test/add-export/general/re-exporting/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _foo = require("foo"); 8 | 9 | Object.defineProperty(exports, "default", { 10 | enumerable: true, 11 | get: function () { 12 | return babelHelpers.interopRequireDefault(_foo).default; 13 | } 14 | }); 15 | module.exports = exports['default']; -------------------------------------------------------------------------------- /test/add-export/general/with-named/actual.js: -------------------------------------------------------------------------------- 1 | function foo() {} 2 | export { foo, foo as default } 3 | -------------------------------------------------------------------------------- /test/add-export/general/with-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | function foo() {} 7 | exports.foo = foo; 8 | exports.default = foo; -------------------------------------------------------------------------------- /test/fixtures/auxiliary-comment/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", "transform-es2015-modules-commonjs"], 3 | "auxiliaryCommentBefore": "before", 4 | "auxiliaryCommentAfter": "after" 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/auxiliary-comment/overview/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | import foo from "foo2"; 5 | import * as foo2 from "foo3"; 6 | import {bar} from "foo4"; 7 | import {foo as bar2} from "foo5"; 8 | 9 | export {test}; 10 | export var test = 5; 11 | 12 | bar(foo, bar2); 13 | 14 | /* my comment */ 15 | bar2; 16 | foo; 17 | -------------------------------------------------------------------------------- /test/fixtures/auxiliary-comment/overview/expected.js: -------------------------------------------------------------------------------- 1 | /*before*/"use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.test = undefined; 7 | /*after*/ 8 | /*before*/require("foo"); /*after*/ 9 | 10 | /*before*/require("foo-bar"); /*after*/ 11 | 12 | /*before*/require("./directory/foo-bar"); /*after*/ 13 | 14 | var /*before*/_foo = require("foo2") /*after*/; 15 | 16 | /*before*/ 17 | var _foo2 = babelHelpers.interopRequireDefault(_foo); 18 | 19 | /*after*/ 20 | var /*before*/_foo3 = require("foo3") /*after*/; 21 | 22 | /*before*/var /*after*/foo2 = babelHelpers.interopRequireWildcard(_foo3); 23 | 24 | var /*before*/_foo4 = require("foo4") /*after*/; 25 | 26 | var /*before*/_foo5 = require("foo5") /*after*/; 27 | 28 | /*before*/exports. /*after*/test = test; 29 | var test = /*before*/exports. /*after*/test = 5; 30 | 31 | /*before*/(0, _foo4.bar) /*after*/( /*before*/_foo2.default /*after*/, /*before*/_foo5.foo /*after*/); 32 | 33 | /* my comment */ 34 | /*before*/_foo5.foo; /*after*/ 35 | /*before*/_foo2.default; /*after*/ 36 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-default-non-function/actual.js: -------------------------------------------------------------------------------- 1 | export default new Cachier() 2 | 3 | export function Cachier(databaseName) {} 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-default-non-function/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.Cachier = Cachier; 7 | exports.default = new Cachier(); 8 | function Cachier(databaseName) {} -------------------------------------------------------------------------------- /test/fixtures/interop/exports-default/actual.js: -------------------------------------------------------------------------------- 1 | export default 42; 2 | export default {}; 3 | export default []; 4 | export default foo; 5 | export default function () {} 6 | export default class {} 7 | export default function foo () {} 8 | export default class Foo {} 9 | export { foo as default }; 10 | export default (function(){return "foo"})(); 11 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function () {}; 8 | 9 | exports.default = foo; 10 | exports.default = 42; 11 | exports.default = {}; 12 | exports.default = []; 13 | exports.default = foo; 14 | exports.default = class {}; 15 | function foo() {} 16 | class Foo {} 17 | exports.default = Foo; 18 | exports.default = foo; 19 | 20 | exports.default = function () { 21 | return "foo"; 22 | }(); 23 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-from/actual.js: -------------------------------------------------------------------------------- 1 | export * from "foo"; 2 | export {foo} from "foo"; 3 | export {foo, bar} from "foo"; 4 | export {foo as bar} from "foo"; 5 | export {foo as default} from "foo"; 6 | export {foo as default, bar} from "foo"; 7 | export {default as foo} from "foo"; 8 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-from/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _foo = require("foo"); 8 | 9 | Object.keys(_foo).forEach(function (key) { 10 | if (key === "default") return; 11 | Object.defineProperty(exports, key, { 12 | enumerable: true, 13 | get: function () { 14 | return _foo[key]; 15 | } 16 | }); 17 | }); 18 | Object.defineProperty(exports, "foo", { 19 | enumerable: true, 20 | get: function () { 21 | return _foo.foo; 22 | } 23 | }); 24 | Object.defineProperty(exports, "foo", { 25 | enumerable: true, 26 | get: function () { 27 | return _foo.foo; 28 | } 29 | }); 30 | Object.defineProperty(exports, "bar", { 31 | enumerable: true, 32 | get: function () { 33 | return _foo.bar; 34 | } 35 | }); 36 | Object.defineProperty(exports, "bar", { 37 | enumerable: true, 38 | get: function () { 39 | return _foo.foo; 40 | } 41 | }); 42 | Object.defineProperty(exports, "default", { 43 | enumerable: true, 44 | get: function () { 45 | return _foo.foo; 46 | } 47 | }); 48 | Object.defineProperty(exports, "default", { 49 | enumerable: true, 50 | get: function () { 51 | return _foo.foo; 52 | } 53 | }); 54 | Object.defineProperty(exports, "bar", { 55 | enumerable: true, 56 | get: function () { 57 | return _foo.bar; 58 | } 59 | }); 60 | Object.defineProperty(exports, "foo", { 61 | enumerable: true, 62 | get: function () { 63 | return babelHelpers.interopRequireDefault(_foo).default; 64 | } 65 | }); -------------------------------------------------------------------------------- /test/fixtures/interop/exports-named/actual.js: -------------------------------------------------------------------------------- 1 | export {foo}; 2 | export {foo, bar}; 3 | export {foo as bar}; 4 | export {foo as default}; 5 | export {foo as default, bar}; 6 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.foo = foo; 7 | exports.foo = foo; 8 | exports.bar = bar; 9 | exports.bar = foo; 10 | exports.default = foo; 11 | exports.default = foo; 12 | exports.bar = bar; -------------------------------------------------------------------------------- /test/fixtures/interop/exports-variable/actual.js: -------------------------------------------------------------------------------- 1 | export var foo = 1; 2 | export var foo = 1, bar = 2; 3 | export var foo2 = function () {}; 4 | export var foo3; 5 | export let foo4 = 2; 6 | export let foo5; 7 | export const foo6 = 3; 8 | export function foo7 () {} 9 | export class foo8 {} 10 | -------------------------------------------------------------------------------- /test/fixtures/interop/exports-variable/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.foo7 = foo7; 7 | var foo = exports.foo = 1; 8 | var foo = exports.foo = 1, 9 | bar = exports.bar = 2; 10 | var foo2 = exports.foo2 = function () {}; 11 | var foo3 = exports.foo3 = undefined; 12 | let foo4 = exports.foo4 = 2; 13 | let foo5 = exports.foo5 = undefined; 14 | const foo6 = exports.foo6 = 3; 15 | function foo7() {} 16 | class foo8 {} 17 | exports.foo8 = foo8; -------------------------------------------------------------------------------- /test/fixtures/interop/hoist-function-exports/actual.js: -------------------------------------------------------------------------------- 1 | import { isEven } from "./evens"; 2 | 3 | export function nextOdd(n) { 4 | return isEven(n) ? n + 1 : n + 2; 5 | } 6 | 7 | export var isOdd = (function (isEven) { 8 | return function (n) { 9 | return !isEven(n); 10 | }; 11 | })(isEven); 12 | -------------------------------------------------------------------------------- /test/fixtures/interop/hoist-function-exports/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.isOdd = undefined; 7 | exports.nextOdd = nextOdd; 8 | 9 | var _evens = require("./evens"); 10 | 11 | function nextOdd(n) { 12 | return (0, _evens.isEven)(n) ? n + 1 : n + 2; 13 | } 14 | 15 | var isOdd = exports.isOdd = function (isEven) { 16 | return function (n) { 17 | return !isEven(n); 18 | }; 19 | }(_evens.isEven); 20 | -------------------------------------------------------------------------------- /test/fixtures/interop/illegal-export-esmodule-2/actual.js: -------------------------------------------------------------------------------- 1 | export { __esModule }; 2 | -------------------------------------------------------------------------------- /test/fixtures/interop/illegal-export-esmodule-2/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "throws": "Illegal export \"__esModule\"" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/illegal-export-esmodule/actual.js: -------------------------------------------------------------------------------- 1 | export var __esModule = false; 2 | -------------------------------------------------------------------------------- /test/fixtures/interop/illegal-export-esmodule/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "throws": "Illegal export \"__esModule\"" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-default/actual.js: -------------------------------------------------------------------------------- 1 | import foo from "foo"; 2 | import {default as foo2} from "foo"; 3 | 4 | foo; 5 | foo2; 6 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var _foo2 = babelHelpers.interopRequireDefault(_foo); 6 | 7 | _foo2.default; 8 | _foo2.default; -------------------------------------------------------------------------------- /test/fixtures/interop/imports-default/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-glob/actual.js: -------------------------------------------------------------------------------- 1 | import * as foo from "foo"; 2 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-glob/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo = babelHelpers.interopRequireWildcard(_foo); -------------------------------------------------------------------------------- /test/fixtures/interop/imports-hoisting/actual.js: -------------------------------------------------------------------------------- 1 | tag`foo`; 2 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-hoisting/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _taggedTemplateLiteral2 = require("babel-runtime/helpers/taggedTemplateLiteral"); 4 | 5 | var _taggedTemplateLiteral3 = _interopRequireDefault(_taggedTemplateLiteral2); 6 | 7 | var _templateObject = (0, _taggedTemplateLiteral3.default)(["foo"], ["foo"]); 8 | 9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 10 | 11 | tag(_templateObject); 12 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-hoisting/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-runtime", 4 | "transform-es2015-template-literals", 5 | "transform-es2015-modules-commonjs" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-mixing/actual.js: -------------------------------------------------------------------------------- 1 | import foo, {baz as xyz} from "foo"; 2 | 3 | foo; 4 | xyz; 5 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-mixing/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var _foo2 = babelHelpers.interopRequireDefault(_foo); 6 | 7 | _foo2.default; 8 | _foo.baz; -------------------------------------------------------------------------------- /test/fixtures/interop/imports-mixing/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-named/actual.js: -------------------------------------------------------------------------------- 1 | import {bar} from "foo"; 2 | import {bar2, baz} from "foo"; 3 | import {bar as baz2} from "foo"; 4 | import {bar as baz3, xyz} from "foo"; 5 | 6 | bar; 7 | bar2; 8 | baz; 9 | baz2; 10 | baz3; 11 | xyz; 12 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | _foo.bar; 6 | _foo.bar2; 7 | _foo.baz; 8 | _foo.bar; 9 | _foo.bar; 10 | _foo.xyz; -------------------------------------------------------------------------------- /test/fixtures/interop/imports/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/imports/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require("foo"); 4 | 5 | require("foo-bar"); 6 | 7 | require("./directory/foo-bar"); -------------------------------------------------------------------------------- /test/fixtures/interop/module-shadow/actual.js: -------------------------------------------------------------------------------- 1 | export function module() { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/module-shadow/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | function _module() {} 7 | exports.module = _module; -------------------------------------------------------------------------------- /test/fixtures/interop/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", "transform-es2015-modules-commonjs"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/overview/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | import foo from "foo2"; 5 | import * as foo2 from "foo3"; 6 | import {bar} from "foo4"; 7 | import {foo as bar2} from "foo5"; 8 | 9 | export {test}; 10 | export var test = 5; 11 | 12 | bar; 13 | bar2; 14 | foo; 15 | -------------------------------------------------------------------------------- /test/fixtures/interop/overview/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.test = undefined; 7 | 8 | require("foo"); 9 | 10 | require("foo-bar"); 11 | 12 | require("./directory/foo-bar"); 13 | 14 | var _foo = require("foo2"); 15 | 16 | var _foo2 = babelHelpers.interopRequireDefault(_foo); 17 | 18 | var _foo3 = require("foo3"); 19 | 20 | var foo2 = babelHelpers.interopRequireWildcard(_foo3); 21 | 22 | var _foo4 = require("foo4"); 23 | 24 | var _foo5 = require("foo5"); 25 | 26 | exports.test = test; 27 | var test = exports.test = 5; 28 | 29 | _foo4.bar; 30 | _foo5.foo; 31 | _foo2.default; -------------------------------------------------------------------------------- /test/fixtures/interop/overview/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/interop/remap/actual.js: -------------------------------------------------------------------------------- 1 | export var test = 2; 2 | test = 5; 3 | test++; 4 | 5 | (function () { 6 | var test = 2; 7 | test = 3; 8 | test++; 9 | })(); 10 | 11 | var a = 2; 12 | export { a }; 13 | a = 3; 14 | 15 | var b = 2; 16 | export { b as c }; 17 | b = 3; 18 | 19 | var d = 3; 20 | export { d as e, d as f }; 21 | d = 4; 22 | -------------------------------------------------------------------------------- /test/fixtures/interop/remap/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | var test = exports.test = 2; 7 | exports.test = test = 5; 8 | exports.test = test += 1; 9 | 10 | (function () { 11 | var test = 2; 12 | test = 3; 13 | test++; 14 | })(); 15 | 16 | var a = 2; 17 | exports.a = a; 18 | 19 | exports.a = a = 3; 20 | 21 | var b = 2; 22 | exports.c = b; 23 | 24 | exports.c = b = 3; 25 | 26 | var d = 3; 27 | exports.e = d; 28 | exports.f = d; 29 | 30 | exports.f = exports.e = d = 4; -------------------------------------------------------------------------------- /test/fixtures/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["transform-es2015-modules-commonjs"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7160/actual.js: -------------------------------------------------------------------------------- 1 | export var foo = function(gen, ctx = null) { 2 | 3 | } 4 | 5 | export var bar = (gen, ctx = null) => { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7160/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | var foo = exports.foo = function foo(gen) { 7 | var ctx = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; 8 | }; 9 | 10 | var bar = exports.bar = function bar(gen) { 11 | var ctx = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; 12 | }; 13 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7160/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7165/actual.js: -------------------------------------------------------------------------------- 1 | import foo from 'foo'; 2 | import { something } from 'bar'; 3 | 4 | const anything = {}; 5 | 6 | export * from 'bar'; 7 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7165/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _bar = require('bar'); 8 | 9 | Object.keys(_bar).forEach(function (key) { 10 | if (key === "default") return; 11 | Object.defineProperty(exports, key, { 12 | enumerable: true, 13 | get: function () { 14 | return _bar[key]; 15 | } 16 | }); 17 | }); 18 | 19 | var _foo = require('foo'); 20 | 21 | var _foo2 = _interopRequireDefault(_foo); 22 | 23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 24 | 25 | var anything = {}; 26 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7165/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-block-scoping", 4 | "transform-es2015-modules-commonjs" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7199/actual.js: -------------------------------------------------------------------------------- 1 | import foo from 'foo'; 2 | const [x] = bar; 3 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7199/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); 4 | 5 | var _foo = require('foo'); 6 | 7 | var _foo2 = _interopRequireDefault(_foo); 8 | 9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } 10 | 11 | var _bar = bar; 12 | 13 | var _bar2 = _slicedToArray(_bar, 1); 14 | 15 | const x = _bar2[0]; 16 | -------------------------------------------------------------------------------- /test/fixtures/regression/T7199/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-destructuring", 4 | "transform-es2015-modules-commonjs", 5 | "transform-es3-member-expression-literals", 6 | "transform-es3-property-literals" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/regression/es3-compatibility/actual.js: -------------------------------------------------------------------------------- 1 | 2 | import foo from 'foo'; 3 | console.log(foo); 4 | 5 | export default 5; 6 | -------------------------------------------------------------------------------- /test/fixtures/regression/es3-compatibility/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _foo = require('foo'); 8 | 9 | var _foo2 = _interopRequireDefault(_foo); 10 | 11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } 12 | 13 | console.log(_foo2['default']); 14 | 15 | exports['default'] = 5; 16 | -------------------------------------------------------------------------------- /test/fixtures/regression/es3-compatibility/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-modules-commonjs", 4 | "transform-es3-member-expression-literals", 5 | "transform-es3-property-literals" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/source-map/exec.js: -------------------------------------------------------------------------------- 1 | var tests = [ 2 | 'import "foo";', 3 | 'import foo from "foo";', 4 | 'import {default as foo2} from "foo";', 5 | 'import * as foo from "foo";', 6 | 'import {bar} from "foo";', 7 | 'import {bar2, baz} from "foo";', 8 | 'import {bar as baz2} from "foo";', 9 | 'import {bar as baz3, xyz} from "foo";', 10 | 'import bar, * as bar2 from "foo";', 11 | 'import bar, {bar2, bar3 as bar4} from "foo";', 12 | 13 | 'export var a;', 14 | 'export default function(){};', 15 | 'export default function f(){};', 16 | 'export default 42;', 17 | 'export {foo};', 18 | 'export { foo as default };', 19 | 'export * from "foo";', 20 | 'export {foo} from "foo";', 21 | 'export {default as foo} from "foo";', 22 | ]; 23 | 24 | tests.forEach(function (code) { 25 | var res = transform(code, { 26 | sourceMap: true, 27 | plugins: opts.plugins 28 | }); 29 | 30 | // Should create mapping 31 | assert.notEqual( 32 | res.map.mappings, 33 | '', 34 | 'expected to generate sourcemap for: ' + code 35 | ); 36 | }); 37 | -------------------------------------------------------------------------------- /test/fixtures/source-map/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", "transform-es2015-modules-commonjs"] 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/strict/export-1/actual.js: -------------------------------------------------------------------------------- 1 | export default foo; 2 | -------------------------------------------------------------------------------- /test/fixtures/strict/export-1/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.default = foo; -------------------------------------------------------------------------------- /test/fixtures/strict/export-2/actual.js: -------------------------------------------------------------------------------- 1 | export { foo as default }; 2 | -------------------------------------------------------------------------------- /test/fixtures/strict/export-2/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.default = foo; -------------------------------------------------------------------------------- /test/fixtures/strict/export-all/actual.js: -------------------------------------------------------------------------------- 1 | export * from 'mod'; 2 | -------------------------------------------------------------------------------- /test/fixtures/strict/export-all/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _mod = require('mod'); 4 | 5 | Object.keys(_mod).forEach(function (key) { 6 | if (key === "default") return; 7 | Object.defineProperty(exports, key, { 8 | enumerable: true, 9 | get: function () { 10 | return _mod[key]; 11 | } 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/fixtures/strict/export-all/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-block-scoping", 4 | "transform-es2015-modules-commonjs" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/strict/export/actual.js: -------------------------------------------------------------------------------- 1 | export function foo() {} 2 | -------------------------------------------------------------------------------- /test/fixtures/strict/export/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.foo = foo; 4 | function foo() {} -------------------------------------------------------------------------------- /test/fixtures/strict/import/actual.js: -------------------------------------------------------------------------------- 1 | import foo from "foo"; 2 | import { default as foo2 } from "foo"; 3 | import { foo3 } from "foo"; 4 | import * as foo4 from "foo"; 5 | 6 | foo; 7 | foo2; 8 | foo3; 9 | foo3(); 10 | -------------------------------------------------------------------------------- /test/fixtures/strict/import/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | _foo.default; 6 | _foo.default; 7 | _foo.foo3; 8 | (0, _foo.foo3)(); 9 | -------------------------------------------------------------------------------- /test/fixtures/strict/import/source-mappings.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "original": { 3 | "line": 6, "column": 0 4 | }, 5 | "generated": { 6 | "line": 5, "column": 0 7 | } 8 | },{ 9 | "original": { 10 | "line": 6, "column": 0 11 | }, 12 | "generated": { 13 | "line": 5, "column": 12 14 | } 15 | },{ 16 | "original": { 17 | "line": 8, "column": 0 18 | }, 19 | "generated": { 20 | "line": 7, "column": 0 21 | } 22 | },{ 23 | "original": { 24 | "line": 8, "column": 0 25 | }, 26 | "generated": { 27 | "line": 7, "column": 9 28 | } 29 | },{ 30 | "original": { 31 | "line": 9, "column": 0 32 | }, 33 | "generated": { 34 | "line": 8, "column": 0 35 | } 36 | },{ 37 | "original": { 38 | "line": 9, "column": 0 39 | }, 40 | "generated": { 41 | "line": 8, "column": 9 42 | } 43 | }] 44 | -------------------------------------------------------------------------------- /test/fixtures/strict/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "strict": true }]] 3 | } 4 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /* Run tests expecting the following structure under test 2 | 3 | group/ 4 | subgroup/ 5 | options.json 6 | testName/ 7 | actual.js 8 | expected.js 9 | options.json 10 | options.json 11 | options.json 12 | 13 | 14 | options.json are babel options for the tests in that group or test. Test options will be merged with group options. 15 | Plugin options will be merged by plugin as well. 16 | 17 | A special option "throws" can be present, which will test for an error being thrown matching the regex that's the value of 18 | the option. 19 | 20 | You can invoke this with options 21 | 22 | --path group/subgroup/test 23 | --path group/subgroup 24 | 25 | Only run tests that match the pattern, use a * to match any group or test. If only one segment is passed, 26 | will run all tests in the group. For example: 27 | 28 | mocha test/index.js --path fixtures/interop/imports-hoisting // run just test fixtures/interop/imports-hoisting 29 | mocha test/index.js --path nomangle/interop // run all tests under nomangle/interop 30 | 31 | */ 32 | 33 | var assert = require('assert'); 34 | var eol = require('os').EOL; 35 | var fs = require('fs'); 36 | var path = require('path'); 37 | var _ = require('lodash'); 38 | var args = require('yargs').argv; 39 | var appRoot = require('app-root-path'); 40 | 41 | var babel = require('babel-core'); 42 | var codeFrame = require("babel-code-frame"); 43 | 44 | var buildExternalHelpers = babel.buildExternalHelpers; 45 | 46 | 47 | var babelHelpers = eval(buildExternalHelpers(null, "var")); 48 | 49 | var PLUGIN_NAME = "transform-es2015-modules-commonjs-simple"; 50 | var ORIGINAL_PLUGIN_NAME = "transform-es2015-modules-commonjs"; 51 | 52 | var testPath = args.path; 53 | var parts = []; 54 | 55 | if (testPath) { 56 | parts = testPath.split('/'); 57 | if (parts.length === 0 || parts.length > 3) { 58 | console.log("--path must include an argument that 1-3 parts, separated by a slash, e.g. '*/overview' or 'auxiliary-comment'"); 59 | process.exit(1); 60 | } 61 | } 62 | 63 | while (parts.length < 3) parts.push('*'); 64 | 65 | var maxDepth = 2; 66 | var textEncoding = 'utf8'; 67 | 68 | function testGroup(dir, name, options, depth) { 69 | //var fixtureRoot = path.join(testRoot, testGroup); 70 | 71 | depth = depth || 0; 72 | var opts = mergeOpts(options, getOpts(dir)); 73 | 74 | var filter = parts[depth]; 75 | 76 | 77 | // check for exec.js 78 | try { 79 | var fname = '/test'+name+'/exec.js'; 80 | var js = fs.readFileSync(appRoot.resolve(fname), 'utf-8'); 81 | 82 | testExec(name, js, opts); 83 | } 84 | catch(e) { 85 | if (e.code !== 'ENOENT') { 86 | throw e; 87 | } 88 | 89 | // no exec.js - traverse for fixtures 90 | 91 | getDirectories(dir) 92 | .filter(function(folder) { 93 | return filter === '*' || folder === filter; 94 | }).map(function(folder) { 95 | (depth === maxDepth ? test : testGroup)(path.join(dir, folder), `${name}/${folder}`, opts, depth+1); 96 | }); 97 | } 98 | } 99 | 100 | function runExec(opts, execCode) { 101 | var fn = new Function('babelHelpers', 'assert', 'transform', 'opts', 'exports', execCode); 102 | return fn.apply(null, [babelHelpers, assert, babel.transform, opts, {}]); 103 | } 104 | 105 | // from babel-transform-fixture-test-runner 106 | 107 | function testExec(name, execCode, execOpts) { 108 | var result = babel.transform(execCode, finalizeOpts(execOpts)); 109 | execCode = result.code; 110 | try { 111 | it(name, function() { 112 | runExec(execOpts, execCode); 113 | }) 114 | } catch (err) { 115 | err.message = name + ": " + err.message; 116 | err.message += codeFrame(execCode); 117 | throw err; 118 | } 119 | } 120 | 121 | function test(dir, name, options) { 122 | it(name, function () { 123 | var actualPath = path.join(dir, 'actual.js'); 124 | var expectedPath = path.join(dir, 'expected.js'); 125 | 126 | var opts = mergeOpts(options, getOpts(dir)); 127 | var throwsOpt = opts.throws; 128 | 129 | 130 | deleteTestingOptions(opts); 131 | 132 | opts = finalizeOpts(opts); 133 | 134 | var actual; 135 | 136 | try { 137 | actual = babel.transformFileSync(actualPath, opts).code; 138 | } 139 | catch(e) { 140 | if (throwsOpt) { 141 | var regex = new RegExp(throwsOpt); 142 | var expectedPattern = "Pattern /" + throwsOpt + "/"; 143 | if (!regex.test(e.message)) { 144 | assert.equal(expectedPattern, e.message, "Should throw an error matching the pattern"); 145 | } else { 146 | return assert.ok(expectedPattern); 147 | } 148 | } 149 | throw e; 150 | } 151 | 152 | var expected = fs.readFileSync(expectedPath, textEncoding); 153 | 154 | assert.equal(normalizeEndings(actual), normalizeEndings(expected)); 155 | }); 156 | } 157 | 158 | function deleteTestingOptions(options) { 159 | ["throws"].forEach(function(e) { 160 | if (options[e]) delete options[e]; 161 | }); 162 | } 163 | 164 | function getDirectories(srcpath) { 165 | return fs.readdirSync(srcpath).filter(function(file) { 166 | return fs.statSync(path.join(srcpath, file)).isDirectory(); 167 | }); 168 | } 169 | 170 | function getOpts(srcpath) { 171 | var opts = {}; 172 | try { 173 | opts = JSON.parse(fs.readFileSync(path.resolve(srcpath, "options.json"), textEncoding)); 174 | } 175 | catch(e) { } 176 | return opts; 177 | } 178 | 179 | function finalizeOpts(opts) { 180 | 181 | // convert module name to relative path 182 | 183 | opts.plugins = (opts.plugins||[]).map(function(e) { 184 | 185 | if (e[0] === PLUGIN_NAME) { 186 | e[0] = appRoot.resolve("/lib"); 187 | } 188 | 189 | return e.length === 1 ? 190 | e[0] : 191 | e; 192 | }); 193 | 194 | return opts; 195 | 196 | } 197 | 198 | function mergeOpts(target, src) { 199 | var last = null; 200 | 201 | var plugins = (target.plugins || []) 202 | .concat(src.plugins || []); 203 | 204 | plugins = plugins.map(function(e) { 205 | e = asArray(e); 206 | 207 | if (e[0] === ORIGINAL_PLUGIN_NAME) { 208 | e[0] = PLUGIN_NAME; 209 | } 210 | return e; 211 | }) 212 | // remove dup module defs 213 | .sort(function(a,b) { 214 | 215 | return a[0].localeCompare(b[0]); 216 | }) 217 | .filter(function(e) { 218 | if (!last || e[0] !== last[0]) { 219 | last = e; 220 | return true; 221 | }; 222 | 223 | // merge plugin options from child options.json 224 | if (e[1]) { 225 | last[1] = _.assign({}, last[1], e[1]); 226 | } 227 | 228 | return false; 229 | }); 230 | 231 | 232 | var options = _.assign({}, target, src); 233 | options.plugins = plugins; 234 | 235 | return options; 236 | } 237 | 238 | function asArray(obj) { 239 | if (obj === null || obj === undefined) return []; 240 | return Array.isArray(obj) ? 241 | obj.map(function(e) { 242 | return e; 243 | }) : 244 | [obj]; 245 | } 246 | 247 | /* ensure o/s specific line endings, and multiple blank lines aren't a problem: 248 | -- normalize line endings 249 | -- remove duplicate all all blank lines 250 | -- ensure every file ends with a newline 251 | */ 252 | 253 | function normalizeEndings(text) { 254 | 255 | text = text.replace(/\r\n/g, '\n'); 256 | text = text + '\n'; 257 | 258 | text = text.replace(/[\n]{2,}/g, '\n'); 259 | text = text.replace(/\n/g, eol); 260 | 261 | return text; 262 | } 263 | 264 | // main entry 265 | 266 | testGroup(__dirname, '', {}); 267 | 268 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter dot --ui tdd --timeout 10000 -------------------------------------------------------------------------------- /test/no-mangle/auxiliary-comment/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers"], 3 | "auxiliaryCommentBefore": "before", 4 | "auxiliaryCommentAfter": "after" 5 | } 6 | -------------------------------------------------------------------------------- /test/no-mangle/auxiliary-comment/overview/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | import foo from "foo2"; 5 | import * as foo2 from "foo3"; 6 | import {bar} from "foo4"; 7 | import {foo as bar2} from "foo5"; 8 | 9 | export {test}; 10 | export var test = 5; 11 | 12 | bar(foo, bar2); 13 | 14 | /* my comment */ 15 | bar2; 16 | foo; -------------------------------------------------------------------------------- /test/no-mangle/auxiliary-comment/overview/expected.js: -------------------------------------------------------------------------------- 1 | /*before*/"use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.test = undefined; 7 | 8 | /*after*/ 9 | /*before*/require("foo"); /*after*/ 10 | /*before*/require("foo-bar"); /*after*/ 11 | /*before*/require("./directory/foo-bar"); /*after*/ 12 | var /*before*/_foo = require("foo2") /*after*/; 13 | /*before*/var /*after*/foo = babelHelpers.interopRequireDefault(_foo).default; 14 | var /*before*/_foo2 = require("foo3") /*after*/; 15 | /*before*/var /*after*/foo2 = babelHelpers.interopRequireWildcard(_foo2); 16 | var /*before*/_foo3 = require("foo4") /*after*/; 17 | /*before*/var /*after*/bar = _foo3.bar; 18 | var /*before*/_foo4 = require("foo5") /*after*/; 19 | /*before*/var /*after*/bar2 = _foo4.foo; 20 | /*before*/exports. /*after*/test = test; 21 | var test = /*before*/exports. /*after*/test = 5; 22 | 23 | bar(foo, bar2); 24 | 25 | /* my comment */ 26 | bar2; 27 | foo; -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-default-non-function/actual.js: -------------------------------------------------------------------------------- 1 | export default new Cachier() 2 | 3 | export function Cachier(databaseName) {} 4 | -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-default-non-function/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.Cachier = Cachier; 7 | exports.default = new Cachier(); 8 | function Cachier(databaseName) {} -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-default/actual.js: -------------------------------------------------------------------------------- 1 | export default 42; 2 | export default {}; 3 | export default []; 4 | export default foo; 5 | export default function () {} 6 | export default class {} 7 | export default function foo () {} 8 | export default class Foo {} 9 | export { foo as default }; 10 | export default (function(){return "foo"})(); -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | exports.default = function () {}; 8 | 9 | exports.default = foo; 10 | exports.default = 42; 11 | exports.default = {}; 12 | exports.default = []; 13 | exports.default = foo; 14 | exports.default = class {}; 15 | function foo() {} 16 | class Foo {} 17 | exports.default = Foo; 18 | exports.default = foo; 19 | 20 | exports.default = function () { 21 | return "foo"; 22 | }(); -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-from/actual.js: -------------------------------------------------------------------------------- 1 | export * from "foo"; 2 | export {foo} from "foo"; 3 | export {foo, bar} from "foo"; 4 | export {foo as bar} from "foo"; 5 | export {foo as default} from "foo"; 6 | export {foo as default, bar} from "foo"; 7 | export {default as foo} from "foo"; 8 | -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-from/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _foo = require("foo"); 8 | 9 | Object.keys(_foo).forEach(function (key) { 10 | if (key === "default") return; 11 | Object.defineProperty(exports, key, { 12 | enumerable: true, 13 | get: function () { 14 | return _foo[key]; 15 | } 16 | }); 17 | }); 18 | Object.defineProperty(exports, "foo", { 19 | enumerable: true, 20 | get: function () { 21 | return _foo.foo; 22 | } 23 | }); 24 | Object.defineProperty(exports, "foo", { 25 | enumerable: true, 26 | get: function () { 27 | return _foo.foo; 28 | } 29 | }); 30 | Object.defineProperty(exports, "bar", { 31 | enumerable: true, 32 | get: function () { 33 | return _foo.bar; 34 | } 35 | }); 36 | Object.defineProperty(exports, "bar", { 37 | enumerable: true, 38 | get: function () { 39 | return _foo.foo; 40 | } 41 | }); 42 | Object.defineProperty(exports, "default", { 43 | enumerable: true, 44 | get: function () { 45 | return _foo.foo; 46 | } 47 | }); 48 | Object.defineProperty(exports, "default", { 49 | enumerable: true, 50 | get: function () { 51 | return _foo.foo; 52 | } 53 | }); 54 | Object.defineProperty(exports, "bar", { 55 | enumerable: true, 56 | get: function () { 57 | return _foo.bar; 58 | } 59 | }); 60 | Object.defineProperty(exports, "foo", { 61 | enumerable: true, 62 | get: function () { 63 | return babelHelpers.interopRequireDefault(_foo).default; 64 | } 65 | }); -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-named/actual.js: -------------------------------------------------------------------------------- 1 | export {foo}; 2 | export {foo, bar}; 3 | export {foo as bar}; 4 | export {foo as default}; 5 | export {foo as default, bar}; 6 | -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.foo = foo; 7 | exports.foo = foo; 8 | exports.bar = bar; 9 | exports.bar = foo; 10 | exports.default = foo; 11 | exports.default = foo; 12 | exports.bar = bar; -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-variable/actual.js: -------------------------------------------------------------------------------- 1 | export var foo = 1; 2 | export var foo = 1, bar = 2; 3 | export var foo2 = function () {}; 4 | export var foo3; 5 | export let foo4 = 2; 6 | export let foo5; 7 | export const foo6 = 3; 8 | export function foo7 () {} 9 | export class foo8 {} 10 | -------------------------------------------------------------------------------- /test/no-mangle/interop/exports-variable/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.foo7 = foo7; 7 | var foo = exports.foo = 1; 8 | var foo = exports.foo = 1, 9 | bar = exports.bar = 2; 10 | var foo2 = exports.foo2 = function () {}; 11 | var foo3 = exports.foo3 = undefined; 12 | let foo4 = exports.foo4 = 2; 13 | let foo5 = exports.foo5 = undefined; 14 | const foo6 = exports.foo6 = 3; 15 | function foo7() {} 16 | class foo8 {} 17 | exports.foo8 = foo8; -------------------------------------------------------------------------------- /test/no-mangle/interop/hoist-function-exports/actual.js: -------------------------------------------------------------------------------- 1 | import { isEven } from "./evens"; 2 | 3 | export function nextOdd(n) { 4 | return isEven(n) ? n + 1 : n + 2; 5 | } 6 | 7 | export var isOdd = (function (isEven) { 8 | return function (n) { 9 | return !isEven(n); 10 | }; 11 | })(isEven); 12 | -------------------------------------------------------------------------------- /test/no-mangle/interop/hoist-function-exports/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.isOdd = undefined; 7 | exports.nextOdd = nextOdd; 8 | 9 | var _evens = require("./evens"); 10 | 11 | var isEven = _evens.isEven; 12 | function nextOdd(n) { 13 | return isEven(n) ? n + 1 : n + 2; 14 | } 15 | 16 | var isOdd = exports.isOdd = function (isEven) { 17 | return function (n) { 18 | return !isEven(n); 19 | }; 20 | }(isEven); -------------------------------------------------------------------------------- /test/no-mangle/interop/illegal-export-esmodule-2/actual.js: -------------------------------------------------------------------------------- 1 | export { __esModule }; 2 | -------------------------------------------------------------------------------- /test/no-mangle/interop/illegal-export-esmodule-2/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "throws": "Illegal export \"__esModule\"" 3 | } 4 | -------------------------------------------------------------------------------- /test/no-mangle/interop/illegal-export-esmodule/actual.js: -------------------------------------------------------------------------------- 1 | export var __esModule = false; 2 | -------------------------------------------------------------------------------- /test/no-mangle/interop/illegal-export-esmodule/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "throws": "Illegal export \"__esModule\"" 3 | } 4 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-default/actual.js: -------------------------------------------------------------------------------- 1 | import foo from "foo"; 2 | import {default as foo2} from "foo"; 3 | 4 | foo; 5 | foo2; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo = babelHelpers.interopRequireDefault(_foo).default; 6 | var foo2 = babelHelpers.interopRequireDefault(_foo).default; 7 | 8 | 9 | foo; 10 | foo2; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-glob/actual.js: -------------------------------------------------------------------------------- 1 | import * as foo from "foo"; 2 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-glob/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo = babelHelpers.interopRequireWildcard(_foo); -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-hoisting/actual.js: -------------------------------------------------------------------------------- 1 | tag`foo`; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-hoisting/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _taggedTemplateLiteral2 = require("babel-runtime/helpers/taggedTemplateLiteral"); 4 | 5 | var _taggedTemplateLiteral = _interopRequireDefault(_taggedTemplateLiteral2).default; 6 | 7 | var _templateObject = _taggedTemplateLiteral(["foo"], ["foo"]); 8 | 9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 10 | 11 | tag(_templateObject); -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-hoisting/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-runtime", 4 | "transform-es2015-template-literals" 5 | ] 6 | } -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-mixing/actual.js: -------------------------------------------------------------------------------- 1 | import foo, {baz as xyz} from "foo"; 2 | 3 | foo; 4 | xyz; 5 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-mixing/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo = babelHelpers.interopRequireDefault(_foo).default; 6 | var xyz = _foo.baz; 7 | 8 | 9 | foo; 10 | xyz; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-multiple-default/actual.js: -------------------------------------------------------------------------------- 1 | import foo from "foo"; 2 | import { default as foo2 } from "foo"; 3 | import { foo3 } from "foo"; 4 | import * as foo4 from "foo"; 5 | 6 | foo; 7 | foo2; 8 | foo3; 9 | foo4.foo2; 10 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-multiple-default/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo4 = babelHelpers.interopRequireWildcard(_foo); 6 | var foo = babelHelpers.interopRequireDefault(_foo).default; 7 | var foo2 = babelHelpers.interopRequireDefault(_foo).default; 8 | var foo3 = _foo.foo3; 9 | 10 | 11 | foo; 12 | foo2; 13 | foo3; 14 | foo4.foo2; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-named/actual.js: -------------------------------------------------------------------------------- 1 | import {bar} from "foo"; 2 | import {bar2, baz} from "foo"; 3 | import {bar as baz2} from "foo"; 4 | import {bar as baz3, xyz} from "foo"; 5 | 6 | bar; 7 | bar2; 8 | baz; 9 | baz2; 10 | baz3; 11 | xyz; 12 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports-named/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var bar = _foo.bar; 6 | var bar2 = _foo.bar2; 7 | var baz = _foo.baz; 8 | var baz2 = _foo.bar; 9 | var baz3 = _foo.bar; 10 | var xyz = _foo.xyz; 11 | 12 | 13 | bar; 14 | bar2; 15 | baz; 16 | baz2; 17 | baz3; 18 | xyz; -------------------------------------------------------------------------------- /test/no-mangle/interop/imports/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | -------------------------------------------------------------------------------- /test/no-mangle/interop/imports/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | require("foo"); 4 | 5 | require("foo-bar"); 6 | 7 | require("./directory/foo-bar"); -------------------------------------------------------------------------------- /test/no-mangle/interop/module-shadow/actual.js: -------------------------------------------------------------------------------- 1 | export function module() { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /test/no-mangle/interop/module-shadow/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | function _module() {} 7 | exports.module = _module; -------------------------------------------------------------------------------- /test/no-mangle/interop/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers"] 3 | } -------------------------------------------------------------------------------- /test/no-mangle/interop/overview/actual.js: -------------------------------------------------------------------------------- 1 | import "foo"; 2 | import "foo-bar"; 3 | import "./directory/foo-bar"; 4 | import foo from "foo2"; 5 | import * as foo2 from "foo3"; 6 | import {bar} from "foo4"; 7 | import {foo as bar2} from "foo5"; 8 | 9 | export {test}; 10 | export var test = 5; 11 | 12 | bar; 13 | bar2; 14 | foo; 15 | -------------------------------------------------------------------------------- /test/no-mangle/interop/overview/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | exports.test = undefined; 7 | 8 | require("foo"); 9 | 10 | require("foo-bar"); 11 | 12 | require("./directory/foo-bar"); 13 | 14 | var _foo = require("foo2"); 15 | 16 | var foo = babelHelpers.interopRequireDefault(_foo).default; 17 | 18 | var _foo2 = require("foo3"); 19 | 20 | var foo2 = babelHelpers.interopRequireWildcard(_foo2); 21 | 22 | var _foo3 = require("foo4"); 23 | 24 | var bar = _foo3.bar; 25 | 26 | var _foo4 = require("foo5"); 27 | 28 | var bar2 = _foo4.foo; 29 | exports.test = test; 30 | var test = exports.test = 5; 31 | 32 | bar; 33 | bar2; 34 | foo; -------------------------------------------------------------------------------- /test/no-mangle/interop/remap/actual.js: -------------------------------------------------------------------------------- 1 | export var test = 2; 2 | test = 5; 3 | test++; 4 | 5 | (function () { 6 | var test = 2; 7 | test = 3; 8 | test++; 9 | })(); 10 | 11 | var a = 2; 12 | export { a }; 13 | a = 3; 14 | 15 | var b = 2; 16 | export { b as c }; 17 | b = 3; 18 | 19 | var d = 3; 20 | export { d as e, d as f }; 21 | d = 4; 22 | -------------------------------------------------------------------------------- /test/no-mangle/interop/remap/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | var test = exports.test = 2; 7 | exports.test = test = 5; 8 | exports.test = test += 1; 9 | 10 | (function () { 11 | var test = 2; 12 | test = 3; 13 | test++; 14 | })(); 15 | 16 | var a = 2; 17 | exports.a = a; 18 | 19 | exports.a = a = 3; 20 | 21 | var b = 2; 22 | exports.c = b; 23 | 24 | exports.c = b = 3; 25 | 26 | var d = 3; 27 | exports.e = d; 28 | exports.f = d; 29 | 30 | exports.f = exports.e = d = 4; -------------------------------------------------------------------------------- /test/no-mangle/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [["transform-es2015-modules-commonjs", { "noMangle": true }]] 3 | } -------------------------------------------------------------------------------- /test/no-mangle/regression/T7165/actual.js: -------------------------------------------------------------------------------- 1 | import foo from 'foo'; 2 | import { something } from 'bar'; 3 | 4 | const anything = {}; 5 | 6 | export * from 'bar'; -------------------------------------------------------------------------------- /test/no-mangle/regression/T7165/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _bar = require('bar'); 8 | 9 | Object.keys(_bar).forEach(function (key) { 10 | if (key === "default") return; 11 | Object.defineProperty(exports, key, { 12 | enumerable: true, 13 | get: function () { 14 | return _bar[key]; 15 | } 16 | }); 17 | }); 18 | 19 | var _foo = require('foo'); 20 | var foo = _interopRequireDefault(_foo).default; 21 | var something = _bar.something; 22 | 23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 24 | 25 | var anything = {}; -------------------------------------------------------------------------------- /test/no-mangle/regression/T7165/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-block-scoping" 4 | ] 5 | } -------------------------------------------------------------------------------- /test/no-mangle/strict/export-1/actual.js: -------------------------------------------------------------------------------- 1 | export default foo; 2 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export-1/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.default = foo; -------------------------------------------------------------------------------- /test/no-mangle/strict/export-2/actual.js: -------------------------------------------------------------------------------- 1 | export { foo as default }; 2 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export-2/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.default = foo; -------------------------------------------------------------------------------- /test/no-mangle/strict/export-all/actual.js: -------------------------------------------------------------------------------- 1 | export * from 'mod'; 2 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export-all/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _mod = require('mod'); 4 | 5 | Object.keys(_mod).forEach(function (key) { 6 | if (key === "default") return; 7 | Object.defineProperty(exports, key, { 8 | enumerable: true, 9 | get: function () { 10 | return _mod[key]; 11 | } 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export-all/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "transform-es2015-block-scoping" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export/actual.js: -------------------------------------------------------------------------------- 1 | export function foo() {} 2 | -------------------------------------------------------------------------------- /test/no-mangle/strict/export/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | exports.foo = foo; 4 | function foo() {} -------------------------------------------------------------------------------- /test/no-mangle/strict/import/actual.js: -------------------------------------------------------------------------------- 1 | import foo from "foo"; 2 | import { default as foo2 } from "foo"; 3 | import { foo3 } from "foo"; 4 | import * as foo4 from "foo"; 5 | 6 | foo; 7 | foo2; 8 | foo3; 9 | foo3(); -------------------------------------------------------------------------------- /test/no-mangle/strict/import/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _foo = require("foo"); 4 | 5 | var foo = babelHelpers.interopRequireDefault(_foo).default; 6 | var foo2 = babelHelpers.interopRequireDefault(_foo).default; 7 | var foo3 = _foo.foo3; 8 | 9 | 10 | foo; 11 | foo2; 12 | foo3; 13 | foo3(); -------------------------------------------------------------------------------- /test/no-mangle/strict/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "strict": true }]] 3 | } 4 | --------------------------------------------------------------------------------