使用 webpack 内联 JavaScript 和 CSS
Inline JavaScript and CSS with webpack
我正在使用 Webpack 来捆绑资源。目前它将 CSS 和 JS 文件捆绑到一个名为 bundle.js 的单独文件中。我怎样才能使 JS 和 CSS 嵌入到 html 文件中?
import HtmlWebpackPlugin from 'html-webpack-plugin';
import {HotModuleReplacementPlugin} from 'webpack';
export default {
entry: {app: './test/dev'},
module: {
loaders: [
{test: /\.js/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.scss$/, loader: 'style!css!sass'}
]
},
plugins: [new HotModuleReplacementPlugin(), new HtmlWebpackPlugin()],
devtool: 'eval-source-map'
};
使用https://github.com/DustinJackson/html-webpack-inline-source-plugin
plugins: [
new HtmlWebpackPlugin({
inlineSource: '.(js|css)$' // embed all javascript and css inline
}),
new HtmlWebpackInlineSourcePlugin()
]
html-webpack-inline-source-plugin 不再有效,您可以使用 script-ext-html-webpack-plugin 代替。
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js'
},
output: {
path: __dirname + '/dist',
},
plugins: [
new HtmlWebpackPlugin({
cache: false
}),
new ScriptExtHtmlWebpackPlugin({
inline: [/\.js$/],
})
]
}
使用 InlineChunkHtmlPlugin 来自 react-dev-utils
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
module.exports = {
// ...
output: { filename: 'client-bundle.js' },
plugins: [
new HtmlWebpackPlugin(),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/client-bundle/]),
],
// ...
};
*其中 "/client-bundle/" 正则表达式应匹配输出文件名
对于内联 css 您需要额外的规则对象:
module.exports = {
entry: ['./src/style.css', './src/client.js'],
// ...
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader', // Creates `style` nodes from JS strings
'css-loader', // Translates CSS into CommonJS
],
},
],
},
}
我正在使用 Webpack 来捆绑资源。目前它将 CSS 和 JS 文件捆绑到一个名为 bundle.js 的单独文件中。我怎样才能使 JS 和 CSS 嵌入到 html 文件中?
import HtmlWebpackPlugin from 'html-webpack-plugin';
import {HotModuleReplacementPlugin} from 'webpack';
export default {
entry: {app: './test/dev'},
module: {
loaders: [
{test: /\.js/, loader: 'babel-loader', exclude: /node_modules/},
{test: /\.scss$/, loader: 'style!css!sass'}
]
},
plugins: [new HotModuleReplacementPlugin(), new HtmlWebpackPlugin()],
devtool: 'eval-source-map'
};
使用https://github.com/DustinJackson/html-webpack-inline-source-plugin
plugins: [
new HtmlWebpackPlugin({
inlineSource: '.(js|css)$' // embed all javascript and css inline
}),
new HtmlWebpackInlineSourcePlugin()
]
html-webpack-inline-source-plugin 不再有效,您可以使用 script-ext-html-webpack-plugin 代替。
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js'
},
output: {
path: __dirname + '/dist',
},
plugins: [
new HtmlWebpackPlugin({
cache: false
}),
new ScriptExtHtmlWebpackPlugin({
inline: [/\.js$/],
})
]
}
使用 InlineChunkHtmlPlugin 来自 react-dev-utils
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
module.exports = {
// ...
output: { filename: 'client-bundle.js' },
plugins: [
new HtmlWebpackPlugin(),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/client-bundle/]),
],
// ...
};
*其中 "/client-bundle/" 正则表达式应匹配输出文件名
对于内联 css 您需要额外的规则对象:
module.exports = {
entry: ['./src/style.css', './src/client.js'],
// ...
module: {
rules: [
{
test: /\.css$/i,
use: [
'style-loader', // Creates `style` nodes from JS strings
'css-loader', // Translates CSS into CommonJS
],
},
],
},
}