Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Update except babel & jest
Browse files Browse the repository at this point in the history
  • Loading branch information
occar421 committed Jan 1, 2021
1 parent 1269eef commit 469f2b7
Show file tree
Hide file tree
Showing 11 changed files with 1,102 additions and 1,758 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:

- restore_cache:
keys:
- 12-dep-{{ checksum "package.json" }}
- 12-dep-
- 12-dep-{{ checksum "package.json" }}
- 12-dep-

- run: yarn install

Expand All @@ -31,8 +31,8 @@ jobs:

- restore_cache:
keys:
- 14-dep-{{ checksum "package.json" }}
- 14-dep-
- 14-dep-{{ checksum "package.json" }}
- 14-dep-

- run: yarn install

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ Assume this file structure while using CSS Modules.
+- Button.css.d.ts
```

Here, also assume we use Webpack Hot Module Replacement for dev server.
Here, also assume we use Webpack Hot Module Replacement for dev server.
When 'Button.css' is updated, 'Button.css.d.ts' could be updated.
Although, 'Button.tsx' will not be notified that 'Button.css.d.ts' is updated.
If 'Button.css' selector has changed, 'Button.tsx' won't be decorated correctly.
If 'Button.css' selector has changed, 'Button.tsx' won't be decorated correctly.

Fortunately, it seems that webpack detects update with watching the same event when it is `touch`-ed.

'Button.css' -(generate)-> 'Button.css.d.ts' -(watch)-(`touch`)-> 'Button.tsx' recompile.

# TODO

* Options
* Throttle
* [TBD]
- Options
- Throttle
- [TBD]
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
testEnvironment: "node"
testEnvironment: "node",
};
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
"babel-core": "^6.26.0",
"babel-jest": "^22.4.1",
"babel-preset-env": "^1.6.1",
"eslint": "^4.18.2",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"fs-extra": "^5.0.0",
"eslint": "^7.16.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"fs-extra": "^9.0.1",
"jest": "^22.4.2",
"memory-fs": "^0.4.1",
"prettier": "^1.11.1",
"webpack": "^4.0.1"
"memory-fs": "^0.5.0",
"prettier": "^2.2.1",
"webpack": "^5.11.1"
},
"dependencies": {
"loader-utils": "^1.1.0",
"schema-utils": "^0.4.5",
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0",
"touch": "^3.1.0"
},
"scripts": {
Expand Down
30 changes: 15 additions & 15 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ const schema = {
type: "object",
properties: {
test: {
type: "object" // RegExp
type: "object", // RegExp
},
targets: {
type: "array",
minItems: 1,
items: {
type: "string"
}
}
type: "string",
},
},
},
required: ["test", "targets"]
}
required: ["test", "targets"],
},
},
noCreate: {
type: "boolean"
}
type: "boolean",
},
},
required: ["transmitRules"]
required: ["transmitRules"],
};

const getObjectType = obj => {
const getObjectType = (obj) => {
const str = Object.prototype.toString.call(obj); // [object FooBar]
return str.substring(8, str.length - 1);
};
Expand All @@ -54,7 +54,7 @@ export default function loader(source) {
"Options for transmit-update-loader are invalid."
);

if (options.transmitRules.some(r => getObjectType(r.test) !== "RegExp")) {
if (options.transmitRules.some((r) => getObjectType(r.test) !== "RegExp")) {
throw new Error(
"`transmitRules.test` should be regex in transmit-update-loader."
);
Expand All @@ -68,9 +68,9 @@ export default function loader(source) {
(acc, rule) =>
acc.concat(
rule.targets
.map(target => sourcePath.replace(rule.test, target))
.map((target) => sourcePath.replace(rule.test, target))
.filter(
targetPath =>
(targetPath) =>
path.normalize(sourcePath) !== path.normalize(targetPath)
)
),
Expand All @@ -84,10 +84,10 @@ export default function loader(source) {
const callback = this.async();

Promise.all(
targetPaths.map(targetPath =>
targetPaths.map((targetPath) =>
touch(targetPath, {
mtime: true,
nocreate: options.noCreate !== false
nocreate: options.noCreate !== false,
})
)
).then(() => {
Expand Down
10 changes: 5 additions & 5 deletions test/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export default (fixture, options = {}) => {
test: /.$/, // everything
use: {
loader: path.resolve(__dirname, "../src/loader.js"),
options: options
}
}
]
}
options: options,
},
},
],
},
});

compiler.outputFileSystem = new MemoryFs();
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/foo.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FooMd
FooMd
24 changes: 13 additions & 11 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@ import path from "path";
* @param {string} fileName
* @returns {Promise<string>}
*/
export const readFileAsync = fileName =>
fs.readFile(path.join(__dirname, fileName)).then(b => b.toString());
export const readFileAsync = (fileName) =>
fs.readFile(path.join(__dirname, fileName)).then((b) => b.toString());

/**
* @param {string} fileName
* @returns {Promise<{ctime:Date}>}
*/
export const fileStatAsync = fileName =>
export const fileStatAsync = (fileName) =>
fs.stat(path.join(__dirname, fileName));

/**
* @param {string} dirPath
* @returns {array<string>}
*/
const enumerateFiles = dirPath =>
const enumerateFiles = (dirPath) =>
fs
.readdirSync(dirPath)
.map(f => path.join(dirPath, f))
.filter(f => fs.statSync(f).isFile());
.map((f) => path.join(dirPath, f))
.filter((f) => fs.statSync(f).isFile());

/**
* @param {string} fileName
* @returns {Promise<boolean>}
*/
export const existsAsync = fileName =>
export const existsAsync = (fileName) =>
fs.pathExists(path.join(__dirname, fileName));

export const removeGeneratedFiles = () => {
const files = enumerateFiles(path.join(__dirname, "fixtures"));
files.filter(f => f.endsWith(".null")).forEach(f => {
fs.unlink(f, err => {
if (err && err.code !== "ENOENT") throw err;
files
.filter((f) => f.endsWith(".null"))
.forEach((f) => {
fs.unlink(f, (err) => {
if (err && err.code !== "ENOENT") throw err;
});
});
});
};
34 changes: 18 additions & 16 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
removeGeneratedFiles,
readFileAsync,
fileStatAsync,
existsAsync
existsAsync,
} from "./helpers";

afterEach(removeGeneratedFiles);
Expand All @@ -12,7 +12,7 @@ it("should not affect file content in webpack pipeline", async () => {
const sourceFileName = "./fixtures/foo.txt";
const sourceFileContent = await readFileAsync(sourceFileName);
const status = await compiler(sourceFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1.null"] }]
transmitRules: [{ test: /\/(.*$)/, targets: ["$1.null"] }],
});
const output = status.toJson().modules[0].source;
expect(output).toBe(sourceFileContent);
Expand All @@ -23,7 +23,7 @@ it("should not renew itself by default", async () => {
const targetFileName = "./fixtures/foo.txt";
const preStat = await fileStatAsync(targetFileName);
await compiler(targetFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1"] }]
transmitRules: [{ test: /\/(.*$)/, targets: ["$1"] }],
}); // identity mapping
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
Expand All @@ -34,7 +34,7 @@ it("should not renew itself within multi targets", async () => {
const targetFileName = "./fixtures/foo.txt";
const preStat = await fileStatAsync(targetFileName);
await compiler(targetFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] }]
transmitRules: [{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] }],
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
Expand All @@ -47,8 +47,8 @@ it("should not renew itself within multi rules", async () => {
await compiler(targetFileName, {
transmitRules: [
{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] },
{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] }
]
{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] },
],
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
Expand All @@ -59,7 +59,7 @@ it("should renew foo.md from foo.txt", async () => {
const targetFileName = "./fixtures/foo.md";
const preStat = await fileStatAsync(targetFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md"] }]
transmitRules: [{ test: /(\.txt$)/, targets: [".md"] }],
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBeGreaterThan(preStat.ctime.getTime());
Expand All @@ -72,7 +72,7 @@ it("should renew foo.md and foo.asciidoc from foo.txt", async () => {
const preStatMd = await fileStatAsync(targetMdFileName);
const preStatAd = await fileStatAsync(targetAdFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md", ".asciidoc"] }]
transmitRules: [{ test: /(\.txt$)/, targets: [".md", ".asciidoc"] }],
});
const postStatMd = await fileStatAsync(targetMdFileName);
const postStatAd = await fileStatAsync(targetAdFileName);
Expand All @@ -89,7 +89,9 @@ it("should renew foo.md and foo.asciidoc and foo.rst from foo.txt", async () =>
const preStatAd = await fileStatAsync(targetAdFileName);
const preStatRst = await fileStatAsync(targetRstFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md", ".asciidoc", ".rst"] }]
transmitRules: [
{ test: /(\.txt$)/, targets: [".md", ".asciidoc", ".rst"] },
],
});
const postStatMd = await fileStatAsync(targetMdFileName);
const postStatAd = await fileStatAsync(targetAdFileName);
Expand All @@ -110,8 +112,8 @@ it("should renew foo.md from foo.txt and bar.txt from foo.txt", async () => {
await compiler(sourceFileName, {
transmitRules: [
{ test: /(\.txt$)/, targets: [".md"] },
{ test: /(foo)/, targets: ["bar"] }
]
{ test: /(foo)/, targets: ["bar"] },
],
});
const postStatFoo = await fileStatAsync(targetFooFileName);
const postStatBar = await fileStatAsync(targetBarFileName);
Expand All @@ -127,7 +129,7 @@ it("should not create a new file by default", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetFileName = "./fixtures/foo.null";
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".null"] }]
transmitRules: [{ test: /(\.txt$)/, targets: [".null"] }],
});
expect(await existsAsync(targetFileName)).toBe(false);
});
Expand All @@ -137,7 +139,7 @@ it("should create a new file if option is set", async () => {
const targetFileName = "./fixtures/foo.null";
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".null"] }],
noCreate: false
noCreate: false,
});
expect(await existsAsync(targetFileName)).toBe(true);
});
Expand All @@ -147,7 +149,7 @@ it("should renew txt.foo from foo.txt with complex rule", async () => {
const targetFileName = "./fixtures/txt.foo";
const preStat = await fileStatAsync(targetFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\w+)\.(txt$)/, targets: ["$2.$1"] }]
transmitRules: [{ test: /(\w+)\.(txt$)/, targets: ["$2.$1"] }],
});
const postStatFoo = await fileStatAsync(targetFileName);
expect(postStatFoo.ctime.getTime()).toBeGreaterThan(preStat.ctime.getTime());
Expand All @@ -158,7 +160,7 @@ it("should not affect source file content", async () => {
const preSourceFileContent = await readFileAsync(sourceFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(.*$)/, targets: ["$1.null"] }],
noCreate: false
noCreate: false,
});
const postSourceFileContent = await readFileAsync(sourceFileName);
expect(postSourceFileContent).toBe(preSourceFileContent);
Expand All @@ -169,7 +171,7 @@ it("should not affect target file content", async () => {
const targetFileName = "./fixtures/foo.md";
const preTargetFileContent = await readFileAsync(targetFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md"] }]
transmitRules: [{ test: /(\.txt$)/, targets: [".md"] }],
});
const postTargetFileContent = await readFileAsync(targetFileName);
expect(postTargetFileContent).toBe(preTargetFileContent);
Expand Down
8 changes: 4 additions & 4 deletions test/option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ it("should reject null `transmitRules` option", async () => {
it("should reject object `transmitRules` option", async () => {
const targetFileName = "./fixtures/foo.txt";
const status = await compiler(targetFileName, {
transmitRules: { test: /(\.$)/, targets: ["$1.null"] }
transmitRules: { test: /(\.$)/, targets: ["$1.null"] },
});
expect(status.compilation.errors.length).toBe(1);
const error = status.compilation.errors[0];
Expand All @@ -36,7 +36,7 @@ it("should reject string `transmitRules.test` option", async () => {
const targetFileName = "./fixtures/foo.txt";
// eslint-disable-next-line no-useless-escape
const status = await compiler(targetFileName, {
transmitRules: [{ test: "(.$)", targets: ["$1.null"] }]
transmitRules: [{ test: "(.$)", targets: ["$1.null"] }],
});
expect(status.compilation.errors.length).toBe(1);
const error = status.compilation.errors[0];
Expand All @@ -47,7 +47,7 @@ it("should reject string `transmitRules.test` option", async () => {
it("should reject non RegExp `transmitRules.test` option", async () => {
const targetFileName = "./fixtures/foo.txt";
const status = await compiler(targetFileName, {
transmitRules: [{ test: {}, targets: ["$1.null"] }]
transmitRules: [{ test: {}, targets: ["$1.null"] }],
});
expect(status.compilation.errors.length).toBe(1);
const error = status.compilation.errors[0];
Expand All @@ -58,7 +58,7 @@ it("should reject non RegExp `transmitRules.test` option", async () => {
it("should accept RegExp `transmitRules.test` option", async () => {
const targetFileName = "./fixtures/foo.txt";
const status = await compiler(targetFileName, {
transmitRules: [{ test: /(\.$)/, targets: ["$1.null"] }]
transmitRules: [{ test: /(\.$)/, targets: ["$1.null"] }],
});
expect(status.compilation.errors.length).toBe(0);
});
Loading

0 comments on commit 469f2b7

Please sign in to comment.