@frozzare/pkg 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.editorconfig ADDED
@@ -0,0 +1,15 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.md]
12
+ trim_trailing_whitespace = false
13
+
14
+ [*.{js,ts}]
15
+ quote_type = single
package/.eslintrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "plugins": ["@typescript-eslint"],
5
+ "extends": [
6
+ "eslint:recommended",
7
+ "plugin:@typescript-eslint/eslint-recommended",
8
+ "plugin:@typescript-eslint/recommended"
9
+ ]
10
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": [
3
+ "config:base"
4
+ ],
5
+ "rangeStrategy": "bump",
6
+ "automerge": true,
7
+ "major": {
8
+ "automerge": false
9
+ },
10
+ "minor": {
11
+ "automerge": false
12
+ },
13
+ "separateMultipleMajor": true
14
+ }
@@ -0,0 +1,37 @@
1
+ name: build
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+
8
+ # Run tests for any PRs.
9
+ pull_request:
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ node-version:
18
+ - 14
19
+ - 16
20
+ - 18
21
+
22
+ steps:
23
+ - uses: actions/checkout@v3
24
+ - name: Use Node.js ${{ matrix.node-version }}
25
+ uses: actions/setup-node@v3.6.0
26
+ with:
27
+ node-version: ${{ matrix.node-version }}
28
+ - name: npm install
29
+ run: npm install
30
+ - name: npm run lint
31
+ run: npm run lint
32
+ - name: npm run build
33
+ run: npm run build
34
+ - name: npm run test
35
+ run: npm run test
36
+ env:
37
+ CI: true
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ MIT License
2
+
3
+ Copyright (c) Fredrik Forsmo
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ import { BuildOptions as ESBuildOptions } from 'esbuild';
2
+ export type BuildOptions = Omit<ESBuildOptions, 'bundle' | 'entryPoints'>;
3
+ export declare const build: (entryPoint: string, config?: BuildOptions) => Promise<string | void>;
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { build as esbuild } from 'esbuild';
4
+ export const build = async (entryPoint, config = {}) => {
5
+ const out = await esbuild({
6
+ ...config,
7
+ entryPoints: [entryPoint],
8
+ bundle: true,
9
+ write: false,
10
+ });
11
+ const file = out.outputFiles[0];
12
+ let text = file.text;
13
+ if (config.format === 'cjs') {
14
+ const reg = /module.exports = __toCommonJS\((\w+)\);/;
15
+ const match = text.match(reg);
16
+ if (!match) {
17
+ throw new Error('Failed to find export name');
18
+ }
19
+ const name = match === null || match === void 0 ? void 0 : match[1].replace('_exports', '_default');
20
+ text = text.replace((match === null || match === void 0 ? void 0 : match[0]) || '', '') + `\nmodule.exports = ${name}`;
21
+ }
22
+ if (!config.write) {
23
+ return text;
24
+ }
25
+ fs.mkdirSync(path.dirname(file.path));
26
+ return !config.write ? text : fs.writeFileSync(file.path, text);
27
+ };
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ transform: {
3
+ '^.+\\.[j|t]sx?$': 'esbuild-jest',
4
+ },
5
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@frozzare/pkg",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "format": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
9
+ "lint": "eslint src --ext .ts",
10
+ "prepublishOnly": "npm run build",
11
+ "test": "jest"
12
+ },
13
+ "keywords": [],
14
+ "author": "Fredrik Forsmo <fredrik.forsmo@gmail.com>",
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "esbuild": "^0.17.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/jest": "^29.4.0",
21
+ "@typescript-eslint/eslint-plugin": "^5.51.0",
22
+ "@typescript-eslint/parser": "^5.51.0",
23
+ "esbuild-jest": "^0.5.0",
24
+ "eslint": "^8.33.0",
25
+ "jest": "^29.4.2",
26
+ "prettier": "^2.8.4",
27
+ "rimraf": "^4.1.2",
28
+ "tsx": "^3.12.3",
29
+ "typescript": "^4.9.5"
30
+ }
31
+ }
package/src/index.ts ADDED
@@ -0,0 +1,37 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { build as esbuild, BuildOptions as ESBuildOptions } from 'esbuild';
4
+
5
+ export type BuildOptions = Omit<ESBuildOptions, 'bundle' | 'entryPoints'>;
6
+
7
+ export const build = async (entryPoint: string, config: BuildOptions = {}) => {
8
+ const out = await esbuild({
9
+ ...config,
10
+ entryPoints: [entryPoint],
11
+ bundle: true,
12
+ write: false,
13
+ });
14
+
15
+ const file = out.outputFiles[0];
16
+ let text = file.text;
17
+
18
+ if (config.format === 'cjs') {
19
+ const reg = /module.exports = __toCommonJS\((\w+)\);/;
20
+ const match = text.match(reg);
21
+
22
+ if (!match) {
23
+ throw new Error('Failed to find export name');
24
+ }
25
+
26
+ const name = match?.[1].replace('_exports', '_default');
27
+ text = text.replace(match?.[0] || '', '') + `\nmodule.exports = ${name}`;
28
+ }
29
+
30
+ if (!config.write) {
31
+ return text;
32
+ }
33
+
34
+ fs.mkdirSync(path.dirname(file.path));
35
+
36
+ return !config.write ? text : fs.writeFileSync(file.path, text);
37
+ };
@@ -0,0 +1,45 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`build should build cjs 1`] = `
4
+ ""use strict";
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+
23
+ // test/fixtures/class.ts
24
+ var class_exports = {};
25
+ __export(class_exports, {
26
+ default: () => class_default
27
+ });
28
+
29
+ var Test = class {
30
+ };
31
+ var class_default = Test;
32
+
33
+ module.exports = class_default"
34
+ `;
35
+
36
+ exports[`build should build esm 1`] = `
37
+ ""use strict";
38
+ (() => {
39
+ // test/fixtures/class.ts
40
+ var Test = class {
41
+ };
42
+ var class_default = Test;
43
+ })();
44
+ "
45
+ `;
@@ -0,0 +1,21 @@
1
+ import { build } from '../src';
2
+
3
+ describe('build', () => {
4
+ it('should build esm', async () => {
5
+ const output = await build(`${__dirname}/fixtures/class.ts`, {
6
+ write: false,
7
+ });
8
+
9
+ expect(output).toMatchSnapshot();
10
+ });
11
+
12
+ it('should build cjs', async () => {
13
+ const output = await build(`${__dirname}/fixtures/class.ts`, {
14
+ write: false,
15
+ format: 'cjs',
16
+ });
17
+
18
+ expect(output).toMatchSnapshot();
19
+ expect(output).toContain('module.exports = class_default');
20
+ });
21
+ });
@@ -0,0 +1,3 @@
1
+ class Test {}
2
+
3
+ export default Test;
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "lib": ["es2018", "dom"],
5
+ "allowJs": false,
6
+ "declaration": true,
7
+ "emitDeclarationOnly": false,
8
+ "outDir": "dist",
9
+ "rootDirs": ["src"],
10
+ "noEmit": false,
11
+ "strict": true,
12
+ "moduleResolution": "node",
13
+ "types": ["node"],
14
+ "allowSyntheticDefaultImports": true,
15
+ "esModuleInterop": true,
16
+ "experimentalDecorators": true,
17
+ "emitDecoratorMetadata": true,
18
+ "skipLibCheck": true,
19
+ "baseUrl": "src",
20
+ "forceConsistentCasingInFileNames": true,
21
+ "noImplicitReturns": true,
22
+ "suppressImplicitAnyIndexErrors": true,
23
+ "noUnusedLocals": true
24
+ },
25
+ "include": ["src"]
26
+ }