Skip to content

Commit

Permalink
feat: implement syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 12, 2024
1 parent 3ad4ae9 commit e2abdd1
Show file tree
Hide file tree
Showing 31 changed files with 1,172 additions and 1,891 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
dist
compiled
doc_build
__snapshots__
LICENSE
scripts/dictionary.txt
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ The project is still under development, so it possible dependents on unstable Rs

Current unstable versions are:

| Package | Version | Link |
| ------------ | ------------------------------------------------------- | ------------------------------------------------------- |
| @rspack/core | @rspack/core-canary@1.0.0-canary-26460a2-20240710092718 | [PR](https://github.com/web-infra-dev/rspack/pull/7102) |
<!-- | Package | Version | Link |
| ------------ | ------------------------------------------------------- | --------------------------------------------------------- |
| @rspack/core | @rspack/[email protected]0b368b6-20240704171208 | [PR](https://github.com/webpack/webpack/pull/11751/files) | -->
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"javascript": {
"formatter": {
"quoteStyle": "single"
"enabled": false
}
},
"json": {
Expand Down
4 changes: 2 additions & 2 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| jsx | ⚪️ | |
| metafile | ⚪️ | |
| minify | ⚪️ | |
| platform | ⚪️ | |
| platform | 🟢 | |
| redirect | ⚪️ | |
| resolve | ⚪️ | |
| shims | ⚪️ | |
Expand All @@ -37,7 +37,7 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| sourceMap | ⚪️ | |
| splitting | ⚪️ | |
| style | ⚪️ | |
| target | ⚪️ | |
| target | 🟢 | |
| transformImport | ⚪️ | |
| transformLodash | ⚪️ | |
| tsconfig | ⚪️ | |
Expand Down
4 changes: 2 additions & 2 deletions e2e/cases/alias/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`alias should work 1`] = `
exports[`source.alias 1`] = `
"
;// CONCATENATED MODULE: ./e2e/cases/alias/src/a.ts
const a = 'hello world';
Expand All @@ -13,7 +13,7 @@ export { a };
"
`;

exports[`alias should work 2`] = `
exports[`source.alias 2`] = `
"(() => { // webpackBootstrap
"use strict";
var __webpack_exports__ = {};
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('alias should work', async () => {
test('source.alias', async () => {
const fixturePath = __dirname;
const { contents } = await buildAndGetEntryJsResults(fixturePath);

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/define/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('define should work', async () => {
test('source.define', async () => {
const fixturePath = __dirname;
const { contents } = await buildAndGetEntryJsResults(fixturePath);

Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/externals/browser/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'node:path';
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('should fail when platform is not "node"', async () => {
test('should fail to build when `output.target` is not "node"', async () => {
const fixturePath = join(__dirname);
const build = buildAndGetEntryJsResults(fixturePath);
await expect(build).rejects.toThrowError('Rspack build failed!');
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/externals/node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'node:path';
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('auto externalize Node.js built-in modules when platform is "node"', async () => {
test('auto externalize Node.js built-in modules when `output.target` is "node"', async () => {
const fixturePath = join(__dirname);
const { contents } = await buildAndGetEntryJsResults(fixturePath);

Expand Down
48 changes: 48 additions & 0 deletions e2e/cases/syntax/config/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should downgrade class private method by default 1`] = `
"
;// CONCATENATED MODULE: ./node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/esm/_class_private_method_get.js
function _class_private_method_get(receiver, privateSet, fn) {
if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");
return fn;
}
;// CONCATENATED MODULE: ./node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/esm/_check_private_redeclaration.js
function _check_private_redeclaration(obj, privateCollection) {
if (privateCollection.has(obj)) {
throw new TypeError("Cannot initialize the same private elements twice on an object");
}
}
;// CONCATENATED MODULE: ./node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/esm/_class_private_method_init.js
function _class_private_method_init(obj, privateSet) {
_check_private_redeclaration(obj, privateSet);
privateSet.add(obj);
}
;// CONCATENATED MODULE: ./e2e/cases/syntax/config/src/foo.ts
var _bar = new WeakSet();
class Foo {
constructor(){
_class_private_method_init(this, _bar);
_class_private_method_get(this, _bar, bar).call(this);
}
}
function bar() {}
/*#__PURE__*/
;// CONCATENATED MODULE: ./e2e/cases/syntax/config/src/index.ts
export { Foo };
"
`;
12 changes: 12 additions & 0 deletions e2e/cases/syntax/config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('should downgrade class private method by default', async () => {
const fixturePath = __dirname;
const { contents } = await buildAndGetEntryJsResults(fixturePath);

expect(contents.esm).toMatchSnapshot();
expect(contents.esm).not.toContain('#bar');

expect(contents.cjs).toContain('#bar');
});
23 changes: 23 additions & 0 deletions e2e/cases/syntax/config/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
output: {
syntax: 'es2015',
},
}),
generateBundleCjsConfig(__dirname, {
output: {
syntax: ['node 20'],
},
}),
],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
},
});
9 changes: 9 additions & 0 deletions e2e/cases/syntax/config/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {
constructor() {
this.#bar();
}

#bar() {}
}

