-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (92 loc) · 2.87 KB
/
index.js
File metadata and controls
111 lines (92 loc) · 2.87 KB
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
111
// Dependencies.
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { getWebpackEntryPoints, getProjectSourcePath } = require( '@wordpress/scripts/utils/config' );
const path = require( 'path' );
const fs = require( 'fs' );
const { globSync } = require( 'glob' );
const projectSourcePath = getProjectSourcePath();
/**
* Finds the value of `--output-path` among the process arguments,
* if present. Returns `null` if not found.
*/
function getOutputPathFromArgs() {
const argPrefix = '--output-path=';
for ( const arg of process.argv ) {
if ( arg.startsWith( argPrefix ) ) {
return arg.slice( argPrefix.length );
}
}
return null;
};
/**
* Processes individual block stylesheets for a specific block namespace. These
* are not imported into the primary stylesheets and are enqueued separately.
*
* @return {Object.<string, string>}
*/
const getAllAssets = ( options = {} ) => {
const userExcludes = options.excludeDirs || [];
const excludeDirs = [ 'blocks', ...userExcludes ];
const remapDirs = options.remapDirs || {};
const subDirs = fs.readdirSync( projectSourcePath )
.map( name => ({
name,
fullPath: path.join( projectSourcePath, name )
} ) )
.filter( dir => fs.statSync( dir.fullPath ).isDirectory() )
.filter( dir => ! excludeDirs.includes( dir.name ) && ! dir.name.startsWith( '_' ) );
const allFiles = {};
for ( const dir of subDirs ) {
Object.assign( allFiles, getAsset( dir.fullPath, { remapDirs } ) );
}
return allFiles;
};
/**
* Processes individual block stylesheets for a specific block namespace. These
* are not imported into the primary stylesheets and are enqueued separately.
*
* @param {string} dir
* @return {Object.<string, string>}
*/
const getAsset = ( dir, { remapDirs = {} } = {} ) => {
const pattern = path.join( dir, '**', '*.*' );
return globSync( pattern ).reduce(
( files, filepath ) => {
const strippedPath = path.relative(
projectSourcePath,
filepath
);
const ext = path.parse( strippedPath ).ext;
const filename = path.basename( filepath );
// Skip files that start with underscore.
if ( filename.startsWith( '_' ) ) {
return files;
}
const allowed = [ '.js', '.ts', '.jsx', '.tsx', '.scss', '.css' ];
if ( ! allowed.includes( ext ) ) {
return files; // skip
}
// Default chunk name.
let entryChunkName = strippedPath.slice( 0, -ext.length );
// Apply remapping if path matches.
for ( const [ from, to ] of Object.entries( remapDirs ) ) {
if ( entryChunkName.startsWith( from ) ) {
entryChunkName = entryChunkName.replace( from, to );
break;
}
}
const entryFile = path.resolve(
projectSourcePath,
strippedPath
);
files[ entryChunkName ] = entryFile;
return files;
}, {}
);
};
module.exports = {
getAllAssets,
getAsset,
// future utils go here, e.g.:
// anotherHelper,
};