1 | const rollup = require("rollup");
|
2 | const json = require("rollup-plugin-json");
|
3 | const babel = require("rollup-plugin-babel");
|
4 | const builtins = require("rollup-plugin-node-builtins");
|
5 | const glob = require("glob");
|
6 | const path = require("path");
|
7 | const fs = require("fs");
|
8 |
|
9 | const allMock = path.join(__dirname, "./allMock.js");
|
10 |
|
11 | const inputOptions = {
|
12 | input: allMock,
|
13 | plugins: [
|
14 | builtins(),
|
15 | json({
|
16 | preferConst: true,
|
17 | indent: " "
|
18 | }),
|
19 | babel({
|
20 | exclude: "node_modules/**",
|
21 | babelrc: false,
|
22 | runtimeHelpers: true,
|
23 | presets: [
|
24 | [
|
25 | "env",
|
26 | {
|
27 | targets: {
|
28 | node: "6.11.5"
|
29 | },
|
30 | modules: false
|
31 | }
|
32 | ]
|
33 | ]
|
34 | })
|
35 | ]
|
36 | };
|
37 | const outputOptions = {
|
38 | file: "./mock.js",
|
39 | format: "umd",
|
40 | name: "mock"
|
41 | };
|
42 | const importMockFiles = (mockFiles, mockPath) => {
|
43 | const importString = [];
|
44 | const dataString = [];
|
45 | mockFiles.forEach(filePath => {
|
46 | const fileName = filePath.replace(".js", "");
|
47 | importString.push(
|
48 | `import ${fileName} from "${path.join(mockPath, filePath)}";`
|
49 | );
|
50 | dataString.push(`${fileName}`);
|
51 | });
|
52 | return `
|
53 | ${importString.join("\n")}
|
54 |
|
55 | const data = Object.assign({},${dataString.join(",")});
|
56 |
|
57 | export default data;
|
58 | `;
|
59 | };
|
60 |
|
61 | async function build(mockPath, outputfile) {
|
62 | const mockFiles = glob.sync("**/*.js", {
|
63 | cwd: mockPath
|
64 | });
|
65 | const allMockText = importMockFiles(mockFiles, mockPath);
|
66 | fs.writeFileSync(allMock, allMockText);
|
67 |
|
68 | const bundle = await rollup.rollup(inputOptions);
|
69 |
|
70 | outputOptions.file = outputfile;
|
71 | await bundle.generate(outputOptions);
|
72 |
|
73 | await bundle.write(outputOptions);
|
74 | }
|
75 | build(
|
76 | "/Users/jim/Documents/GitHub/ant-design-pro/mock",
|
77 | "/Users/jim/Documents/GitHub/ant-design-pro/functions/mock/index.js"
|
78 | );
|
79 | module.exports = build;
|