@gjsify/esbuild-plugin-gjsify 0.0.2 → 0.0.3

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.
@@ -1,4 +1,4 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
1
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
2
2
  import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
3
3
  import { merge } from "lodash";
4
4
  import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
@@ -7,16 +7,19 @@ import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
7
7
  import type { PluginBuild, BuildOptions } from "esbuild";
8
8
  import type { PluginOptions } from '../types/plugin-options.js';
9
9
 
10
- export const setupEsmLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
10
+ export const setupLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
11
+
12
+ const format = pluginOptions.format || 'esm';
11
13
 
12
14
  pluginOptions.aliases ||= {};
13
15
  pluginOptions.exclude ||= [];
14
16
 
15
17
  const esbuildOptions: BuildOptions = {
18
+ format,
16
19
  bundle: false,
17
20
  minify: false,
18
21
  sourcemap: false,
19
- splitting: true, // Works only on esm
22
+ splitting: format === 'esm' ? true : false, // Works only on esm
20
23
  loader: {
21
24
  '.ts': 'ts',
22
25
  '.mts': 'ts',
@@ -29,10 +32,10 @@ export const setupEsmLib = async (build: PluginBuild, pluginOptions: PluginOptio
29
32
  '.js': 'ts',
30
33
  },
31
34
  target: [ "esnext" ],
32
- platform: "browser",
35
+ platform: "neutral",
36
+ mainFields: format === 'esm' ? ['module', 'main'] : ['main'],
33
37
  // https://esbuild.github.io/api/#conditions
34
- conditions: ['module','import'],
35
- format: 'esm'
38
+ conditions: format === 'esm' ? ['module','import'] : ['require'],
36
39
  };
37
40
 
38
41
  merge(build.initialOptions, esbuildOptions);
@@ -0,0 +1,46 @@
1
+ declare module 'lodash' {
2
+ /**
3
+ * Recursively merges own and inherited enumerable properties of source
4
+ * objects into the destination object, skipping source properties that resolve
5
+ * to `undefined`. Array and plain object properties are merged recursively.
6
+ * Other objects and value types are overridden by assignment. Source objects
7
+ * are applied from left to right. Subsequent sources overwrite property
8
+ * assignments of previous sources.
9
+ *
10
+ * **Note:** This method mutates `object`.
11
+ *
12
+ * @category Object
13
+ * @param object The destination object.
14
+ * @param [sources] The source objects.
15
+ * @returns Returns `object`.
16
+ * @example
17
+ *
18
+ * var users = {
19
+ * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
20
+ * };
21
+ *
22
+ * var ages = {
23
+ * 'data': [{ 'age': 36 }, { 'age': 40 }]
24
+ * };
25
+ *
26
+ * _.merge(users, ages);
27
+ * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
28
+ */
29
+ function merge<TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
30
+ /**
31
+ * @see _.merge
32
+ */
33
+ function merge<TObject, TSource1, TSource2>(object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2;
34
+ /**
35
+ * @see _.merge
36
+ */
37
+ function merge<TObject, TSource1, TSource2, TSource3>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3;
38
+ /**
39
+ * @see _.merge
40
+ */
41
+ function merge<TObject, TSource1, TSource2, TSource3, TSource4>(object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4;
42
+ /**
43
+ * @see _.merge
44
+ */
45
+ function merge(object: any, ...otherArgs: any[]): any;
46
+ }
package/src/plugin.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Plugin } from "esbuild";
2
2
  import type { PluginOptions } from './types/plugin-options.js';
3
- import { setupCjsLib, setupEsmLib } from './lib/index.js';
3
+ import { setupLib } from './lib/index.js';
4
4
  import { setupForGjs, setupForNode, setupForDeno, setupForBrowser } from './app/index.js';
5
5
 
6
6
  export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
@@ -12,9 +12,8 @@ export const gjsifyPlugin = (pluginOptions: PluginOptions = {}) => {
12
12
  if(pluginOptions.library) {
13
13
  switch (pluginOptions.library) {
14
14
  case 'esm':
15
- return setupEsmLib(build, pluginOptions)
16
15
  case 'cjs':
17
- return setupCjsLib(build, pluginOptions)
16
+ return setupLib(build, pluginOptions)
18
17
  default:
19
18
  throw new TypeError('Unknown library type: ' + pluginOptions.library);
20
19
  }
@@ -8,7 +8,7 @@ export interface PluginOptions extends DeepkitPluginOptions {
8
8
  /** An array of glob patterns to exclude matches and aliases */
9
9
  exclude?: string[];
10
10
  jsExtension?: string;
11
- /** Override the format, only be considered if the target app platform is `'node'`, otherwise it is always `'esm'` */
11
+ /** Override the format */
12
12
  format?: 'esm' | 'cjs'
13
13
  /**
14
14
  * Library Mode
@@ -1,6 +1,6 @@
1
- export const getJsExtensions = (allowExt: string) => {
1
+ export const getJsExtensions = (allowExt?: string) => {
2
2
  const extensions = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
3
- if(extensions[allowExt]) {
3
+ if(allowExt && extensions[allowExt]) {
4
4
  delete extensions[allowExt]
5
5
  }
6
6
  return extensions;
package/tsconfig.json CHANGED
@@ -3,11 +3,15 @@
3
3
  "module": "ESNext",
4
4
  "target": "ESNext",
5
5
  "outDir": "dist",
6
+ "types": ["node"],
7
+ "lib": ["ESNext", "DOM"],
6
8
  "declarationDir": "dist/types",
7
9
  "declaration": true,
10
+ "emitDeclarationOnly": true,
8
11
  "allowSyntheticDefaultImports": true,
9
12
  "moduleResolution": "bundler",
10
13
  "allowImportingTsExtensions": true
11
14
  },
15
+ "include": ["src/lodash.d.ts"],
12
16
  "files": ["src/index.ts"]
13
17
  }
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "esbuild";
2
- export declare const debugPlugin: () => Plugin;
@@ -1,3 +0,0 @@
1
- import type { PluginBuild } from "esbuild";
2
- import type { PluginOptions } from '../types/plugin-options.js';
3
- export declare const setupEsmLib: (build: PluginBuild, pluginOptions: PluginOptions) => Promise<void>;
@@ -1,68 +0,0 @@
1
- import { existsSync } from "fs";
2
- import { realpath } from "fs/promises";
3
-
4
- import type { Plugin } from "esbuild";
5
-
6
- export const aliasPlugin = (aliasObj: Record<string, string>) => {
7
- const aliases = Object.keys(aliasObj);
8
- const re = new RegExp(`^(${aliases.map(x => escapeRegExp(x)).join('|')})$`);
9
-
10
- const plugin: Plugin = {
11
- name: 'alias',
12
- setup(build) {
13
- // we do not register 'file' namespace here, because the root file won't be processed
14
- // https://github.com/evanw/esbuild/issues/791
15
- build.onResolve({ filter: re }, async (args) => {
16
- let resolvedAliasPath = aliasObj[args.path];
17
-
18
- let namespace = args.namespace;
19
-
20
- if (resolvedAliasPath) {
21
-
22
- if (resolvedAliasPath.startsWith('http://')) {
23
- namespace = 'http';
24
- resolvedAliasPath = resolvedAliasPath.slice(5)
25
- } else if (resolvedAliasPath.startsWith('https://')) {
26
- namespace = 'https';
27
- resolvedAliasPath = resolvedAliasPath.slice(6)
28
- } else {
29
- const resolvedAlias = (await build.resolve(resolvedAliasPath, {
30
- importer: args.importer,
31
- kind: args.kind,
32
- namespace: namespace,
33
- resolveDir: args.resolveDir,
34
- pluginData: args.pluginData,
35
- }));
36
-
37
- if (resolvedAlias.errors) {
38
- return resolvedAlias;
39
- } else {
40
- resolvedAliasPath = resolvedAlias.path;
41
- namespace = resolvedAlias.namespace;
42
- }
43
- }
44
-
45
- if (existsSync(resolvedAliasPath)) {
46
- resolvedAliasPath = await realpath(resolvedAliasPath);
47
- }
48
-
49
- // console.debug(`resolvedAliasPath: ${args.path} -> ${resolvedAliasPath}`);
50
-
51
- return {
52
- path: resolvedAliasPath,
53
- namespace: namespace,
54
- }
55
- }
56
-
57
- return null;
58
- });
59
- },
60
- };
61
-
62
- return plugin;
63
- };
64
-
65
- function escapeRegExp(str: string) {
66
- // $& means the whole matched string
67
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
68
- }
package/src/lib/cjs.ts DELETED
@@ -1,46 +0,0 @@
1
- import { aliasPlugin } from '../alias-plugin.js';
2
- import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
3
- import { merge } from "lodash";
4
- import { getJsExtensions, globToEntryPoints } from "../utils/index.js";
5
-
6
- // Types
7
- import type { PluginBuild, BuildOptions } from "esbuild";
8
- import type { PluginOptions } from '../types/plugin-options.js';
9
-
10
- export const setupCjsLib = async (build: PluginBuild, pluginOptions: PluginOptions) => {
11
-
12
- pluginOptions.aliases ||= {};
13
- pluginOptions.exclude ||= [];
14
-
15
- const esbuildOptions: BuildOptions = {
16
- bundle: false,
17
- splitting: false, // only works with esm, see https://esbuild.github.io/api/#splitting
18
- minify: false,
19
- sourcemap: false,
20
- loader: {
21
- '.ts': 'ts',
22
- '.mts': 'ts',
23
- '.cts': 'ts',
24
- '.tsx': 'ts',
25
- '.mtsx': 'ts',
26
- '.ctsx': 'ts',
27
- '.mjs': 'ts',
28
- '.cjs': 'ts',
29
- '.js': 'ts',
30
- },
31
- target: ['esnext'],
32
- platform: "browser",
33
- // https://esbuild.github.io/api/#conditions
34
- conditions: ['require'],
35
- format: 'cjs'
36
- };
37
-
38
- merge(build.initialOptions, esbuildOptions);
39
-
40
- build.initialOptions.entryPoints = await globToEntryPoints(build.initialOptions.entryPoints, pluginOptions.exclude)
41
-
42
- if(pluginOptions.debug) console.debug("initialOptions", build.initialOptions);
43
-
44
- await aliasPlugin(pluginOptions.aliases).setup(build);
45
- await transformExtPlugin({ outExtension: getJsExtensions(pluginOptions.jsExtension) }).setup(build);
46
- }