export { Foo };
1 change: 1 addition & 0 deletions e2e/cases/syntax/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Foo } from './foo';
10 changes: 10 additions & 0 deletions e2e/cases/syntax/config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@src/*": ["./src/*"]
}
},
"include": ["src"]
}
19 changes: 19 additions & 0 deletions e2e/cases/syntax/default/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should downgrade class private method by default 1`] = `
"
;// CONCATENATED MODULE: ./e2e/cases/syntax/default/src/foo.ts
class Foo {
constructor(){
this.#bar();
}
#bar() {}
}
;// CONCATENATED MODULE: ./e2e/cases/syntax/default/src/index.ts
export { Foo };
"
`;
12 changes: 12 additions & 0 deletions e2e/cases/syntax/default/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, test } from 'vitest';
import { buildAndGetEntryJsResults } from '#shared';

test('should downgrade class private method by default', async () => {
const fixturePath = __dirname;
const { contents } = await buildAndGetEntryJsResults(fixturePath);

expect(contents.esm).toMatchSnapshot();
expect(contents.esm).toContain('#bar');

expect(contents.cjs).toContain('#bar');
});
12 changes: 12 additions & 0 deletions e2e/cases/syntax/default/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
},
});
9 changes: 9 additions & 0 deletions e2e/cases/syntax/default/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {
constructor() {
this.#bar();
}

#bar() {}
}

export { Foo };
1 change: 1 addition & 0 deletions e2e/cases/syntax/default/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Foo } from './foo';
10 changes: 10 additions & 0 deletions e2e/cases/syntax/default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@src/*": ["./src/*"]
}
},
"include": ["src"]
}
4 changes: 2 additions & 2 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
},
"devDependencies": {
"@playwright/test": "1.43.1",
"@rsbuild/core": "1.0.0-alpha.3",
"@rsbuild/core": "1.0.0-alpha.9",
"@rslib/core": "workspace:*",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
"@types/node": "18.x",
"@types/node": "~18.19.39",
"@types/react": "^18.3.3",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0"
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build:examples": "cross-env NX_DAEMON=false nx run-many -t build --projects @examples/* --parallel=10",
"check-dependency-version": "check-dependency-version-consistency .",
"check-spell": "npx cspell",
"lint": "biome check . --diagnostic-level=warn && pnpm run check-spell",
"lint": "biome check . --diagnostic-level=warn && prettier --check ./**/*.* && pnpm run check-spell",
"prebundle": "nx run-many -t prebundle",
"prepare": "pnpm run build && simple-git-hooks",
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
Expand All @@ -28,19 +28,19 @@
"package.json": "pnpm run check-dependency-version"
},
"devDependencies": {
"@biomejs/biome": "^1.8.1",
"@modern-js/module-tools": "^2.53.0",
"@biomejs/biome": "^1.8.3",
"@modern-js/module-tools": "^2.55.0",
"@types/fs-extra": "^11.0.4",
"check-dependency-version-consistency": "^4.1.0",
"cross-env": "^7.0.3",
"cspell-ban-words": "^0.0.3",
"fs-extra": "^11.2.0",
"nano-staged": "^0.8.0",
"nx": "^19.3.0",
"nx": "^19.4.2",
"prettier": "^3.3.2",
"prettier-plugin-packagejson": "^2.5.0",
"simple-git-hooks": "^2.11.1",
"typescript": "^5.4.5",
"typescript": "^5.5.3",
"vitest": "^1.6.0"
},
"packageManager": "[email protected]",
Expand All @@ -50,7 +50,7 @@
},
"pnpm": {
"overrides": {
"@rspack/core": "npm:@rspack/core-canary@1.0.0-canary-26460a2-20240710092718"
"@rspack/core": "1.0.0-alpha.3"
}
}
}
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prebundle": "prebundle"
},
"dependencies": {
"@rsbuild/core": "1.0.0-alpha.3"
"@rsbuild/core": "1.0.0-alpha.9"
},
"devDependencies": {
"@rslib/tsconfig": "workspace:*",
Expand All @@ -48,7 +48,7 @@
"picocolors": "1.0.1",
"prebundle": "1.1.0",
"rslog": "^1.2.2",
"typescript": "^5.4.5"
"typescript": "^5.5.3"
},
"engines": {
"node": ">=16.0.0"
Expand Down
26 changes: 24 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getDefaultExtension } from './utils/extension';
import { color } from './utils/helper';
import { nodeBuiltInModules } from './utils/helper';
import { logger } from './utils/logger';
import { transformSyntaxToBrowserslist } from './utils/syntax';

/**
* This function helps you to autocomplete configuration types.
Expand Down Expand Up @@ -130,6 +131,7 @@ const getDefaultFormatConfig = (format: Format): RsbuildConfig => {
rspack: {
externalsType: 'commonjs',
output: {
chunkFormat: 'commonjs',
library: {
type: 'commonjs',
},
Expand Down Expand Up @@ -175,8 +177,28 @@ const getDefaultAutoExtensionConfig = (
};
};

const getDefaultSyntax = (_syntax?: Syntax): RsbuildConfig => {
return {};
const getDefaultSyntax = (syntax?: Syntax): RsbuildConfig => {
// Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
return syntax === undefined
? {
tools: {
rspack: {
// The highest is 2022 in Rspack
target: 'es2022',
},
swc(config) {
config.jsc ??= {};
config.jsc.target = 'esnext';
delete config.env;
return config;
},
},
}
: {
output: {
overrideBrowserslist: transformSyntaxToBrowserslist(syntax),
},
};
};

export function convertLibConfigToRsbuildConfig(
Expand Down
Loading

0 comments on commit e2abdd1

Please sign in to comment.