Skip to content

Commit

Permalink
build: create all kind of builds
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Oct 7, 2021
1 parent 91f5224 commit fcd30f1
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 28 deletions.
6 changes: 4 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const { factories } = require('./babel-plugin-factories.json');
const { factories } = require('./src/babel-plugin-factories.json');

module.exports = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
modules: process.env.ESMODULES === 'true' ? false : 'cjs',
targets: {
node: 'current',
esmodules: process.env.ESMODULES === 'true',
},
},
],
['./babel-preset'],
['./src/babel-preset'],
],
plugins: [
[
Expand Down
2 changes: 0 additions & 2 deletions macro.d.ts

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"commit": "git-cz",
"format": "prettier --write \"./src/**/**.{ts,tsx,js,jsx,json}\"",
"lint": "eslint ./",
"cleanup": "rm -rf dist && mkdir dist",
"build:indexes": "node scripts/build.js",
"build:types": "tsc --project ./tsconfig.build.json",
"build:commonjs": "yarn babel --out-dir ./dist src -x .ts --ignore '**/*.test.ts','**/*.d.ts'",
"build:methods": "node scripts/build.js && prettier --config ./.prettierrc --write ./babel-plugin-factories.json",
"build:cjs": "rollup -c",
"build:mjs": "ESMODULES=true yarn build:commonjs --out-file-extension .mjs",
"build:bundles": "rollup -c",
"build": "run-s cleanup build:indexes build:types build:commonjs build:mjs build:bundles",
"prepublishOnly": "yarn build:methods && yarn build:cjs"
},
"author": "Sergey Sova <[email protected]> (https://sergeysova.com/)",
Expand Down Expand Up @@ -43,6 +46,7 @@
"husky": "3.1.0",
"jest": "^27.2.4",
"lint-staged": "^9.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.0",
"rollup": "^2.32.1",
"rollup-plugin-terser": "^7.0.2",
Expand Down
10 changes: 4 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');

const plugins = [
nodeResolve({ extensions: ['.js'] }),
commonjs({ extensions: ['.js'] }),
terser({}),
nodeResolve({ jsnext: true, skip: ['effector'], extensions: ['.js', '.mjs'] }),
commonjs({ extensions: ['.js', '.mjs'] }),
// terser({}),
];

const input = 'dist/index.js';

// eslint-disable-next-line import/no-anonymous-default-export
export default [
{
input,
input: 'dist/index.mjs',
external: ['effector'],
plugins,
output: {
file: './dist/patronum.mjs',
format: 'es',
freeze: false,
exports: 'named',
sourcemap: true,
externalLiveBindings: false,
},
Expand Down
21 changes: 13 additions & 8 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,41 @@ const {
createTypingsIndex,
createFactoriesJson,
createDistribution,
writeFile,
createMjsIndex,
} = require('./libraries');
const packageJson = require('./source.package.js');

const packageMarker = 'index.ts';

async function main() {
const library = 'patronum';
const package = packageJson();
const staticFiles = ['macro.d.ts', 'macro.js', 'babel-preset.js'];
const pkg = packageJson();
const staticFiles = ['macro.js', 'babel-preset.js'];

const directory = await createDistribution('./dist');
await directory.copyList('.', staticFiles);
await directory.copyList('./src', staticFiles);

package.files.push(...staticFiles);
pkg.files.push(...staticFiles);

const found = await globby(`./src/*/${packageMarker}`);
const names = found.map((name) =>
name.replace(`/${packageMarker}`, '').replace('./src/', ''),
);

await directory.write('index.js', createCommonJsIndex(names));
await directory.write('index.mjs', createMjsIndex(names));
await directory.write('index.d.ts', createTypingsIndex(names));
await directory.write('macro.d.ts', 'export * from "./index";');

const productionMethods = names.filter((method) => method !== 'debug');
await directory.write(
'babel-plugin-factories.json',
JSON.stringify(createFactoriesJson(library, productionMethods), null, 2),
const factoriesJson = createFactoriesJson(library, productionMethods);
await writeFile(
'./src/babel-plugin-factories.json',
JSON.stringify(factoriesJson, null, 2),
);

await directory.write('package.json', JSON.stringify(package));
await directory.write('package.json', JSON.stringify(pkg));
}

main().catch((error) => {
Expand Down
11 changes: 11 additions & 0 deletions scripts/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ function createCommonJsIndex(names) {
return imports.join('\n') + '\n';
}

function createMjsIndex(names) {
const imports = names.sort().map((name) => {
const camel = camelCase(name);
return `export { ${camel} } from './${name}/index.mjs'`;
});

return imports.join('\n') + '\n';
}

function createTypingsIndex(names) {
const types = names
.sort()
Expand Down Expand Up @@ -43,7 +52,9 @@ async function createDistribution(dir) {
}

module.exports = {
writeFile,
createCommonJsIndex,
createMjsIndex,
createTypingsIndex,
createFactoriesJson,
createDistribution,
Expand Down
6 changes: 6 additions & 0 deletions src/babel-plugin-factories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"factories": ["patronum", "patronum/delay"],
"mapping": {
"delay": "delay"
}
}
File renamed without changes.
2 changes: 0 additions & 2 deletions src/delay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export function delay<T>({
return target as unknown as Event<T>;
}

module.exports = { delay };

function validateTimeout<T>(
timeout: number | ((_: T) => number) | Store<number> | unknown,
) {
Expand Down
File renamed without changes.
Loading

0 comments on commit fcd30f1

Please sign in to comment.