Skip to content

Commit

Permalink
feat(textlint-script): support import() (#1284)
Browse files Browse the repository at this point in the history
* feat(textlint-script): support `import()`

textlint-script should not transpile `import()` to `require()`.
It will be required for ESM support.

* remove unused test

* test: normalize path
  • Loading branch information
azu committed Nov 7, 2023
1 parent 5f81d98 commit 9925b62
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 50 deletions.
46 changes: 29 additions & 17 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"packages/@textlint/*",
"packages/textlint-scripts/example",
"packages/textlint-scripts/example-ts",
"packages/textlint-scripts/example-dynamic-import",
"test/*",
"website"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { TextlintKernelOptions } from "../src/textlint-kernel-interface";
import { TextlintKernel } from "../src";

const SNAPSHOTS_DIRECTORY = path.join(__dirname, "snapshots");
// normalize based on the OS
const normalizePath = (value: string) => {
return path.sep === "\\" ? value.replace(/\\/g, "/") : value;
};
const pathReplacer = (dirPath: string) => {
return function replacer(key: string, value: any) {
if (key === "filePath") {
return value.replace(dirPath, "<root>");
return normalizePath(value.replace(dirPath, "<root>"));
}
return value;
};
Expand Down
6 changes: 5 additions & 1 deletion packages/textlint-scripts/configs/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ module.exports = {
// https://babeljs.io/docs/en/babel-preset-env#targetsesmodules
targets: {
esmodules: true
}
},
// Allow to use native `import()` for loading ESM modules
// https://github.com/babel/babel/issues/10194
// TODO: It will not required in Babel 8
exclude: ["proposal-dynamic-import"]
}
]
].concat(useTypeScript ? [["@babel/preset-typescript"]] : []),
Expand Down
85 changes: 85 additions & 0 deletions packages/textlint-scripts/example-dynamic-import/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Debug log from npm
npm-debug.log


### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Global/JetBrains.gitignore

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


/lib
14 changes: 14 additions & 0 deletions packages/textlint-scripts/example-dynamic-import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "textlint-scripts-example-dynamic-import",
"version": "13.3.2",
"private": true,
"scripts": {
"build": "textlint-scripts build",
"test": "textlint-scripts test"
},
"dependencies": {
},
"devDependencies": {
"textlint-scripts": "^13.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function esmWorker() {
return "from esm";
}
14 changes: 14 additions & 0 deletions packages/textlint-scripts/example-dynamic-import/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function (context, options = {}) {
const { Syntax, RuleError, report, getSource } = context;
return {
async [Syntax.Document](node) {
const { esmWorker } = await import("./esm-worker.mjs");
const result = await esmWorker();
const text = getSource(node);
if (/valid/.test(text)) {
return;
}
report(node, new RuleError(`esmWorker result: ${result}`));
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
// See https://github.com/textlint/textlint/tree/master/packages/textlint-tester
import TextLintTester from "textlint-tester";
// rule
import rule from "../src/index";

const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
tester.run("rule", rule, {
valid: ["valid"],
invalid: [
{
text: "error",
errors: [
{
message: "esmWorker result: from esm",
index: 0
}
]
}
]
});
31 changes: 0 additions & 31 deletions packages/textlint-tester/test/textlint-tester-invalid-test.ts

This file was deleted.

0 comments on commit 9925b62

Please sign in to comment.