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

feat: use new babel parser #1435

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"url": "https://github.com/netlify/zip-it-and-ship-it/issues"
},
"dependencies": {
"@babel/parser_latest": "npm:@babel/parser@7.21.8",
"@babel/parser": "7.16.8",
"@netlify/binary-info": "^1.0.0",
"@netlify/esbuild": "0.14.39",
Expand Down
3 changes: 3 additions & 0 deletions src/feature_flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export const defaultFlags = {

// Create unique entry file instead of a file that is based on the function name.
zisi_unique_entry_file: false,

// Uses the latest babel parser version
zisi_use_latest_babel_version: false,
} as const

export type FeatureFlags = Partial<Record<keyof typeof defaultFlags, boolean>>
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const bundleJsFile = async function ({
moduleNames: nodeModulesWithDynamicImports,
processImports: config.processDynamicNodeImports !== false,
srcDir,
featureFlags,
}),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Plugin } from '@netlify/esbuild'
import { findUp, findUpStop, pathExists } from 'find-up'
import normalizePath from 'normalize-path'

import type { FeatureFlags } from '../../../../feature_flags.js'
import { parseExpression } from '../../parser/index.js'
import { readPackageJson } from '../../utils/package_json.js'

Expand All @@ -22,12 +23,14 @@ export const getDynamicImportsPlugin = ({
moduleNames,
processImports,
srcDir,
featureFlags,
}: {
basePath?: string
includedPaths: Set<string>
moduleNames: Set<string>
processImports: boolean
srcDir: string
featureFlags: FeatureFlags
}): Plugin => ({
name: 'dynamic-imports',
setup(build) {
Expand All @@ -41,7 +44,8 @@ export const getDynamicImportsPlugin = ({
// Also don't parse the expression if we're not interested in processing
// the dynamic import expressions.
if (basePath && processImports) {
const { includedPathsGlob, type: expressionType } = parseExpression({ basePath, expression, resolveDir }) || {}
const { includedPathsGlob, type: expressionType } =
parseExpression({ basePath, expression, resolveDir, featureFlags }) || {}

if (includedPathsGlob) {
// The parser has found a glob of paths that should be included in the
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/node/in_source_config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ArgumentPlaceholder, Expression, SpreadElement, JSXNamespacedName } from '@babel/types'

import { FeatureFlags } from '../../../feature_flags.js'
import type { FeatureFlags } from '../../../feature_flags.js'
import { InvocationMode, INVOCATION_MODE } from '../../../function.js'
import { FunctionBundlingUserError } from '../../../utils/error.js'
import { nonNullable } from '../../../utils/non_nullable.js'
Expand Down Expand Up @@ -54,7 +54,7 @@ export const findISCDeclarationsInPath = async (
}

export const findISCDeclarations = (source: string, functionName: string, featureFlags: FeatureFlags): ISCValues => {
const ast = safelyParseSource(source)
const ast = safelyParseSource(source, featureFlags)

if (ast === null) {
return {}
Expand Down
18 changes: 14 additions & 4 deletions src/runtimes/node/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { promises as fs } from 'fs'
import { join, relative, resolve } from 'path'

import { parse } from '@babel/parser'
import { parse as parseOld } from '@babel/parser'
import { parse as parseLatest } from '@babel/parser_latest'
import type { BinaryExpression, CallExpression, Expression, PrivateName, TemplateLiteral, TSType } from '@babel/types'

import type { FeatureFlags } from '../../../feature_flags.js'
import { nonNullable } from '../../../utils/non_nullable.js'

const GLOB_WILDCARD = '**'
Expand Down Expand Up @@ -63,18 +65,24 @@ const getWildcardFromASTNode = (node: Expression | PrivateName | TSType) => {
}
}

const getParser = (featureFlags: FeatureFlags) => (featureFlags.zisi_use_latest_babel_version ? parseOld : parseLatest)

// Tries to parse an expression, returning an object with:
// - `includedPathsGlob`: A glob with the files to be included in the bundle
// - `type`: The expression type (e.g. "require", "import")
export const parseExpression = ({
basePath,
expression: rawExpression,
resolveDir,
featureFlags,
}: {
basePath: string
expression: string
resolveDir: string
featureFlags: FeatureFlags
}) => {
const parse = getParser(featureFlags)

const { program } = parse(rawExpression, {
sourceType: 'module',
})
Expand Down Expand Up @@ -105,7 +113,9 @@ export const parseExpression = ({
}

// Parses a JS/TS source and returns the resulting AST.
export const parseSource = (source: string) => {
const parseSource = (source: string, featureFlags: FeatureFlags) => {
const parse = getParser(featureFlags)

const ast = parse(source, {
plugins: ['typescript'],
sourceType: 'module',
Expand All @@ -120,9 +130,9 @@ export const parseSource = (source: string) => {

// Parses a JS/TS source and returns the resulting AST. If there is a parsing
// error, it will get swallowed and `null` will be returned.
export const safelyParseSource = (source: string) => {
export const safelyParseSource = (source: string, featureFlags: FeatureFlags) => {
try {
return parseSource(source)
return parseSource(source, featureFlags)
} catch {
return null
}
Expand Down
18 changes: 18 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ describe('zip-it-and-ship-it', () => {
expect(files[0].mainFile).toBe(join(FIXTURES_DIR, fixtureName, 'function.js'))
})

testMany(
'Zips Node.js function files with latest version of babel',
[...allBundleConfigs, 'bundler_none'],
async (options) => {
const fixtureName = 'simple'
const { files } = await zipNode(fixtureName, {
opts: {
...options,
featureFlags: { zisi_use_latest_babel_version: true },
},
})

expect(files[0].invocationMode).toBeUndefined()
expect(files[0].runtime).toBe('js')
expect(files[0].mainFile).toBe(join(FIXTURES_DIR, fixtureName, 'function.js'))
},
)

testMany(
'Zips Node.js function files from an internal functions dir with a configured fields',
[...allBundleConfigs, 'bundler_none'],
Expand Down