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

chore: introduce linaria-scripts (SWC) #1131

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 0 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
__fixtures__/
coverage/
lib/
esm/
packages/*/bin/
packages/*/types/
dist/
build/
node_modules/
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ jspm_packages/
.env

# generated files
lib/
esm/
types/
dist/
build/
tsconfig.tsbuildinfo
Expand Down
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "pnpm build",
"name": "Run npm start",
"request": "launch",
"cwd": "${workspaceFolder}/website",
"type": "node-terminal"
}
],
"compounds": []
}
12 changes: 6 additions & 6 deletions examples/esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"private": true,
"name": "esbuild-example",
"version": "0.0.0",
"private": true,
"license": "MIT",
"author": "Anton Evzhakov",
"scripts": {
"build": "node build.js"
},
"dependencies": {
"linaria-website": "workspace:^"
},
"devDependencies": {
"@linaria/esbuild": "workspace:^",
"esbuild": "^0.14.48"
},
"scripts": {
"build": "node build.js"
},
"author": "Anton Evzhakov"
}
}
14 changes: 7 additions & 7 deletions examples/rollup/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"private": true,
"name": "rollup-example",
"version": "0.0.0",
"private": true,
"license": "MIT",
"author": "Anton Evzhakov",
"scripts": {
"build": "rollup -c"
},
"dependencies": {
"linaria-website": "workspace:^"
},
"devDependencies": {
"@babel/core": "^7.18.6",
"@babel/preset-react": "^7.18.6",
"@linaria/rollup": "workspace:^",
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-image": "^2.1.1",
"@rollup/plugin-node-resolve": "^13.3.0",
"rollup": "^2.76.0",
"rollup-plugin-css-only": "^3.1.0"
},
"scripts": {
"build": "rollup -c"
},
"author": "Anton Evzhakov"
}
}
15 changes: 6 additions & 9 deletions examples/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"private": true,
"name": "vite-example",
"version": "0.0.0",
"private": true,
"license": "MIT",
"author": "Anton Evzhakov",
"main": "index.js",
"type": "module",
"scripts": {
"build": "vite build"
},
"dependencies": {
"linaria-website": "workspace:^"
},
Expand All @@ -13,12 +16,6 @@
"@linaria/shaker": "workspace:^",
"@rollup/plugin-node-resolve": "^15.0.0",
"@vitejs/plugin-react": "^2.1.0",
"rollup": "^3.2.2",
"vite": "^3.1.8"
},
"scripts": {
"build": "vite build"
},
"author": "Anton Evzhakov"
}
}

14 changes: 7 additions & 7 deletions examples/webpack4/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"private": true,
"name": "webpack4-example",
"version": "0.0.0",
"private": true,
"license": "MIT",
"author": "Anton Evzhakov",
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"preview": "pnpm build && pnpm dlx http-server ."
},
"dependencies": {
"linaria-website": "workspace:^4.1.8"
},
Expand All @@ -17,10 +22,5 @@
"mini-css-extract-plugin": "^0.5.0",
"webpack": "^4.46.0",
"webpack-cli": "4.7.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"preview": "pnpm build && pnpm dlx http-server ."
},
"author": "Anton Evzhakov"
}
}
14 changes: 7 additions & 7 deletions examples/webpack5/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"private": true,
"name": "webpack5-example",
"version": "0.0.0",
"private": true,
"license": "MIT",
"author": "Anton Evzhakov",
"main": "index.js",
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"preview": "pnpm build && pnpm dlx http-server ."
},
"dependencies": {
"linaria-website": "workspace:^4.1.8"
},
Expand All @@ -18,10 +23,5 @@
"mini-css-extract-plugin": "^2.7.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"preview": "pnpm build && pnpm dlx http-server ."
},
"author": "Anton Evzhakov"
}
}
200 changes: 200 additions & 0 deletions linaria-scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#!/usr/bin/env node
/* eslint-disable */
import process from 'node:process';
import path from 'node:path';
import chokidar from 'chokidar';
import ts from 'typescript';
// import esbuild from 'esbuild';
import fg from 'fast-glob';
import fs from 'node:fs/promises';
import swc from '@swc/core';

const [cmd, ...args] = process.argv.slice(2);

