1 | const path = require('path')
|
2 | const fs = require('fs')
|
3 |
|
4 | exports.extend = api => {
|
5 | api.chainWebpack(config => {
|
6 | const { pages } = api.config
|
7 |
|
8 | for (const entryName of Object.keys(pages)) {
|
9 | const { entry } = pages[entryName]
|
10 | if (typeof entry === 'string') {
|
11 | config.entry(entryName).add(api.resolveBaseDir(entry))
|
12 | } else if (Array.isArray(entry)) {
|
13 | config.entry(entryName).merge(entry.map(v => api.resolveBaseDir(v)))
|
14 | } else {
|
15 | throw new TypeError('Entry value must be either string or array!')
|
16 | }
|
17 | }
|
18 |
|
19 | const defaultDevtool =
|
20 | api.options.command === 'build'
|
21 | ? 'source-map'
|
22 | : 'cheap-module-eval-source-map'
|
23 | const devtool =
|
24 | typeof api.config.sourceMap === 'string'
|
25 | ? api.config.sourceMap
|
26 | : api.config.sourceMap
|
27 | ? defaultDevtool
|
28 | : false
|
29 | config.devtool(devtool)
|
30 |
|
31 | config.merge({
|
32 | performance: {
|
33 | hints: false
|
34 | },
|
35 | mode: api.options.command === 'build' ? 'production' : 'development'
|
36 | })
|
37 |
|
38 | config.output
|
39 | .path(api.resolveBaseDir(api.config.outDir))
|
40 |
|
41 | .filename(api.config.filenames.js)
|
42 | .publicPath(api.config.publicPath)
|
43 | .chunkFilename(api.config.filenames.chunk)
|
44 |
|
45 | config.resolve.alias.set('@', api.resolveBaseDir('src'))
|
46 |
|
47 | const baseDir = api.resolveBaseDir()
|
48 | require('../webpack/rules/vue')(config, { baseDir })
|
49 | require('../webpack/rules/babel')(config, { baseDir })
|
50 | require('../webpack/rules/graphql')(config)
|
51 | require('../webpack/rules/yaml')(config)
|
52 | require('../webpack/rules/toml')(config)
|
53 | require('../webpack/rules/fonts')(config, api.config.filenames.font)
|
54 | require('../webpack/rules/images')(config, api.config.filenames.image)
|
55 |
|
56 |
|
57 |
|
58 | if (
|
59 | api.cliOptions.progress !== false &&
|
60 | !api.cliOptions.debug &&
|
61 | process.stdout.isTTY
|
62 | ) {
|
63 | const { ProgressPlugin } = require('webpack')
|
64 | ProgressPlugin.__expression = `require('webpack').ProgressPlugin`
|
65 | config.plugin('progress').use(ProgressPlugin)
|
66 | }
|
67 |
|
68 | config
|
69 | .plugin('report-status')
|
70 | .use(require('../webpack/plugins/report-status-plugin'), [
|
71 | {
|
72 | showFileStats: api.options.command === 'build',
|
73 | command: api.options.command,
|
74 | devServer: api.config.devServer
|
75 | }
|
76 | ])
|
77 |
|
78 | config.plugin('constants').use(require('webpack').DefinePlugin, [
|
79 | Object.assign(
|
80 | {
|
81 | POI_PUBLIC_PATH: JSON.stringify(api.config.publicPath),
|
82 | POI_COMMAND: JSON.stringify(api.command),
|
83 | __DEV__: JSON.stringify(api.command !== 'build')
|
84 | },
|
85 | api.config.constants
|
86 | )
|
87 | ])
|
88 |
|
89 |
|
90 |
|
91 | if (
|
92 | api.options.command !== 'dev' &&
|
93 | fs.existsSync(api.resolveBaseDir('public'))
|
94 | ) {
|
95 | const CopyPlugin = require('copy-webpack-plugin')
|
96 | CopyPlugin.__expression = `require('copy-webpack-plugin')`
|
97 |
|
98 | api.chainWebpack(config => {
|
99 | config.plugin('copy-public').use(CopyPlugin, [
|
100 | [
|
101 | {
|
102 | from: api.resolveBaseDir('public'),
|
103 | to: '.',
|
104 | ignore: ['.DS_Store']
|
105 | }
|
106 | ]
|
107 | ])
|
108 | })
|
109 | }
|
110 |
|
111 |
|
112 | const inWorkspaces = __dirname.includes('/poi/packages/poi/')
|
113 | const ownModules = inWorkspaces
|
114 | ? path.join(__dirname, '../../../../node_modules')
|
115 | : path.join(__dirname, '../../node_modules')
|
116 | config.resolve.modules.add(ownModules).add('node_modules')
|
117 | config.resolveLoader.modules.add(ownModules).add('node_modules')
|
118 | })
|
119 | }
|
120 |
|
121 | exports.name = 'builtin:config-base'
|