我正在构建一个组件库,我正在使用Webpack捆绑它。有些组件只依赖于我编写的html模板,css和JavaScript,但有些组件需要外部库。
我想要实现的是vendor.js
可选,如果您要使用的组件需要它,则包括
例如,如果用户只需要一个没有供应商依赖关系的组件,那么使用只包含我自己的代码的main.bundle.js
就足够了。
在我的index.js
中,我有以下导入:
import { Header } from './components/header/header.component';
import { Logotype } from './components/logotype/logotype.component';
import { Card } from './components/card/card.component';
import { NavigationCard } from './components/navigation-card/navigation-card.component';
import { AbstractComponent } from './components/base/component.abstract';
import { Configuration } from './system.config';
import 'bootstrap-table';
import './scss/base.scss';
所有这些导入都是我自己的,期待bootstrap-table
。
我已经像这样配置了Webpack:
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractScss = new ExtractTextPlugin({
filename: "[name].bundle.css"
});
module.exports = {
entry: {
main: './src/index.ts'
},
output: {
path: path.resolve(__dirname, 'dist/release'),
filename: "[name].bundle.js",
chunkFilename: "[name].bundle.js"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', // Specify the common bundle's name.
minChunks: function (module) {
// Here I would like to tell Webpack to give
// each bundle the ability to run independently
return module.context && module.context.indexOf('node_modules') >= 0;
}
}),
extractScss
],
devtool: "source-map",
resolve: {
// Add `.ts` as a resolvable extension.
extensions: ['.webpack.js', '.web.js', '.ts', '.js', '.ejs']
},
module: {
rules: [
// All files with a '.ts' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.ts?$/, exclude: /node_modules/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
// Allows for templates in separate ejs files
{test: /\.ejs$/, loader: 'ejs-compiled-loader'},
{
test: /\.scss$/,
use: extractScss.extract({
use: [{
loader: 'css-loader', options: {
sourceMap: true
}
}, {
loader: 'sass-loader', options: {
soureMap: true
}
}]
})}
]
}
}
这会产生两个.js
个文件和一个.css
个文件。但是,webpacks常用模块加载功能驻留在vendor.js
中,如果我不首先包含供应商,则会导致我的主要不可用,并且并不总是需要它。
总而言之,如果用户只需要页脚(没有外部依赖项),这就足够了:
<script src="main.bundle.js"></script>
如果用户想要使用具有外部依赖关系的表,则需要包括:
<script src="vendor.js"></script>
<script src="main.bundle.js"></script>
现在,只包括main.bundle.js
给了我这个错误:
Uncaught ReferenceError: webpackJsonp is not defined
。
我知道在Webpack配置中创建供应商块后,我可以通过添加它来提取所有常用功能:
new webpack.optimize.CommonsChunkPlugin({
name: 'common'
})
但是这种方法仍然要求用户包含两个.js
文件。
我该如何实现这一目标?当我不像上面那样提取常见模块时,它似乎只有2 kb不同,这对我没问题。
答案 0 :(得分:0)
事实证明,如果你能够做一些手工工作并且真正理解Webpack的作用(我没有做过),这很容易做到。我这样解决了:
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractScss = new ExtractTextPlugin({
filename: "[name].bundle.css"
});
module.exports = {
entry: {
main: './src/index.ts',
vendor: './src/vendor/vendor.ts'
},
output: {
path: path.resolve(__dirname, 'dist/release'),
filename: "[name].bundle.js",
chunkFilename: "[name].bundle.js"
},
plugins: [
extractScss
],
devtool: "source-map",
resolve: {
// Add `.ts` as a resolvable extension.
extensions: ['.webpack.js', '.web.js', '.ts', '.js', '.ejs']
},
module: {
rules: [
// All files with a '.ts' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.ts?$/, exclude: /node_modules/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
// Allows for templates in separate ejs files
{test: /\.ejs$/, loader: 'ejs-compiled-loader'},
{
test: /\.scss$/,
use: extractScss.extract({
use: [{
loader: 'css-loader', options: {
sourceMap: true
}
}, {
loader: 'sass-loader', options: {
soureMap: true
}
}]
})}
]
}
}
在vendor.ts
中,我只需导入我拥有的任何供应商依赖项:
import 'jquery';
import 'bootstrap-table';
这导致两个不同的文件,都有Webpacks引导逻辑。
希望这有助于某人。