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

Commit

Permalink
feat(build): Upgrade babel Config
Browse files Browse the repository at this point in the history
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
  • Loading branch information
azu committed Jul 6, 2019
1 parent 2d7d1d5 commit 134f6da
Show file tree
Hide file tree
Showing 13 changed files with 754 additions and 75 deletions.
8 changes: 4 additions & 4 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node
var spawn = require("cross-spawn");
var script = process.argv[2];
var args = process.argv.slice(3);
const spawn = require("cross-spawn");
const script = process.argv[2];
const args = process.argv.slice(3);

switch (script) {
case "init":
case "build":
case "test":
var result = spawn.sync("node", [require.resolve("../scripts/" + script)].concat(args), { stdio: "inherit" });
const result = spawn.sync("node", [require.resolve("../scripts/" + script)].concat(args), { stdio: "inherit" });
process.exit(result.status);
break;
default:
Expand Down
3 changes: 1 addition & 2 deletions configs/babel-register.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"use strict";
require("@babel/register")(require("./babelrc"));
require("@babel/register")(require("./babel.config"));
18 changes: 18 additions & 0 deletions configs/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
presets: [
[
"@babel/env",
{
// For async/await support
// https://babeljs.io/docs/en/babel-preset-env#targetsesmodules
targets: {
esmodules: true
}
}
]
],
plugins: [
// inline fs content
"static-fs"
]
};
3 changes: 0 additions & 3 deletions configs/babelrc.js

This file was deleted.

3 changes: 3 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"build": "textlint-scripts build",
"test": "textlint-scripts test"
},
"dependencies": {
"prh": "^5.4.3"
},
"devDependencies": {
"textlint-scripts": "file:../"
}
Expand Down
8 changes: 8 additions & 0 deletions example/src/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const prh = require("prh");
const fs = require("fs");
const path = require("path");
module.exports = function(text) {
const dict = fs.readFileSync(path.join(__dirname, "prh.yml"), "utf-8");
const engine = prh.fromYAML("", dict);
return engine.makeChangeSet("", text);
};
20 changes: 19 additions & 1 deletion example/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
"use strict";
import assert from "assert";
import mod from "./module";

const path = require("path");
const common = require("./common");
module.exports = function(context, options = {}) {
const { Syntax, RuleError, report, getSource } = context;
return {
// async test
async [Syntax.Code](node) {
return null;
},
[Syntax.Str](node) {
// "Str" node
const text = getSource(text);
// check prh
const result = common(text);
if (result.diffs.length > 0) {
result.diffs.forEach(diff => {
const ruleError = new RuleError("Found " + diff.expected + "!", {
index: diff.index // padding of index
});
report(node, ruleError);
});
}
// check inline
if (/bugs/.test(text)) {
const indexOfBugs = text.search(/bugs/);
const ruleError = new RuleError("Found bugs.", {
Expand Down
1 change: 1 addition & 0 deletions example/src/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
8 changes: 8 additions & 0 deletions example/src/prh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
rules:
- expected: jQuery
specs:
- from: jquery
to: jQuery
- from: JQUERY
to: jQuery
10 changes: 10 additions & 0 deletions example/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ tester.run("rule", rule, {
"OK."
],
invalid: [
// inline file
{
text: "jquery",
errors: [
{
message: "Found jQuery!",
index: 0
}
]
},
// single match
{
text: "It is bugs.",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@babel/core": "^7.5.0",
"@babel/preset-env": "^7.5.0",
"@babel/register": "^7.4.4",
"babel-plugin-static-fs": "^1.2.0",
"confirmer": "^1.1.2",
"cross-spawn": "^6.0.5",
"mocha": "^6.1.4",
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ process.env.NODE_ENV = "production";
const spawn = require("cross-spawn");
const args = process.argv.slice(2);
const babel = require.resolve(".bin/babel");
const babelrc = require("../configs/babelrc");
const babelConfigFilePath = require.resolve("../configs/babel.config");
// babel src --out-dir lib --watch --source-maps
const child = spawn(
babel,
["--presets", babelrc.presets.join(","), "--source-maps", "--out-dir", "lib", "src"].concat(args)
["--config-file", babelConfigFilePath, "--source-maps", "--out-dir", "lib", "src"].concat(args)
);
child.stderr.on("data", function(data) {
process.stderr.write(data);
Expand Down
Loading

0 comments on commit 134f6da

Please sign in to comment.