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

feat: support browser option for bundle #657

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/khaki-bananas-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/pkg': minor
---

feat: support `browser` option for bundle mode
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ packages/*/es/
/packages/**/es2017
/packages/**/cjs
/packages/**/esm
/packages/**/dist
/packages/pkg/tests/fixtures/**/build.config.for-test.mts
**/node_modules
/.pnpm-debug.log
Expand Down
2 changes: 1 addition & 1 deletion packages/pkg/src/helpers/getBabelOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getBabelOptions(
jsxRuntime === 'automatic'
? {
runtime: jsxRuntime,
importSource: JSX_RUNTIME_SOURCE
importSource: JSX_RUNTIME_SOURCE,
}
: {
pragma,
Expand Down
1 change: 1 addition & 0 deletions packages/pkg/src/helpers/getRollupOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export function getRollupOptions(
'.ts', '.jsx', '.tsx', '.mts', '.cjs', '.cts', // @ice/pkg default extensions
...(taskConfig.extensions || []),
],
browser: taskConfig.browser,
}),
commonjs({ // To convert commonjs to import, make it compatible with rollup to bundle
extensions: [
Expand Down
5 changes: 5 additions & 0 deletions packages/pkg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export interface BundleUserConfig {
* Weather or not compile the dependencies in node_modules.
*/
compileDependencies?: boolean | Array<RegExp | string>;

/**
* Resolve node module by prefer using `browser` field in package.json.
*/
browser?: boolean;
}

export interface UserConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/pkg/tests/babelPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('transform', () => {
const ret = babelPlugin.transform('<div x-if={false}></div>', 'src/test.tsx');
expect(cleanCode(ret.code)).toBe(
cleanCode(`import { createCondition as __create_condition__ } from "babel-runtime-jsx-plus";
import { jsx as _jsx } from "react/jsx-runtime";
__create_condition__([[() => false, () => /*#__PURE__*/_jsx("div", {})]]);`),
import { jsx as _jsx } from "@ice/jsx-runtime/jsx-runtime";
__create_condition__([[() => false, () => _jsx("div", {})]]);`),
);
});

Expand Down
9 changes: 9 additions & 0 deletions packages/pkg/tests/fixtures/bundle-browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@ice/pkg-tests-fixtures-bundle-browser",
"version": "0.0.0",
"private": true,
"dependencies": {
"@ice/pkg": "workspace:*",
"@ice/pkg-tests-fixtures-mock-entry-package": "workspace:*"
}
}
2 changes: 2 additions & 0 deletions packages/pkg/tests/fixtures/bundle-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { id } from '@ice/pkg-tests-fixtures-mock-entry-package'
console.log(id)
1 change: 1 addition & 0 deletions packages/pkg/tests/fixtures/mock-entry-package/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const id = 'browser'
1 change: 1 addition & 0 deletions packages/pkg/tests/fixtures/mock-entry-package/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const id: string
1 change: 1 addition & 0 deletions packages/pkg/tests/fixtures/mock-entry-package/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const id = 'main'
1 change: 1 addition & 0 deletions packages/pkg/tests/fixtures/mock-entry-package/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const id = 'module'
12 changes: 12 additions & 0 deletions packages/pkg/tests/fixtures/mock-entry-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@ice/pkg-tests-fixtures-mock-entry-package",
"version": "0.0.0",
"private": true,
"main": "./main.js",
"module": "./module.js",
"browser": "./browser.js",
"types": "./index.d.ts",
"description": "模块入口测试",
"dependencies": {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Vitest Snapshot v1

exports[`Run config default > cjs structure 1`] = `null`;

exports[`Run config default > dist structure 1`] = `
{
"files": [
{
"name": "index.cjs.es5.production.js",
},
{
"name": "index.esm.es5.production.js",
},
],
"name": "dist",
}
`;

exports[`Run config default > es2017 structure 1`] = `null`;

exports[`Run config default > esm structure 1`] = `null`;

exports[`Run config default > file content dist/index.cjs.es5.production.js 1`] = `
"\\"use strict\\";console.log(\\"module\\");
"
`;

exports[`Run config default > file content dist/index.esm.es5.production.js 1`] = `
"console.log(\\"module\\");
"
`;

exports[`Run config enable-browser > cjs structure 1`] = `null`;

exports[`Run config enable-browser > dist structure 1`] = `
{
"files": [
{
"name": "index.cjs.es5.production.js",
},
{
"name": "index.esm.es5.production.js",
},
],
"name": "dist",
}
`;

exports[`Run config enable-browser > es2017 structure 1`] = `null`;

exports[`Run config enable-browser > esm structure 1`] = `null`;

exports[`Run config enable-browser > file content dist/index.cjs.es5.production.js 1`] = `
"\\"use strict\\";console.log(\\"browser\\");
"
`;

exports[`Run config enable-browser > file content dist/index.esm.es5.production.js 1`] = `
"console.log(\\"browser\\");
"
`;
20 changes: 20 additions & 0 deletions packages/pkg/tests/projects/bundle-browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { runProjectTest } from "./helper";

runProjectTest('bundle-browser', [{
name: 'default',
config: {
transform: { formats: [] },
bundle: {
formats: ['esm', 'cjs']
}
}
}, {
name: 'enable-browser',
config: {
transform: { formats: [] },
bundle: {
formats: ['esm', 'cjs'],
browser: true
}
}
}])
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions website/docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,25 @@ export default defineConfig({
});
```

#### browser

+ 类型: `boolean`
+ 默认值: `false`
+ 可用版本: `1.6.0`

配置解析 Node 模块的时候,是否优先读取 package.json 中的 `browser` 字段。
如果你的模块**只运行**在浏览器端,可以开启此选项只 bundle 浏览器相关的代码。

```ts title="build.config.mts"
import { defineConfig } from '@ice/pkg';

export default defineConfig({
bundle: {
browser: true,
},
});
```

#### development

:::caution
Expand Down
Loading