搭建nextjs脚手架遇到的问题及解决

问题

nextjs是react的ssr后端渲染框架,在搭建过程中遇到一些问题,再次记录下来。

  1. 使用antd ui框架,同时使用scss预处理器,在next.config.js中同时使用with-lesswith-sass,并没有达到预期中的结果,scss文件解析正常,antd样式全丢。
  2. 实现antd的按需加载
  3. 使用typescript,如何配置。

解决方法

  1. 在next官网上告诉我们的with-sass,with-less串联的方法并不管用,需要自己新建next.config.js文件,配置loader。但是有个特例,with-typescript,我们把上面修改好的webpack配置传给他就可以开启typescript支持了,所有代码在下面。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    const path = require('path');
    const withTypescript = require('@zeit/next-typescript');
    const cssLoaderConfig = require('@zeit/next-css/css-loader-config');
    const lessToJS = require('less-vars-to-js');
    const fs = require('fs');
    const themeVariables = lessToJS(
    fs.readFileSync(path.resolve(__dirname, './assets/antd-custom.less'), 'utf8'),
    );

    // fix: prevents error when .less files are required by node
    if (typeof require !== 'undefined') {
    require.extensions['.less'] = (file) => {};
    }

    module.exports = withTypescript({
    pageExtensions: ['jsx', 'js', 'ts', 'tsx'],
    webpack(config, options) {
    if (!options.defaultLoaders) {
    throw new Error(
    'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade',
    );
    }
    const { dev, defaultLoaders, isServer } = options;
    // const nextConfig = {};
    const {
    cssModules,
    cssLoaderOptions,
    postcssLoaderOptions,
    lessLoaderOptions = {},
    } = {};

    console.log('options.defaultLoaders', defaultLoaders);

    config.resolve.extensions.push('.ts', '.tsx');
    config.resolve.alias = Object.assign({}, config.resolve.alias, {
    '@utils': path.resolve(__dirname, 'utils'),
    '@assets': path.resolve(__dirname, 'assets'),
    '@pages': path.resolve(__dirname, 'pages'),
    '@components': path.resolve(__dirname, 'components'),
    '@services': path.resolve(__dirname, 'services'),
    });

    defaultLoaders.less = cssLoaderConfig(config, {
    extensions: ['less'],
    cssModules,
    cssLoaderOptions,
    postcssLoaderOptions,
    dev,
    isServer,
    loaders: [
    {
    loader: 'less-loader',
    options: lessLoaderOptions,
    },
    ],
    });

    config.module.rules.push({
    test: /\.less$/,
    exclude: /node_modules/,
    use: options.defaultLoaders.less,
    });

    // 我们禁用了antd的cssModules
    config.module.rules.push({
    test: /\.less$/,
    include: /node_modules/,
    use: cssLoaderConfig(config, {
    extensions: ['less'],
    cssModules: false,
    cssLoaderOptions: {},
    dev,
    isServer,
    loaders: [
    {
    loader: 'less-loader',
    options: {
    javascriptEnabled: true,
    modifyVars: themeVariables,
    },
    },
    ],
    }),
    });

    config.module.rules.push({
    test: /\.scss$/,
    exclude: /node_modules/,
    use: cssLoaderConfig(config, {
    extensions: ['scss'],
    cssModules: true,
    cssLoaderOptions: {
    importLoaders: 1,
    localIdentName: '[local]_[hash:base64:6]',
    },
    dev,
    isServer,
    loaders: [
    {
    loader: 'sass-loader',
    options: {
    data: '@import "_base.scss";',
    includePaths: [path.resolve('./styles')],
    },
    },
    ],
    }),
    });
    if (dev) {
    config.module.rules.push({
    test: /\.(ts|tsx|js|jsx)$/,
    enforce: 'pre',
    exclude: /node_modules/,
    options: {
    configFile: path.resolve('.eslintrc'),
    eslint: {
    configFile: path.resolve(__dirname, '.eslintrc'),
    },
    fix: true,
    },
    loader: 'eslint-loader',
    });
    config.devtool = 'cheap-module-eval-source-map';
    }

    return config;
    },
    });
    这样less和scss,ts就可以同时使用了.
  2. 常规操作
1
yarn add  babel-plugin-impor

修改babelrc (add .babelrc file in your project)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"presets": [
"next/babel"
],
"plugins": [
[
"import",
{
"libraryName": "antd",
"libraryDirectory":"lib",
"style": true
}
]
]
}
分享到 评论