1 |
|
2 | "use strict";
|
3 |
|
4 | const fs = require("fs");
|
5 | const path = require("path");
|
6 |
|
7 | const gulp = require("gulp");
|
8 | const gulpRename = require("gulp-rename");
|
9 |
|
10 |
|
11 | const npm = require("../lib/npm-dependency");
|
12 | const loadPlugins = require("../lib/rollup-plugins");
|
13 | const error = require("../log/error");
|
14 | const warn = require("../log/warn");
|
15 | const debug = require("../log/compile");
|
16 | const size = require("../log/size");
|
17 |
|
18 | const TITLE = "npm";
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function createMpTask(dependencyName, mpDistPath, destName) {
|
27 | return () => {
|
28 | return gulp
|
29 | .src(path.join(mpDistPath, "**/*"))
|
30 | .pipe(
|
31 | debug({
|
32 | title: TITLE,
|
33 | srcName: `<${dependencyName}(component)>`,
|
34 | dist: path.join("miniprogram_npm", dependencyName) + path.sep,
|
35 |
|
36 |
|
37 | }),
|
38 | )
|
39 | .pipe(gulp.dest(destName))
|
40 | .on("error", error(`${TITLE} <${dependencyName}(component)>`))
|
41 | .pipe(
|
42 | size({
|
43 | title: TITLE,
|
44 | sub: `${dependencyName}(component)`,
|
45 | showFiles: false,
|
46 | showTotal: true,
|
47 | }),
|
48 | );
|
49 | };
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function createNpmTask(dependencyName, entryFilePath, destName, dependencyNames) {
|
60 | const gulpRollup = require("gulp-better-rollup");
|
61 | const rollup = require("rollup");
|
62 | if (!fs.existsSync(entryFilePath)) {
|
63 | warn(TITLE)(`can't resolve ${entryFilePath}`);
|
64 | return (cb) => {
|
65 | cb();
|
66 | }
|
67 | }
|
68 | return () =>
|
69 | gulp
|
70 | .src(entryFilePath)
|
71 | .pipe(
|
72 | debug({
|
73 | title: TITLE,
|
74 | srcName: `<${dependencyName}>`,
|
75 | dist: path.join("miniprogram_npm", dependencyName),
|
76 |
|
77 | }),
|
78 | )
|
79 | .pipe(
|
80 | gulpRollup(
|
81 | {
|
82 | rollup: rollup,
|
83 | onwarn: warn(TITLE, dependencyName),
|
84 | external: dependencyNames,
|
85 | treeshake: { propertyReadSideEffects: false },
|
86 | plugins: loadPlugins(),
|
87 | },
|
88 | {
|
89 | format: "es",
|
90 | esModule: false,
|
91 | }
|
92 | ),
|
93 | )
|
94 | .on("error", error(`${TITLE} <${dependencyName}>`))
|
95 | .pipe(
|
96 | gulpRename({
|
97 | basename: "index",
|
98 | extname: ".js",
|
99 | }),
|
100 | )
|
101 | .pipe(gulp.dest(destName))
|
102 | .pipe(size({ title: TITLE, sub: dependencyName, showFiles: false, showTotal: true }));
|
103 | }
|
104 |
|
105 | module.exports =
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function buildNpm(cwd, distPath, baseDependencies) {
|
113 | const mp_npm = npm.getMp_NpmDependencies(baseDependencies, cwd, cwd);
|
114 | const mpDependencies = mp_npm[0];
|
115 | const npmDependencies = mp_npm[1];
|
116 | const mpTask = Object.keys(mpDependencies).reduce((result, depName) => {
|
117 | const mpDistPath = mpDependencies[depName];
|
118 | const dest = path.join(distPath, "miniprogram_npm", depName);
|
119 | result.push(createMpTask(depName, mpDistPath, dest));
|
120 | return result
|
121 | }, []);
|
122 |
|
123 | const dependencyNames = Object.keys(npmDependencies);
|
124 | const npmTasks = dependencyNames.reduce((result, dependencyName) => {
|
125 | const entryFilePath = npmDependencies[dependencyName];
|
126 | const dest = path.join(distPath, "miniprogram_npm", dependencyName);
|
127 | result.push(createNpmTask(dependencyName, entryFilePath, dest, dependencyNames));
|
128 | return result;
|
129 | }, []);
|
130 | return gulp.parallel(mpTask.concat(npmTasks));
|
131 | };
|