Skip to content
This repository was archived by the owner on May 23, 2021. It is now read-only.

Commit 134f6da

Browse files
committedJul 6, 2019
feat(build): Upgrade babel Config
BREAKING CHANGE: this build output will not work on IE 11 anymore - Add "targets" to "esmodules" for async/await - Add babel-plugin-static-fs for inliing static content - it will improve browser compatibility fix #23 fix #31
1 parent 2d7d1d5 commit 134f6da

13 files changed

+754
-75
lines changed
 

‎bin/cmd.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env node
2-
var spawn = require("cross-spawn");
3-
var script = process.argv[2];
4-
var args = process.argv.slice(3);
2+
const spawn = require("cross-spawn");
3+
const script = process.argv[2];
4+
const args = process.argv.slice(3);
55

66
switch (script) {
77
case "init":
88
case "build":
99
case "test":
10-
var result = spawn.sync("node", [require.resolve("../scripts/" + script)].concat(args), { stdio: "inherit" });
10+
const result = spawn.sync("node", [require.resolve("../scripts/" + script)].concat(args), { stdio: "inherit" });
1111
process.exit(result.status);
1212
break;
1313
default:

‎configs/babel-register.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
"use strict";
2-
require("@babel/register")(require("./babelrc"));
1+
require("@babel/register")(require("./babel.config"));

‎configs/babel.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
"@babel/env",
5+
{
6+
// For async/await support
7+
// https://babeljs.io/docs/en/babel-preset-env#targetsesmodules
8+
targets: {
9+
esmodules: true
10+
}
11+
}
12+
]
13+
],
14+
plugins: [
15+
// inline fs content
16+
"static-fs"
17+
]
18+
};

‎configs/babelrc.js

-3
This file was deleted.

‎example/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"build": "textlint-scripts build",
55
"test": "textlint-scripts test"
66
},
7+
"dependencies": {
8+
"prh": "^5.4.3"
9+
},
710
"devDependencies": {
811
"textlint-scripts": "file:../"
912
}

‎example/src/common.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const prh = require("prh");
2+
const fs = require("fs");
3+
const path = require("path");
4+
module.exports = function(text) {
5+
const dict = fs.readFileSync(path.join(__dirname, "prh.yml"), "utf-8");
6+
const engine = prh.fromYAML("", dict);
7+
return engine.makeChangeSet("", text);
8+
};

‎example/src/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
"use strict";
2-
import assert from "assert";
2+
import mod from "./module";
3+
4+
const path = require("path");
5+
const common = require("./common");
36
module.exports = function(context, options = {}) {
47
const { Syntax, RuleError, report, getSource } = context;
58
return {
9+
// async test
10+
async [Syntax.Code](node) {
11+
return null;
12+
},
613
[Syntax.Str](node) {
714
// "Str" node
815
const text = getSource(text);
16+
// check prh
17+
const result = common(text);
18+
if (result.diffs.length > 0) {
19+
result.diffs.forEach(diff => {
20+
const ruleError = new RuleError("Found " + diff.expected + "!", {
21+
index: diff.index // padding of index
22+
});
23+
report(node, ruleError);
24+
});
25+
}
26+
// check inline
927
if (/bugs/.test(text)) {
1028
const indexOfBugs = text.search(/bugs/);
1129
const ruleError = new RuleError("Found bugs.", {

‎example/src/module.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};

‎example/src/prh.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1
2+
rules:
3+
- expected: jQuery
4+
specs:
5+
- from: jquery
6+
to: jQuery
7+
- from: JQUERY
8+
to: jQuery

‎example/test/index-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ tester.run("rule", rule, {
1313
"OK."
1414
],
1515
invalid: [
16+
// inline file
17+
{
18+
text: "jquery",
19+
errors: [
20+
{
21+
message: "Found jQuery!",
22+
index: 0
23+
}
24+
]
25+
},
1626
// single match
1727
{
1828
text: "It is bugs.",

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@babel/core": "^7.5.0",
4242
"@babel/preset-env": "^7.5.0",
4343
"@babel/register": "^7.4.4",
44+
"babel-plugin-static-fs": "^1.2.0",
4445
"confirmer": "^1.1.2",
4546
"cross-spawn": "^6.0.5",
4647
"mocha": "^6.1.4",

‎scripts/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ process.env.NODE_ENV = "production";
66
const spawn = require("cross-spawn");
77
const args = process.argv.slice(2);
88
const babel = require.resolve(".bin/babel");
9-
const babelrc = require("../configs/babelrc");
9+
const babelConfigFilePath = require.resolve("../configs/babel.config");
1010
// babel src --out-dir lib --watch --source-maps
1111
const child = spawn(
1212
babel,
13-
["--presets", babelrc.presets.join(","), "--source-maps", "--out-dir", "lib", "src"].concat(args)
13+
["--config-file", babelConfigFilePath, "--source-maps", "--out-dir", "lib", "src"].concat(args)
1414
);
1515
child.stderr.on("data", function(data) {
1616
process.stderr.write(data);

0 commit comments

Comments
 (0)