UNPKG

1.92 kBJavaScriptView Raw
1const rollup = require("rollup");
2const json = require("rollup-plugin-json");
3const babel = require("rollup-plugin-babel");
4const builtins = require("rollup-plugin-node-builtins");
5const glob = require("glob");
6const path = require("path");
7const fs = require("fs");
8
9const allMock = path.join(__dirname, "./allMock.js");
10
11const inputOptions = {
12 input: allMock,
13 plugins: [
14 builtins(),
15 json({
16 preferConst: true, // Default: false
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};
37const outputOptions = {
38 file: "./mock.js",
39 format: "umd",
40 name: "mock"
41};
42const 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
55const data = Object.assign({},${dataString.join(",")});
56
57export default data;
58`;
59};
60
61async 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 // create a bundle
68 const bundle = await rollup.rollup(inputOptions);
69 // generate code and a sourcemap
70 outputOptions.file = outputfile;
71 await bundle.generate(outputOptions);
72 // or write the bundle to disk
73 await bundle.write(outputOptions);
74}
75build(
76 "/Users/jim/Documents/GitHub/ant-design-pro/mock",
77 "/Users/jim/Documents/GitHub/ant-design-pro/functions/mock/index.js"
78);
79module.exports = build;