ts+webpack+postcss-loader下autoprefixer失效

需求

使用webpack打包index.ts的代碼,涉及DOM操做,引入.scssjavascript

postcss-loader
-> scss-loader
-> css-loader
-> style-loader

基本配置以下

package.jsoncss

{
  "name": "session1",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "bundle": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^13.7.0",
    "autoprefixer": "^9.7.4",
    "css-loader": "^3.4.2",
    "file-loader": "^5.0.2",
    "node-sass": "^4.13.1",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.1.3",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.5",
    "url-loader": "^3.0.0",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10"
  }
}

webpack.config.jsjava

const path = require('path');


module.exports = {
    mode: 'development',
    // entry: './src/index.ts', // 簡寫
    entry: {
        main: './src/index.ts',
    },

    module: {
        rules: [{
            test: /\.tsx?$/,
            use: 'ts-loader',
            exclude: /node_modules/,
        }, {
            test: /\.(png|jp?g|gif)$/,
            use: [{
                loader: 'url-loader',
                options: {
                    name: '[name].[ext]', // placeholder 佔位符
                    outputPath: 'images/',
                    limit: 2048
                }
            }],
        }, {
            test: /\.scss$/,
            use: [
                'style-loader',
                'css-loader',
                'sass-loader',
                'postcss-loader'
            ]
        }
        ],
    },

    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },

    output: {
        filename: 'bundle.js', // 打包後文件名
        path: path.resolve(__dirname, 'dist') // 輸出路徑(必須爲絕對路徑)
    }
}

postcss.config.jsnode

module.exports = {
    plugins: [
        require('autoprefixer')
    ],
}

index.tswebpack

import avatar from './static/avatar.jpg';
import './index.scss';

const img = new Image();
img.src = avatar;
img.classList.add('avatar')

const root = document.getElementById('root');
root!.append(img);

bug復現

執行npm run bundle # 即webpack打包 ,web

image.png
postcss-loader內使用的autoprefixer失效。typescript

解決辦法

package.json添加瀏覽器支持配置npm

{
  "name": "session1",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "bundle": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^13.7.0",
    "autoprefixer": "^9.7.4",
    "css-loader": "^3.4.2",
    "file-loader": "^5.0.2",
    "node-sass": "^4.13.1",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.1.3",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.5",
    "url-loader": "^3.0.0",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10"
  },
  "browserslist": [
    "defaults",
    "not ie <= 8",
    "last 2 versions",
    "> 1%",
    "iOS >= 7",
    "Android >= 4.0"
  ]
}

autoprefixer生效
image.pngjson

相關文章
相關標籤/搜索