-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for new ES dynamic import()
#143
Changes from 1 commit
cc078e7
1197c2c
1d56752
7e3cd36
8bedab3
ecbff83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-env": "^1.2.2", | ||
"babel-preset-stage-2": "^6.22.0", | ||
"common-tags": "^1.4.0", | ||
"eslint": "^3.18.0", | ||
"eslint-config-airbnb-base": "^11.1.2", | ||
|
@@ -55,7 +56,7 @@ | |
"pretest": "npm run lint", | ||
"test": "jest --coverage", | ||
"test:suite": "jest", | ||
"test:watch": "jest --watch", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still want to keep |
||
"test:watch": "jest --watch test/dynamicImport.test.js", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert? ;) |
||
"prepublish": "npm run compile", | ||
"release": "standard-version" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import { matchesPattern, mapPathString } from '../utils'; | ||
import { | ||
matchesPattern, | ||
mapPathString, | ||
isImportCall, | ||
} from '../utils'; | ||
|
||
|
||
const patterns = [ | ||
|
@@ -14,8 +18,13 @@ const patterns = [ | |
|
||
export default function transformCall(nodePath, state) { | ||
const calleePath = nodePath.get('callee'); | ||
const isNormalCall = patterns.some(pattern => matchesPattern(state.types, calleePath, pattern)); | ||
|
||
if (patterns.some(pattern => matchesPattern(state.types, calleePath, pattern))) { | ||
if (isNormalCall) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should group these 2 conditions |
||
mapPathString(nodePath.get('arguments.0'), state); | ||
} | ||
|
||
if (isImportCall(state.types, nodePath)) { | ||
mapPathString(nodePath.get('arguments.0'), state); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-env jest */ | ||
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies | ||
import plugin from '../src'; | ||
|
||
// According to https://github.com/tc39/proposal-dynamic-import | ||
|
||
describe('import()', () => { | ||
const transformerOpts = { | ||
babelrc: false, | ||
// We need to add the stage-2 preset in order to parse the `import()`-calls | ||
presets: ['stage-2'], | ||
plugins: [ | ||
[plugin, { | ||
root: [ | ||
'./test/testproject/src', | ||
], | ||
alias: { | ||
test: './test/testproject/test', | ||
}, | ||
}], | ||
], | ||
}; | ||
|
||
it('CFG should resolve the path based on the root config', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const code = 'import("app").then(() => {}).catch(() => {});'; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe('import("./test/testproject/src/app").then(() => {}).catch(() => {});'); | ||
}); | ||
|
||
it('should alias the path', () => { | ||
const code = 'import("test/tools").then(() => {}).catch(() => {});'; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe('import("./test/testproject/test/tools").then(() => {}).catch(() => {});'); | ||
}); | ||
|
||
it('should not change the path', () => { | ||
const code = 'import("./something").then(() => {}).catch(() => {});'; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe('import("./something").then(() => {}).catch(() => {});'); | ||
}); | ||
|
||
it('should handle the first argument not being a string literal', () => { | ||
const code = 'import(path).then(() => {}).catch(() => {});'; | ||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe('import(path).then(() => {}).catch(() => {});'); | ||
}); | ||
|
||
it('should handle an empty path', () => { | ||
const code = 'import(\'\').then(() => {}).catch(() => {});'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is what @fatfisz mentioned. Use |
||
const result = transform(code, transformerOpts); | ||
|
||
expect(result.code).toBe('import(\'\').then(() => {}).catch(() => {});'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you only use the required plugin instead?