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

Register dependency serializers #205

Merged
merged 17 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
21 changes: 18 additions & 3 deletions src/AureliaDependenciesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IncludeDependency } from "./IncludeDependency";
import { ClassSerializer } from "./ClassSerializer";
import * as estree from 'estree';
import * as webpack from 'webpack';

Expand All @@ -8,8 +9,8 @@ const BasicEvaluatedExpression: $BasicEvaluatedExpression = require("webpack/lib
const TAP_NAME = "Aurelia:Dependencies";

class AureliaDependency extends IncludeDependency {
constructor(request: string,
public range: [number, number],
constructor(public request: string,
public range: [number, number],
options?: DependencyOptions) {
super(request, options);
}
Expand All @@ -21,8 +22,22 @@ class AureliaDependency extends IncludeDependency {
get [dependencyImports]() {
return webpack.Dependency.EXPORTS_OBJECT_REFERENCED as any;
}

serialize(context: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know the type of this context? if we do, I'd prefer that we have it here instead of any

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ObjectSerializerContext . Unfortunately, this type is only listed but not exported in the webpack ts definition file if I see this correctly.

const { write } = context;
write(this.range);
super.serialize(context);
}

deserialize(context: any) {
const { read } = context;
this.range = read();
super.deserialize(context);
}
}

webpack.util.serialization.register(AureliaDependency, "AureliaDependency", null as any, new ClassSerializer(AureliaDependency));

class Template {
apply(dep: AureliaDependency, source: webpack.sources.ReplaceSource) {
source.replace(dep.range[0], dep.range[1] - 1, "'" + dep.request.replace(/^async(?:\?[^!]*)?!/, "") + "'");
Expand Down Expand Up @@ -100,7 +115,7 @@ class ParserPlugin {
let value = parser.evaluateExpression(prop.value as estree.Literal)!;
switch (prop.key.name) {
case "chunk":
if (value.isString())
if (value.isString())
options.chunk = value.string;
break;
case "exports":
Expand Down
19 changes: 19 additions & 0 deletions src/ClassSerializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class ClassSerializer<T extends ISerializable> {
constructor(private ctor: { new(...params: any[]): T }) {
}

serialize(obj: T, context: any) {
obj.serialize(context);
}

deserialize(context: any) {
const obj = new this.ctor();
obj.deserialize(context);
return obj;
}
}

interface ISerializable {
serialize(context: any): void;
deserialize(context: any): void
}
17 changes: 16 additions & 1 deletion src/IncludeDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dependencyImports } from "./PreserveExportsPlugin";
import { preserveModuleName } from "./PreserveModuleNamePlugin";
import * as webpack from 'webpack';
import { DependencyOptions, ReferencedExport } from "./interfaces";
import { ClassSerializer } from "./ClassSerializer";

export class IncludeDependency extends webpack.dependencies.ModuleDependency {
protected options?: DependencyOptions;
Expand Down Expand Up @@ -30,7 +31,21 @@ export class IncludeDependency extends webpack.dependencies.ModuleDependency {
get [dependencyImports]() {
return this.options?.exports;
}

serialize(context: any) {
const { write } = context;
write(this.options);
super.serialize(context);
}

deserialize(context: any) {
const { read } = context;
this.options = read();
super.deserialize(context);
}
};

webpack.util.serialization.register(IncludeDependency, "IncludeDependency", null as any, new ClassSerializer(IncludeDependency));

export type NullDependencyTemplate = typeof webpack.dependencies.NullDependency.Template;
export const Template: NullDependencyTemplate = webpack.dependencies.NullDependency.Template;
export const Template: NullDependencyTemplate = webpack.dependencies.NullDependency.Template;