打包没有css文件的问题

打包没有css文件的问题

webpack.js

const path=require('path');

const HtmlWebpackPlugin=require('html-webpack-plugin');

//const { turn } = require('core-js/core/array');

const MiniCssExtractPlugin=require('mini-css-extract-plugin');

module.exports={

mode:'development',

entry:{

index:'./src/index.js',//多个js文件导入 键就是默认文件名

ajax:'./src/ajax.js'

},

output:{

path:path.resolve(__dirname,'dist'),

filename:'[name].js'

},

module:{

rules:[

{

test:/\.js$/,//什么文件就输入什么 js,css

exclude:/node_modules/,

//loader:"babel-loader",

use:['style-loader','css-loader'],

//loader:"css-loader",

//use:[MiniCssExtractPlugin.loader,'css-loader'],可以把css文件单独提取出来放入html文件里


},

{

test:/\.(jpg|png|gif)$/,

use:'file-loader'

}

]

},

plugins:[

new HtmlWebpackPlugin({

template:'./index.html',

filename:'index.html',//默认文件名 必须要加 不然多个导入后面会覆盖前面的

chunks:['index'],//告诉插件每个指定的html文件要引入的js文件

minify:{

removeComments:false,//删除index.html中的注释

collapseWhitespace:false,//删除index.html中的空格

removeAttributeQuotes:false,//删除index.html文件里的html标签属性值的双引号

}

}),

new HtmlWebpackPlugin({

template:'./inde.html',

filename:'inde.html',

chunks:['ajax'],

}),

new MiniCssExtractPlugin({

filename:'css/[name].css',

})

]

}


json文件:

