Skip to content

Commit

Permalink
feat: Build builder with esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3k0 authored and 3y3 committed Apr 19, 2023
1 parent bfc794d commit 3c51c1f
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 74 deletions.
12 changes: 12 additions & 0 deletions esbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
esbuild src/index.ts --outfile=build/index.js \
--platform=node --format=cjs \
--bundle --packages=external \
--sourcemap \
--define:VERSION="\"$(node -pe "require('./package.json').version")\"" \
--banner:js="#!/usr/bin/env node"

esbuild src/workers/linter/index.ts --outfile=build/linter.js \
--platform=node --format=cjs \
--bundle --packages=external \
--sourcemap \
--define:VERSION="\"$(node -pe "require('./package.json').version")\""
184 changes: 184 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"src"
],
"scripts": {
"build": "webpack",
"build": "npm run build:app && npm run build:bin",
"build:app": "webpack",
"build:bin": "./esbuild.sh",
"start": "node build/index.js",
"start-ts": "ts-node src/index.ts",
"lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
Expand Down Expand Up @@ -90,6 +92,7 @@
"babel-loader": "8.2.3",
"bem-cn-lite": "^3.0.0",
"css-loader": "3.6.0",
"esbuild": "^0.17.17",
"eslint": "6.8.0",
"eslint-plugin-react": "7.28.0",
"eslint-plugin-react-hooks": "3.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/steps/processLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {spawn, Worker, Thread} from 'threads';
import {extname} from 'path';

import {ArgvService, TocService, PresetService, PluginService} from '../services';
import {ProcessLinterWorker} from '../workers/processLinter';
import {ProcessLinterWorker} from '../workers/linter';
import {logger} from '../utils';
import {LINTING_FINISHED, WORKERS_COUNT, MIN_CHUNK_SIZE} from '../constants';
import {lintPage} from '../resolvers';
Expand Down Expand Up @@ -76,7 +76,8 @@ export async function initLinterWorkers() {
const workersCount = navigationPathsChunks.length;

processLinterWorkers = await Promise.all((new Array(workersCount)).fill(null).map(() => {
return spawn<ProcessLinterWorker>(new Worker('../workers/processLinter/worker'), {timeout: 60000});
// TODO: get linter path from env
return spawn<ProcessLinterWorker>(new Worker('./linter'), {timeout: 60000});
}));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import log from '@doc-tools/transform/lib/log';
import {extname} from 'path';
import {Observable, Subject} from 'threads/observable';
import {expose} from 'threads';

import {ArgvService, PluginService, PresetService, TocService} from '../../services';
import {TocServiceData} from '../../services/tocs';
Expand Down Expand Up @@ -48,11 +49,14 @@ function getProcessedPages() {
return Observable.from(processedPages);
}

export type ProcessLinterWorker = {
run: typeof run;
finish: typeof finish;
getProcessedPages: typeof getProcessedPages;
};

export const processLinterWorker = {
expose({
run,
finish,
getProcessedPages,
};

export type ProcessLinterWorker = typeof processLinterWorker;
});
4 changes: 0 additions & 4 deletions src/workers/processLinter/worker.ts

This file was deleted.

71 changes: 8 additions & 63 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
const webpack = require('webpack');
const {resolve} = require('path');
const ThreadsPlugin = require('threads-plugin');
const dotenv = require('dotenv');

dotenv.config();

const {NODE_ENV} = process.env;

const devExcludeConditions = [
(req) => !/^\./.test(req),
(req) => !req.includes('threads-plugin'),
];

const prodExcludeConditions = [
(req) => req.includes('@yandex-cloud/nodejs-sdk'),
];

const excludeConditions = NODE_ENV === 'development'
? devExcludeConditions
: prodExcludeConditions;

const filterBy = (predicates) =>
(req) => predicates.every((predicate) => predicate(req));

const shouldExcludeDependency = filterBy(excludeConditions);

function processDependencies(context, request, callback) {
if (shouldExcludeDependency(request)) {
return callback(null, 'commonjs ' + request);
}

return callback();
}

module.exports = [
{
mode: 'production',
mode: 'development',
target: 'web',
entry: './src/app/index.tsx',
output: {
path: resolve(__dirname, 'build'),
filename: 'app.js',
},
resolve: {
alias: {
react: require.resolve('react'),
},
extensions: ['.tsx', '.ts', '.js', '.scss'],
},
module: {
rules: [
{
test: /\.[tj]sx?$/,
use: ['babel-loader'],
exclude: /node_modules/,
include: [
resolve(__dirname, 'src'),
require.resolve('@diplodoc/mermaid-extension'),
],
}, {
test: /\.s?css$/,
use: [
Expand Down Expand Up @@ -90,32 +63,4 @@ module.exports = [
],
},
},
{
mode: 'production',
target: 'node',
entry: './src/index.ts',
devtool: 'eval-source-map',
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: resolve(__dirname, 'build'),
filename: 'index.js',
},
module: {
rules: [{
test: /\.[tj]sx?$/,
use: ['babel-loader'],
exclude: /node_modules/,
}],
},
plugins: [
new webpack.BannerPlugin({banner: '#!/usr/bin/env node', raw: true}),
new webpack.DefinePlugin({
VERSION: JSON.stringify(require('./package.json').version),
}),
new ThreadsPlugin(),
],
externals: [processDependencies],
},
];

0 comments on commit 3c51c1f

Please sign in to comment.