我有一个像这样的目录结构:
和node_modules内部:
>node_modules
>./bin
>webpack.config.js
>bootstrap
>bootstrap.css
>bootstrap.js
我需要像这样生成单独的CSS和JS包:
custom-styles.css,custom-js.js,style-libs.css,js-libs.js
其中style-libs
和js-libs
应该包含所有库的syles和js文件,例如bootstrap和jQuery。以下是我到目前为止所做的事情:
webpack.config.js:
const path = require('path');
const basedir = path.join(__dirname, '../../client');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const stylesPath = path.join(__dirname, '../bootstrap/dist/css');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
watch: true,
// Script to bundle using webpack
entry: path.join(basedir, 'src', 'Client.js'),
// Output directory and bundled file
output: {
path: path.join(basedir, 'dist'),
filename: 'app.js'
},
// Configure module loaders (for JS ES6, JSX, etc.)
module: {
// Babel loader for JS(X) files, presets configured in .babelrc
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
babelrc: false,
query: {
presets: ["es2015", "stage-0", "react"],
cacheDirectory: true // TODO: only on development
}
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader")
},
]
},
// Set plugins (for index.html, optimizations, etc.)
plugins: [
// Generate index.html
new HtmlWebpackPlugin({
template: path.join(basedir, 'src', 'index.html'),
filename: 'index.html'
}),
new ExtractTextPlugin(stylesPath + "/bootstrap.css", {
allChunks: true,
})
]
};
Client.js
import * as p from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';
ReactDOM.render(<App />, document.getElementById('app'));
我可以运行应用程序并正确呈现所有组件,除了使用webpack
加载外部JS和CSS文件。
我对webpack没有多少经验,发现我很难听到它的声音。有一些简单的问题:
1- 此配置是否正确?如果是,那么如何使用ES6将我的CSS和JS文件包含在组件中。类似于import
关键字。
2- 我是否应该将webpack用于CSS文件?
3- 如何在webpack中为输入及其各自的输出文件指定单个目录?应为all-custom.js
和custom1.js
输出custom2.js
之类的内容吗?
我知道这些是一些非常基本的问题,我尝试过谷歌,但没有找到一个简单的Webpack教程,并且针对初学者。
答案 0 :(得分:10)
import&#39; ./ style.css&#39;;
从&#39; ./ path / style.js&#39;;
导入样式
NB。通常,您需要在 webpack.config.js 文件中的es5中进行编码。如果您想使用es6,请点击链接How can I use ES6 in webpack.config.js?
您可以使用https://github.com/webpack/css-loader进行CSS配置。
您可以在webpack中使用代码拆分并指定多个入口点,但这会生成多个输出文件。看看以下链接的多个入口点部分。 https://webpack.github.io/docs/code-splitting.html
答案 1 :(得分:8)
在多个项目中使用Webpack后,我想出了Webpack如何加载东西。由于这个问题仍然没有答案,我决定自己为有同样需要的人做这件事。
目录结构
->assets
->css
->my-style-1.css //custom styling file 1
->my-style-2.css //custom styling file 2
->src
->app
->app.js
->variables.js
->libs.js //require all js libraries here
->styles-custom.js //require all custom css files here
->styles-libs.js //require all style libraries here
->node_modules
->index.html
->package.json
->webpack.config.js
捆绑1(应用程序主要代码)
app.js
:假设这是主文件,应用从这里开始
var msgs = require('./variables');
//similarly import other js files you need in this bundle
//your application code here...
document.getElementById('heading').innerText = msgs.foo;
document.getElementById('sub-heading').innerText = msgs.bar;
捆绑2(js模块)
libs.js
:此文件需要所有需要的模块
require('bootstrap');
//similarly import other js libraries you need in this bundle
捆绑3(外部css文件)
styles-libs.js
:此文件将需要所有外部css文件
require('bootstrap/dist/css/bootstrap.css');
//similarly import other css libraries you need in this bundle
捆绑4(自定义css文件)
styles-custom.js
:此文件将需要所有自定义css文件
require('../assets/css/my-style-1.css');
require('../assets/css/my-style-2.css');
//similarly import other css files you need in this bundle
<强> webpack.config.js 强>
const path = require('path');
const webpack = require('webpack');
const extractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: {
'app': './src/app/app.js', //specifying bundle with custom js files
'libs': './src/libs.js', //specifying bundle with js libraries
'styles-custom': './src/styles-custom.js', //specifying bundle with custom css files
'styles-libs': './src/styles-libs.js', //specifying bundle with css libraries
},
module: {
loaders: [
//used for loading css files
{
test: /\.css$/,
loader: extractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader?sourceMap' })
},
//used for loading fonts and images
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file-loader?name=assets/[name].[hash].[ext]'
}
]
},
output: {
path: path.resolve(__dirname, 'dist'), //directory for output files
filename: '[name].js' //using [name] will create a bundle with same file name as source
},
plugins: [
new extractTextPlugin('[name].css'), //is used for generating css file bundles
//use this for adding jquery
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jQuery'
})
]
}
<强>的index.html 强>
<head>
<link rel="stylesheet" href="dist/styles-libs.css" />
<link rel="stylesheet" href="dist/styles-custom.css" />
</head>
<body>
<h2 id="heading"></h2>
<h3>
<label id="sub-heading" class="label label-info"></label>
</h3>
<script src="dist/libs.js"></script>
<script src="dist/app.js"></script>
</body>