站長資訊網
        最全最豐富的資訊網站

        解決React官方腳手架不支持Less的問題

        說在前面

        create-react-app是由 React 官方提供并推薦使用構建新的 React 單頁面應用程序的最佳方式,不過目前版本(1.5.x)其構建的項目中默認是不支持動態樣式語言Less的。如果我們的項目必須要使用 Less 呢,這就需要我們手動集成一下。本篇主要針對集成的過程做一個簡要記錄。

        環境準備

        本小節先用 create-react-app 構建一個全新的 React 項目作為實驗環境。

        如果您之前未曾使用過 create-react-app,請先通過如下命令全局安裝(假定您本機已經安裝了 Node.js):

          npm install -g create-react-app

        然后,通過如下命令構建一個新的項目my-app:

          npx create-react-app my-app

        通過cd my-app命令進入項目文件夾,執行yarn start命令啟動程序,成功運行,則實驗環境準備完畢。

        最終項目結構:

          ┌─node_modules   ├─public  ├─src             ├─.gitignore  ├─package.json  ├─README.md  └─yarn.lock

        安裝 less & less-loader

        要使 create-react-app 構建的項目能正常解析 less 文件,只需要讓webpack能夠把 less 文件編譯成 css 文件即可。

        所以,首先要把 less 和less-loader(less 編譯器)添加到項目中:

          yarn add less less-loader

        這樣就 OK 了?以上只是在項目中安裝了 less 和 less-loader ,但還未曾通過 webpack 使用 less-loader。

        至于怎么使用?幾種使用方式?請參見webpack 文檔,這里不再贅述。

        假定您已經仔細閱讀過上述 webpack 文檔,想必您也清楚我們應該選擇在webpack.config.js文件中配置 less-loader。

        暴露 webpack 配置文件

        突然,您會發現在我們實驗項目中找不到 webpack 相關配置文件。

        因為腳手架為了實現“零配置”,會默認把一些通用的腳本和配置集成到react-scripts,目的是讓我們專注于src目錄下的開發工作,不再操心環境配置。同時,被其集成的腳本和配置也會從程序目錄中消失 ,程序目錄也會變得干凈許多。

        如果我們要自定義環境配置怎么辦?

        項目構建完成后,會提供一個命令yarn eject,通過這個命令,我們可以把被 react-scripts 集成的配置和腳本暴露出來。

        以下是腳手架關于yarn eject命令的介紹:

        yarn ejectRemoves this tool and copies build dependencies, configuration files and scripts into the app directory. If you do this, you can’t go back!

        大概意思是,執行該命令后會把已構建依賴項、配置文件和腳本復制到程序目錄中。該操作是不可逆轉的,執行完成后會刪除這個命令,也就是說只能執行一次。

        至于 react-scripts 都集成了哪些東西,通過yarn eject命令的執行記錄也能看出個大概:

          λ yarn eject  yarn run v1.6.0  $ react-scripts eject   Are you sure you want to eject This action is permanent. Yes  Ejecting...    Copying files into E:Reactmy-app    Adding configenv.js to the project    Adding configpaths.js to the project    Adding configpolyfills.js to the project    Adding configwebpack.config.dev.js to the project    Adding configwebpack.config.prod.js to the project    Adding configwebpackDevServer.config.js to the project    Adding configjestcssTransform.js to the project    Adding configjestfileTransform.js to the project    Adding scriptsbuild.js to the project    Adding scriptsstart.js to the project    Adding scriptstest.js to the project    Updating the dependencies    Removing react-scripts from dependencies    Adding autoprefixer to dependencies    Adding babel-core to dependencies    Adding babel-eslint to dependencies    Adding babel-jest to dependencies    Adding babel-loader to dependencies    Adding babel-preset-react-app to dependencies    Adding babel-runtime to dependencies    Adding case-sensitive-paths-webpack-plugin to dependencies    Adding chalk to dependencies    Adding css-loader to dependencies    Adding dotenv to dependencies    Adding dotenv-expand to dependencies    Adding eslint to dependencies    Adding eslint-config-react-app to dependencies    Adding eslint-loader to dependencies    Adding eslint-plugin-flowtype to dependencies    Adding eslint-plugin-import to dependencies    Adding eslint-plugin-jsx-a11y to dependencies    Adding eslint-plugin-react to dependencies    Adding extract-text-webpack-plugin to dependencies    Adding file-loader to dependencies    Adding fs-extra to dependencies    Adding html-webpack-plugin to dependencies    Adding jest to dependencies    Adding object-assign to dependencies    Adding postcss-flexbugs-fixes to dependencies    Adding postcss-loader to dependencies    Adding promise to dependencies    Adding raf to dependencies    Adding react-dev-utils to dependencies    Adding resolve to dependencies    Adding style-loader to dependencies    Adding sw-precache-webpack-plugin to dependencies    Adding url-loader to dependencies    Adding webpack to dependencies    Adding webpack-dev-server to dependencies    Adding webpack-manifest-plugin to dependencies    Adding whatwg-fetch to dependencies    Updating the scripts    Replacing "react-scripts start" with "node scripts/start.js"    Replacing "react-scripts build" with "node scripts/build.js"    Replacing "react-scripts test" with "node scripts/test.js"    Configuring package.json    Adding Jest configuration    Adding Babel preset    Adding ESLint configuration    Ejected successfully!    Please consider sharing why you ejected in this survey:    http://goo.gl/forms/Bi6CZjk1EqsdelXk1    Done in 5.37s.

        說了這么多,現在怎樣才能在我們的項目中暴露 webpack 的配置文件?沒錯,你沒猜錯,只需要運行一下yarn eject即可。

        再來看我們的實驗項目的目錄,您會發現其中多了一個config文件夾,其中就有三個關于 webpack 的配置文件:

          webpack.config.dev.js       # 開發環境配置  webpack.config.prod.js      # 生產環境配置  webpackDevServer.config.js  # 開發服務器配置

        我們需要關注的是前兩個,最后一個是關于本地開發服務器http://localhost:3000的一些相關配置。

        修改 webpack 配置

        理論上講,需要同步修改webpack.config.dev.js和webpack.config.prod.js配置文件:

        在module.rules節點中找到 css 文件的加載規則:

        test: /.css$/修改為test: /.(css|less)$/;在use數組最后新增一個對象元素{loader: require.resolve('less-loader')}。

        修改完成后:

          // "postcss" loader applies autoprefixer to our CSS.  // "css" loader resolves paths in CSS and adds assets as dependencies.  // "style" loader turns CSS into JS modules that inject 

        至此,就已經完成了create-react-app 對 Less 的支持。

        效果驗證

        最后,在我們的實驗項目中驗證一下配置是否生效。

        首先在src根目錄下使用 Less 語法創建一個 less 文件,取名為Test.less:

          @title-color:#f00;    .App-title {      color: @title-color    }

        然后在App.js文件中通過如下API導入上述的 less 文件:

          import './Test.less';

        再次yarn start運行我們的程序,如果標題Welcome to React變成紅色則說明配置沒有問題。

        解決React官方腳手架不支持Less的問題

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 柠檬福利精品视频导航| 日韩精品在线看| 久久精品成人免费看| 亚洲日韩国产精品第一页一区| 亚洲精品综合一二三区在线| 久久精品国产亚洲av日韩| 欧美日韩综合精品| 国产精品视频不卡| 91精品国产91久久久久久蜜臀| 国产精品视频二区不卡| 亚洲高清国产拍精品26U| 欧美日韩精品| 精品久久久久久无码免费| 亚洲精品国产成人专区| 国产精品视频色拍拍| 久久亚洲私人国产精品vA| 在线精品亚洲| 亚洲国产精品综合久久一线| 久久99精品久久久久久野外| 国产精品日韩欧美在线第3页 | 乱码精品一区二区三区| 亚洲欧美精品丝袜一区二区| 欧美成人精品欧美一级乱黄码| 国产网红主播无码精品| 国产日韩高清三级精品人成| 国产精品狼人久久久久影院 | 999在线视频精品免费播放观看| 精品一区二区三区无码免费视频| 一本一道精品欧美中文字幕| 呦交小u女国产精品视频| 亚洲精品乱码久久久久久蜜桃图片| 亚洲精品97久久中文字幕无码| 影院无码人妻精品一区二区| 少妇精品无码一区二区三区| 日韩精品久久无码人妻中文字幕| 日韩精品久久久久久久电影蜜臀| 精品无码久久久久国产| 国产精品高清一区二区三区 | 2021久久精品国产99国产精品| 人妻熟妇乱又伦精品视频| 久久国产精品无码HDAV|