Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from airbnb/shar--refactor-index
Browse files Browse the repository at this point in the history
Remove dependencies on babel-template and babel-plugin-syntax-dynamic-import
  • Loading branch information
sharmilajesupaul authored Oct 8, 2018
2 parents b948bd6 + e4decee commit 8b21309
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,5 @@
"rimraf": "^2.6.2",
"safe-publish-latest": "^1.1.1",
"tape": "^4.9.0"
},
"dependencies": {
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-template": "^6.26.0"
}
}
33 changes: 18 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import template from 'babel-template';
import syntax from 'babel-plugin-syntax-dynamic-import';

const buildImport = template(`
export default ({ template }) => {
const buildImport = template(`
(new Promise((resolve) => {
require.ensure([], (require) => {
resolve(require(SOURCE));
});
}))
`);

export default () => ({
inherits: syntax,

visitor: {
Import(path) {
const newImport = buildImport({
SOURCE: path.parentPath.node.arguments,
});
path.parentPath.replaceWith(newImport);
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');
},
visitor: {
Import(path) {
const newImport = buildImport({
SOURCE: path.parentPath.node.arguments,
});
path.parentPath.replaceWith(newImport);
},
},
},
});
};
};

0 comments on commit 8b21309

Please sign in to comment.