2019-11-25 17:44:24 +01:00
|
|
|
/* eslint-env jest */
|
|
|
|
import { transform } from '@babel/core'
|
|
|
|
|
|
|
|
const trim = s =>
|
|
|
|
s
|
|
|
|
.join('\n')
|
|
|
|
.trim()
|
|
|
|
.replace(/^\s+/gm, '')
|
|
|
|
|
|
|
|
// avoid generating __source annotations in JSX during testing:
|
|
|
|
const NODE_ENV = process.env.NODE_ENV
|
|
|
|
process.env.NODE_ENV = 'production'
|
2019-12-05 22:43:11 +01:00
|
|
|
const plugin = require('next/dist/build/babel/plugins/next-ssg-transform')
|
2019-11-25 17:44:24 +01:00
|
|
|
process.env.NODE_ENV = NODE_ENV
|
|
|
|
|
|
|
|
const babel = (code, esm = true, pluginOptions = {}) =>
|
|
|
|
transform(code, {
|
|
|
|
filename: 'noop.js',
|
|
|
|
presets: [['@babel/preset-react', { development: false, pragma: '__jsx' }]],
|
|
|
|
plugins: [[plugin, pluginOptions]],
|
|
|
|
babelrc: false,
|
|
|
|
configFile: false,
|
|
|
|
sourceType: 'module',
|
|
|
|
compact: true,
|
|
|
|
caller: {
|
|
|
|
name: 'tests',
|
|
|
|
supportsStaticESM: esm,
|
|
|
|
},
|
|
|
|
}).code
|
|
|
|
|
2019-12-05 22:43:11 +01:00
|
|
|
describe('babel plugin (next-ssg-transform)', () => {
|
2019-11-25 17:44:24 +01:00
|
|
|
describe('getStaticProps support', () => {
|
|
|
|
it('should remove separate named export specifiers', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths } from '.'
|
|
|
|
export { a as getStaticProps } from '.'
|
2019-11-25 17:44:24 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-11-25 17:44:24 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove combined named export specifiers', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths, a as getStaticProps } from '.'
|
2019-11-25 17:44:24 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-11-25 17:44:24 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should retain extra named export specifiers', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths, a as getStaticProps, foo, bar as baz } from '.'
|
2019-11-25 17:44:24 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export{foo,bar as baz}from'.';export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-11-25 17:44:24 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-12-05 23:22:41 +01:00
|
|
|
it('should remove named export function declarations', () => {
|
2019-11-25 17:44:24 +01:00
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticPaths() {
|
2019-11-25 17:44:24 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2019-11-25 17:44:24 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-11-25 17:44:24 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-12-05 23:22:41 +01:00
|
|
|
it('should remove named export function declarations (async)', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export async function getStaticPaths() {
|
2019-12-05 23:22:41 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export async function getStaticProps() {
|
2019-12-05 23:22:41 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-05 23:22:41 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not remove extra named export function declarations', () => {
|
2019-11-25 17:44:24 +01:00
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2019-11-25 17:44:24 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Noop() {}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export function Noop(){}export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-11-25 17:44:24 +01:00
|
|
|
)
|
|
|
|
})
|
2019-12-05 23:43:18 +01:00
|
|
|
|
|
|
|
it('should remove named export variable declarations', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticPaths = () => {
|
2019-12-05 23:43:18 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticProps = function() {
|
2019-12-05 23:43:18 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-05 23:43:18 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove named export variable declarations (async)', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticPaths = async () => {
|
2019-12-05 23:43:18 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticProps = async function() {
|
2019-12-05 23:43:18 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-05 23:43:18 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not remove extra named export variable declarations', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticPaths = () => {
|
2019-12-05 23:43:18 +01:00
|
|
|
return []
|
|
|
|
}, foo = 2
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticProps = function() {
|
2019-12-05 23:43:18 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export const foo=2;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-05 23:43:18 +01:00
|
|
|
)
|
|
|
|
})
|
2019-12-06 00:05:50 +01:00
|
|
|
|
|
|
|
it('should remove re-exported variable declarations', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
const getStaticPaths = () => {
|
2019-12-06 00:05:50 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths }
|
2019-12-06 00:05:50 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-06 00:05:50 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-12-06 00:06:35 +01:00
|
|
|
it('should remove re-exported variable declarations (safe)', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
const getStaticPaths = () => {
|
2019-12-06 00:06:35 +01:00
|
|
|
return []
|
|
|
|
}, a = 2
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths }
|
2019-12-06 00:06:35 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"const a=2;export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-06 00:06:35 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-12-06 00:05:50 +01:00
|
|
|
it('should remove re-exported function declarations', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
function getStaticPaths() {
|
2019-12-06 00:05:50 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths }
|
2019-12-06 00:05:50 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-06 00:05:50 +01:00
|
|
|
)
|
|
|
|
})
|
2019-12-06 17:50:50 +01:00
|
|
|
|
|
|
|
it('should not crash for class declarations', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
function getStaticPaths() {
|
2019-12-06 17:50:50 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticPaths }
|
2019-12-06 17:50:50 +01:00
|
|
|
|
|
|
|
export class MyClass {}
|
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export class MyClass{}export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-06 17:50:50 +01:00
|
|
|
)
|
|
|
|
})
|
2019-12-07 04:03:55 +01:00
|
|
|
|
2019-12-07 04:57:14 +01:00
|
|
|
it(`should remove re-exported function declarations' dependents (variables, functions, imports)`, () => {
|
2019-12-07 04:03:55 +01:00
|
|
|
const output = babel(trim`
|
2019-12-07 04:57:14 +01:00
|
|
|
import keep_me from 'hello'
|
|
|
|
import {keep_me2} from 'hello2'
|
|
|
|
import * as keep_me3 from 'hello3'
|
|
|
|
|
|
|
|
import drop_me from 'bla'
|
|
|
|
import { drop_me2 } from 'foo'
|
|
|
|
import { drop_me3, but_not_me } from 'bar'
|
|
|
|
import * as remove_mua from 'hehe'
|
|
|
|
|
|
|
|
var leave_me_alone = 1;
|
|
|
|
function dont_bug_me_either() {}
|
|
|
|
|
2019-12-07 04:03:55 +01:00
|
|
|
const inceptionVar = 'hahaa';
|
|
|
|
var var1 = 1;
|
|
|
|
let var2 = 2;
|
2019-12-07 04:57:14 +01:00
|
|
|
const var3 = inceptionVar + remove_mua;
|
2019-12-07 04:03:55 +01:00
|
|
|
|
2019-12-07 04:57:14 +01:00
|
|
|
function inception1() {var2;drop_me2;}
|
2019-12-07 04:03:55 +01:00
|
|
|
|
|
|
|
function abc() {}
|
2019-12-07 04:57:14 +01:00
|
|
|
const b = function() {var3;drop_me3;};
|
2019-12-07 04:03:55 +01:00
|
|
|
const b2 = function apples() {};
|
|
|
|
const bla = () => {inception1};
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
function getStaticProps() {
|
2019-12-07 04:03:55 +01:00
|
|
|
abc();
|
2019-12-07 04:57:14 +01:00
|
|
|
drop_me;
|
2019-12-07 04:03:55 +01:00
|
|
|
b;
|
|
|
|
b2;
|
|
|
|
bla();
|
|
|
|
return { props: {var1} }
|
|
|
|
}
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticProps }
|
2019-12-07 04:03:55 +01:00
|
|
|
|
|
|
|
export default function Test() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"import keep_me from'hello';import{keep_me2}from'hello2';import*as keep_me3 from'hello3';import{but_not_me}from'bar';var leave_me_alone=1;function dont_bug_me_either(){}export var __N_SSG=true;export default function Test(){return __jsx(\\"div\\",null);}"`
|
2019-12-07 04:03:55 +01:00
|
|
|
)
|
|
|
|
})
|
2019-12-07 06:52:24 +01:00
|
|
|
|
|
|
|
it('should not mix up bindings', () => {
|
|
|
|
const output = babel(trim`
|
|
|
|
function Function1() {
|
|
|
|
return {
|
|
|
|
a: function bug(a) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function Function2() {
|
|
|
|
var bug = 1;
|
|
|
|
return { bug };
|
|
|
|
}
|
2019-12-07 07:03:29 +01:00
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
export { getStaticProps } from 'a'
|
2019-12-07 06:52:24 +01:00
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
|
|
|
`"function Function1(){return{a:function bug(a){return 2;}};}function Function2(){var bug=1;return{bug};}"`
|
|
|
|
)
|
|
|
|
})
|
2020-02-13 06:25:44 +01:00
|
|
|
|
|
|
|
it('should support class exports', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2020-02-13 06:25:44 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Test extends React.Component {
|
|
|
|
render() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"export var __N_SSG=true;export default class Test extends React.Component{render(){return __jsx(\\"div\\",null);}}"`
|
2020-02-13 06:25:44 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support class exports 2', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2020-02-13 06:25:44 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
class Test extends React.Component {
|
|
|
|
render() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Test;
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"class Test extends React.Component{render(){return __jsx(\\"div\\",null);}}export var __N_SSG=true;export default Test;"`
|
2020-02-13 06:25:44 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support export { _ as default }', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2020-02-13 06:25:44 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
function El() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
|
|
|
|
export { El as default }
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"function El(){return __jsx(\\"div\\",null);}export var __N_SSG=true;export{El as default};"`
|
2020-02-13 06:25:44 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support export { _ as default } with other specifiers', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2020-02-13 06:25:44 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
function El() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
|
|
|
|
const a = 5
|
|
|
|
|
|
|
|
export { El as default, a }
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"function El(){return __jsx(\\"div\\",null);}const a=5;export var __N_SSG=true;export{El as default,a};"`
|
2020-02-13 06:25:44 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support export { _ as default } with a class', () => {
|
|
|
|
const output = babel(trim`
|
2020-02-27 18:57:39 +01:00
|
|
|
export function getStaticProps() {
|
2020-02-13 06:25:44 +01:00
|
|
|
return { props: {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
class El extends React.Component {
|
|
|
|
render() {
|
|
|
|
return <div />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const a = 5
|
|
|
|
|
|
|
|
export { El as default, a }
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
2020-03-06 05:15:10 +01:00
|
|
|
`"class El extends React.Component{render(){return __jsx(\\"div\\",null);}}const a=5;export var __N_SSG=true;export{El as default,a};"`
|
2020-02-13 06:25:44 +01:00
|
|
|
)
|
|
|
|
})
|
2020-03-06 07:35:31 +01:00
|
|
|
|
|
|
|
it('should support full re-export', () => {
|
|
|
|
const output = babel(trim`
|
|
|
|
export { getStaticProps, default } from 'a'
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(output).toMatchInlineSnapshot(
|
|
|
|
`"export var __N_SSG=true;export{default}from'a';"`
|
|
|
|
)
|
|
|
|
})
|
2019-11-25 17:44:24 +01:00
|
|
|
})
|
|
|
|
})
|