Skip to content

Commit

Permalink
serialize preserveModuleName on NormalModule
Browse files Browse the repository at this point in the history
  • Loading branch information
elitastic committed May 6, 2022
1 parent 4e0e059 commit 906e25a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 38 deletions.
13 changes: 1 addition & 12 deletions dist/AureliaDependenciesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const TAP_NAME = "Aurelia:Dependencies";
class AureliaDependency extends IncludeDependency_1.IncludeDependency {
constructor(request, range, options) {
super(request, options);
this.request = request;
this.range = range;
}
get type() {
Expand All @@ -19,18 +18,8 @@ class AureliaDependency extends IncludeDependency_1.IncludeDependency {
get [PreserveExportsPlugin_1.dependencyImports]() {
return webpack.Dependency.EXPORTS_OBJECT_REFERENCED;
}
serialize(context) {
const { write } = context;
write(this.range);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.range = read();
super.deserialize(context);
}
}
webpack.util.serialization.register(AureliaDependency, "AureliaDependency", null, new ClassSerializer_1.ClassSerializer(AureliaDependency));
webpack.util.serialization.register(AureliaDependency, "AureliaDependency", "AureliaDependency", new ClassSerializer_1.ClassSerializer(AureliaDependency));
class Template {
apply(dep, source) {
source.replace(dep.range[0], dep.range[1] - 1, "'" + dep.request.replace(/^async(?:\?[^!]*)?!/, "") + "'");
Expand Down
8 changes: 3 additions & 5 deletions dist/IncludeDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ class IncludeDependency extends webpack.dependencies.ModuleDependency {
return (_a = this.options) === null || _a === void 0 ? void 0 : _a.exports;
}
serialize(context) {
const { write } = context;
write(this.options);
context.write(this.options);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.options = read();
this.options = context.read();
super.deserialize(context);
}
}
exports.IncludeDependency = IncludeDependency;
;
webpack.util.serialization.register(IncludeDependency, "IncludeDependency", null, new ClassSerializer_1.ClassSerializer(IncludeDependency));
webpack.util.serialization.register(IncludeDependency, "IncludeDependency", "IncludeDependency", new ClassSerializer_1.ClassSerializer(IncludeDependency));
exports.Template = webpack.dependencies.NullDependency.Template;
23 changes: 23 additions & 0 deletions dist/PreserveModuleNamePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreserveModuleNamePlugin = exports.preserveModuleName = void 0;
const path = require("path");
const Webpack = require("webpack");
const logger_1 = require("./logger");
exports.preserveModuleName = Symbol();
const TAP_NAME = "Aurelia:PreserveModuleName";
Expand All @@ -16,6 +17,13 @@ class PreserveModuleNamePlugin {
this.isDll = isDll;
}
apply(compiler) {
// Override NormalModule serializer: "preserveModuleName" should be serialized
// to ensure correct module serialization of conventional dependencies (ConventionDependenciesPlugin)
// when "webpack filesystem cache" is enabled, https://github.com/aurelia/webpack-plugin/issues/199
const isFilesystemCacheEnabled = typeof (compiler.options.cache) != 'boolean' && compiler.options.cache.type == 'filesystem';
if (isFilesystemCacheEnabled) {
overrideNormalModuleSerializer();
}
compiler.hooks.compilation.tap(TAP_NAME, compilation => {
compilation.hooks.beforeModuleIds.tap(TAP_NAME, $modules => {
let modules = Array.from($modules);
Expand Down Expand Up @@ -177,3 +185,18 @@ function removeLoaders(request) {
let lastBang = request.lastIndexOf("!");
return lastBang < 0 ? request : request.substr(lastBang + 1);
}
function overrideNormalModuleSerializer() {
const originalSerialize = Webpack.NormalModule.prototype.serialize;
Webpack.NormalModule.prototype.serialize = function (context) {
context.write(this[exports.preserveModuleName]);
originalSerialize.call(this, context);
};
const originalDeserialize = Webpack.NormalModule.prototype.deserialize;
Webpack.NormalModule.prototype.deserialize = function (context) {
const preserve = context.read();
if (preserve) {
this[exports.preserveModuleName] = preserve;
}
originalDeserialize.call(this, context);
};
}
9 changes: 5 additions & 4 deletions dist/types/ClassSerializer.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ObjectDeserializerContext, ObjectSerializerContext } from "./webpack";
export declare class ClassSerializer<T extends ISerializable> {
private ctor;
constructor(ctor: {
new (...params: any[]): T;
});
serialize(obj: T, context: any): void;
deserialize(context: any): T;
serialize(obj: T, context: ObjectSerializerContext): void;
deserialize(context: ObjectDeserializerContext): T;
}
interface ISerializable {
serialize(context: any): void;
deserialize(context: any): void;
serialize(context: ObjectSerializerContext): void;
deserialize(context: ObjectDeserializerContext): void;
}
export {};
14 changes: 1 addition & 13 deletions src/AureliaDependenciesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BasicEvaluatedExpression: $BasicEvaluatedExpression = require("webpack/lib
const TAP_NAME = "Aurelia:Dependencies";

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

serialize(context: any) {
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", "AureliaDependency", new ClassSerializer(AureliaDependency));
Expand Down
6 changes: 2 additions & 4 deletions src/IncludeDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ export class IncludeDependency extends webpack.dependencies.ModuleDependency {
}

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

deserialize(context: any) {
const { read } = context;
this.options = read();
this.options = context.read();
super.deserialize(context);
}
};
Expand Down
25 changes: 25 additions & 0 deletions src/PreserveModuleNamePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export class PreserveModuleNamePlugin {
}

apply(compiler: Webpack.Compiler) {
// Override NormalModule serializer: "preserveModuleName" should be serialized
// to ensure correct module serialization of conventional dependencies (ConventionDependenciesPlugin)
// when "webpack filesystem cache" is enabled, https://github.com/aurelia/webpack-plugin/issues/199
const isFilesystemCacheEnabled = typeof (compiler.options.cache) != 'boolean' && compiler.options.cache.type == 'filesystem';
if (isFilesystemCacheEnabled) {
overrideNormalModuleSerializer();
}

compiler.hooks.compilation.tap(TAP_NAME, compilation => {
compilation.hooks.beforeModuleIds.tap(TAP_NAME, $modules => {
let modules = Array.from($modules) as Webpack.NormalModule[];
Expand Down Expand Up @@ -189,3 +197,20 @@ function removeLoaders(request: string | undefined) {
let lastBang = request.lastIndexOf("!");
return lastBang < 0 ? request : request.substr(lastBang + 1);
}

function overrideNormalModuleSerializer() {
const originalSerialize = Webpack.NormalModule.prototype.serialize;
Webpack.NormalModule.prototype.serialize = function (context) {
context.write(this[preserveModuleName]);
originalSerialize.call(this, context);
}

const originalDeserialize = Webpack.NormalModule.prototype.deserialize;
Webpack.NormalModule.prototype.deserialize = function (context) {
const preserve = context.read();
if (preserve) {
this[preserveModuleName] = preserve;
}
originalDeserialize.call(this, context);
}
}

0 comments on commit 906e25a

Please sign in to comment.