Skip to content

Commit

Permalink
Fix HtmlRspackPlugin not being filtered bug (#28)
Browse files Browse the repository at this point in the history
* fix: fix the bug of HtmlRspackPlugin's raw method failure

* fix: remove some unused codes and export types

update the codes of copying from webpack

* feat: update the codes of getting Compiler's rspack
  • Loading branch information
xfsnowind authored Sep 15, 2024
1 parent d6433eb commit ea8f2fc
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 85 deletions.
1 change: 0 additions & 1 deletion dist/CypressCTRspackPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface CypressCTRspackPluginOptions {
projectRoot: string;
supportFile: string | false;
devServerEvents: EventEmitter;
rspack: Function;
indexHtmlFile: string;
}
export type CypressCTContextOptions = Omit<CypressCTRspackPluginOptions, 'devServerEvents' | 'rspack'>;
Expand Down
6 changes: 5 additions & 1 deletion dist/CypressCTRspackPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class CypressCTRspackPlugin {
*/
this.addCompilationHooks = (compilation) => {
this.compilation = compilation;
const loader = this.compilation.compiler.rspack.NormalModule.getCompilationHooks(compilation).loader;
// still use legacy `webpack` here since in version 0.x.x does not have rspack
const compiler = ('rspack' in this.compilation.compiler
? this.compilation.compiler.rspack
: this.compilation.compiler.webpack);
const loader = compiler.NormalModule.getCompilationHooks(compilation).loader;
loader.tap('CypressCTPlugin', this.addLoaderContext);
};
this.files = options.files;
Expand Down
10 changes: 2 additions & 8 deletions dist/devServer.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EventEmitter } from 'events';
import type { RspackDevServer } from '@rspack/dev-server';
import type { Configuration } from '@rspack/core';
import { SourceRelativeRspackResult } from './helpers/sourceRelativeRspackModules';
export type Frameworks = Extract<Cypress.DevServerConfigOptions, {
bundler: 'webpack';
}>['framework'];
Expand All @@ -13,7 +12,7 @@ type FrameworkConfig = {
projectConfig: Cypress.AngularDevServerProjectConfig;
};
};
export type ConfigHandler = Partial<Configuration> | (() => Partial<Configuration> | Promise<Partial<Configuration>>);
type ConfigHandler = Partial<Configuration> | (() => Partial<Configuration> | Promise<Partial<Configuration>>);
export type DevServerConfig = {
specs: Cypress.Spec[];
cypressConfig: Cypress.PluginConfigOptions;
Expand All @@ -36,9 +35,4 @@ export declare namespace devServer {
compiler: any;
}>;
}
export type PresetHandlerResult = {
frameworkConfig: Configuration;
sourceRspackModulesResult: SourceRelativeRspackResult;
};
export declare function isThirdPartyDefinition(framework: string): boolean;
export default devServer;
export {};
3 changes: 0 additions & 3 deletions dist/devServer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.devServer = devServer;
exports.isThirdPartyDefinition = isThirdPartyDefinition;
const tslib_1 = require("tslib");
/// <reference types="cypress" />
const debug_1 = tslib_1.__importDefault(require("debug"));
const createRspackDevServer_1 = require("./createRspackDevServer");
const sourceRelativeRspackModules_1 = require("./helpers/sourceRelativeRspackModules");
Expand Down Expand Up @@ -97,4 +95,3 @@ devServer.create = async function (devServerConfig) {
});
return { server, compiler };
};
exports.default = devServer;
1 change: 0 additions & 1 deletion dist/helpers/sourceRelativeRspackModules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface SourceRelativeRspackResult {
rspack: SourcedRspack;
rspackDevServer: SourcedRspackDevServer;
}
export declare const cypressRspackPath: (config: DevServerConfig) => string;
export declare function sourceFramework(config: DevServerConfig): SourcedDependency | null;
export declare function sourceRspack(config: DevServerConfig, framework: SourcedDependency | null): SourcedRspack;
export declare function sourceRspackDevServer(config: DevServerConfig, framework?: SourcedDependency | null): SourcedRspackDevServer;
Expand Down
26 changes: 3 additions & 23 deletions dist/helpers/sourceRelativeRspackModules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cypressRspackPath = void 0;
exports.sourceFramework = sourceFramework;
exports.sourceRspack = sourceRspack;
exports.sourceRspackDevServer = sourceRspackDevServer;
Expand All @@ -13,12 +12,6 @@ const debug_1 = tslib_1.__importDefault(require("debug"));
const debug = (0, debug_1.default)('cypress-rspack-dev-server:sourceRelativeRspackModules');
const originalModuleLoad = module_1.default._load;
const originalModuleResolveFilename = module_1.default._resolveFilename;
const cypressRspackPath = (config) => {
return require.resolve('@cypress/rspack-batteries-included-preprocessor', {
paths: [config.cypressConfig.cypressBinaryRoot],
});
};
exports.cypressRspackPath = cypressRspackPath;
const frameworkRspackMapper = {
'create-react-app': 'react-scripts',
'vue-cli': '@vue/cli-service',
Expand Down Expand Up @@ -68,22 +61,9 @@ function sourceRspack(config, framework) {
const searchRoot = (_a = framework === null || framework === void 0 ? void 0 : framework.importPath) !== null && _a !== void 0 ? _a : config.cypressConfig.projectRoot;
debug('Rspack: Attempting to source rspack from %s', searchRoot);
const rspack = {};
let rspackJsonPath;
try {
rspackJsonPath = require.resolve('@rspack/core/package.json', {
paths: [searchRoot],
});
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
debug('Rspack: Failed to source rspack - %s', e);
throw e;
}
debug('rspack: Falling back to bundled version');
rspackJsonPath = require.resolve('@rspack/core/package.json', {
paths: [(0, exports.cypressRspackPath)(config)],
});
}
const rspackJsonPath = require.resolve('@rspack/core/package.json', {
paths: [searchRoot],
});
rspack.importPath = path_1.default.dirname(rspackJsonPath);
rspack.packageJson = require(rspackJsonPath);
rspack.module = require(rspack.importPath).rspack;
Expand Down
5 changes: 1 addition & 4 deletions dist/makeDefaultRspackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ function makeCypressRspackConfig(config) {
// rspack does not recognize the sideEffects flag in the package.json and thus files are not unintentionally
// dropped during testing in production mode.
sideEffects: false,
splitChunks: {
chunks: 'all',
},
splitChunks: { chunks: 'all' },
};
const publicPath = path_1.default.sep === posixSeparator
? path_1.default.join(devServerPublicPathRoute, posixSeparator)
Expand All @@ -44,7 +42,6 @@ function makeCypressRspackConfig(config) {
projectRoot,
devServerEvents,
supportFile,
rspack,
indexHtmlFile,
}),
],
Expand Down
3 changes: 1 addition & 2 deletions dist/makeRspackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ exports.CYPRESS_RSPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js');
*/
function modifyRspackConfigForCypress(rspackConfig) {
if (rspackConfig === null || rspackConfig === void 0 ? void 0 : rspackConfig.plugins) {
rspackConfig.plugins = rspackConfig.plugins.filter((plugin) => plugin &&
!removeList.includes('raw' in plugin ? plugin.raw({ options: { output: {} } }).name : plugin.constructor.name));
rspackConfig.plugins = rspackConfig.plugins.filter((plugin) => plugin && !removeList.includes(plugin.constructor.name));
}
delete rspackConfig.entry;
delete rspackConfig.output;
Expand Down
11 changes: 8 additions & 3 deletions src/CypressCTRspackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface CypressCTRspackPluginOptions {
projectRoot: string
supportFile: string | false
devServerEvents: EventEmitter
rspack: Function
indexHtmlFile: string
}

Expand Down Expand Up @@ -138,8 +137,14 @@ export class CypressCTRspackPlugin {
private addCompilationHooks = (compilation: RspackCompilation) => {
this.compilation = compilation

const loader =
this.compilation.compiler.rspack.NormalModule.getCompilationHooks(compilation).loader
// still use legacy `webpack` here since in version 0.x.x does not have rspack
const compiler = (
'rspack' in this.compilation.compiler
? this.compilation.compiler.rspack
: (this.compilation.compiler as Compiler).webpack
) as Compiler['webpack']

const loader = compiler.NormalModule.getCompilationHooks(compilation).loader

loader.tap('CypressCTPlugin', this.addLoaderContext)
}
Expand Down
9 changes: 3 additions & 6 deletions src/devServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="cypress" />
import debugLib from 'debug'
import type { EventEmitter } from 'events'
import type { RspackDevServer } from '@rspack/dev-server'
Expand Down Expand Up @@ -28,7 +27,7 @@ type FrameworkConfig =
}
}

