Skip to content

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
merged 7 commits into from
Jan 2, 2024

Conversation

azu
Copy link
Member

@azu azu commented Jan 2, 2024

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

If package.json has type=module, webpack does not transpile `require`.
(I dont know the reason).
To use static `import` is a safer way.
@azu azu linked an issue Jan 2, 2024 that may be closed by this pull request
@azu azu changed the title fix(script-compiler): use static import instead of require fix(script-compiler): use static import instead of require for template Jan 2, 2024
@azu azu added the Type: Bug Bug or Bug fixes label 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")}
Copy link
Member Author

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.

@azu azu merged commit 97f9ad4 into master Jan 2, 2024
@azu azu deleted the feature/84 branch January 2, 2024 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug or Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Any plans to support ESM on textlint-script-compiler?
1 participant