-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(script-compiler): use static import
instead of require
for template
#85
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If package.json has type=module, webpack does not transpile `require`. (I dont know the reason). To use static `import` is a safer way.
import
instead of require
for template
azu
commented
Jan 2, 2024
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")} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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
import __rule0 from "textlint-rule-no-dropping-the-ra";
// import filterRules
// import plugins
import __plugin0 from "@textlint/textlint-plugin-text";
import __plugin1 from "@textlint/textlint-plugin-markdown";
// ====
const kernel = new TextlintKernel();
const rules = [
{
"ruleId": "no-dropping-the-ra",
"rule": moduleInterop(__rule0),
"options": true
}
];
const filterRules = [];
const plugins = [
{
"pluginId": "@textlint/text",
"plugin": moduleInterop(__plugin0),
"options": true
},
{
"pluginId": "@textlint/markdown",
"plugin": moduleInterop(__plugin1),
"options": true
}
];
const config = {
rules: rules,
filterRules: filterRules,
plugins: plugins
};
// merge config
const assignConfig = (textlintrc) => {
const userDefinedConfig = parseOptionsFromConfig(textlintrc);
if (userDefinedConfig.rules) {
config.rules = config.rules.map(rule => {
const override = userDefinedConfig.rules.find(o => o.ruleId === rule.ruleId);
return { ...rule, ...override };
});
}
if (userDefinedConfig.filterRules) {
config.filterRules = config.filterRules.map(rule => {
const override = userDefinedConfig.filterRules.find(o => o.ruleId === rule.ruleId);
return { ...rule, ...override };
});
}
if (userDefinedConfig.plugins) {
config.plugins = config.plugins.map(rule => {
const override = userDefinedConfig.plugins.find(o => o.pluginId === rule.pluginId);
return { ...rule, ...override };
});
}
};
self.addEventListener('error', (event) => {
self.postMessage({
command: "error",
// wrapping any type error with Error
error: new Error("unexpected error", {
cause: event.error
})
})
});
self.addEventListener('unhandledrejection', (event) => {
self.postMessage({
command: "error",
// wrapping any type error with Error
error: new Error("unexpected unhandled promise rejection", {
cause: event.reason
})
})
});
self.addEventListener('message', (event) => {
const data = event.data;
const rules = data.ruleId === undefined
? config.rules
: config.rules.filter(rule => rule.ruleId === data.ruleId);
switch (data.command) {
case "merge-config":
return assignConfig(data.textlintrc);
case "lint":
return kernel.lintText(data.text, {
rules: rules,
filterRules: config.filterRules,
plugins: config.plugins,
filePath: "/path/to/README" + data.ext,
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "lint:result",
result
});
}).catch(error => {
return self.postMessage({
id: data.id,
command: "error",
// wrapping any type error with Error
error: new Error("failed to lint text", {
cause: error
})
})
});
case "fix":
return kernel.fixText(data.text, {
rules: rules,
filterRules: config.filterRules,
plugins: config.plugins,
filePath: "/path/to/README" + data.ext,
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "fix:result",
result
});
}).catch(error => {
return self.postMessage({
id: data.id,
command: "error",
error
})
});
default:
console.log("Unknown command: " + data.command);
}
});
// ====
self.postMessage({
command: "init",
metadata: process.env.TEXTLINT_SCRIPT_METADATA
});
An example template result for script-compier.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Current script-compiler template use
require
.webpack allow to compile it and inlining the module.
But, If package.json has type=module, webpack does not transpile
require
. (I dont know the reason).Using static
import
is a safer way.Additionaly, this change will help us to support ESM-ed rules/plugins.
fix #84