export type ConfigHandler =
type ConfigHandler =
| Partial<Configuration>
| (() => Partial<Configuration> | Promise<Partial<Configuration>>)

Expand Down Expand Up @@ -90,7 +89,7 @@ export function devServer(
})
}

export type PresetHandlerResult = {
type PresetHandlerResult = {
frameworkConfig: Configuration
sourceRspackModulesResult: SourceRelativeRspackResult
}
Expand All @@ -103,7 +102,7 @@ const thirdPartyDefinitionPrefixes = {
globalPrefix: 'cypress-ct-',
}

export function isThirdPartyDefinition(framework: string) {
function isThirdPartyDefinition(framework: string) {
return (
framework.startsWith(thirdPartyDefinitionPrefixes.globalPrefix) ||
thirdPartyDefinitionPrefixes.namespacedPrefixRe.test(framework)
Expand Down Expand Up @@ -170,5 +169,3 @@ devServer.create = async function (devServerConfig: DevServerConfig) {

return { server, compiler }
}

export default devServer
27 changes: 3 additions & 24 deletions src/helpers/sourceRelativeRspackModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ export interface SourceRelativeRspackResult {
const originalModuleLoad = (Module as ModuleClass)._load
const originalModuleResolveFilename = (Module as ModuleClass)._resolveFilename

export const cypressRspackPath = (config: DevServerConfig) => {
return require.resolve('@cypress/rspack-batteries-included-preprocessor', {
paths: [config.cypressConfig.cypressBinaryRoot],
})
}

type FrameworkRspackMapper = { [Property in Frameworks]: string | undefined }

const frameworkRspackMapper: FrameworkRspackMapper = {
Expand Down Expand Up @@ -121,24 +115,9 @@ export function sourceRspack(

const rspack = {} as SourcedRspack

let rspackJsonPath: string

try {
rspackJsonPath = require.resolve('@rspack/core/package.json', {
paths: [searchRoot],
})
} catch (e) {
if ((e as { code?: string }).code !== 'MODULE_NOT_FOUND') {
debug('Rspack: Failed to source rspack - %s', e)
throw e
}

debug('rspack: Falling back to bundled version')

rspackJsonPath = require.resolve('@rspack/core/package.json', {
paths: [cypressRspackPath(config)],
})
}
const rspackJsonPath: string = require.resolve('@rspack/core/package.json', {
paths: [searchRoot],
})

rspack.importPath = path.dirname(rspackJsonPath)
rspack.packageJson = require(rspackJsonPath)
Expand Down
5 changes: 1 addition & 4 deletions src/makeDefaultRspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export function makeCypressRspackConfig(config: CreateFinalRspackConfig): Config
// rspack does not recognize the sideEffects flag in the package.json and thus files are not unintentionally
// dropped during testing in production mode.
sideEffects: false,
splitChunks: {
chunks: 'all',
},
splitChunks: { chunks: 'all' },
}

const publicPath =
Expand Down Expand Up @@ -65,7 +63,6 @@ export function makeCypressRspackConfig(config: CreateFinalRspackConfig): Config
projectRoot,
devServerEvents,
supportFile,
rspack,
indexHtmlFile,
}),
],
Expand Down
6 changes: 1 addition & 5 deletions src/makeRspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ export const CYPRESS_RSPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js')
function modifyRspackConfigForCypress(rspackConfig: Partial<Configuration>) {
if (rspackConfig?.plugins) {
rspackConfig.plugins = rspackConfig.plugins.filter(
(plugin) =>
plugin &&
!removeList.includes(
'raw' in plugin ? plugin.raw({ options: { output: {} } }).name : plugin.constructor.name,
),
(plugin) => plugin && !removeList.includes(plugin.constructor.name),
)
}

Expand Down

0 comments on commit ea8f2fc

Please sign in to comment.