Skip to content

Commit b56a9a4

Browse files
authored
Merge pull request #66 from tyler-johnson/babel-7
upgrade for babel 7
2 parents d76d035 + 3d934a2 commit b56a9a4

5 files changed

Lines changed: 785 additions & 112 deletions

File tree

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"postversion": "git push --follow-tags && conventional-github-releaser -p angular -r 0"
1515
},
1616
"devDependencies": {
17+
"@babel/core": "^7.0.0",
18+
"@babel/plugin-proposal-export-default-from": "^7.0.0",
19+
"@babel/preset-env": "^7.0.0",
1720
"babel-cli": "^6.26.0",
1821
"babel-core": "^6.26.3",
1922
"babel-eslint": "^8.2.6",

src/index.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,44 @@
55

66
module.exports = ({ template }) => {
77
let pluginOptions
8+
9+
function addModuleExportsDefaults(path) {
10+
const finder = new ExportsFinder(path)
11+
if (!finder.isOnlyExportsDefault()) {
12+
return
13+
}
14+
if (finder.isAmd()) {
15+
return
16+
}
17+
const rootPath = finder.getRootPath()
18+
19+
// HACK: `path.node.body.push` instead of path.pushContainer(due doesn't work in Plugin.post)
20+
rootPath.node.body.push(template('module.exports = exports.default')())
21+
if (pluginOptions.addDefaultProperty) {
22+
rootPath.node.body.push(template('module.exports.default = exports.default')())
23+
}
24+
}
25+
826
const ExportsDefaultVisitor = {
9-
AssignmentExpression(path) {
10-
if (path.get('left').matchesPattern('exports.default')) {
11-
const finder = new ExportsFinder(path)
12-
if (!finder.isOnlyExportsDefault()) {
13-
return
14-
}
15-
if (finder.isAmd()) {
16-
return
17-
}
18-
const rootPath = finder.getRootPath()
27+
CallExpression(path) {
28+
if (!path.get('callee').matchesPattern('Object.defineProperty')) {
29+
return
30+
}
1931

20-
// HACK: `path.node.body.push` instead of path.pushContainer(due doesn't work in Plugin.post)
21-
rootPath.node.body.push(template('module.exports = exports.default')())
22-
if (pluginOptions.addDefaultProperty) {
23-
rootPath.node.body.push(template('module.exports.default = exports.default')())
24-
}
32+
const [identifier, prop] = path.get('arguments')
33+
const objectName = identifier.get('name').node
34+
const propertyName = prop.get('value').node
35+
36+
if ((objectName === 'exports' || objectName === '_exports') && propertyName === 'default') {
37+
addModuleExportsDefaults(path)
38+
}
39+
},
40+
AssignmentExpression(path) {
41+
if (
42+
path.get('left').matchesPattern('exports.default') ||
43+
path.get('left').matchesPattern('_exports.default')
44+
) {
45+
addModuleExportsDefaults(path)
2546
}
2647
}
2748
}
@@ -48,7 +69,9 @@ class ExportsFinder {
4869
}
4970

5071
getRootPath() {
51-
return this.path.parentPath.parentPath
72+
return this.path.findParent(path => {
73+
return path.key === 'body' || !path.parentPath
74+
})
5275
}
5376

5477
isOnlyExportsDefault() {
@@ -81,7 +104,7 @@ class ExportsFinder {
81104

82105
const objectName = path.get(`${property}.left.object.name`).node
83106
const propertyName = path.get(`${property}.left.property.name`).node
84-
if (objectName === 'exports') {
107+
if (objectName === 'exports' || objectName === '_exports') {
85108
if (propertyName === 'default') {
86109
this.hasExportsDefault = true
87110
} else if (propertyName !== '__esModule') {
@@ -104,8 +127,16 @@ class ExportsFinder {
104127
const [identifier, prop] = path.get('arguments')
105128
const objectName = identifier.get('name').node
106129
const propertyName = prop.get('value').node
107-
if (objectName === 'exports' && propertyName !== '__esModule') {
108-
self.hasExportsNamed = true
130+
131+
if (
132+
(objectName === 'exports' || objectName === '_exports') &&
133+
propertyName !== '__esModule'
134+
) {
135+
if (propertyName === 'default') {
136+
self.hasExportsDefault = true
137+
} else {
138+
self.hasExportsNamed = true
139+
}
109140
}
110141
}
111142
})

test/helpers.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import vm from 'vm'
22
import util from 'util'
3-
import { transform as babelTransform } from 'babel-core'
43
import assert from 'assert'
54

65
export function createSandbox() {
@@ -34,7 +33,7 @@ export function createSandboxAmd() {
3433
return sandbox
3534
}
3635

37-
export function testPlugin(code, options, fn, useAmdSandbox = false) {
36+
export function testPlugin(babelTransform, code, options, fn, useAmdSandbox = false) {
3837
const result = babelTransform(code, options)
3938
const sandbox = useAmdSandbox ? createSandboxAmd() : createSandbox()
4039

@@ -64,8 +63,8 @@ function equalObject(actual, expected, previouslyChecked) {
6463
previouslyChecked.push(expected)
6564

6665
// Check if both have the same properties
67-
const actualKeys = Object.keys(actual)
68-
const expectedKeys = Object.keys(expected)
66+
const actualKeys = Object.keys(actual).sort()
67+
const expectedKeys = Object.keys(expected).sort()
6968
if (Array.isArray(expected)) {
7069
assert(actual.length === expected.length)
7170
} else {

test/index.js

Lines changed: 108 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,125 @@
11
import assert from 'assert'
22
import { transform as babelTransform } from 'babel-core'
3+
import { transform as babelTransform7 } from '@babel/core'
34
import { testPlugin, equal } from './helpers'
45
import testCases from './spec'
56

6-
describe('babel-plugin-add-module-exports', () => {
7-
it('should not export default to `module.exports` by default.', () =>
8-
testPlugin(
9-
testCases[0].code,
10-
{
11-
presets: ['env']
12-
},
13-
module => {
14-
assert(module !== 'default-entry')
15-
assert(module.default === 'default-entry')
16-
}
17-
))
7+
const babelVersions = {
8+
'babel@6': babelTransform,
9+
'babel@7': babelTransform7
10+
}
1811

19-
it('should not handle an pure esmodule', () => {
20-
const code = `export default 'default-entry';`
21-
const result = babelTransform(code, {
22-
presets: [['env', { modules: false }]],
23-
plugins: ['./src/index.js']
24-
})
12+
Object.keys(babelVersions).forEach(ver => {
13+
const transform = babelVersions[ver]
14+
const env = ver === 'babel@6' ? 'env' : '@babel/preset-env'
2515

26-
// use code comparison instead of vm.runInNewContext(doesn't work `export` syntax)
27-
assert(code === result.code)
28-
})
16+
describe('babel-plugin-add-module-exports ' + ver, () => {
17+
it('should not export default to `module.exports` by default.', () =>
18+
testPlugin(
19+
transform,
20+
testCases[0].code,
21+
{
22+
presets: [env]
23+
},
24+
module => {
25+
assert(module !== 'default-entry')
26+
assert(module.default === 'default-entry')
27+
}
28+
))
2929

30-
it('should not handle an amd module', () =>
31-
testPlugin(
32-
`export default 'default-entry';`,
33-
{
34-
presets: [['env', { modules: 'amd' }]],
30+
it('should not handle an pure esmodule', () => {
31+
const code = `export default 'default-entry';`
32+
const result = transform(code, {
33+
presets: [[env, { modules: false }]],
3534
plugins: ['./src/index.js']
36-
},
37-
module => {
38-
assert(module.default === 'default-entry')
39-
},
40-
true
41-
))
42-
43-
it('plugin should export to module.exports(#31)', () => {
44-
const plugin = require('../src')
45-
assert(typeof plugin === 'function')
46-
})
47-
48-
it('should handle duplicated plugin references (#1)', () =>
49-
testPlugin(
50-
testCases[0].code,
51-
{
52-
presets: ['env'],
53-
plugins: ['./src/index.js', './src/index.js', './src/index.js']
54-
},
55-
(module, code) => {
56-
assert(module === 'default-entry')
57-
58-
// @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722
59-
assert(module.default === undefined)
60-
61-
assert(
62-
code ===
63-
`"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;`
64-
)
65-
}
66-
))
35+
})
6736

68-
it('should export with `babel-plugin-rewire` (#19)', () =>
69-
testPlugin(
70-
"export default { stuff: 'things' }",
71-
{
72-
presets: ['react', 'env'],
73-
plugins: ['./src/index.js', 'rewire']
74-
},
75-
module => {
76-
assert(module.stuff === 'things')
77-
}
78-
))
37+
// use code comparison instead of vm.runInNewContext(doesn't work `export` syntax)
38+
assert(code === result.code)
39+
})
7940

80-
testCases.forEach(testCase =>
81-
it(`should ${testCase.name}`, () =>
41+
it('should not handle an amd module', () =>
8242
testPlugin(
83-
testCase.code,
43+
transform,
44+
`export default 'default-entry';`,
8445
{
85-
presets: [['env', testCase.env]],
86-
plugins: [
87-
'transform-export-extensions', // use export-from syntax
88-
['./src/index.js', testCase.options]
89-
]
46+
presets: [[env, { modules: 'amd' }]],
47+
plugins: ['./src/index.js']
9048
},
9149
module => {
92-
// assert module root (module.exports) object
93-
equal(module, testCase.expected.module)
94-
95-
// assert each common entry is exported without error
96-
Object.keys(testCase.expected.exports).forEach(key =>
97-
equal(module[key], testCase.expected.exports[key])
98-
)
99-
}
50+
assert(module.default === 'default-entry')
51+
},
52+
true
10053
))
101-
)
54+
55+
it('plugin should export to module.exports(#31)', () => {
56+
const plugin = require('../src')
57+
assert(typeof plugin === 'function')
58+
})
59+
60+
if (ver === 'babel@6') {
61+
// babel 7 throws an error with duplicate plugins
62+
it('should handle duplicated plugin references (#1)', () =>
63+
testPlugin(
64+
transform,
65+
testCases[0].code,
66+
{
67+
presets: [env],
68+
plugins: ['./src/index.js', './src/index.js', './src/index.js']
69+
},
70+
(module, code) => {
71+
assert(module === 'default-entry')
72+
73+
// @see https://github.com/59naga/babel-plugin-add-module-exports/issues/12#issuecomment-157023722
74+
assert(module.default === undefined)
75+
76+
assert(
77+
code ===
78+
`"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.default = "default-entry";\nmodule.exports = exports.default;`
79+
)
80+
}
81+
))
82+
83+
// rewire hasn't been updated for babel 7
84+
// https://github.com/speedskater/babel-plugin-rewire/issues/209
85+
it('should export with `babel-plugin-rewire` (#19)', () =>
86+
testPlugin(
87+
transform,
88+
"export default { stuff: 'things' }",
89+
{
90+
presets: ['react', env],
91+
plugins: ['./src/index.js', 'rewire']
92+
},
93+
module => {
94+
assert(module.stuff === 'things')
95+
}
96+
))
97+
}
98+
99+
testCases.forEach(testCase =>
100+
it(`should ${testCase.name}`, () =>
101+
testPlugin(
102+
transform,
103+
testCase.code,
104+
{
105+
presets: [[env, testCase.env]],
106+
plugins: [
107+
ver === 'babel@6' // use export-from syntax
108+
? 'transform-export-extensions'
109+
: '@babel/plugin-proposal-export-default-from',
110+
['./src/index.js', testCase.options]
111+
]
112+
},
113+
module => {
114+
// assert module root (module.exports) object
115+
equal(module, testCase.expected.module)
116+
117+
// assert each common entry is exported without error
118+
Object.keys(testCase.expected.exports).forEach(key =>
119+
equal(module[key], testCase.expected.exports[key])
120+
)
121+
}
122+
))
123+
)
124+
})
102125
})

0 commit comments

Comments
 (0)