UNPKG

2.87 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5const webpack = require('webpack')
6const merge = require('webpack-merge')
7const HtmlWebpackPlugin = require('html-webpack-plugin')
8const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin')
9const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
10const perfInstallModulePlugin = require('../lib/perfInstallModulePlugin')
11const BuildProgressPlugin = require('../lib/BuildProgressPlugin')
12const InlineUmdHtmlPlugin = require('../lib/InlineUmdHtmlPlugin')
13const { getEntryPoints } = require('../lib/utils')
14const config = require('../config')
15const { GLOB, VIEWS_DIR } = require('../config/const')
16
17module.exports = function(context, spinner) {
18 const entry = context.entry
19 const baseWebpackConfig = require('./webpack.base.conf')(context, 'dev')
20 const servantEntry = getEntryPoints(
21 `${VIEWS_DIR}/${entry}/${GLOB.SERVANT_ENTRY}`
22 )
23 const htmlTemplatePath = `${config.paths.views}/${entry}/index.html`
24 const hasHtml = fs.existsSync(htmlTemplatePath)
25
26 // https://github.com/survivejs/webpack-merge
27 // 当 entry 为数组时,webpack-merge 默认执行 append
28 const webpackConfig = merge(baseWebpackConfig, {
29 mode: 'development',
30 devtool: 'cheap-module-source-map',
31 entry: servantEntry,
32 output: {
33 // Add /* filename */ comments to generated require()s in the output.
34 pathinfo: true,
35 publicPath: context.publicPath,
36 // Point sourcemap entries to original disk location (format as URL on Windows)
37 devtoolModuleFilenameTemplate: info =>
38 path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')
39 },
40 plugins: [
41 // 由于 base.conf 会被外部引用,在一些情况下不需要 ProgressPlugin
42 // 因此独立放在 dev.conf 中
43 new BuildProgressPlugin({
44 spinner,
45 name: 'Starting',
46 type: config.marax.progress
47 }),
48 hasHtml &&
49 new HtmlWebpackPlugin({
50 // dev 模式以页面文件夹名作为模板名称
51 filename: `${entry}.html`,
52 // 生成各自的 html 模板
53 template: htmlTemplatePath,
54 inject: true
55 }),
56 hasHtml && new InlineUmdHtmlPlugin(HtmlWebpackPlugin),
57 // 替换 html 内的环境变量
58 // %PUBLIC% 转换为具体路径
59 // 在 dev 环境下为空字符串
60 hasHtml &&
61 new InterpolateHtmlPlugin(HtmlWebpackPlugin, context.buildEnv.raw),
62 // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
63 new webpack.HotModuleReplacementPlugin(),
64 // 等待稳定
65 // new perfInstallModulePlugin({
66 // url:"http://exp.smfe.sina.cn/service/addPerf"
67 // }),
68 // 严格区分大小写
69 new CaseSensitivePathsPlugin()
70 ].filter(Boolean)
71 })
72
73 return webpackConfig
74}