-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
160 lines (134 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
import * as path from 'node:path'
import { rollup } from 'rollup'
import virtual from '@rollup/plugin-virtual'
import MemoryFS from 'memory-fs'
import webpack from 'webpack'
import * as esbuild from 'esbuild'
import * as acorn from 'acorn'
import * as walk from 'acorn-walk'
const bundlers = {
async Rollup(file) {
const bundle = await rollup({
input: 'entry',
plugins: [virtual({ entry: `import ${JSON.stringify(file)}` })],
logLevel: 'silent',
})
const result = await bundle.generate({ format: 'esm' })
return result.output[0].code
},
async Webpack(file) {
const compiler = webpack({
mode: 'production',
entry: file,
output: {
filename: 'output.js',
path: '/',
module: true,
},
experiments: {
outputModule: true,
},
})
compiler.outputFileSystem = new MemoryFS()
const code = await new Promise((resolve, reject) => {
compiler.run((error, stats) => {
if (error) return reject(error)
if (stats.hasErrors()) {
return reject(new Error(stats.toString({ errorDetails: true })))
}
resolve(compiler.outputFileSystem.data['output.js'].toString())
})
})
return code
},
async ESBuild(file) {
const { code } = await esbuild.transform(`import ${JSON.stringify(file)}`, {
sourcemap: false,
target: 'esnext',
minify: true,
treeShaking: true,
})
return code
},
}
const lineNumbers = (source, offset = 1, lines = source.split('\n').length) =>
source.replace(/^/gm, () => `${offset++}:`.padStart(lines.toString().length + 3))
const hasIdent = (node) => node.type === 'AssignmentExpression' || node.type === 'PropertyDefinition'
function hasScope(node) {
switch (node.type) {
case 'FunctionDeclaration':
case 'FunctionExpression':
case 'ObjectMethod':
case 'ArrowFunctionExpression':
case 'ClassMethod':
case 'ClassPrivateMethod':
return true
case 'PropertyDefinition':
return !node.static
default:
return false
}
}
try {
const input = process.argv[2]
const file = path.resolve(input)
await Promise.all(
Object.entries(bundlers).map(async ([bundler, compile]) => {
const code = await compile(file)
const lines = lineNumbers(code).split('\n')
const errors = []
let maxLine = 0
let maxColumn = 0
function filterEffects(node, _state, ancestors) {
let anonymous = true
for (const ancestor of ancestors) {
if (node.type === 'MemberExpression' && ancestor.type === 'CallExpression') return
if (hasIdent(ancestor)) anonymous = false
if (hasScope(ancestor)) return
}
let error = null
if (node.type === 'CallExpression' || node.type === 'NewExpression') {
const type = node.type === 'CallExpression' ? 'function' : 'class'
error = anonymous
? `Anonymous ${type} invocations must be assigned a value and annotated with /* @__PURE__ */!`
: `Top-level ${type} invocations must be annotated with /* @__PURE__ */!`
} else if (node.type === 'MemberExpression') {
error = 'Top-level member expressions may call code! Prefer destructuring or IIFE.'
}
if (error) {
const { line, column } = node.loc.start
lines[line - 1] = '>' + lines[line - 1].slice(1)
maxLine = Math.max(maxLine, line.toString().length)
maxColumn = Math.max(maxColumn, column.toString().length)
errors.push({ line, column, error })
}
}
const ast = acorn.parse(code, {
ecmaVersion: 'latest',
sourceType: 'module',
locations: true,
})
walk.ancestor(ast, {
CallExpression: filterEffects,
NewExpression: filterEffects,
MemberExpression: filterEffects,
})
if (errors.length) {
console.log(lines.join('\n'))
for (const { line, column, error } of errors) {
const _line = line.toString().padStart(maxLine)
const _column = column.toString().padEnd(maxColumn)
console.error(`${_line}:${_column} ${error}`)
}
throw `Couldn't tree-shake "${input}" with ${bundler}!`
}
}),
)
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' })
const targets = formatter.format(Object.keys(bundlers))
console.info(`Successfully tree-shaken "${input}" with ${targets}!`)
} catch (e) {
console.error(e)
process.exit(1)
}