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

Remove webpack imports #14

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Changes from all 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
90 changes: 50 additions & 40 deletions packages/webpack/plugin.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
const minifier = require("@yushijinhun/three-minifier-common");
const Dependency = require("webpack/lib/Dependency");
const NormalModule = require("webpack/lib/NormalModule");

const pluginName = "ThreeMinifierPlugin";

class ThreeReplaceDependency extends Dependency {
constructor(file) {
super();
this.file = file;
}
}

ThreeReplaceDependency.Template = class ThreeReplaceTemplate {
apply(dep, source) {
const originalSource = source.original().source();
for (const match of minifier.transformCode(originalSource, dep.file)) {
source.replace(match.start, match.end - 1, match.replacement);
}
}
};

class ThreeMinifierPlugin {
constructor() {
this.resolver = {};
this.resolver.apply = resolver => {
resolver.getHook("resolve").tapAsync(pluginName, (request, resolveContext, callback) => {
resolver.doResolve(resolver.ensureHook("internal-resolve"), request, null, resolveContext,
(error, result) => {
if (result && result.path) {
const transformed = minifier.transformModule(result.path);
if (transformed !== null) {
resolver.doResolve(resolver.ensureHook("internal-resolve"),
{
...request,
request: transformed
},
null, resolveContext, this._clearSideEffects(callback));
return;
this.resolver.apply = (resolver) => {
resolver
.getHook("resolve")
.tapAsync(pluginName, (request, resolveContext, callback) => {
resolver.doResolve(
resolver.ensureHook("internal-resolve"),
request,
null,
resolveContext,
(error, result) => {
if (result && result.path) {
const transformed = minifier.transformModule(result.path);
if (transformed !== null) {
resolver.doResolve(
resolver.ensureHook("internal-resolve"),
{
...request,
request: transformed,
},
null,
resolveContext,
this._clearSideEffects(callback)
);
return;
}
}
this._clearSideEffects(callback)(error, result);
}
this._clearSideEffects(callback)(error, result);
}
);

});
}
);
});
};
}

_clearSideEffects(callback) {
Expand All @@ -59,7 +50,26 @@ class ThreeMinifierPlugin {
}

apply(compiler) {
compiler.hooks.compilation.tap(pluginName, compilation => {
const Dependency = compiler.webpack.Dependency;
const NormalModule = compiler.webpack.NormalModule;

class ThreeReplaceDependency extends Dependency {
constructor(file) {
super();
this.file = file;
}
}

ThreeReplaceDependency.Template = class ThreeReplaceTemplate {
apply(dep, source) {
const originalSource = source.original().source();
for (const match of minifier.transformCode(originalSource, dep.file)) {
source.replace(match.start, match.end - 1, match.replacement);
}
}
};

compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.dependencyTemplates.set(
ThreeReplaceDependency,
new ThreeReplaceDependency.Template()
Expand All @@ -72,6 +82,6 @@ class ThreeMinifierPlugin {
});
});
}
};
}

module.exports = ThreeMinifierPlugin;