Skip to content

Commit

Permalink
feat(peggy-loader): allow inject dependencies by importing inline (#1368
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ggazzo authored Oct 17, 2024
1 parent 1ca8371 commit c276aa5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
37 changes: 18 additions & 19 deletions packages/peggy-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import type {
BuildOptionsBase,
OutputFormatAmdCommonjsEs,
OutputFormatBare,
OutputFormatGlobals,
OutputFormatUmd,
SourceOptionsBase,
} from 'peggy';
import type { BuildOptionsBase, OutputFormatAmdCommonjsEs } from 'peggy';
import peggy from 'peggy';
import type { LoaderContext } from 'webpack';

type Options =
| BuildOptionsBase &
(
| Omit<
OutputFormatAmdCommonjsEs<'source'>,
keyof SourceOptionsBase<'source'>
>
| Omit<OutputFormatUmd<'source'>, keyof SourceOptionsBase<'source'>>
| Omit<OutputFormatGlobals<'source'>, keyof SourceOptionsBase<'source'>>
| Omit<OutputFormatBare<'source'>, keyof SourceOptionsBase<'source'>>
);
type Options = Partial<BuildOptionsBase & OutputFormatAmdCommonjsEs<'source'>>;

function peggyLoader(
this: LoaderContext<Options>,
grammarContent: string
): string {
const dependenciesKeyValue = this.resourceQuery
? [...new URLSearchParams(this.resourceQuery).entries()]
: [];

const fromEntries = (entries: (readonly [string, string])[]) => {
const obj: Record<string, string> = {};
for (const [key, value] of entries) {
obj[key] = value;
}
return obj;
};

const { dependencies } = this.getOptions();
const options: Options = {
format: 'commonjs',
...this.getOptions(),
...(dependenciesKeyValue.length && {
dependencies: { ...fromEntries(dependenciesKeyValue), ...dependencies },
}),
};

return peggy.generate(grammarContent, {
Expand Down
2 changes: 1 addition & 1 deletion packages/peggy-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2015",
"module": "ESNext",
"declaration": true,
"declarationMap": true,
Expand Down

0 comments on commit c276aa5

Please sign in to comment.