Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack refactor #50

Draft
wants to merge 81 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
020fb00
wip
j4k0xb Jan 13, 2024
821bf89
feat: webpack chunk class
j4k0xb Jan 13, 2024
7369268
fix: remove trailing webpack comment
j4k0xb Jan 13, 2024
95e519a
feat: export default expression
j4k0xb Jan 13, 2024
c41576b
refactor: store multiple chunk ids in a chunk
j4k0xb Jan 13, 2024
a3e126e
refactor: group export tests by webpack 4/5
j4k0xb Jan 13, 2024
a169879
perf: stop traversing once webpack chunk or module is found
j4k0xb Jan 13, 2024
463f47c
perf: skip traversing in var-injections
j4k0xb Jan 13, 2024
6d9cbe0
feat: export analysis
j4k0xb Jan 13, 2024
9a773f9
refactor: import export manager
j4k0xb Jan 13, 2024
ecc0a1f
fix: export already exported variable
j4k0xb Jan 13, 2024
639d633
feat: default export
j4k0xb Jan 13, 2024
18667e8
feat: track exports
j4k0xb Jan 13, 2024
d79e94b
fix: export variable named and default
j4k0xb Jan 13, 2024
5251afd
refactor: track export names only
j4k0xb Jan 13, 2024
336e9f1
refactor: export default
j4k0xb Jan 13, 2024
cf01037
refactor: remove RequireVar name
j4k0xb Jan 13, 2024
fbf2be0
fix: re-export all name
j4k0xb Jan 13, 2024
9913e50
refactor: use babel template
j4k0xb Jan 13, 2024
8050dd1
refactor: add unexpected export comment instead of throwing
j4k0xb Jan 13, 2024
4f5679f
feat: track imports
j4k0xb Jan 13, 2024
df71e78
feat: remove `__webpack_require__.hmd` and `__webpack_require__.nmd`
j4k0xb Jan 13, 2024
8d0faac
feat: remove `__webpack_require__.r`
j4k0xb Jan 13, 2024
21417e5
feat: replace `__webpack_require__.o`
j4k0xb Jan 13, 2024
16ac04d
refactor: `__webpack_require__.d`
j4k0xb Jan 13, 2024
01315f9
feat: replace `__webpack_require__.g`
j4k0xb Jan 13, 2024
bbe7574
refactor: use this instead of a state param in transform.run
j4k0xb Jan 13, 2024
9efe754
perf: use references for webpack transforms
j4k0xb Jan 13, 2024
7b1c7e2
feat: apply webpack runtime transforms
j4k0xb Jan 13, 2024
dce09bd
docs: webpack runtime comments
j4k0xb Jan 13, 2024
ea6d667
feat: imports
j4k0xb Jan 13, 2024
691794b
docs: todo comments
j4k0xb Jan 13, 2024
542b014
fix: imports being mixed up
j4k0xb Jan 13, 2024
7731634
fix: don't remove require var without references
j4k0xb Jan 13, 2024
afbabb8
fix: normalize module paths
j4k0xb Jan 13, 2024
024df05
feat: more specific webpack parameter names, handle custom namespace …
j4k0xb Jan 13, 2024
2075348
fix: properly implement concatenated module exports
j4k0xb Jan 13, 2024
4faef28
feat: default import
j4k0xb Jan 13, 2024
7a7c00c
refactor: import/export management
j4k0xb Jan 13, 2024
90d92cc
test: add more import tests
j4k0xb Jan 13, 2024
a2f7419
refactor: remove unused files
j4k0xb Jan 13, 2024
069cb49
test: add nested var injections
j4k0xb Jan 13, 2024
94c8d14
style: lint consistent-type-imports
j4k0xb Jan 13, 2024
ad3fed2
test: add failing inlined variable export test case
j4k0xb Jan 13, 2024
e1d54b2
wip
j4k0xb Jan 13, 2024
27f5813
test: more exports
j4k0xb Jan 13, 2024
4d8cf35
refactor: remove redundant test
j4k0xb Jan 13, 2024
8641259
test: fix many import test cases
j4k0xb Jan 13, 2024
bbedc10
fix: indirect call import
j4k0xb Jan 13, 2024
949248e
refactor
j4k0xb Jan 13, 2024
da538fd
fix: default import
j4k0xb Jan 13, 2024
5420156
fix: imports
j4k0xb Jan 13, 2024
cc20371
test: enable remaining export tests
j4k0xb Jan 13, 2024
6975fb0
test: mixed import/require
j4k0xb Jan 13, 2024
b7543c4
feat: sort import specifiers alphabetically
j4k0xb Jan 13, 2024
f1ac839
refactor: separate namespace and named export properties
j4k0xb Jan 13, 2024
5f778fc
feat: better default import
j4k0xb Jan 13, 2024
ed1b93e
refactor(perf): avoid container matcher for webpack function
j4k0xb Jan 13, 2024
1d14033
refactor: separate webpack 4 and 5 matching
j4k0xb Jan 13, 2024
10074a7
refactor: remove unused webpack samples
j4k0xb Jan 13, 2024
780b891
feat: rename module and exports in commonjs
j4k0xb Jan 13, 2024
34c5938
refactor: remove unused code
j4k0xb Jan 13, 2024
1f137d6
fix: support multiple namespace imports
j4k0xb Jan 13, 2024
d8a2381
Revert "refactor: separate webpack 4 and 5 matching"
j4k0xb Jan 13, 2024
39f9aaf
fix: multiple named imports
j4k0xb Jan 13, 2024
de98b60
fix: indirect calls for default import
j4k0xb Jan 13, 2024
cd26c95
fix: decompile jsx after unpacking the bundle
j4k0xb Jan 13, 2024
69c4f93
test: remove `__webpack_require__.r` in every import test
j4k0xb Jan 13, 2024
379017e
test: temporarily disable failing tests
j4k0xb Jan 13, 2024
31c2e70
feat: remove `__esModule` property
j4k0xb Jan 13, 2024
3a91796
feat: transform json modules
j4k0xb Jan 13, 2024
6a0e470
feat(playground): add json file icons
j4k0xb Jan 13, 2024
0238007
feat: inline external modules
j4k0xb Jan 15, 2024
0c9a328
fix: use `__webpack_module__` for json module matcher
j4k0xb Jan 15, 2024
b74ee88
test: re-enable unpack samples
j4k0xb Jan 15, 2024
51c9f10
fix: normalize with posix path
j4k0xb Jan 15, 2024
a3bb7f0
style: fix comments
j4k0xb Jan 15, 2024
4a28006
feat: expose export names in webpack module
j4k0xb Jan 15, 2024
3140dd0
test: update samples
j4k0xb Jan 15, 2024
7fd08bc
fix: path file extension for webpack development builds
j4k0xb Jan 22, 2024
4cc7b2c
fix: export variable as named and default
j4k0xb Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: track exports
  • Loading branch information
