导入CSS和SASS文件nextjs 7

时间:2018-11-14 02:30:32

标签: reactjs webpack-4 next.js

我希望能够在项目的任何文件中导入两种类型的文件。

    import 'styles/index.scss';
    import 'styles/build/_style.css'

重要的是要注意使用Nextjs 7和webpack 4(nextjs7附带)

在Nextjs 6中,我们曾经在next.config.js中使用

const withCSS = require('@zeit/next-css')
const withSass = require('@zeit/next-sass')

const commonsChunkConfig = (config, test = /\.css$/) => {
  config.plugins = config.plugins.map(plugin => {
      if (
          plugin.constructor.name === 'CommonsChunkPlugin' &&
          plugin.minChunks != null
  ) {
      const defaultMinChunks = plugin.minChunks;
      plugin.minChunks = (module, count) => {
          if (module.resource && module.resource.match(test)) {
              return true;
          }
          return defaultMinChunks(module, count);
      };
  }
  return plugin;
  });
  return config;
};

module.exports = withCSS(withSass({
  webpack: (config, { isServer }) => {
      config = commonsChunkConfig(config, /\.(sass|scss|css)$/)
      return config
  }
}))

4 个答案:

答案 0 :(得分:2)

这个基本示例适用于next-sassnext-css并排使用

/next.config.js

const withSass = require('@zeit/next-sass');
const withCSS = require('@zeit/next-css');

module.exports = withCSS(withSass());

/pages/index.js

import '../styles.scss';
import '../styles.css';

export default () => {
  return (
    <div className="example-sass">
      <h1 className="example-css">Here I am</h1>
    </div>
  );
};

/styles.css

.example-css {
  background-color: #ccc;
}

/styles.scss

$font-size: 50px;

.example-sass {
  font-size: $font-size;
}

/package.json

"dependencies": {
  "@zeit/next-css": "^1.0.1",
  "@zeit/next-sass": "^1.0.1",
  "next": "^7.0.2",
  "node-sass": "^4.10.0",
  "react": "^16.6.3",
  "react-dom": "^16.6.3"
}

这里是what I see on the screen

希望这会有所帮助!

PS官方GitHub仓库上也有some info

答案 1 :(得分:0)

使用next-compose-plugins

它提供了一个更干净的API,用于创建next.js配置,您无需安装@zeit/next-css,也不必进行任何webpack loader配置即可使其工作。

这里是一个@zeit/next-source-maps插件的示例,用于演示:

/* Import modules */
const withSourceMaps = require( '@zeit/next-source-maps' )();
const withSass = require( '@zeit/next-sass' );
const withPlugins = require( 'next-compose-plugins' );

/* Configuration */
const NextAppConfig = ({
  // config stuff goes here, no webpack loader config required
})

/* Export declaration */
module.exports = withPlugins([ 
  [ withSourceMaps ],  
  [ withSass ]
], NextAppConfig );

我更喜欢在导出之前声明数组,因为它的设置更加简洁:

// ... imports go here

/* Plugins array variable */
var plugins = [ [ withSourceMaps ], [ withSass ] ];

/* CONFIGURATION */
const NextAppConfig = ({
  // Config stuff goes here, no webpack configuration required
})

/* EXPORT DECLARATION */
module.exports = withPlugins( plugins, NextAppConfig );

https://github.com/cyrilwanner/next-compose-plugins

答案 2 :(得分:0)

我初始化了这样的项目,包括SCSS CSS PNG SVG TTF。

 npm install withSass@canary withCSS@canary node-sass

//next.config.js

const withSass = require('@zeit/next-sass');
const withCSS = require("@zeit/next-css");
module.exports = withCSS(withSass({
webpack (config, options) {
   config.module.rules.push({
       test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
       use: {
           loader: 'url-loader',
           options: {
               limit: 100000
           }
       }
   });

    return config;
    }
  }));

答案 3 :(得分:0)

对于那些带有外部软件包(位于node_modules中)的用户,这是下一个9.3.0上的CSS + SASS解决方案。它涉及通过SASS标准导入CSS。此示例使用videojs:

package.json

  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "next": "^9.3.0",
    "next-absolute-url": "^1.2.2",
    "node-sass": "^4.13.1",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "video.js": "^7.7.5"
  },

next.config.js:

const withSass = require("@zeit/next-sass");
module.exports = withSass({
  assetPrefix: process.env.NEXT_BASE_PATH || '',
  exportTrailingSlash: true,
  exportPathMap: function() {
    return {
      '/': { page: '/' }
    };
  }
});

需要外部CSS的组件“ VideoPlayer.jsx”

import './VideoPlayer.scss';

VideoPlayer.scss:

@import '~video.js/dist/video-js.css';