From 5ebc8026457d723b482f88d478af0acec9492d0c Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 6 Jul 2024 06:35:50 +0800 Subject: [PATCH] chore: move umd plugin to separate repo (#2814) --- e2e/cases/esm-exports/test.mjs | 2 - e2e/cases/umd/basic/index.test.ts | 20 -- e2e/cases/umd/basic/rsbuild.config.ts | 16 -- e2e/cases/umd/basic/src/index.html | 10 - e2e/cases/umd/basic/src/index.js | 3 - e2e/cases/umd/export-array/index.test.ts | 22 -- e2e/cases/umd/export-array/rsbuild.config.ts | 17 -- e2e/cases/umd/export-array/src/index.html | 10 - e2e/cases/umd/export-array/src/index.js | 5 - e2e/cases/umd/export/index.test.ts | 22 -- e2e/cases/umd/export/rsbuild.config.ts | 17 -- e2e/cases/umd/export/src/index.html | 10 - e2e/cases/umd/export/src/index.js | 3 - e2e/cases/umd/split-chunks/index.test.ts | 25 --- e2e/cases/umd/split-chunks/rsbuild.config.ts | 23 --- e2e/cases/umd/split-chunks/src/index.html | 10 - e2e/cases/umd/split-chunks/src/index.js | 8 - e2e/package.json | 1 - packages/plugin-umd/LICENSE | 21 -- packages/plugin-umd/README.md | 19 -- packages/plugin-umd/modern.config.ts | 3 - packages/plugin-umd/package.json | 41 ---- packages/plugin-umd/src/index.ts | 64 ------ packages/plugin-umd/tsconfig.json | 16 -- pnpm-lock.yaml | 12 -- website/docs/en/guide/start/npm-packages.mdx | 10 - website/docs/en/plugins/list/_meta.json | 3 +- website/docs/en/plugins/list/index.mdx | 2 +- website/docs/en/plugins/list/plugin-umd.mdx | 200 ------------------- website/docs/zh/guide/start/npm-packages.mdx | 10 - website/docs/zh/plugins/list/_meta.json | 3 +- website/docs/zh/plugins/list/index.mdx | 2 +- website/docs/zh/plugins/list/plugin-umd.mdx | 200 ------------------- 33 files changed, 4 insertions(+), 826 deletions(-) delete mode 100644 e2e/cases/umd/basic/index.test.ts delete mode 100644 e2e/cases/umd/basic/rsbuild.config.ts delete mode 100644 e2e/cases/umd/basic/src/index.html delete mode 100644 e2e/cases/umd/basic/src/index.js delete mode 100644 e2e/cases/umd/export-array/index.test.ts delete mode 100644 e2e/cases/umd/export-array/rsbuild.config.ts delete mode 100644 e2e/cases/umd/export-array/src/index.html delete mode 100644 e2e/cases/umd/export-array/src/index.js delete mode 100644 e2e/cases/umd/export/index.test.ts delete mode 100644 e2e/cases/umd/export/rsbuild.config.ts delete mode 100644 e2e/cases/umd/export/src/index.html delete mode 100644 e2e/cases/umd/export/src/index.js delete mode 100644 e2e/cases/umd/split-chunks/index.test.ts delete mode 100644 e2e/cases/umd/split-chunks/rsbuild.config.ts delete mode 100644 e2e/cases/umd/split-chunks/src/index.html delete mode 100644 e2e/cases/umd/split-chunks/src/index.js delete mode 100644 packages/plugin-umd/LICENSE delete mode 100644 packages/plugin-umd/README.md delete mode 100644 packages/plugin-umd/modern.config.ts delete mode 100644 packages/plugin-umd/package.json delete mode 100644 packages/plugin-umd/src/index.ts delete mode 100644 packages/plugin-umd/tsconfig.json delete mode 100644 website/docs/en/plugins/list/plugin-umd.mdx delete mode 100644 website/docs/zh/plugins/list/plugin-umd.mdx diff --git a/e2e/cases/esm-exports/test.mjs b/e2e/cases/esm-exports/test.mjs index 37b7c6638b..148f398548 100644 --- a/e2e/cases/esm-exports/test.mjs +++ b/e2e/cases/esm-exports/test.mjs @@ -18,7 +18,6 @@ import { pluginSvelte } from '@rsbuild/plugin-svelte'; import { pluginSvgr } from '@rsbuild/plugin-svgr'; import { pluginSwc } from '@rsbuild/plugin-swc'; import { pluginTypeCheck } from '@rsbuild/plugin-type-check'; -import { pluginUmd } from '@rsbuild/plugin-umd'; import { pluginVue } from '@rsbuild/plugin-vue'; import { pluginVueJsx } from '@rsbuild/plugin-vue-jsx'; import { pluginVue2 } from '@rsbuild/plugin-vue2'; @@ -42,7 +41,6 @@ export default { pluginLightningcss, pluginSourceBuild, pluginStyledComponents, - pluginUmd, pluginTypeCheck, pluginVue, pluginVue2, diff --git a/e2e/cases/umd/basic/index.test.ts b/e2e/cases/umd/basic/index.test.ts deleted file mode 100644 index 6c8defb892..0000000000 --- a/e2e/cases/umd/basic/index.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { build, gotoPage } from '@e2e/helper'; -import { expect, test } from '@playwright/test'; - -test('should generate UMD bundle correctly', async ({ page }) => { - const rsbuild = await build({ - cwd: __dirname, - runServer: true, - }); - - // Browser env - await gotoPage(page, rsbuild); - const test = page.locator('#test'); - await expect(test).toHaveText('2'); - - // Node.js env - const { double } = require('./dist/index.js'); - expect(double(1)).toEqual(2); - - await rsbuild.close(); -}); diff --git a/e2e/cases/umd/basic/rsbuild.config.ts b/e2e/cases/umd/basic/rsbuild.config.ts deleted file mode 100644 index 7a0b092bf1..0000000000 --- a/e2e/cases/umd/basic/rsbuild.config.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { defineConfig } from '@rsbuild/core'; -import { pluginUmd } from '@rsbuild/plugin-umd'; - -export default defineConfig({ - plugins: [ - pluginUmd({ - name: 'myLib', - }), - ], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, -}); diff --git a/e2e/cases/umd/basic/src/index.html b/e2e/cases/umd/basic/src/index.html deleted file mode 100644 index f645bfe70e..0000000000 --- a/e2e/cases/umd/basic/src/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - -
- - - diff --git a/e2e/cases/umd/basic/src/index.js b/e2e/cases/umd/basic/src/index.js deleted file mode 100644 index ba18b0e091..0000000000 --- a/e2e/cases/umd/basic/src/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export function double(a) { - return a * 2; -} diff --git a/e2e/cases/umd/export-array/index.test.ts b/e2e/cases/umd/export-array/index.test.ts deleted file mode 100644 index 5749714522..0000000000 --- a/e2e/cases/umd/export-array/index.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { build, gotoPage } from '@e2e/helper'; -import { expect, test } from '@playwright/test'; - -test('should generate UMD bundle with default export correctly', async ({ - page, -}) => { - const rsbuild = await build({ - cwd: __dirname, - runServer: true, - }); - - // Browser env - await gotoPage(page, rsbuild); - const test = page.locator('#test'); - await expect(test).toHaveText('2'); - - // Node.js env - const double = require('./dist/index.js'); - expect(double(1)).toEqual(2); - - await rsbuild.close(); -}); diff --git a/e2e/cases/umd/export-array/rsbuild.config.ts b/e2e/cases/umd/export-array/rsbuild.config.ts deleted file mode 100644 index 9f90b2a460..0000000000 --- a/e2e/cases/umd/export-array/rsbuild.config.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { defineConfig } from '@rsbuild/core'; -import { pluginUmd } from '@rsbuild/plugin-umd'; - -export default defineConfig({ - plugins: [ - pluginUmd({ - name: 'myLib', - export: ['default', 'subModule'], - }), - ], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, -}); diff --git a/e2e/cases/umd/export-array/src/index.html b/e2e/cases/umd/export-array/src/index.html deleted file mode 100644 index debf14f4f2..0000000000 --- a/e2e/cases/umd/export-array/src/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - -
- - - diff --git a/e2e/cases/umd/export-array/src/index.js b/e2e/cases/umd/export-array/src/index.js deleted file mode 100644 index aae12998d9..0000000000 --- a/e2e/cases/umd/export-array/src/index.js +++ /dev/null @@ -1,5 +0,0 @@ -export default { - subModule(a) { - return a * 2; - }, -}; diff --git a/e2e/cases/umd/export/index.test.ts b/e2e/cases/umd/export/index.test.ts deleted file mode 100644 index 5749714522..0000000000 --- a/e2e/cases/umd/export/index.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { build, gotoPage } from '@e2e/helper'; -import { expect, test } from '@playwright/test'; - -test('should generate UMD bundle with default export correctly', async ({ - page, -}) => { - const rsbuild = await build({ - cwd: __dirname, - runServer: true, - }); - - // Browser env - await gotoPage(page, rsbuild); - const test = page.locator('#test'); - await expect(test).toHaveText('2'); - - // Node.js env - const double = require('./dist/index.js'); - expect(double(1)).toEqual(2); - - await rsbuild.close(); -}); diff --git a/e2e/cases/umd/export/rsbuild.config.ts b/e2e/cases/umd/export/rsbuild.config.ts deleted file mode 100644 index 7ab7bdb8a6..0000000000 --- a/e2e/cases/umd/export/rsbuild.config.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { defineConfig } from '@rsbuild/core'; -import { pluginUmd } from '@rsbuild/plugin-umd'; - -export default defineConfig({ - plugins: [ - pluginUmd({ - name: 'myLib', - export: 'default', - }), - ], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, -}); diff --git a/e2e/cases/umd/export/src/index.html b/e2e/cases/umd/export/src/index.html deleted file mode 100644 index debf14f4f2..0000000000 --- a/e2e/cases/umd/export/src/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - -
- - - diff --git a/e2e/cases/umd/export/src/index.js b/e2e/cases/umd/export/src/index.js deleted file mode 100644 index 75d0a38ae8..0000000000 --- a/e2e/cases/umd/export/src/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function double(a) { - return a * 2; -} diff --git a/e2e/cases/umd/split-chunks/index.test.ts b/e2e/cases/umd/split-chunks/index.test.ts deleted file mode 100644 index 03e3694ec2..0000000000 --- a/e2e/cases/umd/split-chunks/index.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { build, gotoPage } from '@e2e/helper'; -import { expect, test } from '@playwright/test'; - -test('should allow to configure split chunks with UMD plugin', async ({ - page, -}) => { - const rsbuild = await build({ - cwd: __dirname, - runServer: true, - }); - - const files = await rsbuild.unwrapOutputJSON(); - const indexBundle = - files[Object.keys(files).find((file) => file.endsWith('index.js'))!]; - const reactBundle = - files[Object.keys(files).find((file) => file.endsWith('lib-react.js'))!]; - expect(indexBundle).toBeTruthy(); - expect(reactBundle).toBeTruthy(); - - await gotoPage(page, rsbuild); - const test = page.locator('#test'); - await expect(test).toHaveText('2'); - - await rsbuild.close(); -}); diff --git a/e2e/cases/umd/split-chunks/rsbuild.config.ts b/e2e/cases/umd/split-chunks/rsbuild.config.ts deleted file mode 100644 index d1b2799fff..0000000000 --- a/e2e/cases/umd/split-chunks/rsbuild.config.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { defineConfig } from '@rsbuild/core'; -import { pluginReact } from '@rsbuild/plugin-react'; -import { pluginUmd } from '@rsbuild/plugin-umd'; - -export default defineConfig({ - plugins: [ - pluginUmd({ - name: 'myLib', - }), - pluginReact(), - ], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, - performance: { - chunkSplit: { - strategy: 'split-by-experience', - }, - }, -}); diff --git a/e2e/cases/umd/split-chunks/src/index.html b/e2e/cases/umd/split-chunks/src/index.html deleted file mode 100644 index f645bfe70e..0000000000 --- a/e2e/cases/umd/split-chunks/src/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - -
- - - diff --git a/e2e/cases/umd/split-chunks/src/index.js b/e2e/cases/umd/split-chunks/src/index.js deleted file mode 100644 index 014e42b0bf..0000000000 --- a/e2e/cases/umd/split-chunks/src/index.js +++ /dev/null @@ -1,8 +0,0 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; - -export { React, ReactDOM }; - -export function double(a) { - return a * 2; -} diff --git a/e2e/package.json b/e2e/package.json index caa5318c00..df6fa15c65 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -42,7 +42,6 @@ "@rsbuild/plugin-swc": "workspace:*", "@rsbuild/plugin-type-check": "workspace:*", "@rsbuild/plugin-typed-css-modules": "workspace:*", - "@rsbuild/plugin-umd": "workspace:*", "@rsbuild/plugin-vue": "workspace:*", "@rsbuild/plugin-vue-jsx": "workspace:*", "@rsbuild/plugin-vue2": "workspace:*", diff --git a/packages/plugin-umd/LICENSE b/packages/plugin-umd/LICENSE deleted file mode 100644 index 82d38c25b5..0000000000 --- a/packages/plugin-umd/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2023-present Bytedance, Inc. and its affiliates. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/packages/plugin-umd/README.md b/packages/plugin-umd/README.md deleted file mode 100644 index 92531b7215..0000000000 --- a/packages/plugin-umd/README.md +++ /dev/null @@ -1,19 +0,0 @@ -

- Rsbuild Logo -

- -# Rsbuild - -The Rspack-based build tool. It's fast, out-of-the-box and extensible. - -## Documentation - -https://rsbuild.dev/ - -## Contributing - -Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md). - -## License - -Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE). diff --git a/packages/plugin-umd/modern.config.ts b/packages/plugin-umd/modern.config.ts deleted file mode 100644 index 006abf4e4a..0000000000 --- a/packages/plugin-umd/modern.config.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { configForDualPackage } from '@rsbuild/config/modern.config.ts'; - -export default configForDualPackage; diff --git a/packages/plugin-umd/package.json b/packages/plugin-umd/package.json deleted file mode 100644 index 8926ea2834..0000000000 --- a/packages/plugin-umd/package.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "name": "@rsbuild/plugin-umd", - "version": "1.0.0-alpha.4", - "description": "UMD plugin for Rsbuild", - "homepage": "https://rsbuild.dev", - "repository": { - "type": "git", - "url": "https://github.com/web-infra-dev/rsbuild", - "directory": "packages/plugin-umd" - }, - "license": "MIT", - "type": "module", - "exports": { - ".": { - "types": "./dist/index.d.ts", - "import": "./dist/index.js", - "require": "./dist/index.cjs" - } - }, - "main": "./dist/index.cjs", - "types": "./dist/index.d.ts", - "files": [ - "dist" - ], - "scripts": { - "build": "modern build", - "dev": "modern build --watch" - }, - "devDependencies": { - "@rsbuild/core": "workspace:*", - "typescript": "^5.5.2" - }, - "peerDependencies": { - "@rsbuild/core": "workspace:^1.0.0-alpha.4" - }, - "publishConfig": { - "access": "public", - "provenance": true, - "registry": "https://registry.npmjs.org/" - } -} diff --git a/packages/plugin-umd/src/index.ts b/packages/plugin-umd/src/index.ts deleted file mode 100644 index b0c35c3bd9..0000000000 --- a/packages/plugin-umd/src/index.ts +++ /dev/null @@ -1,64 +0,0 @@ -import type { RsbuildPlugin } from '@rsbuild/core'; - -export type PluginUmdOptions = { - /** - * `name` is a required field used to specify the name of the UMD library. - */ - name: string; - /** - * Specifies which export to use as the content of the UMD library. - */ - export?: string | string[]; -}; - -export const PLUGIN_UMD_NAME = 'rsbuild:umd'; - -export const pluginUmd = (options: PluginUmdOptions): RsbuildPlugin => ({ - name: PLUGIN_UMD_NAME, - - setup(api) { - api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => { - const userConfig = api.getRsbuildConfig('original'); - - return mergeRsbuildConfig(config, { - output: { - distPath: { - js: userConfig.output?.distPath?.js ?? '', - css: userConfig.output?.distPath?.css ?? '', - }, - filenameHash: userConfig.output?.filenameHash ?? false, - }, - html: { - // allows to test the UMD bundle in the browser - scriptLoading: userConfig.html?.scriptLoading ?? 'blocking', - }, - tools: { - htmlPlugin: - userConfig.tools?.htmlPlugin ?? - (process.env.NODE_ENV === 'production' ? false : undefined), - }, - performance: { - chunkSplit: { - // UMD outputs are usually distributed via a single - - -``` - -Then, specify the `template` in `rsbuild.config.ts`: - -```ts title="rsbuild.config.ts" -export default { - plugins: [pluginUmd({ name: 'myLib' })], - html: { - template: './src/index.html', - }, -}; -``` - -Finally, run `npx rsbuild dev` to start. - -## HTML Generation - -After using the UMD plugin, HTML files are not generated by default when running production builds. This is because UMD bundles are usually distributed as a library and do not rely on HTML files. - -If you need to generate HTML files, you can set [tools.htmlPlugin](/config/tools/html-plugin) to `true`: - -```ts title="rsbuild.config.ts" -export default { - plugins: [pluginUmd({ name: 'myLib' })], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, -}; -``` - -## Code Splitting - -The UMD plugin overrides Rsbuild's default chunk splitting rules by setting `performance.chunkSplit.strategy` to `all-in-one` to output a single bundle. This is because UMD outputs are often distributed via CDN and allow direct referencing via ` - - -``` - -然后在 `rsbuild.config.ts` 中指定 `template`: - -```ts title="rsbuild.config.ts" -export default { - plugins: [pluginUmd({ name: 'myLib' })], - html: { - template: './src/index.html', - }, -}; -``` - -最后运行 `npx rsbuild dev` 即可。 - -## HTML 生成 - -在使用 UMD 插件后,执行生产构建时默认不会生成 HTML 文件,这是因为 UMD 产物通常以 library 的形式进行分发,不依赖 HTML 文件。 - -如果你需要生成 HTML 文件,可以将 [tools.htmlPlugin](/config/tools/html-plugin) 设置为 `true`: - -```ts title="rsbuild.config.ts" -export default { - plugins: [pluginUmd({ name: 'myLib' })], - html: { - template: './src/index.html', - }, - tools: { - htmlPlugin: true, - }, -}; -``` - -## 代码拆分 - -UMD 插件覆盖了 Rsbuild 默认的 chunk 拆包规则,它会将 `performance.chunkSplit.strategy` 设置为 `all-in-one`,以输出单个 bundle 产物。这是因为 UMD 产物经常会通过 CDN 来分发,并允许通过 `