Skip to content

Commit

Permalink
fix(script-compiler): use static import instead of require for te…
Browse files Browse the repository at this point in the history
…mplate (#85)

* fix(script-compiler): use static import instead of require

If package.json has type=module, webpack does not transpile `require`.
(I dont know the reason).
To use static `import` is a safer way.

* use import type

* remove unused code

* fix

* add debug log

* CI: add cache

* fix; module name path
  • Loading branch information
azu authored Jan 2, 2024
1 parent 30b6239 commit 97f9ad4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 87 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- name: setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
cache: "yarn"
node-version: ${{ matrix.node-version }}
- name: Install
run: yarn install
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TextlintConfigDescriptor } from "@textlint/config-loader";
import type {
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,40 +64,72 @@ 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 normalizeModuleName = (moduleName: string) => {
// window path -> posix path
return moduleName.replace(/\\/g, "/");
};
const createImportCode = (a: TextlintConfigRule, index: number) => {
if (a.type === "Rule") {
return `import __rule${index} from "${normalizeModuleName(a.moduleName)}";`;
} else if (a.type === "RuleInPreset") {
return `import __rulePreset${index} from "${normalizeModuleName(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 "${normalizeModuleName(a.moduleName)}";`;
};
const createPluginImportCode = (a: TextlintConfigPlugin, index: number) => {
return `import __plugin${index} from "${normalizeModuleName(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((plugin, index) => {
return {
pluginId: plugin.pluginId,
plugin: `!__moduleInterop(require('${plugin.moduleName}'))__!`,
plugin: `!__moduleInterop(__plugin${index})__!`,
options: plugin.options
};
})
Expand Down

0 comments on commit 97f9ad4

Please sign in to comment.