Skip to content

Commit

Permalink
feat!: Rebuild project with esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Sep 5, 2023
1 parent 467313c commit e617864
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 464 deletions.
48 changes: 48 additions & 0 deletions esbuild/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

const {join} = require('node:path');
const esbuild = require('esbuild');
const {sassPlugin} = require('esbuild-sass-plugin');
const {sparsedBuildPlugin} = require('./sparsed-build-plugin');
const {useFromSourcePlugin} = require('./use-from-source-plugin');
const autoprefixer = require('autoprefixer');
const postcssPresetEnv = require('postcss-preset-env');
const postcss = require('postcss');
const tsconfigJson = require('../tsconfig.json');

const {compilerOptions: {target}} = tsconfigJson;

function build({path, format}) {
return esbuild.build({
bundle: true,
sourcemap: true,
target: target,
tsconfig: './tsconfig.json',
entryPoints: [join('./src', path)],
outbase: './src',
outdir: `./build${format ? '/' + format : ''}`,
format: format,
plugins: [
useFromSourcePlugin(/\.svg$/),
sparsedBuildPlugin({
extension: {
'.scss': '.css',
}
}),
sassPlugin({
async transform(source) {
const {css} = await postcss([
autoprefixer({cascade: false}),
postcssPresetEnv({stage: 0}),
]).process(source, {from: undefined});

return css;
},
}),
],
});
}

build({path: 'index.ts', format: 'cjs'});
build({path: 'index.ts', format: 'esm'});
build({path: 'index.scss'});
103 changes: 103 additions & 0 deletions esbuild/sparsed-build-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const assert = require('node:assert');
const {join, resolve} = require('node:path');
const {statSync, existsSync} = require('node:fs');

function resolveFile(path, exts, strict) {
const isLikeFile = exts.some((ext) => path.endsWith(ext));

if (!existsSync(path)) {
if (!isLikeFile) {
for (const ext of exts) {
const file = resolveFile(path + ext, exts);
if (file) {
return file;
}
}
}
} else {
const stat = statSync(path);

if (stat.isFile()) {
return path;
} else if (!isLikeFile && stat.isDirectory()) {
for (const ext of exts) {
const file = resolveFile(join(path, 'index' + ext), exts);
if (file) {
return file;
}
}
}
}

if (strict) {
// throw ENOENT
return statSync(path);
} else {
return null;
}
}

function replaceExt(path, extensions) {
for (const [from, to] of Object.entries(extensions)) {
if (path.endsWith(from)) {
const parts = path.split(from).slice(0, -1);

return parts
.concat(parts.pop() + to)
.join(from);
}
}

return path;
}

function sparsedBuildPlugin({extension = {}, processed = new Set()} = {}) {
extension = Object.assign({
'.ts': '.js',
'.tsx': '.js',
'.json': '.js',
}, extension);

return {
name: 'sparsedBuild',
setup(build) {
const {esbuild, initialOptions} = build;
const {
bundle,
resolveExtensions = ['.tsx', '.ts', '.jsx', '.js', '.css', '.json']
} = initialOptions;

assert(bundle === true, `Option 'bundle' should be 'true' for sparsed build`);

build.onResolve({filter: /.*/}, async ({path, resolveDir, kind}) => {
if (kind === 'entry-point') {
return;
}

if (!path.match(/^\.{1,2}/)) {
return {external: true};
}

const fullpath = resolveFile(resolve(resolveDir, path), resolveExtensions, true);

if (!processed.has(fullpath)) {
processed.add(fullpath);

await esbuild.build({
...initialOptions,
entryPoints: [fullpath],
});
}

return {
path: replaceExt(path, extension),
external: true
};
});
},
};
}

module.exports = {
sparsedBuildPlugin,
};
29 changes: 29 additions & 0 deletions esbuild/use-from-source-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const {dirname, relative, resolve} = require('node:path');