switch (cmd) {
case 'build': {
const outdir = path.resolve(process.cwd(), 'dist');

await fs.rm(outdir, { recursive: true, force: true });
const rootdir = path.resolve(process.cwd(), './src');

let files = await fg(['src/**/*.{ts,tsx}'], {
absolute: true,
cwd: process.cwd(),
ignore: ['**/*.d.ts', 'dist'],
});

let watcher;

const build = async () => {
const at = performance.now();
// esbuild
// const common = {
// platform: 'node',
// target: 'node12',
// loader: 'ts',
// };
// const variants = [
// { format: 'esm', ext: '.js' },
// { format: 'cjs', ext: '.js' },
// ];

const variants = [
{ format: 'nodenext', ext: '.js', dir: 'esm' },
{ format: 'commonjs', ext: '.js', dir: 'cjs' },
];
// swc file-by-file
for (const file of files) {
const inp = path.parse(file);
for (const { ext, format, dir } of variants) {
/**@type {swc.Output}*/ let result;
try {
result = await swc.transformFile(file, {
jsc: {
parser: {
syntax: 'typescript',
dynamicImport: true,
},
target: 'es2022',
loose: true,
},
module: {
type: format,
},
sourceMaps: true,
configFile: false,
swcrc: false,
cwd: process.cwd(),
filename: inp.base,
// env: {
// coreJs: args.includes('--legacy') ? '3.22' : undefined,
// },
});
} catch (e) {
console.error(e);
continue;
}
const dist = path.resolve(outdir, dir);
const outfiledir = inp.dir.replace(rootdir, dist);
const out = path.format({
root: inp.root,
name: inp.name,
ext,
dir: outfiledir,
});
result.code += `\n //# sourceMappingURL=${inp.name}.js.map`;
await fs.mkdir(outfiledir, { recursive: true });
await fs.writeFile(out, result.code);
await fs.writeFile(out + '.map', result.map);
}
}
// for (const { ext, format } of variants) {
// by build
// await esbuild.build({
// absWorkingDir: process.cwd(),
// entryPoints: files,
// platform: 'node',
// loader: {
// '.ts': 'ts',
// },
// sourcemap: 'linked',
// format,
// treeShaking: true,
// bundle: args.includes('--bundle'),
// // external: [],
// outdir: path.resolve(outdir, format),
// outExtension: { '.js': ext },
// });
// }
// file-by-file
// for (const file of files) {
// // const content = await fs.readFile(file, "utf8");
// const inp = path.parse(file);

// for (const { ext, format } of variants) {
// const dist = path.resolve(outdir, format);
// /**@type {esbuild.TransformResult}*/ let result;
// try {
// // result = await esbuild.transform(content, {
// // ...common,
// // format,
// // });
// } catch (e) {
// /**@type {esbuild.TransformFailure}*/ const err = e;
// console.error(err.message);
// continue;
// }

// const outfiledir = inp.dir.replace(rootdir, dist);
// const out = path.format({
// root: inp.root,
// name: inp.name,
// ext,
// dir: outfiledir,
// });
// await fs.mkdir(outfiledir, { recursive: true });
// await fs.writeFile(out, result.code);
// }
// }

const tsConfig = ts.parseJsonConfigFileContent(
ts.readConfigFile(
path.resolve(process.cwd(), './tsconfig.json'),
ts.sys.readFile
).config,
ts.sys,
'./'
);

const tsHost = ts.createCompilerHost(tsConfig);
const tsProgram = ts.createProgram(
files,
{
outDir: path.resolve(outdir, './types'),
declaration: true,
emitDeclarationOnly: true,
skipDefaultLibCheck: true,
skipLibCheck: true,
incremental: true,
...tsConfig.options,
},
tsHost
);
const { diagnostics } = tsProgram.emit();
for (const d of diagnostics) {
console.info(d.messageText);
}

const duraton = ((performance.now() - at) / 1000).toFixed(1);
console.log('Done.', duraton, 's.');
};

const exit = async () => {
await watcher?.close();
process.exit();
};

process.on('SIGINT', exit);
if (process.platform === 'win32') process.on('SIGKILL', exit);

if (!args.includes('--watch')) {
await build();
process.exit();
}
watcher = chokidar
.watch(path.resolve(process.cwd(), rootdir), {
ignoreInitial: false,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10,
},
})
.on('ready', () => console.info('Watching for directory changes.'))
.on('all', build);

break;
}
default: {
throw new Error(`Unsupported command ${cmd}`);
}
}
12 changes: 12 additions & 0 deletions linaria-scripts/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"declaration": true,
"strict": true,
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"target": "esnext"
}
}
Loading