Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"presets": [
"airbnb"
],
"presets": ["airbnb"],
"plugins": [
"add-module-exports",
["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }],
],
}
17 changes: 0 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ os:
- linux
node_js:
- "10"
- "9"
- "8"
- "7"
- "6"
- "5"
- "4"
- "iojs-v3"
- "iojs-v2"
- "iojs-v1"
- "0.12"
before_install:
- 'nvm install-latest-npm'
install:
- 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ] || [ "${TRAVIS_NODE_VERSION}" = "0.9" ]; then nvm install --latest-npm 0.8 && npm install && nvm use "${TRAVIS_NODE_VERSION}"; else npm install; fi;'
script:
- 'if [ -n "${PRETEST-}" ]; then npm run pretest ; fi'
- 'if [ -n "${POSTTEST-}" ]; then npm run posttest ; fi'
Expand All @@ -31,12 +21,5 @@ matrix:
- node_js: "lts/*"
env: PRETEST=true
allow_failures:
- node_js: "9"
- node_js: "7"
- node_js: "5"
- node_js: "iojs-v3"
- node_js: "iojs-v2"
- node_js: "iojs-v1"
- os: osx
- env: TEST=true ALLOW_FAILURE=true
- env: COVERAGE=true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Babel plugin to transpile `import()` to a deferred `require()`, for node. Matches the [proposed spec](https://github.com/domenic/proposal-import-function).

**NOTE:** Babylon >= v6.12.0 is required to correctly parse dynamic imports.
**NOTE:** Babylon >= v7.0.0 is required to correctly parse dynamic imports.

## Installation

Expand Down Expand Up @@ -31,7 +31,7 @@ $ babel --plugins dynamic-import-node script.js
### Via Node API

```javascript
require('babel-core').transform('code', {
require('@babel/core').transform('code', {
plugins: ['dynamic-import-node']
});
```
Expand Down
25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "babel src --out-dir lib",
"pretest": "npm run lint",
"test": "npm run tests-only",
"tests-only": "tape --require airbnb-js-shims --require babel-register test",
"tests-only": "tape --require airbnb-js-shims --require @babel/register test",
"lint": "eslint .",
"prepublish": "not-in-publish || (safe-publish-latest && npm run build)",
"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)",
Expand Down Expand Up @@ -42,16 +42,15 @@
},
"homepage": "https://github.com/airbnb/babel-plugin-dynamic-import-node#readme",
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-transform-template-literals": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"airbnb-js-shims": "^2.1.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^9.0.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-es2015-template-literals": "^6.22.0",
"babel-plugin-transform-replace-object-assign": "^1.0.0",
"babel-preset-airbnb": "^2.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.26.0",
"babel-eslint": "^10.0.1",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-preset-airbnb": "^3.0.1",
"eslint": "^5.6.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
Expand All @@ -60,7 +59,11 @@
"safe-publish-latest": "^1.1.2",
"tape": "^4.9.1"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
},
"dependencies": {
"object.assign": "^4.1.0"
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0"
}
}
17 changes: 8 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export default function ({ template, types: t }) {
import { declare } from '@babel/helper-plugin-utils';
import syntax from '@babel/plugin-syntax-dynamic-import';

export default declare(({ assertVersion, template, types: t }, { noInterop = false }) => {
assertVersion(7);

const buildImport = template('Promise.resolve().then(() => MODULE)');

return {
// NOTE: Once we drop support for Babel <= v6 we should
// update this to import from @babel/plugin-syntax-dynamic-import.
// https://www.npmjs.com/package/@babel/plugin-syntax-dynamic-import
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push('dynamicImport');
},
inherits: syntax,

visitor: {
Import(path) {
Expand All @@ -28,7 +28,6 @@ export default function ({ template, types: t }) {
[].concat(SOURCE),
);

const { noInterop = false } = this.opts;
const MODULE = noInterop === true ? requireCall : t.callExpression(this.addHelper('interopRequireWildcard'), [requireCall]);
const newImport = buildImport({
MODULE,
Expand All @@ -37,4 +36,4 @@ export default function ({ template, types: t }) {
},
},
};
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function _interopRequireWildcard(obj) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function _interopRequireWildcard(obj) {}

Expand All @@ -9,7 +9,6 @@ Promise.resolve().then(function () {
return _interopRequireWildcard(require('test-module-2'));
});
});

Promise.all([Promise.resolve().then(function () {
return _interopRequireWildcard(require('test-1'));
}), Promise.resolve().then(function () {
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/chained-import/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function _interopRequireWildcard(obj) {}

Promise.resolve().then(() => _interopRequireWildcard(require('test-module'))).then(() => Promise.resolve().then(() => _interopRequireWildcard(require('test-module-2'))));

Promise.all([Promise.resolve().then(() => _interopRequireWildcard(require('test-1'))), Promise.resolve().then(() => _interopRequireWildcard(require('test-2'))), Promise.resolve().then(() => _interopRequireWildcard(require('test-3')))]).then(() => {});
1 change: 0 additions & 1 deletion test/fixtures/chained-import/expected.noInterop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Promise.resolve().then(() => require('test-module')).then(() => Promise.resolve().then(() => require('test-module-2')));

Promise.all([Promise.resolve().then(() => require('test-1')), Promise.resolve().then(() => require('test-2')), Promise.resolve().then(() => require('test-3'))]).then(() => {});
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
"use strict";

function _interopRequireWildcard(obj) {}

var MODULE = Object('test-module');

Promise.resolve().then(function () {
return _interopRequireWildcard(require('' + String(MODULE)));
return _interopRequireWildcard(require("".concat(MODULE)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require('test-' + String(MODULE)));
return _interopRequireWildcard(require("test-".concat(MODULE)));
});
1 change: 0 additions & 1 deletion test/fixtures/dynamic-argument/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function _interopRequireWildcard(obj) {}

const MODULE = Object('test-module');

Promise.resolve().then(() => _interopRequireWildcard(require(`${MODULE}`)));
Promise.resolve().then(() => _interopRequireWildcard(require(`test-${MODULE}`)));
1 change: 0 additions & 1 deletion test/fixtures/dynamic-argument/expected.noInterop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const MODULE = Object('test-module');

Promise.resolve().then(() => require(`${MODULE}`));
Promise.resolve().then(() => require(`test-${MODULE}`));
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function _interopRequireWildcard(obj) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

function _interopRequireWildcard(obj) {}

Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/non-string-argument/expected.env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

function _interopRequireWildcard(obj) {}

Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat({
'answer': 42
})));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(['foo', 'bar'])));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(42)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(void 0)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(undefined)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(null)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(true)));
});
Promise.resolve().then(function () {
return _interopRequireWildcard(require("".concat(Symbol())));
});
28 changes: 0 additions & 28 deletions test/fixtures/non-string-argument/expected.es2015.js

