├── .babelrc ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .npmignore ├── .npmrc ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json ├── src ├── index.js └── utils.js ├── test ├── .eslintrc ├── fixtures │ ├── basic-import │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ ├── chained-import │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ ├── dynamic-argument │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ ├── import-with-comment │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ ├── nested-import │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ ├── non-string-argument │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js │ └── template-argument │ │ ├── actual.js │ │ ├── expected.6.es2015.js │ │ ├── expected.6.js │ │ ├── expected.6.noInterop.js │ │ ├── expected.7.es2015.js │ │ ├── expected.7.js │ │ └── expected.7.noInterop.js ├── index.js └── testPlugin.js └── utils.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "airbnb" 4 | ], 5 | "plugins": [ 6 | "add-module-exports", 7 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 8 | ], 9 | } 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | test/fixtures 2 | lib/ 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "extends": "airbnb-base", 4 | "root": true 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Build library 40 | lib/ 41 | 42 | # Only apps should have lockfiles 43 | yarn.lock 44 | package-lock.json 45 | npm-shrinkwrap.json 46 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | test/ 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "13" 4 | - "12" 5 | - "11" 6 | - "10" 7 | - "9" 8 | - "8" 9 | - "7" 10 | - "6" 11 | - "5" 12 | - "4" 13 | - "iojs-v3" 14 | - "iojs-v2" 15 | - "iojs-v1" 16 | - "0.12" 17 | import: 18 | - ljharb/travis-ci:node/latest-npm.yml 19 | - ljharb/travis-ci:node/greenkeeper.yml 20 | - ljharb/travis-ci:node/dependabot.yml 21 | script: 'npm run tests-only' 22 | sudo: false 23 | env: 24 | - TEST=true 25 | matrix: 26 | fast_finish: true 27 | include: 28 | - node_js: "lts/*" 29 | env: PRETEST=true 30 | script: 'npm run pretest' 31 | allow_failures: 32 | - node_js: "9" 33 | - node_js: "7" 34 | - node_js: "5" 35 | - node_js: "iojs-v3" 36 | - node_js: "iojs-v2" 37 | - node_js: "iojs-v1" 38 | - env: TEST=true ALLOW_FAILURE=true 39 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## Unreleased 2 | 3 | ## v2.3.3 4 | - [Fix] Generate webpack-compatible output with static string arg (#84) 5 | - [Fix] Handle visiting the same path gracefully (#71) 6 | 7 | ## v2.3.2 8 | - [Fix] avoid using reserved param name in babel 7 templates 9 | - [Dev Deps] update `airbnb-js-shims` 10 | 11 | ## v2.3.1 12 | - [Fix] Evaluate `import()` argument synchronously (#85) 13 | - [Dev Deps] update `eslint`, `eslint-config-airbnb-base`, `in-publish`, `rimraf`, `safe-publish-latest`, `tape` 14 | 15 | ## v2.3.0 16 | - [New] expose createDynamicImportTransform and getImportSource (#75) 17 | - [Docs] Document noInterop option (#70) 18 | 19 | ## v2.2.0 20 | - [Refactor] remove dependency on babel-plugin-syntax-dynamic-import 21 | - [Dev Deps] update `airbnb-js-shims`, `babel-preset-airbnb`, `eslint` 22 | 23 | ## v2.1.0 24 | - [New] add `noInterop` option (#57) 25 | - [Docs] Fix typo "correct" -> "correctly" in readme (#55) 26 | - [Dev Deps] update `airbnb-js-shims`, `babel-eslint`, `babel-preset-airbnb`, `eslint`, `eslint-config-airbnb-base`, `eslint-plugin-import`, `safe-publish-latest` 27 | 28 | ## v2.0.0 29 | - [Breaking] always return a module namespace object (#52, #47) 30 | - [Breaking] remove `.default` on entry points (#27) 31 | - [Docs] removed $ before npm command (#35) 32 | - [Docs] Improve README.md with a code example (#41) 33 | - [Dev Deps] update `airbnb-js-shims`, `babel-core`, `babel-eslint`, `eslint`, `eslint-plugin-import` 34 | - [Tests] switch from mocha to tape, so we can support older nodes 35 | 36 | ## v1.2.0 37 | - [New] support comments (#37) 38 | - [Refactor] Use template and types from the babel object (#32) 39 | - [Tests] on `node` `v9`; pin included builds to LTS 40 | - [Dev Deps] update `eslint`, `eslint-config-airbnb-base`, `mocha`, `rimraf` 41 | 42 | ## v1.1.0 43 | - Visit Import nodes instead of CallExpressions (#30) 44 | - [Deps] update `babel-template`, `babel-types` 45 | - [Dev Deps] update `airbnb-js-shims`, `babel-cli`, `babel-core`, `babel-preset-airbnb`, `babel-register`, `chai`, `eslint`, `eslint-config-airbnb-base`, `eslint-plugin-import`, `mocha` 46 | - [Tests] on `node` `v8` 47 | - [Tests] use `nvm install-latest-npm` so newer npm doesn’t break older node 48 | 49 | ## v1.0.2 50 | - [Fix] Ensure it works with the ES2015 preset too (#12, #16) 51 | - [Deps] update `babel-template`, `babel-types` 52 | - [Dev Deps] update `babel-cli`, `babel-core`, `babel-eslint`, `babel-register`, `eslint`, `eslint-config-airbnb-base`, `mocha` 53 | 54 | ## v1.0.1 55 | - [Fix] Move `in-publish` to devDeps (#11) 56 | - [Fix] ensure dynamic `import()` input is properly stringified (#2) 57 | - [Fix] async timing of dynamic import to match spec (#3) 58 | - [Fix] Remove spaces in template strings and update Babel (#10) 59 | - [Deps] update `babel-template`, `babel-types` 60 | - [Deps] update `babel-types` (#4, #5, #6) 61 | - [Dev Deps] update `babel-cli`, `babel-core`, `babel-eslint`, `babel-register`, `eslint`, `eslint-config-airbnb-base`, `eslint-plugin-import`, `mocha`, `rimraf` 62 | 63 | ## v1.0.0 64 | - Initial full release. 65 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Airbnb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-dynamic-import-node 2 | 3 | Babel plugin to transpile `import()` to a deferred `require()`, for node. Matches the [proposed spec](https://github.com/domenic/proposal-import-function). 4 | 5 | **NOTE:** Babylon >= v6.12.0 is required to correctly parse dynamic imports. 6 | 7 | **NOTE:** This plugin generates code compatible with Node.js. Webpack >= 2 supports `import()` natively, and for Webpack 1 you can use [`babel-plugin-dynamic-import-webpack`](https://github.com/airbnb/babel-plugin-dynamic-import-webpack) that generates Webpack-compatible output. 8 | 9 | ## Installation 10 | 11 | ```sh 12 | npm install babel-plugin-dynamic-import-node --save-dev 13 | ``` 14 | 15 | ## Usage 16 | 17 | ### Via `.babelrc` (Recommended) 18 | 19 | **.babelrc** 20 | 21 | ```json 22 | { 23 | "plugins": ["dynamic-import-node"] 24 | } 25 | ``` 26 | 27 | #### Options 28 | 29 | - *`noInterop`* - A boolean value, that if true will not interop the require calls. Useful to avoid using `require('module').default` on commonjs modules. 30 | 31 | ```json 32 | { 33 | "plugins": [ 34 | ["dynamic-import-node", { "noInterop": true }] 35 | ] 36 | } 37 | ``` 38 | 39 | ### Via CLI 40 | 41 | ```sh 42 | $ babel --plugins dynamic-import-node script.js 43 | ``` 44 | 45 | ### Via Node API 46 | 47 | ```javascript 48 | require('babel-core').transform('code', { 49 | plugins: ['dynamic-import-node'] 50 | }); 51 | ``` 52 | 53 | ### Code Example 54 | ```javascript 55 | Promise.all([ 56 | import('./lib/import1'), 57 | import('./lib/import2') 58 | ]).then(([ 59 | Import1, 60 | Import2 61 | ]) => { 62 | console.log(Import1); 63 | /* CODE HERE*/ 64 | }); 65 | ``` 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-dynamic-import-node", 3 | "version": "2.3.3", 4 | "description": "Babel plugin to transpile import() to a deferred require(), for node", 5 | "main": "lib/index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "clean": "rimraf lib", 11 | "prebuild": "npm run clean", 12 | "build": "babel src --out-dir lib", 13 | "pretest": "npm run lint", 14 | "test": "npm run tests-only", 15 | "tests-only": "tape --require airbnb-js-shims --require babel-register test", 16 | "lint": "eslint .", 17 | "prepublish": "not-in-publish || (safe-publish-latest && npm run build)", 18 | "check-changelog": "expr $(git status --porcelain 2>/dev/null| grep \"^\\s*M.*CHANGELOG.md\" | wc -l) >/dev/null || (echo 'Please edit CHANGELOG.md' && exit 1)", 19 | "check-only-changelog-changed": "(expr $(git status --porcelain 2>/dev/null| grep -v \"CHANGELOG.md\" | wc -l) >/dev/null && echo 'Only CHANGELOG.md may have uncommitted changes' && exit 1) || exit 0", 20 | "version:major": "npm --no-git-tag-version version major", 21 | "version:minor": "npm --no-git-tag-version version minor", 22 | "version:patch": "npm --no-git-tag-version version patch", 23 | "postversion": "git commit package.json CHANGELOG.md -m \"v$npm_package_version\" && npm run tag && git push && git push --tags", 24 | "preversion": "npm run test && npm run check-changelog && npm run check-only-changelog-changed", 25 | "tag": "git tag v$npm_package_version" 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+https://github.com/airbnb/babel-plugin-dynamic-import-node.git" 30 | }, 31 | "keywords": [ 32 | "babel", 33 | "plugin", 34 | "dynamic", 35 | "import", 36 | "node" 37 | ], 38 | "author": "Jordan Gensler ", 39 | "license": "MIT", 40 | "bugs": { 41 | "url": "https://github.com/airbnb/babel-plugin-dynamic-import-node/issues" 42 | }, 43 | "homepage": "https://github.com/airbnb/babel-plugin-dynamic-import-node#readme", 44 | "devDependencies": { 45 | "@babel/core": "^7.9.0", 46 | "@babel/plugin-external-helpers": "^7.8.3", 47 | "@babel/plugin-transform-template-literals": "^7.8.3", 48 | "@babel/preset-env": "^7.9.5", 49 | "airbnb-js-shims": "^2.2.1", 50 | "babel-cli": "^6.26.0", 51 | "babel-core": "^6.26.3", 52 | "babel-eslint": "^9.0.0", 53 | "babel-plugin-add-module-exports": "^0.2.1", 54 | "babel-plugin-external-helpers": "^6.22.0", 55 | "babel-plugin-transform-es2015-template-literals": "^6.22.0", 56 | "babel-plugin-transform-replace-object-assign": "^1.0.0", 57 | "babel-preset-airbnb": "^2.6.0", 58 | "babel-preset-es2015": "^6.24.1", 59 | "babel-register": "^6.26.0", 60 | "eslint": "^6.8.0", 61 | "eslint-config-airbnb-base": "^14.1.0", 62 | "eslint-plugin-import": "^2.20.2", 63 | "in-publish": "^2.0.1", 64 | "rimraf": "^2.7.1", 65 | "safe-publish-latest": "^1.1.4", 66 | "tape": "^5.0.0-next.5" 67 | }, 68 | "dependencies": { 69 | "object.assign": "^4.1.0" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { createDynamicImportTransform } from './utils'; 2 | 3 | export default function (api) { 4 | const transformImport = createDynamicImportTransform(api); 5 | 6 | return { 7 | // NOTE: Once we drop support for Babel <= v6 we should 8 | // update this to import from @babel/plugin-syntax-dynamic-import. 9 | // https://www.npmjs.com/package/@babel/plugin-syntax-dynamic-import 10 | manipulateOptions(opts, parserOpts) { 11 | parserOpts.plugins.push('dynamicImport'); 12 | }, 13 | 14 | visitor: { 15 | Import(path) { 16 | transformImport(this, path); 17 | }, 18 | }, 19 | }; 20 | } 21 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | export function getImportSource(t, callNode) { 2 | const importArguments = callNode.arguments; 3 | const [importPath] = importArguments; 4 | 5 | const isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath); 6 | if (isString) { 7 | t.removeComments(importPath); 8 | return importPath; 9 | } 10 | 11 | return t.templateLiteral([ 12 | t.templateElement({ raw: '', cooked: '' }), 13 | t.templateElement({ raw: '', cooked: '' }, true), 14 | ], importArguments); 15 | } 16 | 17 | export function createDynamicImportTransform({ template, types: t }) { 18 | const builders = { 19 | static: { 20 | interop: template('Promise.resolve().then(() => INTEROP(require(SOURCE)))'), 21 | noInterop: template('Promise.resolve().then(() => require(SOURCE))'), 22 | }, 23 | dynamic: { 24 | interop: template('Promise.resolve(SOURCE).then(s => INTEROP(require(s)))'), 25 | noInterop: template('Promise.resolve(SOURCE).then(s => require(s))'), 26 | }, 27 | }; 28 | 29 | const visited = typeof WeakSet === 'function' && new WeakSet(); 30 | 31 | const isString = (node) => t.isStringLiteral(node) 32 | || (t.isTemplateLiteral(node) && node.expressions.length === 0); 33 | 34 | return (context, path) => { 35 | if (visited) { 36 | if (visited.has(path)) { 37 | return; 38 | } 39 | visited.add(path); 40 | } 41 | 42 | const SOURCE = getImportSource(t, path.parent); 43 | 44 | const builder = isString(SOURCE) ? builders.static : builders.dynamic; 45 | 46 | const newImport = context.opts.noInterop 47 | ? builder.noInterop({ SOURCE }) 48 | : builder.interop({ SOURCE, INTEROP: context.addHelper('interopRequireWildcard') }); 49 | 50 | path.parentPath.replaceWith(newImport); 51 | }; 52 | } 53 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "mocha": true, 4 | }, 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/actual.js: -------------------------------------------------------------------------------- 1 | const testModule = import('test-module'); 2 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | var testModule = Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('test-module')); 3 | }); 4 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.6.js: -------------------------------------------------------------------------------- 1 | const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); 2 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | const testModule = Promise.resolve().then(() => require('test-module')); 2 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | var testModule = Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('test-module')); 3 | }); 4 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.7.js: -------------------------------------------------------------------------------- 1 | const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); 2 | -------------------------------------------------------------------------------- /test/fixtures/basic-import/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | const testModule = Promise.resolve().then(() => require('test-module')); 2 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/actual.js: -------------------------------------------------------------------------------- 1 | import('test-module').then(() => ( 2 | import('test-module-2') 3 | )); 4 | 5 | Promise.all([ 6 | import('test-1'), 7 | import('test-2'), 8 | import('test-3'), 9 | ]).then(() => {}); 10 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('test-module')); 3 | }).then(function () { 4 | return Promise.resolve().then(function () { 5 | return babelHelpers.interopRequireWildcard(require('test-module-2')); 6 | }); 7 | }); 8 | 9 | Promise.all([Promise.resolve().then(function () { 10 | return babelHelpers.interopRequireWildcard(require('test-1')); 11 | }), Promise.resolve().then(function () { 12 | return babelHelpers.interopRequireWildcard(require('test-2')); 13 | }), Promise.resolve().then(function () { 14 | return babelHelpers.interopRequireWildcard(require('test-3')); 15 | })]).then(function () {}); 16 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.6.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))).then(() => Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module-2')))); 2 | 3 | Promise.all([Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-1'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-2'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-3')))]).then(() => {}); 4 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require('test-module')).then(() => Promise.resolve().then(() => require('test-module-2'))); 2 | 3 | Promise.all([Promise.resolve().then(() => require('test-1')), Promise.resolve().then(() => require('test-2')), Promise.resolve().then(() => require('test-3'))]).then(() => {}); 4 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('test-module')); 3 | }).then(function () { 4 | return Promise.resolve().then(function () { 5 | return babelHelpers.interopRequireWildcard(require('test-module-2')); 6 | }); 7 | }); 8 | Promise.all([Promise.resolve().then(function () { 9 | return babelHelpers.interopRequireWildcard(require('test-1')); 10 | }), Promise.resolve().then(function () { 11 | return babelHelpers.interopRequireWildcard(require('test-2')); 12 | }), Promise.resolve().then(function () { 13 | return babelHelpers.interopRequireWildcard(require('test-3')); 14 | })]).then(function () {}); 15 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.7.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))).then(() => Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module-2')))); 2 | Promise.all([Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-1'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-2'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-3')))]).then(() => {}); 3 | -------------------------------------------------------------------------------- /test/fixtures/chained-import/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require('test-module')).then(() => Promise.resolve().then(() => require('test-module-2'))); 2 | Promise.all([Promise.resolve().then(() => require('test-1')), Promise.resolve().then(() => require('test-2')), Promise.resolve().then(() => require('test-3'))]).then(() => {}); 3 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/actual.js: -------------------------------------------------------------------------------- 1 | import(MODULE); 2 | 3 | let i = 0; 4 | import(i++); 5 | 6 | import(fn()); 7 | 8 | async () => import(await "x"); 9 | 10 | function* f() { import(yield "x"); } 11 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | var _this = this; 2 | 3 | var _marked = /*#__PURE__*/regeneratorRuntime.mark(f); 4 | 5 | Promise.resolve("" + String(MODULE)).then(function (s) { 6 | return babelHelpers.interopRequireWildcard(require(s)); 7 | }); 8 | 9 | var i = 0; 10 | Promise.resolve("" + i++).then(function (s) { 11 | return babelHelpers.interopRequireWildcard(require(s)); 12 | }); 13 | 14 | Promise.resolve("" + String(fn())).then(function (s) { 15 | return babelHelpers.interopRequireWildcard(require(s)); 16 | }); 17 | 18 | babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { 19 | return regeneratorRuntime.wrap(function _callee$(_context) { 20 | while (1) { 21 | switch (_context.prev = _context.next) { 22 | case 0: 23 | _context.t0 = Promise; 24 | _context.t1 = String; 25 | _context.next = 4; 26 | return "x"; 27 | 28 | case 4: 29 | _context.t2 = _context.sent; 30 | _context.t3 = (0, _context.t1)(_context.t2); 31 | _context.t4 = "" + _context.t3; 32 | 33 | _context.t5 = function (s) { 34 | return babelHelpers.interopRequireWildcard(require(s)); 35 | }; 36 | 37 | return _context.abrupt("return", _context.t0.resolve.call(_context.t0, _context.t4).then(_context.t5)); 38 | 39 | case 9: 40 | case "end": 41 | return _context.stop(); 42 | } 43 | } 44 | }, _callee, _this); 45 | })); 46 | 47 | function f() { 48 | return regeneratorRuntime.wrap(function f$(_context2) { 49 | while (1) { 50 | switch (_context2.prev = _context2.next) { 51 | case 0: 52 | _context2.t0 = Promise; 53 | _context2.t1 = String; 54 | _context2.next = 4; 55 | return "x"; 56 | 57 | case 4: 58 | _context2.t2 = _context2.sent; 59 | _context2.t3 = (0, _context2.t1)(_context2.t2); 60 | _context2.t4 = "" + _context2.t3; 61 | 62 | _context2.t5 = function (s) { 63 | return babelHelpers.interopRequireWildcard(require(s)); 64 | }; 65 | 66 | _context2.t0.resolve.call(_context2.t0, _context2.t4).then(_context2.t5); 67 | 68 | case 9: 69 | case "end": 70 | return _context2.stop(); 71 | } 72 | } 73 | }, _marked, this); 74 | } 75 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.6.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 2 | 3 | let i = 0; 4 | Promise.resolve(`${i++}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 5 | 6 | Promise.resolve(`${fn()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 7 | 8 | async () => Promise.resolve(`${await "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 9 | 10 | function* f() { 11 | Promise.resolve(`${yield "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${MODULE}`).then(s => require(s)); 2 | 3 | let i = 0; 4 | Promise.resolve(`${i++}`).then(s => require(s)); 5 | 6 | Promise.resolve(`${fn()}`).then(s => require(s)); 7 | 8 | async () => Promise.resolve(`${await "x"}`).then(s => require(s)); 9 | 10 | function* f() { 11 | Promise.resolve(`${yield "x"}`).then(s => require(s)); 12 | } 13 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | var _marked = /*#__PURE__*/regeneratorRuntime.mark(f); 2 | 3 | Promise.resolve("".concat(MODULE)).then(function (s) { 4 | return babelHelpers.interopRequireWildcard(require(s)); 5 | }); 6 | var i = 0; 7 | Promise.resolve("".concat(i++)).then(function (s) { 8 | return babelHelpers.interopRequireWildcard(require(s)); 9 | }); 10 | Promise.resolve("".concat(fn())).then(function (s) { 11 | return babelHelpers.interopRequireWildcard(require(s)); 12 | }); 13 | 14 | /*#__PURE__*/ 15 | babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { 16 | return regeneratorRuntime.wrap(function _callee$(_context) { 17 | while (1) { 18 | switch (_context.prev = _context.next) { 19 | case 0: 20 | _context.t0 = Promise; 21 | _context.t1 = ""; 22 | _context.next = 4; 23 | return "x"; 24 | 25 | case 4: 26 | _context.t2 = _context.sent; 27 | _context.t3 = _context.t1.concat.call(_context.t1, _context.t2); 28 | return _context.abrupt("return", _context.t0.resolve.call(_context.t0, _context.t3).then(function (s) { 29 | return babelHelpers.interopRequireWildcard(require(s)); 30 | })); 31 | 32 | case 7: 33 | case "end": 34 | return _context.stop(); 35 | } 36 | } 37 | }, _callee); 38 | })); 39 | 40 | function f() { 41 | return regeneratorRuntime.wrap(function f$(_context2) { 42 | while (1) { 43 | switch (_context2.prev = _context2.next) { 44 | case 0: 45 | _context2.t0 = Promise; 46 | _context2.t1 = ""; 47 | _context2.next = 4; 48 | return "x"; 49 | 50 | case 4: 51 | _context2.t2 = _context2.sent; 52 | _context2.t3 = _context2.t1.concat.call(_context2.t1, _context2.t2); 53 | 54 | _context2.t0.resolve.call(_context2.t0, _context2.t3).then(function (s) { 55 | return babelHelpers.interopRequireWildcard(require(s)); 56 | }); 57 | 58 | case 7: 59 | case "end": 60 | return _context2.stop(); 61 | } 62 | } 63 | }, _marked); 64 | } 65 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.7.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 2 | let i = 0; 3 | Promise.resolve(`${i++}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 4 | Promise.resolve(`${fn()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 5 | 6 | async () => Promise.resolve(`${await "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 7 | 8 | function* f() { 9 | Promise.resolve(`${yield "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/dynamic-argument/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${MODULE}`).then(s => require(s)); 2 | let i = 0; 3 | Promise.resolve(`${i++}`).then(s => require(s)); 4 | Promise.resolve(`${fn()}`).then(s => require(s)); 5 | 6 | async () => Promise.resolve(`${await "x"}`).then(s => require(s)); 7 | 8 | function* f() { 9 | Promise.resolve(`${yield "x"}`).then(s => require(s)); 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/actual.js: -------------------------------------------------------------------------------- 1 | import(/* test comment */ 'my-module'); 2 | import('my-module' /* test comment */ ); 3 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('my-module')); 3 | }); 4 | Promise.resolve().then(function () { 5 | return babelHelpers.interopRequireWildcard(require('my-module')); 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.6.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); 2 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); 3 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require('my-module')); 2 | Promise.resolve().then(() => require('my-module')); 3 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(function () { 2 | return babelHelpers.interopRequireWildcard(require('my-module')); 3 | }); 4 | Promise.resolve().then(function () { 5 | return babelHelpers.interopRequireWildcard(require('my-module')); 6 | }); 7 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.7.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); 2 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); 3 | -------------------------------------------------------------------------------- /test/fixtures/import-with-comment/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require('my-module')); 2 | Promise.resolve().then(() => require('my-module')); 3 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/actual.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return import('test-module'); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(function () { 3 | return babelHelpers.interopRequireWildcard(require('test-module')); 4 | }); 5 | } 6 | 7 | getModule().then(function () {}); 8 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.6.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(() => require('test-module')); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(function () { 3 | return babelHelpers.interopRequireWildcard(require('test-module')); 4 | }); 5 | } 6 | 7 | getModule().then(function () {}); 8 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.7.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/nested-import/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | function getModule(path) { 2 | return Promise.resolve().then(() => require('test-module')); 3 | } 4 | 5 | getModule().then(() => {}); 6 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/actual.js: -------------------------------------------------------------------------------- 1 | import({ 'answer': 42 }); 2 | import(['foo', 'bar']); 3 | import(42); 4 | import(void 0); 5 | import(undefined); 6 | import(null); 7 | import(true); 8 | import(Symbol()); 9 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve('' + String({ 'answer': 42 })).then(function (s) { 2 | return babelHelpers.interopRequireWildcard(require(s)); 3 | }); 4 | Promise.resolve('' + String(['foo', 'bar'])).then(function (s) { 5 | return babelHelpers.interopRequireWildcard(require(s)); 6 | }); 7 | Promise.resolve('' + 42).then(function (s) { 8 | return babelHelpers.interopRequireWildcard(require(s)); 9 | }); 10 | Promise.resolve('' + String(void 0)).then(function (s) { 11 | return babelHelpers.interopRequireWildcard(require(s)); 12 | }); 13 | Promise.resolve('' + String(undefined)).then(function (s) { 14 | return babelHelpers.interopRequireWildcard(require(s)); 15 | }); 16 | Promise.resolve('' + String(null)).then(function (s) { 17 | return babelHelpers.interopRequireWildcard(require(s)); 18 | }); 19 | Promise.resolve('' + String(true)).then(function (s) { 20 | return babelHelpers.interopRequireWildcard(require(s)); 21 | }); 22 | Promise.resolve('' + String(Symbol())).then(function (s) { 23 | return babelHelpers.interopRequireWildcard(require(s)); 24 | }); 25 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.6.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${{ 'answer': 42 }}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 2 | Promise.resolve(`${['foo', 'bar']}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 3 | Promise.resolve(`${42}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 4 | Promise.resolve(`${void 0}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 5 | Promise.resolve(`${undefined}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 6 | Promise.resolve(`${null}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 7 | Promise.resolve(`${true}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 8 | Promise.resolve(`${Symbol()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 9 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${{ 'answer': 42 }}`).then(s => require(s)); 2 | Promise.resolve(`${['foo', 'bar']}`).then(s => require(s)); 3 | Promise.resolve(`${42}`).then(s => require(s)); 4 | Promise.resolve(`${void 0}`).then(s => require(s)); 5 | Promise.resolve(`${undefined}`).then(s => require(s)); 6 | Promise.resolve(`${null}`).then(s => require(s)); 7 | Promise.resolve(`${true}`).then(s => require(s)); 8 | Promise.resolve(`${Symbol()}`).then(s => require(s)); 9 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | Promise.resolve("".concat({ 2 | 'answer': 42 3 | })).then(function (s) { 4 | return babelHelpers.interopRequireWildcard(require(s)); 5 | }); 6 | Promise.resolve("".concat(['foo', 'bar'])).then(function (s) { 7 | return babelHelpers.interopRequireWildcard(require(s)); 8 | }); 9 | Promise.resolve("".concat(42)).then(function (s) { 10 | return babelHelpers.interopRequireWildcard(require(s)); 11 | }); 12 | Promise.resolve("".concat(void 0)).then(function (s) { 13 | return babelHelpers.interopRequireWildcard(require(s)); 14 | }); 15 | Promise.resolve("".concat(undefined)).then(function (s) { 16 | return babelHelpers.interopRequireWildcard(require(s)); 17 | }); 18 | Promise.resolve("".concat(null)).then(function (s) { 19 | return babelHelpers.interopRequireWildcard(require(s)); 20 | }); 21 | Promise.resolve("".concat(true)).then(function (s) { 22 | return babelHelpers.interopRequireWildcard(require(s)); 23 | }); 24 | Promise.resolve("".concat(Symbol())).then(function (s) { 25 | return babelHelpers.interopRequireWildcard(require(s)); 26 | }); 27 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.7.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${{ 2 | 'answer': 42 3 | }}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 4 | Promise.resolve(`${['foo', 'bar']}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 5 | Promise.resolve(`${42}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 6 | Promise.resolve(`${void 0}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 7 | Promise.resolve(`${undefined}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 8 | Promise.resolve(`${null}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 9 | Promise.resolve(`${true}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 10 | Promise.resolve(`${Symbol()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 11 | -------------------------------------------------------------------------------- /test/fixtures/non-string-argument/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve(`${{ 2 | 'answer': 42 3 | }}`).then(s => require(s)); 4 | Promise.resolve(`${['foo', 'bar']}`).then(s => require(s)); 5 | Promise.resolve(`${42}`).then(s => require(s)); 6 | Promise.resolve(`${void 0}`).then(s => require(s)); 7 | Promise.resolve(`${undefined}`).then(s => require(s)); 8 | Promise.resolve(`${null}`).then(s => require(s)); 9 | Promise.resolve(`${true}`).then(s => require(s)); 10 | Promise.resolve(`${Symbol()}`).then(s => require(s)); 11 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/actual.js: -------------------------------------------------------------------------------- 1 | import(`1`); 2 | 3 | import(tag`2`); 4 | 5 | import(`3-${MODULE}`); 6 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.6.es2015.js: -------------------------------------------------------------------------------- 1 | var _templateObject = babelHelpers.taggedTemplateLiteral(["2"], ["2"]); 2 | 3 | Promise.resolve().then(function () { 4 | return babelHelpers.interopRequireWildcard(require("1")); 5 | }); 6 | 7 | Promise.resolve("" + String(tag(_templateObject))).then(function (s) { 8 | return babelHelpers.interopRequireWildcard(require(s)); 9 | }); 10 | 11 | Promise.resolve("3-" + String(MODULE)).then(function (s) { 12 | return babelHelpers.interopRequireWildcard(require(s)); 13 | }); 14 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.6.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`1`))); 2 | 3 | Promise.resolve(`${tag`2`}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 4 | 5 | Promise.resolve(`3-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 6 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.6.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require(`1`)); 2 | 3 | Promise.resolve(`${tag`2`}`).then(s => require(s)); 4 | 5 | Promise.resolve(`3-${MODULE}`).then(s => require(s)); 6 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.7.es2015.js: -------------------------------------------------------------------------------- 1 | function _templateObject() { 2 | var data = babelHelpers.taggedTemplateLiteral(["2"]); 3 | 4 | _templateObject = function _templateObject() { 5 | return data; 6 | }; 7 | 8 | return data; 9 | } 10 | 11 | Promise.resolve().then(function () { 12 | return babelHelpers.interopRequireWildcard(require("1")); 13 | }); 14 | Promise.resolve("".concat(tag(_templateObject()))).then(function (s) { 15 | return babelHelpers.interopRequireWildcard(require(s)); 16 | }); 17 | Promise.resolve("3-".concat(MODULE)).then(function (s) { 18 | return babelHelpers.interopRequireWildcard(require(s)); 19 | }); 20 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.7.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`1`))); 2 | Promise.resolve(`${tag`2`}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 3 | Promise.resolve(`3-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); 4 | -------------------------------------------------------------------------------- /test/fixtures/template-argument/expected.7.noInterop.js: -------------------------------------------------------------------------------- 1 | Promise.resolve().then(() => require(`1`)); 2 | Promise.resolve(`${tag`2`}`).then(s => require(s)); 3 | Promise.resolve(`3-${MODULE}`).then(s => require(s)); 4 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | import test from 'tape'; 2 | import { join } from 'path'; 3 | import { 4 | readdirSync, statSync, readFileSync, writeFileSync, 5 | } from 'fs'; 6 | 7 | import testPlugin from './testPlugin'; 8 | 9 | const FIXTURE_PATH = join(__dirname, 'fixtures'); 10 | 11 | const testFolders = readdirSync(FIXTURE_PATH).filter((file) => ( 12 | statSync(join(FIXTURE_PATH, file)).isDirectory() 13 | )); 14 | 15 | // Babel 7 only supports node 6+ 16 | const versions = Number(process.version.match(/\d+/)[0]) >= 6 ? [6, 7] : [6]; 17 | 18 | const pkgs = { 19 | 6: { 20 | env: 'env', 21 | es2015: 'es2015', 22 | templates: 'transform-es2015-template-literals', 23 | }, 24 | 7: { 25 | env: '@babel/env', 26 | templates: '@babel/transform-template-literals', 27 | }, 28 | }; 29 | 30 | function normalize(output) { 31 | return `${output.trim()}\n`; 32 | } 33 | 34 | function tryRead(folder, file) { 35 | try { 36 | return readFileSync(join(FIXTURE_PATH, folder, file), 'utf8'); 37 | } catch (e) { 38 | return ''; 39 | } 40 | } 41 | 42 | function assertOrWrite(st, result, expected, folder, file) { 43 | if (process.env.OVERWRITE) { 44 | writeFileSync(join(FIXTURE_PATH, folder, file), normalize(result)); 45 | } else { 46 | st.equal(normalize(result), normalize(expected)); 47 | } 48 | } 49 | 50 | test('babel-plugin-dynamic-import-node', (t) => { 51 | testFolders.forEach((folderName) => { 52 | const actual = tryRead(folderName, 'actual.js'); 53 | 54 | versions.forEach((version) => { 55 | const expected = tryRead(folderName, `expected.${version}.js`); 56 | const expectedES2015 = tryRead(folderName, `expected.${version}.es2015.js`); 57 | const expectedNoInterop = tryRead(folderName, `expected.${version}.noInterop.js`); 58 | 59 | t.test(`babel ${version} - works with ${folderName}`, (st) => { 60 | const result = testPlugin(version, actual); 61 | assertOrWrite(st, result, expected, folderName, `expected.${version}.js`); 62 | st.end(); 63 | }); 64 | 65 | t.test(`babel ${version} - works with ${folderName} and the 'noInterop': true option`, (st) => { 66 | const result = testPlugin(version, actual, [], [], { noInterop: true }); 67 | assertOrWrite(st, result, expectedNoInterop, folderName, `expected.${version}.noInterop.js`); 68 | st.end(); 69 | }); 70 | 71 | t.test(`babel ${version} - works with ${folderName} and the env preset`, (st) => { 72 | const result = testPlugin( 73 | version, 74 | actual, 75 | // Disable modules, otherwise it includes a different version of this plugin 76 | [[pkgs[version].env, { modules: false }]], 77 | [[pkgs[version].templates, { spec: true }]], 78 | ); 79 | assertOrWrite(st, result, expectedES2015, folderName, `expected.${version}.es2015.js`); 80 | st.end(); 81 | }); 82 | 83 | if (version === 6 && !process.env.OVERWRITE 84 | // The es2015 and env presets have two different output with async functions 85 | && folderName !== 'dynamic-argument') { 86 | t.test(`babel ${version} - works with ${folderName} and the es2015 preset`, (st) => { 87 | const result = testPlugin( 88 | version, 89 | actual, 90 | [[pkgs[version].es2015, { modules: false }]], 91 | [[pkgs[version].templates, { spec: true }]], 92 | ); 93 | st.equal(normalize(result), normalize(expectedES2015)); 94 | st.end(); 95 | }); 96 | } 97 | }); 98 | }); 99 | 100 | t.end(); 101 | }); 102 | -------------------------------------------------------------------------------- /test/testPlugin.js: -------------------------------------------------------------------------------- 1 | import * as babel6 from 'babel-core'; 2 | 3 | // This can't be imported in node 4. 4 | // eslint-disable-next-line global-require 5 | const babel7lazy = () => require('@babel/core'); 6 | 7 | export default function testPlugin(version, code, presets, plugins, options = {}) { 8 | const transform = version === 6 ? babel6.transform : babel7lazy().transformSync; 9 | const helpers = version === 6 ? 'external-helpers' : '@babel/external-helpers'; 10 | 11 | const result = transform(code, { 12 | presets: [].concat(presets || []), 13 | plugins: [].concat(plugins || [], [['./src/index.js', options], helpers]), 14 | }); 15 | 16 | return result.code; 17 | } 18 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | // Re-export lib/utils, so that consumers can import 2 | // babel-plugin-dynamic-import-node/utils instead of 3 | // babel-plugin-dynamic-import-node/lib/utils 4 | 5 | // eslint-disable-next-line import/no-unresolved 6 | module.exports = require('./lib/utils'); 7 | --------------------------------------------------------------------------------