-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
executable file
·112 lines (97 loc) · 2.95 KB
/
webpack.dev.js
File metadata and controls
executable file
·112 lines (97 loc) · 2.95 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
112
/**
* Formidable Forms Development Server
*
* Run `npm run serve` to start the development server. You'll be prompted
* for your WordPress site URL, and a development server will start at localhost:3000.
*
* Development is streamlined with automatic updates:
* - CSS/SCSS changes are injected in real-time without page refresh
* - JavaScript and PHP changes trigger an automatic full page reload
* - Browser opens automatically to localhost:3000
*/
/**
* External dependencies
*/
const browserSync = require( 'browser-sync' );
const webpack = require( 'webpack' );
const webpackDevMiddleware = require( 'webpack-dev-middleware' );
/**
* Config
*/
const config = {
// WordPress site URL from environment variable or default
siteDomain: process.env.SITE_URL || 'formidable.local',
// Server ports
port: 3000,
uiPort: 3001,
// Paths to watch for changes
cssPath: '../formidable*/**/*.css',
jsPath: '../formidable*/**/*.js',
jsSrcPath: '../formidable*/*/js/src/**/*.js',
phpPath: '../formidable*/**/*.php',
// Public path for webpack
publicPath: '/wp-content/plugins/formidable/css/'
};
// Import webpack config
const [ jsConfig, cssConfig ] = require( './webpack.config' );
/**
* Initialize development server
*/
const init = () => {
const browserSyncInstance = browserSync.create( 'FormidableDev' );
// Create separate webpack compilers for CSS and JS
const jsCompiler = webpack( jsConfig );
const cssCompiler = webpack( cssConfig );
// Watch JS with webpack
jsCompiler.watch(
{ aggregateTimeout: 300 },
( err, stats ) => err || stats.hasErrors()
? console.log( `JS compilation ${ err ? 'error: ' + err : 'has errors' }` )
: console.log( 'JS compiled successfully' )
);
browserSyncInstance.init( {
proxy: {
target: config.siteDomain,
middleware: [
webpackDevMiddleware( cssCompiler, {
publicPath: config.publicPath,
writeToDisk: true
} )
]
},
// File watching
files: [
// CSS changes, inject without reload
{
match: [ config.cssPath ],
fn: ( event, file ) => {
console.log( `CSS updated: ${ file }` );
browserSyncInstance.reload( '*.css' );
}
},
// JS source changes
{
match: [ config.jsSrcPath ],
fn: ( event, file ) => console.log( `JS source updated: ${ file }\nRebuilding JS bundles...` )
},
// Conditionally watch compiled JS and PHP files
...( process.env.WATCH_FILES && process.env.WATCH_FILES.toLowerCase().startsWith( 'y' ) ? [ config.jsPath, config.phpPath ] : [] )
],
// Server settings
port: config.port,
ui: { port: config.uiPort },
open: true,
notify: false,
injectChanges: true,
ghostMode: false,
// Resource handling
serveStatic: [ { route: '/css', dir: './css' } ],
rewriteRules: [ {
match: new RegExp( config.siteDomain.replace( /^https?:\/\//, '' ), 'g' ),
replace: `localhost:${ config.port }`
} ]
} );
console.log( `Development server running at: http://localhost:${ config.port }` );
};
// Start the server
init();