forked from prateekbh/preact-material-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CssMigrationWebpackPlugin.js
110 lines (102 loc) · 3.37 KB
/
CssMigrationWebpackPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*globals require, __dirname, module*/
const cp = require('cp');
const globCopy = require('glob-copy');
const path = require('path');
const fs = require('fs');
const mkdirp = require('mkdirp');
const bundleMapping = require('./componentsList');
const srcPath = path.join(__dirname, 'node_modules', '@material');
class CssMigrationWebpackPlugin {
copySuperCss() {
const sourceCssPath = path.join(
__dirname,
'node_modules',
'material-components-web',
'dist',
'material-components-web.min.css'
);
const sourceScssPath = path.join(
__dirname,
'node_modules',
'material-components-web',
'material-components-web.scss'
);
const destCssFilePath = path.join(__dirname, 'style.css');
const destScssFilePath = path.join(__dirname);
//delete already existing file
if (fs.existsSync(destCssFilePath)) {
fs.unlinkSync(destCssFilePath);
}
cp.sync(sourceCssPath, destCssFilePath);
globCopy.sync(sourceScssPath, destScssFilePath);
}
copyIndividualCss(component, destFolder) {
const sourcePath = path.join(srcPath, component);
const sourceCssPath = path.join(
sourcePath,
'dist',
'mdc.' + component + '.css'
);
const sourceScssPath = path.join(sourcePath, '*.scss');
const destCssFilePath = path.join(destFolder, 'style.css');
const destScssFilePath = path.join(destFolder);
//delete already existing file
if (fs.existsSync(destCssFilePath)) {
fs.unlinkSync(destCssFilePath);
}
//copy new file
cp.sync(sourceCssPath, destCssFilePath);
globCopy.sync(sourceScssPath, destScssFilePath);
//get nested folders with .scss files
const isDirectory = source => fs.lstatSync(source).isDirectory();
const getSubDirectories = source => {
if (isDirectory(source)) {
return fs
.readdirSync(source)
.filter(fd => isDirectory(path.join(source, fd)) && fd !== 'dist');
}
return [];
};
const hasScss = source => {
if (isDirectory(source)) {
return (
fs.readdirSync(source).filter(fd => fd.match(/.*\.scss/gi)).length > 0
);
}
return false;
};
const subDirectoriesWithScss = getSubDirectories(sourcePath).filter(dir =>
hasScss(path.join(sourcePath, dir))
);
if (!subDirectoriesWithScss.length) return;
//create new directories in dest and copy over files
subDirectoriesWithScss.forEach(subdir => {
const destSubFolder = path.join(destFolder, subdir);
const srcSubPath = path.join(sourcePath, subdir, '*.scss');
if (!fs.existsSync(destSubFolder)) fs.mkdirSync(destSubFolder);
globCopy.sync(srcSubPath, destSubFolder);
});
}
apply(compiler) {
compiler.plugin('after-emit', (compilation, callback) => {
for (let dest in bundleMapping) {
if (bundleMapping.hasOwnProperty(dest)) {
const source = bundleMapping[dest];
const destFolderPath = path.join(__dirname, dest);
try {
//create folder if not already present
if (!fs.existsSync(destFolderPath)) {
mkdirp.sync(destFolderPath);
}
this.copyIndividualCss(source, destFolderPath);
} catch (err) {
return callback(err);
}
}
}
this.copySuperCss();
callback();
});
}
}
module.exports = CssMigrationWebpackPlugin;