|
|
@@ -1,71 +1,120 @@
|
|
|
-const path = require('path');
|
|
|
-const webpack = require('webpack');
|
|
|
-const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
|
|
|
-const ExtractTextPlugin = require('extract-text-webpack-plugin');
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
|
|
|
const base = require('./webpack.base.conf');
|
|
|
-const { isProd, merge, INIT_FUNC_NAME } = require('./utils');
|
|
|
-
|
|
|
-const entry = {
|
|
|
- 'background/app': './src/background/app.js',
|
|
|
- 'options/app': './src/options/app.js',
|
|
|
- 'confirm/app': './src/confirm/app.js',
|
|
|
- 'popup/app': './src/popup/app.js',
|
|
|
- injected: './src/injected/index.js',
|
|
|
+const { isProd, INIT_FUNC_NAME } = require('./util');
|
|
|
+const MINIFY = isProd && {
|
|
|
+ collapseWhitespace: true,
|
|
|
+ removeAttributeQuotes: true,
|
|
|
+ removeComments: true,
|
|
|
+ removeOptionalTags: true,
|
|
|
+ removeRedundantAttributes: true,
|
|
|
+ removeScriptTypeAttributes: true,
|
|
|
+ removeStyleLinkTypeAttributes: true,
|
|
|
+};
|
|
|
+const defaultTemplateOptions = {
|
|
|
+ minify: MINIFY,
|
|
|
+ template: 'scripts/template.html',
|
|
|
+ meta: {
|
|
|
+ viewport: 'width=device-width,initial-scale=1.0,user-scalable=no',
|
|
|
+ },
|
|
|
+ css: [],
|
|
|
+ js: [],
|
|
|
};
|
|
|
|
|
|
-const targets = [];
|
|
|
-module.exports = targets;
|
|
|
+const targets = module.exports = [];
|
|
|
|
|
|
-targets.push(merge(base, {
|
|
|
- entry,
|
|
|
+const pages = {
|
|
|
+ 'browser': {
|
|
|
+ entry: './src/common/browser',
|
|
|
+ },
|
|
|
+ 'background/index': {
|
|
|
+ entry: './src/background',
|
|
|
+ html: {},
|
|
|
+ },
|
|
|
+ 'options/index': {
|
|
|
+ entry: './src/options',
|
|
|
+ html: {
|
|
|
+ js: [
|
|
|
+ '/public/lib/zip.js/zip.js',
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ },
|
|
|
+ 'confirm/index': {
|
|
|
+ entry: './src/confirm',
|
|
|
+ html: {},
|
|
|
+ },
|
|
|
+ 'popup/index': {
|
|
|
+ entry: './src/popup',
|
|
|
+ html: {},
|
|
|
+ },
|
|
|
+ injected: {
|
|
|
+ entry: './src/injected',
|
|
|
+ },
|
|
|
+};
|
|
|
+const entries = Object.entries(pages)
|
|
|
+.reduce((res, [key, { entry }]) => Object.assign(res, { [key]: entry }), {});
|
|
|
+const htmlPlugins = Object.entries(pages)
|
|
|
+.map(([key, { html }]) => {
|
|
|
+ let options;
|
|
|
+ if (html) {
|
|
|
+ options = {
|
|
|
+ filename: `${key}.html`,
|
|
|
+ chunks: ['browser', key],
|
|
|
+ ...defaultTemplateOptions,
|
|
|
+ };
|
|
|
+ if (typeof html === 'function') {
|
|
|
+ options = html(options);
|
|
|
+ } else {
|
|
|
+ options = {
|
|
|
+ ...options,
|
|
|
+ ...html,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (options) {
|
|
|
+ if (options.inlineSource) options.inject = false;
|
|
|
+ return new HtmlWebpackPlugin(options);
|
|
|
+ }
|
|
|
+})
|
|
|
+.filter(Boolean);
|
|
|
+
|
|
|
+targets.push({
|
|
|
+ ...base,
|
|
|
+ entry: entries,
|
|
|
+ optimization: {
|
|
|
+ ...base.optimization,
|
|
|
+ splitChunks: {
|
|
|
+ cacheGroups: {
|
|
|
+ common: {
|
|
|
+ name: 'common',
|
|
|
+ minChunks: 2,
|
|
|
+ chunks(chunk) {
|
|
|
+ return ![
|
|
|
+ 'browser',
|
|
|
+ 'injected',
|
|
|
+ ].includes(chunk.name);
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
plugins: [
|
|
|
- new webpack.optimize.CommonsChunkPlugin({
|
|
|
- name: 'common',
|
|
|
- chunks: Object.keys(entry).filter(name => name !== 'injected'),
|
|
|
- minChunks: (m, c) => c >= 2,
|
|
|
- }),
|
|
|
- new webpack.optimize.CommonsChunkPlugin({
|
|
|
- name: 'browser',
|
|
|
- chunks: ['common', 'injected'],
|
|
|
- minChunks: (m, c) => c >= 2,
|
|
|
- }),
|
|
|
- new HtmlWebpackPlugin({
|
|
|
- filename: 'background/index.html',
|
|
|
- chunks: ['browser', 'common', 'background/app'],
|
|
|
- }),
|
|
|
- new HtmlWebpackPlugin({
|
|
|
- filename: 'options/index.html',
|
|
|
- template: 'src/options/index.html',
|
|
|
- chunks: ['browser', 'common', 'options/app'],
|
|
|
- }),
|
|
|
- new HtmlWebpackPlugin({
|
|
|
- filename: 'confirm/index.html',
|
|
|
- template: 'src/confirm/index.html',
|
|
|
- chunks: ['browser', 'common', 'confirm/app'],
|
|
|
- }),
|
|
|
- new HtmlWebpackPlugin({
|
|
|
- filename: 'popup/index.html',
|
|
|
- template: 'src/popup/index.html',
|
|
|
- chunks: ['browser', 'common', 'popup/app'],
|
|
|
- }),
|
|
|
- // new FriendlyErrorsPlugin(),
|
|
|
- isProd && new ExtractTextPlugin('[name].css'),
|
|
|
- // new webpack.NormalModuleReplacementPlugin(/\.\/rules\.json$/, resource => {
|
|
|
- // resource.request = path.resolve(__dirname, '../src/resources/empty-rules.json');
|
|
|
- // }),
|
|
|
- ].filter(Boolean),
|
|
|
-}));
|
|
|
+ ...base.plugins,
|
|
|
+ ...htmlPlugins,
|
|
|
+ ],
|
|
|
+});
|
|
|
|
|
|
-targets.push(merge(base, {
|
|
|
+targets.push({
|
|
|
+ ...base,
|
|
|
entry: {
|
|
|
'injected-web': './src/injected/web',
|
|
|
},
|
|
|
output: {
|
|
|
+ ...base.output,
|
|
|
libraryTarget: 'commonjs2',
|
|
|
},
|
|
|
plugins: [
|
|
|
+ ...base.plugins,
|
|
|
new WrapperWebpackPlugin({
|
|
|
header: `\
|
|
|
window.${INIT_FUNC_NAME} = function () {
|
|
|
@@ -77,4 +126,4 @@ window.${INIT_FUNC_NAME} = function () {
|
|
|
};0;`,
|
|
|
}),
|
|
|
],
|
|
|
-}));
|
|
|
+});
|