Skip to content

Commit

Permalink
Feat: npm create @builder.io/mitosis (#1302)
Browse files Browse the repository at this point in the history
* init

* first pass

* fix

* ignore

fix

* mv

* fmt

* f

* ficeroo

* f

* k

* yee

* fixerooni

* install

* sync

* basic doc

* f

* add qwik

* cleanup example

* lint

* another pass

* bye

* fixeroos

* f

* f

* fixes

* yee

* r

* last

* f

* rl
  • Loading branch information
samijaber authored Nov 22, 2023
1 parent c5cdd60 commit c43e61f
Show file tree
Hide file tree
Showing 70 changed files with 1,557 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
**/dist/
*.d.ts
node_modules
node_modules

# ignore the starter package, it is not part of the workspace
packages/starter/
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ packages/e2e-*/**
**/playwright/.cache/
**/tsconfig.json

examples/**/output/**/*

examples/**/output/**/*
# ignore the starter package, it is not part of the workspace
# packages/starter/
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
},
"typescript.format.enable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"typescript.tsdk": "node_modules/typescript/lib"
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
3 changes: 3 additions & 0 deletions .yarn/versions/2ceb5677.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
undecided:
- "@builder.io/mitosis-repo"
- "@builder.io/mitosis-cli"
2 changes: 1 addition & 1 deletion packages/cli/src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const getOptions = (config?: MitosisConfig): MitosisConfig => {

if (checkIsDefined(newConfig.commonOptions?.typescript)) {
for (const target of newConfig.targets) {
if (!checkIsDefined(newConfig.options[target].typescript)) {
if (!checkIsDefined(newConfig.options[target]?.typescript)) {
newConfig.options[target].typescript = newConfig.commonOptions.typescript;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/starter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template/package-lock.json
3 changes: 3 additions & 0 deletions packages/starter/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
template/package-lock.json

node_modules/
1 change: 1 addition & 0 deletions packages/starter/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.13.0
14 changes: 14 additions & 0 deletions packages/starter/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2020",
"jsx": "react",
"strictNullChecks": true,
"strictFunctionTypes": true,
"strict": true,
"checkJs": true
},
"include": ["./script.cjs"],
"exclude": ["node_modules", "**/node_modules/*"]
}
22 changes: 22 additions & 0 deletions packages/starter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@builder.io/create-mitosis",
"version": "0.0.5",
"description": "CLI to create a new Mitosis project from a template.",
"dependencies": {
"@clack/prompts": "^0.7.0",
"cross-spawn": "^7.0.3"
},
"bin": "./script.cjs",
"files": [
"script.cjs",
"template"
],
"scripts": {
"start": "node ./script.cjs",
"release:dev": "yarn version prerelease && yarn npm publish --tag dev",
"release:patch": "yarn version patch && yarn npm publish"
},
"devDependencies": {
"@types/node": "^20.9.1"
}
}
Binary file added packages/starter/package.tgz
Binary file not shown.
149 changes: 149 additions & 0 deletions packages/starter/script.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const { cwd } = require('process');
const p = require('@clack/prompts');
const spawn = require('cross-spawn');

const USER_DIR = cwd();
const SCRIPT_DIR = __dirname;

let projectName = '';

const main = async () => {
p.intro(`Welcome to Mitosis! Let's get started.`);

projectName = await p.text({
message: 'What is your Mitosis project name?',
defaultValue: 'my-project',
placeholder: 'my-project',
validate: (_input) => {
const input = _input || 'my-project';
if (fs.existsSync(path.join(USER_DIR, input))) return 'Folder already exists with this name';
},
});

if (typeof projectName !== 'string') {
p.outro(`Please provide a string for your project name. Exiting...`);
process.exit(0);
}

/**
* @type {string[] | symbol}
*/
const targets = await p.multiselect({
message: 'Select your desired outputs',
options: [{ value: 'react' }, { value: 'svelte' }, { value: 'qwik' }],
required: true,
});

if (!Array.isArray(targets) || targets.length === 0) {
p.outro(`No targets selected. Exiting...`);
process.exit(0);
}

p.note('Generating your template...');

// start by copying over base starter
const templateFolder = path.join(SCRIPT_DIR, './template/');
const outputFolder = path.join(USER_DIR, projectName);

/**
*
* @param {string} filePath
*/
const updateMitosisConfig = (filePath) => {
const config = require(filePath);
config.targets = targets;

return `
/**
* @type {import('@builder.io/mitosis').MitosisConfig}
*/
module.exports = ${JSON.stringify(config, null, 2)}`;
};
/**
*
* Copy `src` to `dest`.
* @param {string} src
* @param {string} dest
*/
const copy = (src, dest) => {
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach((entry) => {
if (entry === 'node_modules') return;
if (entry === 'package-lock.json') return;

// ignore folders meant for excluded targets
const isTargetSpecificFolder =
src.endsWith('/library/packages') || src.endsWith('/test-apps');
if (isTargetSpecificFolder && !targets.some((target) => entry === target)) {
return;
}

const srcPath = path.join(src, entry);
const destPath = path.join(dest, entry);

// update mitosis.config.cjs
if (entry === 'mitosis.config.cjs') {
try {
const newConfig = updateMitosisConfig(srcPath);

fs.writeFileSync(destPath, newConfig);
} catch (error) {
p.note('Error processing `mitosis.config.cjs`. Copying as-is.');
console.error(error);
fs.copyFileSync(srcPath, destPath);
}
return;
}

if (fs.lstatSync(srcPath).isDirectory()) {
copy(srcPath, destPath);
} else {
const fileContents = fs.readFileSync(srcPath, 'utf8');
// update all files to have correct package names
const updatedFileContents = fileContents.replace(/@template/g, '@' + projectName);
fs.writeFileSync(destPath, updatedFileContents);
}
});
};

copy(templateFolder, outputFolder);

p.note('Template generated!');
// ask about installing dependencies
const install = await p.confirm({
message: 'Install dependencies?',
});

if (install) {
p.note(`Installing dependencies...this may take a while!`);
const installProcess = spawn.sync('npm', ['install'], {
cwd: outputFolder,
stdio: 'inherit',
});
}

p.outro(`
You're all set!
Next:
- cd \`${projectName}/\`
- open the README.md for further instructions on how to run your project
`);
process.exit(0);
};

try {
main();
} catch (error) {
p.outro(`An error occurred. Clearing folder and exiting...`);
console.error(error);

if (projectName) {
fs.rmdirSync(path.join(USER_DIR, projectName), { recursive: true });
}
process.exit(1);
}
33 changes: 33 additions & 0 deletions packages/starter/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Mitosis Mono-repo Starter

This is a mono-repo for Mitosis libraries. It contains a few workspaces to get you started.

## Workspaces

- [library](./library/): workspace containing your Mitosis project.
- [library/src](./library/src/): Mitosis source code of your component library.
- [library/packages](./library/packages/): individual outputs generated by Mitosis.
- [test-apps](./test-apps/): dev servers that import your Mitosis components. Useful for testing your library.

## Developing

1. Run Mitosis in watch mode

```bash
cd library
npm run start
```

2. If the output has its own bundling step (like Svelte/Qwik), you will need to run that build step in a separate terminal:

```bash
cd library/packages/qwik
npm run build:watch
```

3. Finally, run the corresponding test server of your library from the previous step to see your Mitosis project in action:

```bash
cd test-apps/qwik
npm run dev
```
19 changes: 19 additions & 0 deletions packages/starter/template/library/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
browser: true,
},
plugins: ['@builder.io/mitosis'],
extends: [
// Use this approach for our recommended rules configuration
'plugin:@builder.io/mitosis/recommended',
],
parser: '@typescript-eslint/parser',
extends: [],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
};
1 change: 1 addition & 0 deletions packages/starter/template/library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages/**/src
18 changes: 18 additions & 0 deletions packages/starter/template/library/mitosis.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @type {import('@builder.io/mitosis').MitosisConfig}
*/
module.exports = {
files: 'src/**',
targets: ['qwik', 'react', 'svelte'],
dest: 'packages',
commonOptions: {
typescript: true,
},
options: {
react: {
stylesType: 'style-tag',
},
svelte: {},
qwik: {},
},
};
22 changes: 22 additions & 0 deletions packages/starter/template/library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@template/library",
"private": true,
"scripts": {
"start": "watch 'npm run build' ./src",
"build": "mitosis build --c mitosis.config.cjs",
"lint": "eslint"
},
"type": "module",
"exports": {
"./*": "./output/*/src/index.js"
},
"dependencies": {
"@builder.io/eslint-plugin-mitosis": "^0.0.15",
"@builder.io/mitosis": "latest",
"@builder.io/mitosis-cli": "latest",
"eslint": "^8.51.0"
},
"devDependencies": {
"watch": "^1.0.2"
}
}
31 changes: 31 additions & 0 deletions packages/starter/template/library/packages/qwik/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
**/*.log
**/.DS_Store
*.
.vscode/settings.json
.history
.yarn
bazel-*
bazel-bin
bazel-out
bazel-qwik
bazel-testlogs
dist
dist-dev
lib
lib-types
etc
external
node_modules
temp
tsc-out
tsdoc-metadata.json
target
output
rollup.config.js
build
.cache
.vscode
.rollup.cache
dist
tsconfig.tsbuildinfo
vite.config.ts
Loading

1 comment on commit c43e61f

@vercel
Copy link

@vercel vercel bot commented on c43e61f Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.