Skip to content
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

feat(textlint-scripts): support type=module #1126

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/textlint-scripts/configs/babel-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ const babelConfig = require("./babel.config");
require("@babel/register")({
plugins: babelConfig.plugins,
presets: babelConfig.presets,
extensions: useTypeScript ? [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"] : [".es6", ".es", ".jsx", ".js", ".mjs"]
extensions: useTypeScript
? [".es6", ".es", ".jsx", ".js", ".cjs", ".mjs", ".ts", ".mts", ".cts"]
: [".es6", ".es", ".jsx", ".js", ".cjs", ".mjs"]
});
35 changes: 19 additions & 16 deletions packages/textlint-scripts/configs/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
const fs = require("fs");
const paths = require("../configs/paths");
const useTypeScript = fs.existsSync(paths.appTsConfig);
const { useTypeScript, useESModules } = require("./conditions.js");
const NO_INLINE = !!process.env.NO_INLINE;
module.exports = {
presets: [
[
"@babel/env",
{
// For async/await support
// https://babeljs.io/docs/en/babel-preset-env#targetsesmodules
targets: {
esmodules: true
presets: useESModules
// If use native ES Modules, don't transpile
? []
// transpile for Node.js CJS
: [
[
"@babel/env",
{
// For async/await support
// https://babeljs.io/docs/en/babel-preset-env#targetsesmodules
targets: {
esmodules: true
}
}
}
]
].concat(useTypeScript ? [["@babel/preset-typescript"]] : []),
]
].concat(useTypeScript ? [["@babel/preset-typescript"]] : []),
plugins: NO_INLINE
? []
: [
// inline fs content
"static-fs"
]
// inline fs content
"static-fs"
]
};
14 changes: 14 additions & 0 deletions packages/textlint-scripts/configs/conditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const paths = require("../configs/paths");
/**
* Return true if TypeScript is enabled
* @type {boolean}
*/
const useTypeScript = fs.existsSync(paths.appTsConfig);
/**
* Return true if package.json has `type: "module"`
* @type {boolean}
*/
const useESModules = fs.existsSync(paths.packageJson) && require(paths.packageJson).type === "module";
module.exports.useTypeScript = useTypeScript;
module.exports.useESModules = useESModules;
3 changes: 2 additions & 1 deletion packages/textlint-scripts/configs/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ const path = require("path");
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
module.exports = {
appTsConfig: resolveApp("tsconfig.json")
appTsConfig: resolveApp("tsconfig.json"),
packageJson: resolveApp("package.json")
};
2 changes: 1 addition & 1 deletion packages/textlint-scripts/configs/ts-node-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
const fs = require("fs");
const paths = require("../configs/paths");
const useTypeScript = fs.existsSync(paths.appTsConfig);
const { useTypeScript } = require("./conditions.js");
if (!useTypeScript) {
throw new Error(`${paths.appTsConfig} not found.

Expand Down
2 changes: 1 addition & 1 deletion packages/textlint-scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ process.env.NODE_ENV = "production";
const fs = require("fs");
const spawn = require("cross-spawn");
const paths = require("../configs/paths");
const { useTypeScript } = require("../configs/conditions.js");
const args = process.argv.slice(2);
const babel = require.resolve(".bin/babel");
const babelConfigFilePath = require.resolve("../configs/babel.config");

const useTypeScript = fs.existsSync(paths.appTsConfig);
// babel src --out-dir lib --watch --source-maps
const babelCommand = [babel, "--config-file", `"${babelConfigFilePath}"`, "--source-maps", "--out-dir", "lib", "src"]
.concat(useTypeScript ? ["--extensions", ".ts"] : [])
Expand Down
15 changes: 8 additions & 7 deletions packages/textlint-scripts/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
process.env.NODE_ENV = "test";
const spawn = require("cross-spawn");
const fs = require("fs");
const { useTypeScript, useESModules } = require("../configs/conditions.js");
const paths = require("../configs/paths.js");
const args = process.argv.slice(2);
const paths = require("../configs/paths");
const useTypeScript = fs.existsSync(paths.appTsConfig);
const mocha = require.resolve(".bin/mocha");
// mocha
const babelRegisterPath = require.resolve("../configs/babel-register");
const mochaCommand = [mocha, "--require", `"${babelRegisterPath}"`, "--timeout", "10000"]
const mochaCommand = [mocha, "--timeout", "10000"]
.concat(useESModules ? ["--loader", `${/* TODO: babel loader */}`] : ["--require", `"${babelRegisterPath}"`])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • babel official loader does not exists yet

.concat(useTypeScript ? ["--watch-extensions", "ts"] : [])
.concat(useTypeScript ? [`"test/**/*.{js,ts}"`] : [`"test/**/*.js"`])
.concat(args)
Expand All @@ -20,15 +21,15 @@ const command = useTypeScript ? `${tscCommand} && ${mochaCommand}` : mochaComman
const child = spawn(command, {
shell: true
});
child.stderr.on("data", function (data) {
child.stderr.on("data", function(data) {
process.stderr.write(data);
});
child.stdout.on("data", function (data) {
child.stdout.on("data", function(data) {
process.stdout.write(data);
});
child.on("error", function (error) {
child.on("error", function(error) {
console.error(error);
});
child.on("close", function (code) {
child.on("close", function(code) {
process.exit(code);
});