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: support specify plugin as string #1292

Merged
merged 2 commits into from
Jun 19, 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
2 changes: 2 additions & 0 deletions e2e/fixtures/plugins/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ const { files } = parseBuildResult(__dirname);
const content = files["index.js"];
assert(content.includes(`children: "foo.bar"`), `jsx in foo.bar works`);
assert(content.includes(`children: ".bar"`), `jsx in hoo.bar works`);
assert(content.includes(`children: ".haha"`), `plugin in node_modules works`);
assert(content.includes(`children: ".hoo"`), `relative plugin works`);
8 changes: 7 additions & 1 deletion e2e/fixtures/plugins/mako.config.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{"minify": false}
{
"plugins": [
"./plugin",
"plugin"
],
"minify": false
}
13 changes: 13 additions & 0 deletions e2e/fixtures/plugins/node_modules/plugin/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions e2e/fixtures/plugins/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = {
async load(path) {
if (path.endsWith('.hoo')) {
return {
content: `export default () => <Foooo>.hoo</Foooo>;`,
type: 'jsx',
};
}
}
};
1 change: 1 addition & 0 deletions e2e/fixtures/plugins/src/foo.haha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// should be handled with load hook
1 change: 1 addition & 0 deletions e2e/fixtures/plugins/src/foo.hoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// should be handled with load hook
2 changes: 2 additions & 0 deletions e2e/fixtures/plugins/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
console.log(require('./foo.bar'));
console.log(require('./hoo.bar'));
console.log(require('./foo.haha'));
console.log(require('./foo.hoo'));
4 changes: 4 additions & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string;
map?: string;
}
export interface JsHooks {
name?: string;
load?: (
Expand Down
2 changes: 2 additions & 0 deletions packages/mako/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"license": "MIT",
"dependencies": {
"@types/resolve": "^1.20.6",
"@swc/helpers": "0.5.1",
"chalk": "^4.1.2",
"less": "^4.2.0",
Expand All @@ -29,6 +30,7 @@
"piscina": "^4.5.1",
"react-error-overlay": "6.0.9",
"react-refresh": "^0.14.0",
"resolve": "^1.22.8",
"semver": "^7.6.2",
"yargs-parser": "^21.1.1"
},
Expand Down
27 changes: 27 additions & 0 deletions packages/mako/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import { omit } from 'lodash';
import resolve from 'resolve';
import * as binding from '../binding';
import { ForkTSChecker as ForkTSChecker } from './forkTSChecker';
import { LessLoaderOpts, lessLoader } from './lessLoader';
Expand Down Expand Up @@ -118,6 +119,32 @@ export async function build(params: BuildParams) {
}

let plugins = params.config.plugins;
plugins = plugins.map((plugin: any) => {
if (typeof plugin === 'string') {
let fn = require(
resolve.sync(plugin, {
basedir: params.root,
}),
);
return fn.default || fn;
} else {
return plugin;
}
});
makoConfig.plugins?.forEach((plugin: any) => {
if (typeof plugin === 'string') {
let fn = require(
resolve.sync(plugin, {
basedir: params.root,
}),
);
plugins.push(fn.default || fn);
} else {
throw new Error(
`Invalid plugin: ${plugin} in mako.config.json, only support string type plugin here.`,
);
}
});
params.config = omit(params.config, [
'less',
'forkTSChecker',
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading