[英]import CSS and JS files using Webpack
我有這樣的目錄結構:
和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中為輸入及其各自的輸出文件指定單個目錄? 應該為custom1.js
和custom2.js
輸出類似all-custom.js
東西嗎?
我知道這些是一些非常基本的問題,我嘗試了谷歌,但沒有找到一個簡單的Webpack初學者教程。
import'./style.css';
從'./path/style.js'導入樣式;
NB。 通常,您需要在webpack.config.js文件中的es5中進行編碼 。 如果你想使用es6,請點擊鏈接我如何在webpack.config.js中使用ES6?
您可以使用https://github.com/webpack/css-loader進行CSS配置。
您可以在webpack中使用代碼拆分並指定多個入口點,但這將生成多個輸出文件。 看看以下鏈接的多個入口點部分。 https://webpack.github.io/docs/code-splitting.html
在多個項目中使用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
:假設這是主文件,app從這里開始
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.