UNPKG

2.32 kBJavaScriptView Raw
1/**
2 * dllplugin
3 */
4const path = require("path");
5const colors = require("colors");
6const cwd = process.cwd();
7
8module.exports = context => {
9 const { dll, load } = context;
10 if (!dll) return;
11
12 const UglifyJSPlugin = load("uglifyjs-webpack-plugin");
13 const webpack = load("webpack");
14 const config = require(path.join(cwd, "ynw.config.js"));
15 const dist = config.dll.dist;
16 const modules = config.dll.modules;
17
18 if (!modules) {
19 return;
20 }
21
22 for (let key in modules) {
23 const library = modules[key];
24 webpack(createOption({ library, dist, key, env: "dev" })).run(
25 exec(r => console.log(`${r}`.green))
26 );
27 webpack(createOption({ library, dist, key, env: "pro" })).run(
28 exec(r => console.log(`${r}`.green))
29 );
30 }
31
32 function createOption({ key, env, dist, library }) {
33 const folder = dist || "dll";
34 const target = path.join(cwd, folder, key);
35 const mode = env == "dev" ? "development" : "production";
36 const config = {
37 mode,
38 entry: { library },
39 output: {
40 path: target,
41 filename: `${key}.${env}.dll.js`,
42 library: "[name]"
43 },
44 plugins: [
45 new webpack.DllPlugin({
46 path: target + "/manifest.json",
47 name: "[name]",
48 context: cwd
49 })
50 ]
51 };
52
53 const createEnv = function(env) {
54 return new webpack.DefinePlugin({
55 "process.env.NODE_ENV": JSON.stringify(env)
56 });
57 };
58
59 if (env !== "dev") {
60 config.plugins.push(createEnv("production"));
61 config.plugins.push(new UglifyJSPlugin());
62 } else {
63 config.plugins.push(createEnv("development"));
64 }
65 return config;
66 }
67};
68
69/////////////////////////////////////////////////////////////
70
71/**
72 * Webpack package
73 * @param {Function} callback
74 */
75const exec = callback => (err, stats) => {
76 if (err) {
77 console.error(err.stack || err);
78 if (err.details) {
79 console.error(err.details);
80 }
81 callback(false);
82 return;
83 }
84 const info = stats.toJson();
85 if (stats.hasErrors()) {
86 console.error(info.errors);
87 }
88 if (stats.hasWarnings()) {
89 console.warn(info.warnings);
90 }
91 console.log(stats.toString({ chunks: false, colors: true }));
92 callback(true);
93};