Skip to content

Commit e002018

Browse files
copy task created
1 parent 9cd5028 commit e002018

10 files changed

+13958
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.DS_Store
33
node_modules
44
dist
5+
/.idea

index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare class WebpackAtomizerPlugin {
2+
apply(compiler: any): void;
3+
}

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
'use strict'
3+
4+
if (process.env.NODE_ENV === 'production') {
5+
module.exports = require('./webpack-atomizer-plugin.cjs.production.min.js')
6+
} else {
7+
module.exports = require('./webpack-atomizer-plugin.cjs.development.js')
8+
}

package-lock.json

+13,822
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "https://github.com/morajlab/webpack-atomizer-plugin/issues"
1616
},
1717
"homepage": "https://github.com/morajlab/webpack-atomizer-plugin#readme",
18-
"version": "0.0.1",
18+
"version": "0.0.2",
1919
"license": "MIT",
2020
"main": "dist/index.js",
2121
"typings": "dist/index.d.ts",
@@ -27,8 +27,9 @@
2727
"node": ">=10"
2828
},
2929
"scripts": {
30+
"task:copy": "ts-node ./task/copy.ts",
3031
"start": "tsdx watch",
31-
"build": "tsdx build",
32+
"build": "tsdx build && npm run task:copy",
3233
"test": "tsdx test",
3334
"lint": "tsdx lint",
3435
"prepare": "tsdx build",
@@ -66,8 +67,10 @@
6667
],
6768
"devDependencies": {
6869
"@size-limit/preset-small-lib": "^4.9.2",
70+
"chalk": "^4.1.0",
6971
"husky": "^5.1.3",
7072
"size-limit": "^4.9.2",
73+
"ts-node": "^9.1.1",
7174
"tsdx": "^0.14.1",
7275
"tslib": "^2.1.0",
7376
"typescript": "^4.2.2"

src/index.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
export const sum = (a: number, b: number) => {
2-
if ('development' === process.env.NODE_ENV) {
3-
console.log('boop');
1+
class WebpackAtomizerPlugin {
2+
apply(compiler: any) {
3+
compiler.hooks.emit.tapAsync(
4+
'WebpackAtomizerPlugin',
5+
(compilation: any, callback: any) => {
6+
console.log('This is an example plugin!');
7+
console.log(
8+
'Here’s the `compilation` object which represents a single build of assets:',
9+
compilation
10+
);
11+
//compilation.addModule(/* ... */);
12+
13+
callback();
14+
}
15+
);
416
}
5-
return a + b;
6-
};
17+
}

task/copy.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { resolve } from 'path';
2+
import fs from 'fs';
3+
4+
let chalk: any;
5+
6+
interface IFileObject {
7+
src: string;
8+
dest: string;
9+
}
10+
11+
export const files: IFileObject[] = [
12+
{
13+
src: 'dist/index.d.ts',
14+
dest: 'index.d.ts',
15+
},
16+
{
17+
src: 'dist/index.js',
18+
dest: 'index.js',
19+
},
20+
];
21+
22+
export const importChalk = (): any => {
23+
if (!chalk) {
24+
chalk = require('chalk');
25+
}
26+
27+
return chalk;
28+
};
29+
30+
export const resolvePath = (f: IFileObject[]) =>
31+
f.map((file: { src: string; dest: string }) => {
32+
let result = file;
33+
34+
Object.entries(file).forEach(
35+
value =>
36+
((result as any)[value[0]] = resolve(
37+
__dirname,
38+
`../${(file as any)[value[0]]}`
39+
))
40+
);
41+
42+
return result;
43+
});
44+
45+
export const pathExist = (path: string): boolean => {
46+
let result: boolean = true;
47+
48+
try {
49+
fs.accessSync(path, fs.constants.F_OK);
50+
} catch (error) {
51+
result = false;
52+
}
53+
54+
return result;
55+
};
56+
57+
export const logError = (message: string): void =>
58+
console.log(importChalk()?.red(message));
59+
60+
export const logSuccess = (message: string): void =>
61+
console.log(importChalk()?.green(message));
62+
63+
export const copyFile = (src: string, dest: string): void =>
64+
fs.copyFile(src, dest, (error: any) => {
65+
if (error) {
66+
logError(error.message);
67+
}
68+
});
69+
70+
try {
71+
resolvePath(files).forEach(file => {
72+
if (pathExist(file.src)) {
73+
copyFile(file.src, file.dest);
74+
} else {
75+
throw new Error(`File ${file.src} doesn't exist`);
76+
}
77+
});
78+
79+
logSuccess('Copy task completed successfully !');
80+
} catch (error) {
81+
logError(error.message);
82+
}

test/blah.test.ts

-7
This file was deleted.

test/task_copy.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { resolve } from 'path';
2+
import { files, pathExist, resolvePath } from '../task/copy';
3+
4+
describe('Testing copy task', () => {
5+
it('Testing pathExist() function', () => {
6+
expect(pathExist(resolve(__dirname, '../dist/index.js'))).toBeTruthy();
7+
expect(
8+
pathExist(resolve(__dirname, '../dist/wrongFileName.js'))
9+
).toBeFalsy();
10+
});
11+
12+
/*it('Testing resolvePath() function', () => {
13+
expect(resolvePath(files)).toEqual([]);
14+
});*/
15+
});

tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
// error out if import and file system have a casing mismatch. Recommended by TS
3131
"forceConsistentCasingInFileNames": true,
3232
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
33-
"noEmit": true,
33+
"noEmit": true
34+
},
35+
"ts-node": {
36+
"compilerOptions": {
37+
"module": "CommonJS"
38+
}
3439
}
3540
}

0 commit comments

Comments
 (0)