Skip to content

Commit

Permalink
fix(script-compiler): use static import instead of require
Browse files Browse the repository at this point in the history
If package.json has type=module, webpack does not transpile `require`.
(I dont know the reason).
To use static `import` is a safer way.
  • Loading branch information
azu committed Jan 2, 2024
1 parent 30b6239 commit 37943b8
Showing 1 changed file with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TextlintConfigDescriptor } from "@textlint/config-loader";
import {
TextlintConfigDescriptor,
TextlintConfigFilterRule,
TextlintConfigPlugin,
TextlintConfigRule
} from "@textlint/config-loader";
import type { TextlintFixResult, TextlintResult } from "@textlint/types";
import { TextlintScriptMetadata } from "@textlint/script-parser";

Expand Down Expand Up @@ -59,41 +64,69 @@ export const generateCode = async (config: TextlintConfigDescriptor) => {
return JSON.stringify(item, null, 4).replace(/"!__(.*)__!"/g, "$1");
};

/*
import __rule1 from "rule1"
const rules = [
{
ruleId: "rule1",
rule: __rule1,
options: {}
}
];
*/
const createImportCode = (a: TextlintConfigRule, index: number) => {
if (a.type === "Rule") {
return `import __rule${index} from "${a.moduleName}";`;
} else if (a.type === "RuleInPreset") {
return `import __rulePreset${index} from "${a.moduleName}";
const __rule${index} = __rulePreset${index}.rules["${a.ruleKey}"];`;
} else {
throw new Error("Unknown type");
}
};
const createFilterRuleImportCode = (a: TextlintConfigFilterRule, index: number) => {
return `import __filterRule${index} from "${a.moduleName}";`;
};
const createPluginImportCode = (a: TextlintConfigPlugin, index: number) => {
return `import __plugin${index} from "${a.moduleName}";`;
};
return `// Generated webworker code by textlint-script-compiler
import { TextlintKernel } from "@textlint/kernel";
import { moduleInterop } from "@textlint/module-interop";
import { parseOptionsFromConfig } from "@textlint/config-partial-parser"
// import rules
${config.rules.map(createImportCode).join("\n")}
// import filterRules
${config.filterRules.map(createFilterRuleImportCode).join("\n")}
// import plugins
${config.plugins.map(createPluginImportCode).join("\n")}
// ====
const kernel = new TextlintKernel();
const rules = ${stringify(
config.rules.flatMap((rule) => {
return rule.type === "Rule"
? {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.filePath}'))__!`,
options: rule.options
}
: {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.moduleName}').rules['${rule.ruleKey}'])__!`,
options: rule.options
};
config.rules.map((rule, index) => {
return {
ruleId: rule.ruleId,
rule: `!__moduleInterop(__rule${index})__!`,
options: rule.options
};
})
)};
const filterRules = ${stringify(
config.filterRules.map((rule) => {
config.filterRules.map((rule, index) => {
return {
ruleId: rule.ruleId,
rule: `!__moduleInterop(require('${rule.moduleName}'))__!`,
rule: `!__moduleInterop(__filterRule${index})__!`,
options: rule.options
};
})
)};
const plugins = ${stringify(
config.plugins.map((plugin) => {
config.plugins.map((rule, index) => {
return {
pluginId: plugin.pluginId,
plugin: `!__moduleInterop(require('${plugin.moduleName}'))__!`,
options: plugin.options
pluginId: rule.pluginId,
plugin: `!__moduleInterop(__plugin${index})__!`,
options: rule.options
};
})
)};
Expand Down

0 comments on commit 37943b8

Please sign in to comment.