{

"name": "webpack",

"version": "1.0.0",

"main": "index.js",

"dependencies": {

"ansi-styles": "^3.2.1",

"anymatch": "^2.0.0",

"arr-diff": "^4.0.0",

"arr-flatten": "^1.1.0",

"arr-union": "^3.1.0",

"array-unique": "^0.3.2",

"assign-symbols": "^1.0.0",

"async-each": "^1.0.3",

"atob": "^2.1.2",

"balanced-match": "^1.0.2",

"base": "^0.11.2",

"binary-extensions": "^1.13.1",

"bindings": "^1.5.0",

"brace-expansion": "^1.1.11",

"braces": "^2.3.2",

"cache-base": "^1.0.1",

"chalk": "^2.4.2",

"chokidar": "^2.1.8",

"class-utils": "^0.3.6",

"collection-visit": "^1.0.0",

"color-convert": "^1.9.3",

"color-name": "^1.1.3",

"commander": "^4.1.1",

"component-emitter": "^1.3.0",

"concat-map": "^0.0.1",

"convert-source-map": "^1.8.0",

"copy-descriptor": "^0.1.1",

"core-util-is": "^1.0.3",

"debug": "^4.3.3",

"decode-uri-component": "^0.2.0",

"define-property": "^2.0.2",

"escape-string-regexp": "^1.0.5",

"expand-brackets": "^2.1.4",

"extend-shallow": "^2.0.1",

"extglob": "^2.0.4",

"file-uri-to-path": "^1.0.0",

"fill-range": "^4.0.0",

"for-in": "^1.0.2",

"fragment-cache": "^0.2.1",

"fs-readdir-recursive": "^1.1.0",

"fs.realpath": "^1.0.0",

"fsevents": "^1.2.13",

"function-bind": "^1.1.1",

"gensync": "^1.0.0-beta.2",

"get-value": "^2.0.6",

"glob": "^7.2.0",

"glob-parent": "^3.1.0",

"globals": "^11.12.0",

"graceful-fs": "^4.2.9",

"has": "^1.0.3",

"has-flag": "^3.0.0",

"has-value": "^1.0.0",

"has-values": "^1.0.0",

"inflight": "^1.0.6",

"inherits": "^2.0.4",

"is-accessor-descriptor": "^1.0.0",

"is-binary-path": "^1.0.1",

"is-buffer": "^1.1.6",

"is-core-module": "^2.8.1",

"is-data-descriptor": "^1.0.0",

"is-descriptor": "^1.0.2",

"is-extendable": "^0.1.1",

"is-extglob": "^2.1.1",

"is-glob": "^4.0.3",

"is-number": "^3.0.0",

"is-plain-object": "^2.0.4",

"is-windows": "^1.0.2",

"isarray": "^1.0.0",

"isobject": "^3.0.1",

"js-tokens": "^4.0.0",

"jsesc": "^2.5.2",

"json5": "^2.2.0",

"kind-of": "^3.2.2",

"lodash": "^4.17.21",

"make-dir": "^2.1.0",

"map-cache": "^0.2.2",

"map-visit": "^1.0.0",

"micromatch": "^3.1.10",

"minimatch": "^3.0.4",

"minimist": "^1.2.5",

"mixin-deep": "^1.3.2",

"ms": "^2.1.2",

"nan": "^2.15.0",

"nanomatch": "^1.2.13",

"normalize-path": "^3.0.0",

"object-copy": "^0.1.0",

"object-visit": "^1.0.1",

"object.pick": "^1.3.0",

"once": "^1.4.0",

"pascalcase": "^0.1.1",

"path-dirname": "^1.0.2",

"path-is-absolute": "^1.0.1",

"path-parse": "^1.0.7",

"pify": "^4.0.1",

"posix-character-classes": "^0.1.1",

"process-nextick-args": "^2.0.1",

"readable-stream": "^2.3.7",

"readdirp": "^2.2.1",

"regex-not": "^1.0.2",

"remove-trailing-separator": "^1.1.0",

"repeat-element": "^1.1.4",

"repeat-string": "^1.6.1",

"resolve": "^1.22.0",

"resolve-url": "^0.2.1",

"ret": "^0.1.15",

"safe-buffer": "^5.1.2",

"safe-regex": "^1.1.0",

"semver": "^5.7.1",

"set-value": "^2.0.1",

"slash": "^2.0.0",

"snapdragon": "^0.8.2",

"snapdragon-node": "^2.1.1",

"snapdragon-util": "^3.0.1",

"source-map": "^0.5.7",

"source-map-resolve": "^0.5.3",

"source-map-url": "^0.4.1",

"split-string": "^3.1.0",

"static-extend": "^0.1.2",

"string_decoder": "^1.1.1",

"supports-color": "^5.5.0",

"supports-preserve-symlinks-flag": "^1.0.0",

"to-fast-properties": "^2.0.0",

"to-object-path": "^0.3.0",

"to-regex": "^3.0.2",

"to-regex-range": "^2.1.1",

"union-value": "^1.0.1",

"unset-value": "^1.0.0",

"upath": "^1.2.0",

"urix": "^0.1.0",

"use": "^3.1.1",

"util-deprecate": "^1.0.2",

"wrappy": "^1.0.2"

},

"devDependencies": {

"@babel/cli": "^7.10.5",

"@babel/core": "^7.11.0",

"@babel/preset-env": "^7.11.0",

"core-js": "^3.6.5",

"css-loader": "^4.1.1",

"file-loader": "^6.0.0",

"html-webpack-plugin": "^4.3.0",

"mini-css-extract-plugin": "^0.9.0",

"style-loader": "^1.2.1",

"webpack": "^4.44.1",

"webpack-cli": "^3.3.12"

},

"scripts": {

"build": "babel src -d dist",

"webpack": "webpack --config webpack.config.js"

},

"author": "",

"license": "ISC",

"description": ""

}




src文件下有

ajax.js

css.png

index.css//这个文件引入了png图片

index.html


输入命令后打包成

dist目录下

ajax.js

inde.html


然后就没有css文件和png文件 不知道怎么回事 老师帮忙看一下



正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕星星 2022-02-18 18:34:03

同学你好,测试代码这边是打包进去的

https://img1.sycdn.imooc.com//climg/620f75f409ddb69802820102.jpg

https://img1.sycdn.imooc.com//climg/620f7606096201f705120101.jpg

建议将dist目录移除后再次打包试试。


  • 提问者 慕尼黑0510008 #1

    https://img1.sycdn.imooc.com//climg/620f852209768cc404600856.jpg

    2022-02-18 19:38:16
  • 提问者 慕尼黑0510008 #2

    const path=require('path');

    const HtmlWebpackPlugin=require('html-webpack-plugin');

    //const { turn } = require('core-js/core/array');

    const MiniCssExtractPlugin=require('mini-css-extract-plugin');

    module.exports={

    mode:'development',

    entry:{

    index:'./src/index.js',//多个js文件导入 键就是默认文件名

    ajax:'./src/ajax.js'

    },

    output:{

    path:path.resolve(__dirname,'dist'),

    filename:'[name].js'

    },

    module:{

    rules:[

    {

    test:/\.css$/,//什么文件就输入什么 js,css

    exclude: /node_modules/,

    //loader:"babel-loader",

    //use:['style-loader','css-loader'],

    //loader:"css-loader",

    use:[{

    loader:MiniCssExtractPlugin.loader,

    options:{

    publicPath:'../'

    }

    },'css-loader']

    //use:[MiniCssExtractPlugin.loader,'css-loader'],可以把css文件单独提取出来放入html文件里


    },

    {

    test:/\.(jpg|png|gif)$/,

    use:'file-loader'

    }

    ]

    },

    plugins:[

    new HtmlWebpackPlugin({

    template:'./index.html',

    filename:'index.html',//默认文件名 必须要加 不然多个导入后面会覆盖前面的

    chunks:['index'],//告诉插件每个指定的html文件要引入的js文件

    minify:{

    removeComments:false,//删除index.html中的注释

    collapseWhitespace:false,//删除index.html中的空格

    removeAttributeQuotes:false,//删除index.html文件里的html标签属性值的双引号

    }

    }),

    // new HtmlWebpackPlugin({

    //    template:'./inde.html',

    //   filename:'inde.html',

    //  chunks:['ajax'],

    // }),

    new MiniCssExtractPlugin({

    filename:'css/[name].css',

    })

    ]

    }


    输入命令后就像是看不见src目录下的png和css文件一样 不知道是不是哪里配置问题

    运行内容如下

    > webpack@1.0.0 webpack

    > webpack --config webpack.config.js

    Hash: d532ed1c48044c16a829

    Version: webpack 4.44.1

    Time: 244ms

    Built at: 2022/02/18 下午7:35:34

         Asset      Size  Chunks             Chunk Names

       ajax.js  3.76 KiB    ajax  [emitted]  ajax

    index.html  2.92 KiB          [emitted]  

      index.js  3.77 KiB   index  [emitted]  index

    Entrypoint index = index.js

    Entrypoint ajax = ajax.js

    [./src/ajax.js] 0 bytes {ajax} [built]

    [./src/index.js] 0 bytes {index} [built]

    Child HtmlWebpackCompiler:

         1 asset

        Entrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0

        [./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 3.31 KiB {HtmlWebpackPlugin_0} [built]


    2022-02-18 19:41:07
  • 好帮手慕星星 回复 提问者 慕尼黑0510008 #3

    你好,如果此问题没有解决,可以看下私信哦。

    2022-02-19 13:31:45
好帮手慕星星 2022-02-18 17:23:11

同学你好,css规则配置的问题,如下修改:

https://img1.sycdn.imooc.com//climg/620f652509db907807740607.jpg

另外src中如果没有inde.html文件,需要把这部分配置去掉,避免报错

https://img1.sycdn.imooc.com//climg/620f656e09fdf1a506610418.jpg

祝学习愉快!

  • 提问者 慕尼黑0510008 #1

    修改后的代码是

    const path=require('path');

    const HtmlWebpackPlugin=require('html-webpack-plugin');

    //const { turn } = require('core-js/core/array');

    const MiniCssExtractPlugin=require('mini-css-extract-plugin');

    module.exports={

    mode:'development',

    entry:{

    index:'./src/index.js',//多个js文件导入 键就是默认文件名

    ajax:'./src/ajax.js'

    },

    output:{

    path:path.resolve(__dirname,'dist'),

    filename:'[name].js'

    },

    module:{

    rules:[

    {

    test:/\.css$/,//什么文件就输入什么 js,css

    exclude: /node_modules/,

    //loader:"babel-loader",

    //use:['style-loader','css-loader'],

    //loader:"css-loader",

    use:[{

    loader:MiniCssExtractPlugin.loader,

    options:{

    publicPath:'../'

    }

    },'css-loader']

    //use:[MiniCssExtractPlugin.loader,'css-loader'],可以把css文件单独提取出来放入html文件里


    },

    {

    test:/\.(jpg|png|gif)$/,

    use:'file-loader'

    }

    ]

    },

    plugins:[

    new HtmlWebpackPlugin({

    template:'./index.html',

    filename:'index.html',//默认文件名 必须要加 不然多个导入后面会覆盖前面的

    chunks:['index'],//告诉插件每个指定的html文件要引入的js文件

    minify:{

    removeComments:false,//删除index.html中的注释

    collapseWhitespace:false,//删除index.html中的空格

    removeAttributeQuotes:false,//删除index.html文件里的html标签属性值的双引号

    }

    }),

    // new HtmlWebpackPlugin({

    //    template:'./inde.html',

    //   filename:'inde.html',

    //  chunks:['ajax'],

    // }),

    new MiniCssExtractPlugin({

    filename:'css/[name].css',

    })

    ]

    }


    但是还是没有把src里面的index.css文件打包到dist文件里



    2022-02-18 18:19:04
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师