Skip to content

Commit

Permalink
fix: support script for cjs (#40)
Browse files Browse the repository at this point in the history
* fix: support script for cjs

* fix: socket can not fully transfer data when the flow is too large

* fix: build release
  • Loading branch information
shulandmimi authored Sep 24, 2024
1 parent 0675097 commit a1a4131
Show file tree
Hide file tree
Showing 20 changed files with 555 additions and 168 deletions.
9 changes: 9 additions & 0 deletions .changeset/fuzzy-crabs-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'farmup': patch
---

- support script for cjs
- support shebang for file
- fix entry clear failed
- fix can't print address when start html
- bump core version
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
indent_size = 4
indent_style = space

[*.{md,json}]
[*.{md,json,yaml}]
indent_size = 2
94 changes: 15 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,82 +51,18 @@ farmup build index.ts --no-exec

## roadmap

- support more entry file
- more cli options
- sourcemap
- ignore some watch file
- execute without output file

## options

### exec

- `alias`: e

can custom exec, select the exec method by default through the suffix of the file

- html use server star
- ts\js use node

```shell
farmup index.ts -e node

farmup index.ts -e cat
```

### format

- option: `cjs` | `esm`

```shell
farmup --format cjs index.ts
```

### minify

```shell
farmup --minify index.ts
```

### config

custom specify farm config, for more, see [farm config](https://www.farmfe.org/docs/config/configuring-farm)

by default, it automatically finds the configuration

if you not use local config, use `--no-config` option

### target

- `option`: `node``browser`, more see [targetEnv](https://www.farmfe.org/docs/config/compilation-options#outputtargetenv)

define your production environment

### watch

- `alias`: `w`

add extra watch files, support [glob](https://www.npmjs.com/package/glob) pattern

### external

set external package or path

### autoExternal

- `default`: `true`

in your code, if not find `package` or `source`, set external

### sourcemap

- `default`: `undefined`
- option: `boolean` | `'inline'` | `'all'` | `'all-inline'`

generate sourcemap

### no-experience-esm

- option: `boolean`

disable exec esm without output to filesystem
- [ ] support more entry file (without execute)
- [ ] define multiple configurations within a single configuration file.
- [ ] provide more convenient configuration for package library.
- [ ] Integration of some built-in plugins (e.g: `dts`)
- [ ] more cli options
- [ ] sourcemap
- [ ] ignore some watch file
- [ ] support monorepo?
- [x] execute without output file
- [x] cjs
- [x] esm

## option

see [here](./docs/flags.md)
73 changes: 73 additions & 0 deletions docs/flags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## options

### exec

- `alias`: e

can custom exec, select the exec method by default through the suffix of the file

- html use server star
- ts\js use node

```shell
farmup index.ts -e node

farmup index.ts -e cat
```

### format

- option: `cjs` | `esm`

```shell
farmup --format cjs index.ts
```

### minify

```shell
farmup --minify index.ts
```

### config

custom specify farm config, for more, see [farm config](https://www.farmfe.org/docs/config/configuring-farm)

by default, it automatically finds the configuration

if you not use local config, use `--no-config` option

### target

- `option`: `node``browser`, more see [targetEnv](https://www.farmfe.org/docs/config/compilation-options#outputtargetenv)

define your production environment

### watch

- `alias`: `w`

add extra watch files, support [glob](https://www.npmjs.com/package/glob) pattern

### external

set external package or path

### autoExternal

- `default`: `true`

in your code, if not find `package` or `source`, set external

### sourcemap

- `default`: `undefined`
- option: `boolean` | `'inline'` | `'all'` | `'all-inline'`

generate sourcemap

### no-experience-script

- option: `boolean`

disable exec esm without output to filesystem
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"type": "module",
"scripts": {
"initial": "pnpm --filter './packages/core' run build",
"dev": "pnpm --filter './packages/core' run dev",
"release": "pnpm --filter './packages/*' run build && changeset publish",
"ready": "farmup ./scripts/ready.ts -o ./dist/ready --no-config"
Expand All @@ -20,8 +21,9 @@
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@changesets/cli": "^2.27.3",
"cspell": "^8.8.3",
"execa": "^8.0.1",
"@changesets/cli": "^2.27.3"
"rimraf": "^6.0.1"
}
}
}
21 changes: 21 additions & 0 deletions packages/core/farm.config.cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@farmfe/core';

export default defineConfig({
compilation: {
presetEnv: false,
input: {
vm: './src/node/cjs/vm.ts',
},
output: {
targetEnv: 'node',
format: 'cjs',
entryFilename: '[entryName].cjs',
clean: false,
},
lazyCompilation: false,
persistentCache: false,
external: ['^@farmfe/core$'],
minify: false,
treeShaking: true,
},
});
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"access": "public"
},
"scripts": {
"dev": "farm watch",
"build": "farm build",
"dev": "farm watch & farm watch --config farm.config.cjs.ts",
"build": "rimraf ./dist && farm build && farm build --config farm.config.cjs.ts",
"preview": "farm preview",
"clean": "farm clean",
"farmup": "node ./bin/farmup.js --no-exec -w",
"prepublish": "mv ../../README.md ./"
},
"bin": "./bin/farmup.js",
"devDependencies": {
"@farmfe/cli": "^1.0.4",
"@farmfe/cli": "catalog:",
"@types/fs-extra": "^11.0.4",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.12.7",
Expand Down Expand Up @@ -64,7 +64,7 @@
"email": "sshuang141@163.com"
},
"dependencies": {
"@farmfe/core": "^1.3.16",
"@farmfe/core": "catalog:",
"tmp": "^0.2.3"
}
}
10 changes: 7 additions & 3 deletions packages/core/src/config/normalize/find-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const formatMapExt: Record<Exclude<Format, undefined>, string> = {
esm: 'mjs',
};

export function pinOutputEntryFilename(options: ResolvedCommonOptions) {
export function pinOutputEntryFilename(options: ResolvedCommonOptions, config: UserConfig) {
if (options.noExecute) return;

const executeMode = options.execute.type;
Expand All @@ -171,12 +171,16 @@ export function pinOutputEntryFilename(options: ResolvedCommonOptions) {
}

if (executeMode === ExecuteMode.Node && !options.noExecute) {
const originInput = config.compilation?.input ?? {};
options.entry = Object.entries(options.entry).reduce(
(res, [key, val]) => {
if (val) res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
if (val) {
if (originInput[key]) res[key] = undefined;
res[`${key}.${formatMapExt[options.format ?? 'cjs']}`] = val;
}
return res;
},
{} as Record<string, string>,
{} as Record<string, string | undefined | null>,
);

options.outputEntry = {
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/config/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ function normalizedExecuted(commonOption: CommonOptions, options: ResolvedCommon

const execute = options.execute;

if (execute.type === ExecuteMode.Node && commonOption.experienceEsm) {
if (execute.type === ExecuteMode.Node && commonOption.experienceScript) {
if (options.format === 'esm' && !execute.args.includes('--import')) {
execute.args.push('--import', path.join(import.meta.dirname, './import_register.js'));
} else if (options.format === 'cjs' && !execute.args.includes('--experimental-vm-modules')) {
// support dynamic import in vm
execute.args.push('--experimental-vm-modules');
}
}
}
Expand Down Expand Up @@ -125,7 +128,6 @@ export function normalizedTargetEnv(
options: ResolvedCommonOptions,
logger: Logger,
) {
config.compilation?.output?.targetEnv;
if (commonOptions.target) {
if (!invalidTargetEnv.includes(commonOptions.target)) {
logger.error(`target ${commonOptions.target} is invalid`);
Expand Down Expand Up @@ -176,7 +178,6 @@ async function normalizedSimpleConfig(
logger: Logger,
) {
const inputs = await tryFindEntryFromUserConfig(logger, config, commonOptions);

options.entry = inputs;

config.compilation ??= {};
Expand Down Expand Up @@ -207,7 +208,7 @@ async function normalizedSimpleConfig(

normalizedExecuted(commonOptions, options);

pinOutputEntryFilename(options);
pinOutputEntryFilename(options, config);
}

function withServerOrWatch(userConfig: UserConfig, resolvedOption: ResolvedCommonOptions): UserConfig {
Expand Down Expand Up @@ -266,6 +267,9 @@ export class NormalizeOption {
...(this.options.outputEntry ? { entryFilename: this.options.outputEntry.name } : {}),
path: this.options.outputDir,
},
// TODO: fix in script mode, resources are now only synchronized once at startup
// and lazyCompilation cannot obtain the latest resources in a timely manner.
...(this.options.execute.type === ExecuteMode.Node ? { lazyCompilation: true } : {}),
...pick(this.options, 'minify', 'sourcemap', 'external'),
},
},
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/core/ipc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@ export class IpcClient<S, R> {
start(socketPath: string) {
const client = net.createConnection(socketPath);

client.on('data', (data) => {
this.events.emit('data', data);
let data = Buffer.alloc(0);

client.on('end', () => {
this.events.emit('data', data.toString('utf-8'));
data = Buffer.alloc(0);
});

client.on('data', (buffer) => {
data = Buffer.concat([new Uint8Array(data), new Uint8Array(buffer)], data.byteLength + buffer.byteLength);
});

this.client = client;
}

send(data: S) {
this.client.write(JSON.stringify(data));
async send(data: S) {
return new Promise<void>((resolve) => {
this.client.end(JSON.stringify(data), () => {
resolve();
});
});
}

onMessage(callback: (data: R) => void) {
this.events.on('data', (data) => {
callback(JSON.parse(data.toString()));
});
}

close() {
this.client.end();
}
}
3 changes: 3 additions & 0 deletions packages/core/src/core/ipc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class IpcServer<S, R> {

close() {
this._server.close();
for (const socket of this.sockets) {
socket.close();
}
this.sockets.clear();
}

Expand Down
Loading

0 comments on commit a1a4131

Please sign in to comment.