gulp构建react项目四:处理css

项目结构

gulp01
├── src
│   ├── components
│   │   ├── Test
│   │   |   └── index.jsx
│   │   ├── Child
│   │   |   ├── index.css
│   │   |   └── index.jsx
│       └── App.jsx
├── node_modules
├── index.js
├── gulpfile.js
├── index.html
└── package.json

项目依赖

image.png

脚本

// index.js
import './src/App.jsx'

// App.jsx
import React from 'react'
import { render } from 'react-dom'
import Test from './components/Test/index.jsx'

render(, document.getElementById("app"))

// Test
import React from 'react'
import Child from '../Child/index.jsx'

export default class Test extends React.Component {
  state = {
    msg: 'hello, world'
  }

  render() {
    const { msg } = this.state
    return 
  }
}

// Child
import React from 'react'

const Child = (props) => 
{props.msg}
export default Child

css

.color {
  color: red;
}

构建脚本

const path = require('path')
const gulp = require('gulp')
const babelify = require('babelify')
const browserify = require('browserify')  // 插件,
    // 转成stream流,gulp系
const source = require('vinyl-source-stream')
    // 转成二进制流,gulp系
const buffer = require('vinyl-buffer')
const { series, parallel } = require('gulp')
const del = require('del')
const concat = require('gulp-concat')


const resolveDir = (dir) => path.resolve(__dirname, dir)

const _path = {
  main_js: resolveDir('./index.js'),
  css: ['./src/components/**/*.css']
}

const clean = (done) => {
  del('./dist')
  done()
}

const _css = () => {
  return gulp.src(_path.css)
    .pipe(concat('app.css'))
    .pipe(gulp.dest('./dist/css'))
}

const _script = () => {
  return browserify({
      entries: _path.main_js,
      transform: [babelify.configure({
        presets: ['@babel/preset-env', '@babel/preset-react'],
        plugins: [
          '@babel/plugin-transform-runtime', 
          ["@babel/plugin-proposal-decorators", { "legacy": true }], 
          ["@babel/plugin-proposal-class-properties", { "loose": true }]
        ]
      })]
    })
    .bundle()
    .pipe(source('app.js'))
    // .pipe(buffer())
    .pipe(gulp.dest('./dist/js'))
}

module.exports.build = series(clean, parallel(_script, _css))

在index.html中引入打包之后的js和css




  
  
  Document
  


  

效果:
image.png

你可能感兴趣的:(gulp,css)