function useFromSourcePlugin(match) {
return {
name: 'useFromSource',
setup(build) {
let {outdir, outfile, outbase = '.'} = build.initialOptions;
outdir = resolve(outdir || dirname(outfile));
outbase = resolve(outbase);

build.onResolve({filter: match}, ({path, resolveDir, importer}) => {
if (!path.match(/^\.{1,2}/)) {
return {external: true};
}

const outpath = resolve(outdir, relative(outbase, dirname(importer)));

return {
external: true,
path: relative(outpath, resolve(resolveDir, path))
};
});
},
};
}

module.exports = {
useFromSourcePlugin,
};
48 changes: 36 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@
"build",
"styles"
],
"exports": {
".": {
"import": {
"types": "./build/esm/index.d.js",
"style": "./build/index.css",
"default": "./build/esm/index.js"
},
"require": {
"types": "./build/cjs/index.d.js",
"style": "./build/index.css",
"default": "./build/cjs/index.js"
},
"default": {
"types": "./build/esm/index.d.js",
"style": "./build/index.css",
"default": "./build/esm/index.js"
}
},
"./assets/icons/*.svg": "./assets/icons/*.svg",
"./styles": "./build/index.css"
},
"sideEffects": [
"./build/index.js",
"./build/i18n/index.js",
"./src/index.ts",
"./src/i18n/index.ts",
"*.css",
"*.scss"
],
"main": "build/index.js",
"typings": "build/index.d.ts",
"main": "build/esm/index.js",
"typings": "build/esm/index.d.ts",
"scripts": {
"deps:install": "npm ci",
"deps:truncate": "npm prune --production",
Expand All @@ -35,10 +53,15 @@
"_lint:styles:fix": "npm run _lint:styles -- --fix",
"typecheck": "tsc --noEmit",
"test": "exit 0",
"dev": "run-s build:clean build:compile _dev:watch",
"_dev:watch": "run-p _build:watch _storybook:watch",
"build:clean": "rimraf build",
"build:copy": "copyfiles -u 1 \"src/components/**/*.scss\" \"src/components/**/*.svg\" build",
"build:compile": "tsc -p tsconfig.publish.json",
"build": "npm run build:clean && npm run build:compile && npm run build:copy",
"build:compile": "./esbuild/build.js",
"_build:declarations:esm": "tsc --emitDeclarationOnly -p tsconfig.esm.json",
"_build:declarations:cjs": "tsc --emitDeclarationOnly -p tsconfig.cjs.json",
"build": "run-s build:clean build:compile _build:declarations:*",
"_build:watch": "./esbuild/build.js --watch",
"_storybook:watch": "cd demo && start-storybook -p 7008",
"prepublishOnly": "npm run lint && npm run test && npm run build",
"prepare": "husky install",
"pre-commit": "lint-staged"
Expand Down Expand Up @@ -72,14 +95,15 @@
"@types/lodash": "4.14.179",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"copyfiles": "2.4.1",
"node-sass": "4.14.1",
"npm-run-all": "4.1.5",
"autoprefixer": "^10.4.15",
"esbuild": "^0.19.2",
"esbuild-sass-plugin": "^2.13.0",
"eslint": "^8.48.0",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.28",
"postcss-preset-env": "^9.1.2",
"prettier": "^2.8.8",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
2 changes: 0 additions & 2 deletions src/components/Controls/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import {
PdfControl,
} from './';


import {PopperPosition} from '../../hooks';
import {Lang, TextSizes, Theme, FeedbackSendData, ControlSizes, SubscribeData} from '../../models';
import EditIcon from '@gravity-ui/icons/svgs/pencil.svg';

import './Controls.scss';

const b = block('dc-controls');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Control} from '../../Control';
import {ControlSizes, Lang} from '../../../models';

import FullScreenClickedIcon from '../../../../assets/icons/full-screen-clicked.svg';
import {PopperPosition} from '../../../hooks';
import FullScreenIcon from '@gravity-ui/icons/svgs/square-dashed.svg';

interface ControlProps {
Expand Down
38 changes: 0 additions & 38 deletions styles/default.scss

This file was deleted.

Loading

0 comments on commit e617864

Please sign in to comment.