This file was deleted.

4 changes: 3 additions & 1 deletion test/fixtures/non-string-argument/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function _interopRequireWildcard(obj) {}

Promise.resolve().then(() => _interopRequireWildcard(require(`${{ 'answer': 42 }}`)));
Promise.resolve().then(() => _interopRequireWildcard(require(`${{
'answer': 42
}}`)));
Promise.resolve().then(() => _interopRequireWildcard(require(`${['foo', 'bar']}`)));
Promise.resolve().then(() => _interopRequireWildcard(require(`${42}`)));
Promise.resolve().then(() => _interopRequireWildcard(require(`${void 0}`)));
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/non-string-argument/expected.noInterop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Promise.resolve().then(() => require(`${{ 'answer': 42 }}`));
Promise.resolve().then(() => require(`${{
'answer': 42
}}`));
Promise.resolve().then(() => require(`${['foo', 'bar']}`));
Promise.resolve().then(() => require(`${42}`));
Promise.resolve().then(() => require(`${void 0}`));
Expand Down
18 changes: 4 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'tape';
import { join } from 'path';
import { readdirSync, statSync, readFileSync } from 'fs';
import templates from 'babel-plugin-transform-es2015-template-literals';
import templates from '@babel/plugin-transform-template-literals';

import testPlugin from './testPlugin';

Expand All @@ -19,7 +19,7 @@ test('babel-plugin-dynamic-import-node', (t) => {
testFolders.forEach((folderName) => {
const actual = readFileSync(join(FIXTURE_PATH, folderName, 'actual.js'), 'utf8');
const expected = readFileSync(join(FIXTURE_PATH, folderName, 'expected.js'), 'utf8');
const expectedES2015 = readFileSync(join(FIXTURE_PATH, folderName, 'expected.es2015.js'), 'utf8');
const expectedPresetEnv = readFileSync(join(FIXTURE_PATH, folderName, 'expected.env.js'), 'utf8');
const expectedNoInterop = readFileSync(join(FIXTURE_PATH, folderName, 'expected.noInterop.js'), 'utf8');

t.test(`works with ${folderName}`, (st) => {
Expand All @@ -34,23 +34,13 @@ test('babel-plugin-dynamic-import-node', (t) => {
st.end();
});

t.test(`works with ${folderName} and the es2015 preset`, (st) => {
const result = testPlugin(
actual,
['es2015'],
[[templates, { spec: true }]],
);
st.equal(normalize(result), normalize(expectedES2015));
st.end();
});

t.test(`works with ${folderName} and the env preset`, (st) => {
const result = testPlugin(
actual,
['env'],
['@babel/preset-env'],
[[templates, { spec: true }]],
);
st.equal(normalize(result), normalize(expectedES2015));
st.equal(normalize(result), normalize(expectedPresetEnv));
st.end();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/testPlugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from 'babel-core';
import { transform } from '@babel/core';

export default function testPlugin(code, presets, plugins, options = {}) {
const result = transform(code, {
Expand Down