Skip to content

Commit

Permalink
fix: use modern-module to output tree-shakable artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 24, 2024
1 parent 09c5549 commit ec674f2
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 110 deletions.
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Rslib Contributing Guide

## Setup the Environment

TODO

## Caveats

The project is still under development, so it possible dependents on unstable Rsbuild / Rspack versions.

Current unstable versions are:

| Package | Version | Link |
| ------------ | ----------------------------------- | ------------------------------------------------------- |
| @rspack/core | 0.7.5-canary-b89dbb3-20240620182932 | [PR](https://github.com/web-infra-dev/rspack/pull/6877) |
5 changes: 4 additions & 1 deletion e2e/cases/define/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type RslibConfig, build } from '@rslib/core';
import { expect, test } from 'vitest';
import { globContentJSON } from '#helper';

test.fails('define', async () => {
test('define', async () => {
delete process.env.NODE_ENV;

const rslibConfig: RslibConfig = {
Expand All @@ -29,6 +29,9 @@ test.fails('define', async () => {
entry: {
main: join(__dirname, './js/src/index.js'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
};

Expand Down
4 changes: 4 additions & 0 deletions e2e/cases/define/js/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { a } from './value';

export { a };

console.info(VERSION);
4 changes: 4 additions & 0 deletions e2e/cases/define/js/src/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO: Rspack only can handle exports from concatenated modules now.
// Adding this file to let the compile pass as the moment.
// Remove this file when Rspack could export from single module.`
export const a = 1;
2 changes: 2 additions & 0 deletions examples/basic/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const hello: string = 'Hello';
export const world: string = 'World';
6 changes: 3 additions & 3 deletions examples/basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const message: string = 'Hello, Rslib';

console.log(message);
import { hello, world } from './constants';
const message: string = `${hello}, ${world}!`;
export { hello, world, message as default };
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
"test:artifact": "vitest run --project artifact",
"test:e2e": "cd e2e && pnpm run test",
"test:unit": "vitest run --project unit"
"test:unit": "vitest run --project unit",
"watch": "pnpm build --watch"
},
"simple-git-hooks": {
"pre-commit": "npx nano-staged"
Expand Down Expand Up @@ -42,5 +43,10 @@
"engines": {
"node": ">=18.0.0",
"pnpm": ">=9.0.0"
},
"pnpm": {
"overrides": {
"@rspack/core": "0.7.5-canary-b89dbb3-20240620182932"
}
}
}
81 changes: 44 additions & 37 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,10 @@ export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
return defineRsbuildConfig({
tools: {
htmlPlugin: false,
rspack: {
output: {
module: true,
library: {
type: 'module',
},
},
optimization: {
concatenateModules: true,
},
experiments: {
outputModule: true,
},
},
},
output: {
filenameHash: false,
// TODO: easy to development at the moment
minify: false,
distPath: {
js: './',
Expand All @@ -104,36 +91,54 @@ export function convertLibConfigToRsbuildConfig(
libConfig: LibConfig,
rsbuildConfig: RsbuildConfig,
): RsbuildConfig {
// TODO: Configuration mapping needs to be implemented according to features added in the future
if (libConfig.format === 'cjs') {
mergeRsbuildConfig(rsbuildConfig, {
tools: {
rspack: {
output: {
library: {
type: 'commonjs',
switch (libConfig.format) {
case 'esm':
return mergeRsbuildConfig(rsbuildConfig, {
tools: {
rspack: {
output: {
module: true,
iife: false,
library: {
type: 'modern-module',
},
},
optimization: {
concatenateModules: true,
},
experiments: {
outputModule: true,
},
},
},
},
});
}

if (libConfig.format === 'esm') {
mergeRsbuildConfig(rsbuildConfig, {
tools: {
rspack: {
output: {
library: {
type: 'module',
});
case 'cjs':
return mergeRsbuildConfig(rsbuildConfig, {
tools: {
rspack: {
output: {
library: {
type: 'commonjs',
},
},
},
},
},
});
});
case 'umd':
return mergeRsbuildConfig(rsbuildConfig, {
tools: {
rspack: {
output: {
library: {
type: 'umd',
},
},
},
},
});
default:
return rsbuildConfig;
}

return rsbuildConfig;
}

export async function composeCreateRsbuildConfig(
Expand All @@ -151,6 +156,8 @@ export async function composeCreateRsbuildConfig(
const composedRsbuildConfig = libConfigsArray.map((libConfig: LibConfig) => {
const { format, ...overrideRsbuildConfig } = libConfig;

// Merge order matters, keep `internalRsbuildConfig` at the last position
// to ensure that the internal config is not overridden by the user's config.
const mergedRsbuildConfig = mergeRsbuildConfig(
sharedRsbuildConfig,
overrideRsbuildConfig,
Expand Down
21 changes: 4 additions & 17 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"concatenateModules": true,
},
"output": {
"iife": false,
"library": {
"type": "module",
"type": "modern-module",
},
"module": true,
},
Expand Down Expand Up @@ -57,17 +58,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"tools": {
"htmlPlugin": false,
"rspack": {
"experiments": {
"outputModule": true,
},
"optimization": {
"concatenateModules": true,
},
"output": {
"library": {
"type": "module",
"type": "commonjs",
},
"module": true,
},
},
},
Expand All @@ -90,17 +84,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"tools": {
"htmlPlugin": false,
"rspack": {
"experiments": {
"outputModule": true,
},
"optimization": {
"concatenateModules": true,
},
"output": {
"library": {
"type": "module",
"type": "umd",
},
"module": true,
},
},
},
Expand Down
Loading

0 comments on commit ec674f2

Please sign in to comment.