j4k0xb committed Jan 13, 2024
commit 18667e8723ffb8c40193a0a69dd583fabd8e97f2
45 changes: 26 additions & 19 deletions packages/webcrack/src/unpack/webpack/import-export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface RequireVar {
}

export class ImportExportManager {
exports: t.ExportDeclaration[] = [];

private ast: t.File;
private webpackRequire: Binding | undefined;
/**
Expand Down Expand Up @@ -60,17 +62,17 @@ export class ImportExportManager {
const requireVar = findRequireVar(binding);

if (requireVar) {
this.reExportAll(binding, requireVar.moduleId);
this.addExportAll(binding, requireVar.moduleId);
} else {
this.exportLocalVar(binding, exportName);
this.addExportDeclaration(binding, exportName);
}
} else if (memberExpressionMatcher.match(value)) {
const binding = scope.getBinding(objectName.current!);
if (!binding) return;
const requireVar = findRequireVar(binding);
if (!requireVar) return;

this.reExportNamed(
this.addExportFrom(
requireVar.binding,
requireVar.moduleId,
propertyName.current!,
Expand All @@ -92,7 +94,7 @@ export class ImportExportManager {
* export { bar as foo } from 'lib';
* ```
*/
private reExportNamed(
private addExportFrom(
binding: Binding,
moduleId: string,
localName: string,
Expand All @@ -112,6 +114,7 @@ export class ImportExportManager {
[specifier],
t.stringLiteral(moduleId),
);
this.exports.push(exportDeclaration);
binding.path.parentPath?.insertAfter(exportDeclaration);
this.reExportCache.set(moduleId, exportDeclaration);
}
Expand All @@ -128,7 +131,7 @@ export class ImportExportManager {
* export var counter = 1;
* ```
*/
private exportLocalVar(binding: Binding, exportName: string) {
private addExportDeclaration(binding: Binding, exportName: string) {
const declaration = findPath(
binding.path,
m.or(
Expand All @@ -141,22 +144,26 @@ export class ImportExportManager {
if (!declaration) return;

if (declaration.type === 'ExportNamedDeclaration') {
declaration.insertAfter(
t.exportNamedDeclaration(null, [
t.exportSpecifier(
t.identifier(binding.identifier.name),
t.identifier(exportName),
),
]),
);
const namedExport = t.exportNamedDeclaration(null, [
t.exportSpecifier(
t.identifier(binding.identifier.name),
t.identifier(exportName),
),
]);
this.exports.push(namedExport);
declaration.insertAfter(namedExport);
} else if (exportName === 'default') {
const value = t.isVariableDeclaration(declaration.node)
? declaration.node.declarations[0].init!
: (declaration.node as t.ClassDeclaration | t.FunctionDeclaration);
declaration.replaceWith(t.exportDefaultDeclaration(value));
const defaultExport = t.exportDefaultDeclaration(value);
this.exports.push(defaultExport);
declaration.replaceWith(defaultExport);
} else {
renameFast(binding, exportName);
declaration.replaceWith(t.exportNamedDeclaration(declaration.node));
const namedExport = t.exportNamedDeclaration(declaration.node);
this.exports.push(namedExport);
declaration.replaceWith(namedExport);
}
}

Expand All @@ -171,11 +178,11 @@ export class ImportExportManager {
* export * as foo from 'lib';
* ```
*/
private reExportAll(binding: Binding, moduleId: string) {
private addExportAll(binding: Binding, moduleId: string) {
// TODO: resolve to file path
binding.path.parentPath?.insertAfter(
t.exportAllDeclaration(t.stringLiteral(moduleId)),
);
const exportAll = t.exportAllDeclaration(t.stringLiteral(moduleId));
this.exports.push(exportAll);
binding.path.parentPath?.insertAfter(exportAll);
}

/**
Expand Down