Skip to content

Commit 9638d90

Browse files
jkzingyyx990803
authored andcommitted
feat(cli-service): add assetsDir option to specify assets root directory (#1322)
close #1311
1 parent 763cf7a commit 9638d90

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

packages/@vue/cli-service/lib/config/base.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = (api, options) => {
22
api.chainWebpack(webpackConfig => {
33
const resolveLocal = require('../util/resolveLocal')
4+
const getAssetPath = require('../util/getAssetPath')
45
const inlineLimit = 10000
56

67
webpackConfig
@@ -65,7 +66,7 @@ module.exports = (api, options) => {
6566
.loader('url-loader')
6667
.options({
6768
limit: inlineLimit,
68-
name: `img/[name].[hash:8].[ext]`
69+
name: getAssetPath(options, `img/[name].[hash:8].[ext]`)
6970
})
7071

7172
// do not base64-inline SVGs.
@@ -76,7 +77,7 @@ module.exports = (api, options) => {
7677
.use('file-loader')
7778
.loader('file-loader')
7879
.options({
79-
name: `img/[name].[hash:8].[ext]`
80+
name: getAssetPath(options, `img/[name].[hash:8].[ext]`)
8081
})
8182

8283
webpackConfig.module
@@ -86,7 +87,7 @@ module.exports = (api, options) => {
8687
.loader('url-loader')
8788
.options({
8889
limit: inlineLimit,
89-
name: `media/[name].[hash:8].[ext]`
90+
name: getAssetPath(options, `media/[name].[hash:8].[ext]`)
9091
})
9192

9293
webpackConfig.module
@@ -96,7 +97,7 @@ module.exports = (api, options) => {
9697
.loader('url-loader')
9798
.options({
9899
limit: inlineLimit,
99-
name: `fonts/[name].[hash:8].[ext]`
100+
name: getAssetPath(options, `fonts/[name].[hash:8].[ext]`)
100101
})
101102

102103
// Other common pre-processors ---------------------------------------------

packages/@vue/cli-service/lib/config/css.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const findExisting = (context, files) => {
1111

1212
module.exports = (api, options) => {
1313
api.chainWebpack(webpackConfig => {
14+
const getAssetPath = require('../util/getAssetPath')
15+
1416
const {
1517
extract = true,
1618
sourceMap = false,
@@ -22,8 +24,8 @@ module.exports = (api, options) => {
2224
const isProd = process.env.NODE_ENV === 'production'
2325
const shouldExtract = isProd && extract !== false && !shadowMode
2426
const extractOptions = Object.assign({
25-
filename: `css/[name].[contenthash:8].css`,
26-
chunkFilename: 'css/[name].[id].[contenthash:8].css'
27+
filename: getAssetPath(options, `css/[name].[contenthash:8].css`),
28+
chunkFilename: getAssetPath(options, 'css/[name].[id].[contenthash:8].css')
2729
}, extract && typeof extract === 'object' ? extract : {})
2830

2931
// check if the project has a valid postcss config

packages/@vue/cli-service/lib/config/prod.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module.exports = (api, options) => {
22
api.chainWebpack(webpackConfig => {
33
if (process.env.NODE_ENV === 'production') {
4+
const getAssetPath = require('../util/getAssetPath')
5+
46
webpackConfig
57
.mode('production')
68
.devtool('source-map')
79
.output
8-
.filename(`js/[name].[chunkhash:8].js`)
9-
.chunkFilename(`js/[name].[chunkhash:8].js`)
10+
.filename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
11+
.chunkFilename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
1012

1113
// keep module.id stable when vendor modules does not change
1214
webpackConfig

packages/@vue/cli-service/lib/options.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { createSchema, validate } = require('@vue/cli-shared-utils')
33
const schema = createSchema(joi => joi.object({
44
baseUrl: joi.string(),
55
outputDir: joi.string(),
6+
assetsDir: joi.string(),
67
compiler: joi.boolean(),
78
transpileDependencies: joi.array(),
89
productionSourceMap: joi.boolean(),
@@ -47,6 +48,9 @@ exports.defaults = () => ({
4748
// where to output built files
4849
outputDir: 'dist',
4950

51+
// where to put static assets (js/css/img/font/...)
52+
assetsDir: '',
53+
5054
// boolean, use full build?
5155
compiler: false,
5256

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const path = require('path')
2+
3+
module.exports = function getAssetPath (options, filePath) {
4+
return options.assetsDir
5+
? path.posix.join(options.assetsDir, filePath)
6+
: filePath
7+
}

0 commit comments

Comments
 (0)