Unverified Commit bbc62a07 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Only Remove Server-Side Imports (#9653)

* Only Remove Server-Side Imports

* Cleanup imports
parent b4db0421
import { NodePath, PluginObj } from '@babel/core'
import * as BabelTypes from '@babel/types'
export default function(): PluginObj<any> {
return {
name: 'dangerously-remove-unused-imports',
visitor: {
Program: {
exit(path: NodePath<BabelTypes.Program>) {
Object.entries(path.scope.bindings).forEach(([k, v]) => {
if (v.referenced || v.kind !== 'module') return
if (
!(
v.path.type === 'ImportDefaultSpecifier' ||
v.path.type === 'ImportSpecifier' ||
v.path.type === 'ImportNamespaceSpecifier'
) ||
!(v.path.parent.type === 'ImportDeclaration')
) {
throw new Error(
`invariant: unknown import binding: ${v.path.type} / parent: ${v.path.parent.type}`
)
}
if (v.path.parent.specifiers.length === 1) {
v.path.parentPath.remove()
} else {
v.path.remove()
}
})
},
},
},
}
}
...@@ -110,6 +110,20 @@ export default function nextTransformSsg({ ...@@ -110,6 +110,20 @@ export default function nextTransformSsg({
} }
} }
function markImport(
path: NodePath<
| BabelTypes.ImportSpecifier
| BabelTypes.ImportDefaultSpecifier
| BabelTypes.ImportNamespaceSpecifier
>,
state: PluginState
) {
const local = path.get('local')
if (isIdentifierReferenced(local)) {
state.refs.add(local.node.name)
}
}
return { return {
visitor: { visitor: {
Program: { Program: {
...@@ -149,6 +163,26 @@ export default function nextTransformSsg({ ...@@ -149,6 +163,26 @@ export default function nextTransformSsg({
} }
} }
function sweepImport(
path: NodePath<
| BabelTypes.ImportSpecifier
| BabelTypes.ImportDefaultSpecifier
| BabelTypes.ImportNamespaceSpecifier
>
) {
const local = path.get('local')
if (refs.has(local.node.name) && !isIdentifierReferenced(local)) {
++count
path.remove()
if (
(path.parent as BabelTypes.ImportDeclaration).specifiers
.length === 0
) {
path.parentPath.remove()
}
}
}
do { do {
;(path.scope as any).crawl() ;(path.scope as any).crawl()
count = 0 count = 0
...@@ -172,6 +206,9 @@ export default function nextTransformSsg({ ...@@ -172,6 +206,9 @@ export default function nextTransformSsg({
FunctionDeclaration: sweepFunction, FunctionDeclaration: sweepFunction,
FunctionExpression: sweepFunction, FunctionExpression: sweepFunction,
ArrowFunctionExpression: sweepFunction, ArrowFunctionExpression: sweepFunction,
ImportSpecifier: sweepImport,
ImportDefaultSpecifier: sweepImport,
ImportNamespaceSpecifier: sweepImport,
}) })
} while (count) } while (count)
...@@ -193,6 +230,9 @@ export default function nextTransformSsg({ ...@@ -193,6 +230,9 @@ export default function nextTransformSsg({
FunctionDeclaration: markFunction, FunctionDeclaration: markFunction,
FunctionExpression: markFunction, FunctionExpression: markFunction,
ArrowFunctionExpression: markFunction, ArrowFunctionExpression: markFunction,
ImportSpecifier: markImport,
ImportDefaultSpecifier: markImport,
ImportNamespaceSpecifier: markImport,
ExportNamedDeclaration(path, state) { ExportNamedDeclaration(path, state) {
const specifiers = path.get('specifiers') const specifiers = path.get('specifiers')
if (specifiers.length) { if (specifiers.length) {
......
import hash from 'string-hash'
import { join, basename } from 'path'
import babelLoader from 'babel-loader' import babelLoader from 'babel-loader'
import { EXPORT_NAME_GET_STATIC_PROPS } from '../../babel/plugins/next-ssg-transform' import { basename, join } from 'path'
import hash from 'string-hash'
// increment 'e' to invalidate cache // increment 'e' to invalidate cache
// eslint-disable-next-line no-useless-concat // eslint-disable-next-line no-useless-concat
...@@ -195,15 +194,6 @@ module.exports = babelLoader.custom(babel => { ...@@ -195,15 +194,6 @@ module.exports = babelLoader.custom(babel => {
{}, {},
]) ])
} }
if (source.includes(EXPORT_NAME_GET_STATIC_PROPS)) {
options.plugins.push([
require.resolve(
'../../babel/plugins/dangerously-remove-unused-imports'
),
{},
])
}
} }
// As next-server/lib has stateful modules we have to transpile commonjs // As next-server/lib has stateful modules we have to transpile commonjs
......
...@@ -262,22 +262,35 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -262,22 +262,35 @@ describe('babel plugin (next-ssg-transform)', () => {
) )
}) })
it(`should remove re-exported function declarations' dependents (variables, functions)`, () => { it(`should remove re-exported function declarations' dependents (variables, functions, imports)`, () => {
const output = babel(trim` const output = babel(trim`
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() {}
const inceptionVar = 'hahaa'; const inceptionVar = 'hahaa';
var var1 = 1; var var1 = 1;
let var2 = 2; let var2 = 2;
const var3 = inceptionVar; const var3 = inceptionVar + remove_mua;
function inception1() {var2;} function inception1() {var2;drop_me2;}
function abc() {} function abc() {}
const b = function() {var3;}; const b = function() {var3;drop_me3;};
const b2 = function apples() {}; const b2 = function apples() {};
const bla = () => {inception1}; const bla = () => {inception1};
function unstable_getStaticProps() { function unstable_getStaticProps() {
abc(); abc();
drop_me;
b; b;
b2; b2;
bla(); bla();
...@@ -292,7 +305,7 @@ describe('babel plugin (next-ssg-transform)', () => { ...@@ -292,7 +305,7 @@ describe('babel plugin (next-ssg-transform)', () => {
`) `)
expect(output).toMatchInlineSnapshot( expect(output).toMatchInlineSnapshot(
`"const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"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(){}const __NEXT_COMP=function Test(){return __jsx(\\"div\\",null);};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
}) })
......
...@@ -254,10 +254,12 @@ describe('next-babel-loader', () => { ...@@ -254,10 +254,12 @@ describe('next-babel-loader', () => {
`import{c,d}from"e";` + `import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` + `import{e as ee,f as ff}from"f";` +
`` + `` +
`export function unstable_getStaticProps() { return { props: {} } }`, `export function unstable_getStaticProps() {foo;bar;baz;cats;baz2;ff; return { props: {} } }`,
{ resourcePath: pageFile } { resourcePath: pageFile }
) )
expect(code).toMatchInlineSnapshot(`"import\\"core-js\\";"`) expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3 from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";"`
)
}) })
it('should keep used exports in a modern-apis page (server)', async () => { it('should keep used exports in a modern-apis page (server)', async () => {
...@@ -278,7 +280,7 @@ describe('next-babel-loader', () => { ...@@ -278,7 +280,7 @@ describe('next-babel-loader', () => {
{ resourcePath: pageFile, isServer: true } { resourcePath: pageFile, isServer: true }
) )
expect(code).toMatchInlineSnapshot( expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{foo,bar}from\\"a\\";import ooo from\\"ooo\\";import baz2 from\\"c\\";import{f as ff}from\\"f\\";export function unstable_getStaticProps(){foo();baz2();ff();ooo();return{props:{}};}export default function(){return bar();}"` `"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import ooo from\\"ooo\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";export function unstable_getStaticProps(){foo();baz2();ff();ooo();return{props:{}};}export default function(){return bar();}"`
) )
}) })
...@@ -295,12 +297,12 @@ describe('next-babel-loader', () => { ...@@ -295,12 +297,12 @@ describe('next-babel-loader', () => {
`import{c,d}from"e";` + `import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` + `import{e as ee,f as ff}from"f";` +
`` + `` +
`export function unstable_getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` + `export function unstable_getStaticProps() {foo();baz2();ff();ooo();cats; return { props: {} }}` +
`export default function () { return cats + bar(); }`, `export default function () { return cats + bar(); }`,
{ resourcePath: pageFile, isServer: false } { resourcePath: pageFile, isServer: false }
) )
expect(code).toMatchInlineSnapshot( expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{bar}from\\"a\\";import{cats}from\\"d\\";const __NEXT_COMP=function(){return cats+bar();};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"import\\"core-js\\";import{bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";const __NEXT_COMP=function(){return cats+bar();};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
...@@ -322,7 +324,7 @@ describe('next-babel-loader', () => { ...@@ -322,7 +324,7 @@ describe('next-babel-loader', () => {
{ resourcePath: pageFile, isServer: false } { resourcePath: pageFile, isServer: false }
) )
expect(code).toMatchInlineSnapshot( expect(code).toMatchInlineSnapshot(
`"import React from\\"react\\";var __jsx=React.createElement;import\\"core-js\\";import{bar}from\\"a\\";import{cats}from\\"d\\";const __NEXT_COMP=function(){return __jsx(\\"div\\",null,cats+bar());};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"` `"var __jsx=React.createElement;import\\"core-js\\";import{bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";const __NEXT_COMP=function(){return __jsx(\\"div\\",null,cats+bar());};__NEXT_COMP.__NEXT_SPR=true export default __NEXT_COMP;"`
) )
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment