Javascript CSS 字型影象的示例
必需的模組
npm install --save-dev webpack extract-text-webpack-plugin file-loader css-loader style-loader
資料夾結構
.
└── assets
├── css
├── images
└── js
webpack.config.js
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const glob = require('glob');
module.exports = {
entry: {
script: path.resolve(__dirname, './assets/js/app.js'),
style: path.resolve(__dirname, './assets/css/app.css'),
images: glob.sync(path.resolve(__dirname, './assets/images/**/*.*')),
},
context: __dirname,
output: {
path: path.resolve('./dist/assets'),
publicPath: '/dist/assets',
filename: '[name].js',
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
}),
},
{
test: /(\.woff2?|\.woff|\.ttf|\.eot|\.svg)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader?name=[name]-[hash:6].[ext]',
},
{
test: /\.(png|jpe?g|gif|ico)$/,
loader: 'file-loader?name=[name].[ext]',
},
],
},
plugins: [
new ExtractTextPlugin('app.css' /* optional: , { allChunks: true } */),
],
};
glob.sync('./assets/images/**/*.*')
將要求 images 資料夾中的所有檔案作為條目。
ExtractTextPlugin
將獲取生成的輸出並建立繫結的 css
檔案。