-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
100 lines (81 loc) · 2.28 KB
/
webpack.config.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
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const BrowserSyncPlugin = require( 'browser-sync-webpack-plugin' );
const path = require( 'path' );
const fs = require( 'fs' );
const glob = require( 'glob' );
const rename = () => {
const { join } = path;
const blockStyleFiles = glob.sync(
join( process.cwd(), 'build', '**', 'style-script.css' ),
);
if ( blockStyleFiles ) {
blockStyleFiles.forEach( blockStyleFile => {
fs.rename( blockStyleFile, blockStyleFile.replace( '-script.css', '.css' ), error => {
if ( error ) {
console.log( error );
}
} );
} );
}
const blockEditorStyleFiles = glob.sync(
join( process.cwd(), 'build', '**', 'index.css' ),
);
if ( blockEditorStyleFiles ) {
blockEditorStyleFiles.forEach( blockEditorStyleFile => {
fs.rename( blockEditorStyleFile, blockEditorStyleFile.replace( 'index.css', 'editor.css' ), error => {
if ( error ) {
console.log( error );
}
} );
} );
}
const blockJsonFiles = glob.sync(
join( process.cwd(), 'build', '**', 'block.json' ),
);
if ( blockJsonFiles ) {
blockJsonFiles.forEach( filePath => {
let blockJson = require( filePath );
if ( blockJson?.editorScript ) {
blockJson.editorScript = blockJson.editorScript.replace( '.tsx', '.js' );
}
if ( blockJson?.script ) {
blockJson.script = blockJson.script.replace( '.tsx', '.js' );
}
if ( blockJson?.viewScript ) {
blockJson.viewScript = blockJson.viewScript.replace( '.tsx', '.js' );
}
if ( blockJson?.editorStyle ) {
blockJson.editorStyle = blockJson.editorStyle.replace( '.scss', '.css' );
}
if ( blockJson?.style ) {
blockJson.style = blockJson.style.replace( '.scss', '.css' );
}
fs.writeFile( filePath, JSON.stringify( blockJson, null, 2 ), function writeJSON( error ) {
if ( error ) {
return console.log( error );
}
} );
} );
}
};
module.exports = env => {
return {
...defaultConfig,
module: {
...defaultConfig.module
},
plugins: [
...defaultConfig.plugins,
new BrowserSyncPlugin( {
host: 'localhost',
port: 8887,
proxy: 'https://blockify.local/'
} ),
{
apply: compiler => {
compiler.hooks.afterEmit.tap( 'rename', rename );
}
},
]
};
};