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

refactor!: remove service-worker target #2657

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Changes from 1 commit
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
Next Next commit
refactor: remove service-worker target
chenjiahan committed Jun 21, 2024
commit 835ccb4d56bc770f168fea96d7e42ddce6ee396a
3 changes: 1 addition & 2 deletions examples/service-worker/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -7,9 +7,8 @@ export default defineConfig({
output: {
distPath: {
root: './dist',
worker: './',
},
target: 'service-worker',
target: 'web-worker',
copy: ['./index.html'],
},
});
132 changes: 1 addition & 131 deletions packages/compat/plugin-swc/tests/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -646,136 +646,6 @@ exports[`plugin-swc > should disable react refresh when target is not web 1`] =
`;

exports[`plugin-swc > should disable react refresh when target is not web 2`] = `
{
"rules": [
{
"include": [
{
"and": [
"<ROOT>/packages/compat/plugin-swc/tests",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true,
"react": {
"refresh": false,
"runtime": "classic",
},
"useDefineForClassFields": false,
},
},
},
},
],
},
{
"mimetype": {
"or": [
"text/javascript",
"application/javascript",
],
},
"resolve": {
"fullySpecified": false,
},
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true,
"react": {
"refresh": false,
"runtime": "classic",
},
"useDefineForClassFields": false,
},
},
},
},
],
},
],
}
`;

exports[`plugin-swc > should disable react refresh when target is not web 3`] = `
{
"rules": [
{
@@ -905,7 +775,7 @@ exports[`plugin-swc > should disable react refresh when target is not web 3`] =
}
`;

exports[`plugin-swc > should disable react refresh when target is not web 4`] = `
exports[`plugin-swc > should disable react refresh when target is not web 3`] = `
{
"rules": [
{
5 changes: 0 additions & 5 deletions packages/compat/plugin-swc/tests/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -268,11 +268,6 @@ describe('plugin-swc', () => {
target: 'node',
},
},
serviceWorker: {
output: {
target: 'service-worker',
},
},
web: {
output: {
target: 'web',
1 change: 0 additions & 1 deletion packages/compat/webpack/src/webpackConfig.ts
Original file line number Diff line number Diff line change
@@ -75,7 +75,6 @@ async function getChainUtils(
web: 'client',
node: 'server',
'web-worker': 'web-worker',
'service-worker': 'service-worker',
};

return {
2 changes: 0 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@ import {
MEDIA_DIST_DIR,
ROOT_DIST_DIR,
SERVER_DIST_DIR,
SERVICE_WORKER_DIST_DIR,
SVG_DIST_DIR,
TS_CONFIG_FILE,
WASM_DIST_DIR,
@@ -140,7 +139,6 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
image: IMAGE_DIST_DIR,
media: MEDIA_DIST_DIR,
server: SERVER_DIST_DIR,
worker: SERVICE_WORKER_DIST_DIR,
},
assetPrefix: DEFAULT_ASSET_PREFIX,
filename: {},
2 changes: 0 additions & 2 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import type { RsbuildTarget } from '@rsbuild/shared';
export const ROOT_DIST_DIR = 'dist';
export const HTML_DIST_DIR = '/';
export const SERVER_DIST_DIR = 'server';
export const SERVICE_WORKER_DIST_DIR = 'worker';
export const JS_DIST_DIR = 'static/js';
export const CSS_DIST_DIR = 'static/css';
export const SVG_DIST_DIR = 'static/svg';
@@ -59,5 +58,4 @@ export const TARGET_ID_MAP: Record<RsbuildTarget, string> = {
web: 'Client',
node: 'Server',
'web-worker': 'Web Worker',
'service-worker': 'Service Worker',
};
37 changes: 17 additions & 20 deletions packages/core/src/plugins/entry.ts
Original file line number Diff line number Diff line change
@@ -29,31 +29,28 @@ export const pluginEntry = (): RsbuildPlugin => ({
name: 'rsbuild:entry',

setup(api) {
api.modifyBundlerChain(
async (chain, { environment, isServer, isServiceWorker }) => {
const config = api.getNormalizedConfig({ environment });
const { preEntry } = config.source;
const { entry } = api.context.environments[environment];
api.modifyBundlerChain(async (chain, { environment, isServer }) => {
const config = api.getNormalizedConfig({ environment });
const { preEntry } = config.source;
const { entry } = api.context.environments[environment];

const injectCoreJsEntry =
config.output.polyfill === 'entry' && !isServer && !isServiceWorker;
const injectCoreJsEntry = config.output.polyfill === 'entry' && !isServer;

for (const entryName of Object.keys(entry)) {
const entryPoint = chain.entry(entryName);
const addEntry = (item: string | EntryDescription) => {
entryPoint.add(item);
};
for (const entryName of Object.keys(entry)) {
const entryPoint = chain.entry(entryName);
const addEntry = (item: string | EntryDescription) => {
entryPoint.add(item);
};

preEntry.forEach(addEntry);
preEntry.forEach(addEntry);

if (injectCoreJsEntry) {
addEntry(createVirtualModule('import "core-js";'));
}

castArray(entry[entryName]).forEach(addEntry);
if (injectCoreJsEntry) {
addEntry(createVirtualModule('import "core-js";'));
}
},
);

castArray(entry[entryName]).forEach(addEntry);
}
});

api.onBeforeCreateCompiler(({ bundlerConfigs }) => {
if (bundlerConfigs.every((config) => !config.entry)) {
12 changes: 1 addition & 11 deletions packages/core/src/plugins/output.ts
Original file line number Diff line number Diff line change
@@ -54,10 +54,7 @@ export const pluginOutput = (): RsbuildPlugin => ({

setup(api) {
api.modifyBundlerChain(
async (
chain,
{ CHAIN_ID, target, isProd, isServer, isServiceWorker, environment },
) => {
async (chain, { CHAIN_ID, target, isProd, isServer, environment }) => {
const config = api.getNormalizedConfig({ environment });

const publicPath = getPublicPath({
@@ -114,13 +111,6 @@ export const pluginOutput = (): RsbuildPlugin => ({
});
}

if (isServiceWorker) {
const workerPath = config.output.distPath.worker;
const filename = posix.join(workerPath, '[name].js');

chain.output.filename(filename).chunkFilename(filename);
}

if (config.output.copy && api.context.bundlerType === 'rspack') {
const { copy } = config.output;
const options = Array.isArray(copy) ? { patterns: copy } : copy;
9 changes: 3 additions & 6 deletions packages/core/src/plugins/splitChunks.ts
Original file line number Diff line number Diff line change
@@ -228,15 +228,12 @@ export const pluginSplitChunks = (): RsbuildPlugin => ({
name: 'rsbuild:split-chunks',
setup(api) {
api.modifyBundlerChain(
async (
chain,
{ environment, isServer, isWebWorker, isServiceWorker },
) => {
if (isServer || isWebWorker || isServiceWorker) {
async (chain, { environment, isServer, isWebWorker }) => {
if (isServer || isWebWorker) {
chain.optimization.splitChunks(false);

// web worker does not support dynamic imports, dynamicImportMode need set to eager
if (isWebWorker || isServiceWorker) {
if (isWebWorker) {
chain.module.parser.merge({
javascript: {
dynamicImportMode: 'eager',
2 changes: 1 addition & 1 deletion packages/core/src/plugins/target.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ export const pluginTarget = (): RsbuildPlugin => ({
const { browserslist } = api.context.environments[environment];
const esVersion = browserslistToESVersion(browserslist);

if (target === 'web-worker' || target === 'service-worker') {
if (target === 'web-worker') {
chain.target(['webworker', `es${esVersion}`]);
return;
}
1 change: 0 additions & 1 deletion packages/core/src/provider/rspackConfig.ts
Original file line number Diff line number Diff line change
@@ -102,7 +102,6 @@ export function getChainUtils(
isProd: nodeEnv === 'production',
isServer: target === 'node',
isWebWorker: target === 'web-worker',
isServiceWorker: target === 'service-worker',
CHAIN_ID,
HtmlPlugin: getHTMLPlugin(),
};
6 changes: 0 additions & 6 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
@@ -54,7 +54,6 @@ exports[`environment config > should normalize environment config correctly 1`]
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
@@ -177,7 +176,6 @@ exports[`environment config > should print environment config when inspect confi
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
@@ -296,7 +294,6 @@ exports[`environment config > should print environment config when inspect confi
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
@@ -420,7 +417,6 @@ exports[`environment config > should support modify environment config by api.mo
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
@@ -539,7 +535,6 @@ exports[`environment config > should support modify environment config by api.mo
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
@@ -659,7 +654,6 @@ exports[`environment config > should support modify environment config by api.mo
"server": "server",
"svg": "static/svg",
"wasm": "static/wasm",
"worker": "worker",
},
"emitAssets": true,
"filename": {},
12 changes: 0 additions & 12 deletions packages/core/tests/html.test.ts
Original file line number Diff line number Diff line change
@@ -46,18 +46,6 @@ describe('plugin-html', () => {
expect(await rsbuild.matchBundlerPlugin('HtmlWebpackPlugin')).toBeFalsy();
});

it('should not register html plugin when target is service-worker', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginEntry(), pluginHtml()],
rsbuildConfig: {
output: {
target: 'service-worker',
},
},
});
expect(await rsbuild.matchBundlerPlugin('HtmlWebpackPlugin')).toBeFalsy();
});

it('should register appIcon plugin when using html.appIcon', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginEntry(), pluginHtml()],
Loading