feat: configurable aliases (#5655)

This commit is contained in:
Pooya Parsa 2019-05-06 18:29:37 +04:30 committed by GitHub
parent ffadb3177f
commit 8576b4fcfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 17 deletions

View File

@ -43,6 +43,7 @@ export default () => ({
},
extensions: [],
styleExtensions: ['css', 'pcss', 'postcss', 'styl', 'stylus', 'scss', 'sass', 'less'],
alias: {},
// Ignores
ignorePrefix: '-',

View File

@ -99,6 +99,18 @@ export function getNuxtConfig(_options) {
// Resolve buildDir
options.buildDir = path.resolve(options.rootDir, options.buildDir)
// Aliases
const { rootDir, srcDir, dir: { assets: assetsDir, static: staticDir } } = options
options.alias = {
'~~': rootDir,
'@@': rootDir,
'~': srcDir,
'@': srcDir,
[assetsDir]: path.join(srcDir, assetsDir),
[staticDir]: path.join(srcDir, staticDir),
...options.alias
}
// Default value for _nuxtConfigFile
if (!options._nuxtConfigFile) {
options._nuxtConfigFile = path.resolve(options.rootDir, `${defaultNuxtConfigFile}.js`)

View File

@ -8,6 +8,14 @@ Object {
"_nuxtConfigFiles": Array [
"/var/nuxt/test/nuxt.config.js",
],
"alias": Object {
"@": "/var/nuxt/test",
"@@": "/var/nuxt/test",
"assets": "/var/nuxt/test/assets",
"static": "/var/nuxt/test/static",
"~": "/var/nuxt/test",
"~~": "/var/nuxt/test",
},
"appTemplatePath": "/var/nuxt/test/.nuxt/views/app.template.html",
"build": Object {
"_publicPath": "/_nuxt/",

View File

@ -4,6 +4,7 @@ exports[`config should return default nuxt configurations 1`] = `
Object {
"ErrorPage": null,
"_nuxtConfigFile": undefined,
"alias": Object {},
"build": Object {
"analyze": false,
"babel": Object {
@ -336,6 +337,7 @@ exports[`config should return nuxt configurations with custom env 1`] = `
Object {
"ErrorPage": null,
"_nuxtConfigFile": undefined,
"alias": Object {},
"build": Object {
"analyze": false,
"babel": Object {

View File

@ -23,11 +23,13 @@ describe('config: options', () => {
test('should return default nuxt config', () => {
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
jest.spyOn(path, 'join').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
expect(getNuxtConfig({})).toMatchSnapshot()
process.cwd.mockRestore()
path.resolve.mockRestore()
path.join.mockRestore()
})
test('should prevent duplicate calls with same options', () => {

View File

@ -196,16 +196,7 @@ export default class WebpackBaseConfig {
}
alias() {
const { srcDir, rootDir, dir: { assets: assetsDir, static: staticDir } } = this.buildContext.options
return {
'~': path.join(srcDir),
'~~': path.join(rootDir),
'@': path.join(srcDir),
'@@': path.join(rootDir),
[assetsDir]: path.join(srcDir, assetsDir),
[staticDir]: path.join(srcDir, staticDir)
}
return { ...this.buildContext.options.alias }
}
rules() {

View File

@ -46,19 +46,14 @@ export default class PostcssConfig {
}
get defaultConfig() {
const { dev, srcDir, rootDir, modulesDir } = this.buildContext.options
const { dev, alias, srcDir, rootDir, modulesDir } = this.buildContext.options
return {
sourceMap: this.buildContext.buildOptions.cssSourceMap,
plugins: {
// https://github.com/postcss/postcss-import
'postcss-import': {
resolve: createResolver({
alias: {
'~': path.join(srcDir),
'~~': path.join(rootDir),
'@': path.join(srcDir),
'@@': path.join(rootDir)
},
alias: { ...alias },
modules: [ srcDir, rootDir, ...modulesDir ]
})
},