Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(peggy-loader): allow inject dependencies by importing inline #1368

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4959,7 +4959,7 @@ __metadata:
lint-all: "workspace:~"
prettier: "npm:~2.8.7"
peerDependencies:
prettier: ~2.7.1
prettier: "*"
languageName: unknown
linkType: soft

Expand Down
Loading