Skip to content

Commit

Permalink
feat: compatible with rspack
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Jun 17, 2024
1 parent a37f4b2 commit 7b90e83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@
"dist"
],
"peerDependencies": {
"@rspack/core": "0.x || 1.x",
"webpack": "^4.0.0 || ^5.0.0"
},
"peerDependenciesMeta": {
"@rspack/core": {
"optional": true
},
"webpack": {
"optional": true
}
},
"dependencies": {
"loader-utils": "^2.0.0",
"schema-utils": "^3.1.0"
Expand Down
46 changes: 28 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import path from "path";
import { getOptions } from "loader-utils";
import { validate } from "schema-utils";

import NodeTargetPlugin from "webpack/lib/node/NodeTargetPlugin";
import SingleEntryPlugin from "webpack/lib/SingleEntryPlugin";
import WebWorkerTemplatePlugin from "webpack/lib/webworker/WebWorkerTemplatePlugin";
import ExternalsPlugin from "webpack/lib/ExternalsPlugin";

import schema from "./options.json";
import supportWebpack5 from "./supportWebpack5";
import supportWebpack4 from "./supportWebpack4";
Expand All @@ -17,23 +12,10 @@ import {
getExternalsType,
} from "./utils";

let FetchCompileWasmPlugin;
let FetchCompileAsyncWasmPlugin;

// determine the version of webpack peer dependency
// eslint-disable-next-line global-require, import/no-unresolved
const useWebpack5 = require("webpack/package.json").version.startsWith("5.");

if (useWebpack5) {
// eslint-disable-next-line global-require, import/no-unresolved
FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmPlugin");
// eslint-disable-next-line global-require, import/no-unresolved
FetchCompileAsyncWasmPlugin = require("webpack/lib/web/FetchCompileAsyncWasmPlugin");
} else {
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmTemplatePlugin");
}

export default function loader() {}

export function pitch(request) {
Expand All @@ -46,6 +28,34 @@ export function pitch(request) {
baseDataPath: "options",
});

let NodeTargetPlugin;
let SingleEntryPlugin;
let WebWorkerTemplatePlugin;
let ExternalsPlugin;
let FetchCompileWasmPlugin;
let FetchCompileAsyncWasmPlugin;

if (useWebpack5) {
const { webpack } = this._compiler;
NodeTargetPlugin = webpack.node.NodeTargetPlugin;
SingleEntryPlugin = webpack.EntryPlugin;
WebWorkerTemplatePlugin = webpack.webworker.WebWorkerTemplatePlugin;
ExternalsPlugin = webpack.ExternalsPlugin;
FetchCompileWasmPlugin = webpack.web.FetchCompileWasmPlugin;
FetchCompileAsyncWasmPlugin = webpack.web.FetchCompileAsyncWasmPlugin;
} else {
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
NodeTargetPlugin = require("webpack/lib/node/NodeTargetPlugin");
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin");
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
ExternalsPlugin = require("webpack/lib/ExternalsPlugin");
// eslint-disable-next-line global-require, import/no-unresolved, import/extensions
FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmTemplatePlugin");
}

const workerContext = {};
const compilerOptions = this._compiler.options || {};
const filename = options.filename
Expand Down

0 comments on commit 7b90e83

Please sign in to comment.