UNPKG

4.45 kBJavaScriptView Raw
1const path = require('path');
2const webpack = require('webpack');
3const { existsSync } = require('fs');
4const HtmlWebpackPlugin = require('html-webpack-plugin');
5const WebpackNotifierPlugin = require('webpack-notifier');
6const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
7const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
8const CopyWebpackPlugin = require('copy-webpack-plugin');
9const loaders = require('@availity/workflow-settings/webpack');
10const html = require('./html');
11
12process.noDeprecation = true;
13
14const plugin = settings => {
15 const babelrcPath = path.join(settings.project(), '.babelrc');
16 const babelrcExists = existsSync(babelrcPath);
17
18 function getVersion() {
19 return settings.pkg().version || 'N/A';
20 }
21
22 const index = [
23 `${require.resolve('webpack-dev-server/client')}?/`,
24 require.resolve('webpack/hot/dev-server'),
25 './index.js'
26 ];
27
28 const config = {
29 mode: 'development',
30
31 context: settings.app(),
32
33 entry: {
34 index
35 },
36
37 output: {
38 path: settings.output(),
39 filename: settings.fileName(),
40 chunkFilename: settings.chunkFileName()
41 },
42
43 devtool: settings.sourceMap(),
44
45 resolve: {
46 // Tell webpack what directories should be searched when resolving modules
47 modules: [
48 settings.app(),
49 'node_modules',
50 path.join(settings.project(), 'node_modules'),
51 path.join(__dirname, 'node_modules')
52 ],
53 symlinks: true,
54 extensions: ['.js', '.jsx', '.json', '.css', 'scss']
55 },
56
57 // This set of options is identical to the resolve property set above,
58 // but is used only to resolve webpack's loader packages.
59 resolveLoader: {
60 modules: [path.join(settings.project(), 'node_modules'), path.join(__dirname, 'node_modules')],
61 symlinks: true
62 },
63
64 module: {
65 rules: [
66 {
67 test: /\.jsx?$/,
68 include: settings.include(),
69 use: [
70 {
71 loader: 'babel-loader',
72 options: {
73 presets: [require.resolve('@availity/workflow-babel-preset')],
74 cacheDirectory: settings.isDevelopment(),
75 babelrc: babelrcExists,
76 plugins: [babelrcExists ? null : require.resolve('react-hot-loader/babel')]
77 }
78 }
79 ]
80 },
81 loaders.css.development,
82 loaders.scss.development,
83 loaders.fonts,
84 loaders.images,
85 loaders.eslint
86 // loaders.eslint
87 ]
88 },
89 plugins: [
90 new webpack.DefinePlugin(settings.globals()),
91
92 new webpack.BannerPlugin({
93 banner: `APP_VERSION=${JSON.stringify(getVersion())};`,
94 test: /\.jsx?/,
95 raw: true,
96 entryOnly: true
97 }),
98
99 new webpack.BannerPlugin({
100 banner: `v${getVersion()} - ${new Date().toJSON()}`
101 }),
102
103 // Converts:
104 // [HMR] Updated modules:
105 // [HMR] - 5
106 // To:
107 // [HMR] Updated modules:
108 // [HMR] - ./src/middleware/api.js
109 new webpack.NamedModulesPlugin(),
110
111 // Generate hot module chunks
112 new webpack.HotModuleReplacementPlugin(),
113
114 new HtmlWebpackPlugin(html(settings)),
115
116 new DuplicatePackageCheckerPlugin({
117 verbose: true,
118 exclude(instance) {
119 return instance.name === 'regenerator-runtime';
120 }
121 }),
122
123 // Ignore all the moment local files
124 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
125
126 new CaseSensitivePathsPlugin(),
127
128 new CopyWebpackPlugin(
129 [
130 {
131 context: `${settings.project()}/project/static`, // copy from this directory
132 from: '**/*', // copy all files
133 to: 'static' // copy into {output}/static folder
134 }
135 ],
136 {
137 debug: 'warning'
138 }
139 ),
140 new webpack.ProvidePlugin({
141 Promise: 'es6-promise-promise',
142 Symbol: 'es6-symbol'
143 })
144 ]
145 };
146
147 if (settings.isHotLoader()) {
148 config.module.rules.push({
149 test: settings.getHotLoaderEntry(),
150 loader: require.resolve('react-hot-loader-loader')
151 });
152 }
153
154 if (settings.isNotifications()) {
155 config.plugins.push(
156 new WebpackNotifierPlugin({
157 contentImage: path.join(__dirname, 'availity.png'),
158 excludeWarnings: true
159 })
160 );
161 }
162
163 return config;
164};
165
166module.exports = plugin;