1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var rollup = require('rollup');
|
12 | var path = require('path');
|
13 |
|
14 | module.exports = function(grunt) {
|
15 |
|
16 | grunt.registerMultiTask('rollup', 'Grunt plugin for rollup - next-generation ES6 module bundler', function() {
|
17 |
|
18 | var done = this.async();
|
19 |
|
20 | var options = this.options({
|
21 | cache: null,
|
22 | external: [],
|
23 | format: 'es',
|
24 | exports: 'auto',
|
25 | moduleId: null,
|
26 | moduleName: null,
|
27 | globals: {},
|
28 | indent: true,
|
29 | useStrict: true,
|
30 | banner: null,
|
31 | footer: null,
|
32 | intro: null,
|
33 | preferConst: false,
|
34 | outro: null,
|
35 | onwarn: null,
|
36 | paths: null,
|
37 | plugins:[],
|
38 | pureExternalModules: false,
|
39 | sourceMap: false,
|
40 | sourceMapFile: null,
|
41 | sourceMapRelativePaths: false,
|
42 | treeshake: true
|
43 | });
|
44 |
|
45 | var promises = this.files.map(function(f) {
|
46 |
|
47 | if (f.src.length === 0) {
|
48 | grunt.fail.warn('No entry point specified.');
|
49 | }
|
50 |
|
51 | var entry;
|
52 | if (f.src.length > 1) {
|
53 | entry = f.src;
|
54 | grunt.log.writeln('Multiple entry points detected. Be sure to include rollup-plugin-multi-entry in plugins.');
|
55 | } else {
|
56 | entry = f.src[0];
|
57 |
|
58 | if (!grunt.file.exists(entry)) {
|
59 | grunt.fail.warn('Entry point "' + entry + '" not found.');
|
60 | }
|
61 | }
|
62 |
|
63 | var plugins = options.plugins;
|
64 |
|
65 | if (typeof plugins === 'function') {
|
66 | plugins = plugins();
|
67 | }
|
68 |
|
69 | return rollup.rollup({
|
70 | cache: options.cache,
|
71 | input: entry,
|
72 | external: options.external,
|
73 | plugins: plugins,
|
74 | context: options.context,
|
75 | moduleContext: options.moduleContext,
|
76 | onwarn: options.onwarn,
|
77 | preferConst: options.preferConst,
|
78 | pureExternalModules: options.pureExternalModules,
|
79 | treeshake: options.treeshake
|
80 | }).then(function(bundle) {
|
81 |
|
82 | var sourceMapFile = options.sourceMapFile;
|
83 | if (!sourceMapFile && options.sourceMapRelativePaths) {
|
84 | sourceMapFile = path.resolve(f.dest);
|
85 | }
|
86 |
|
87 | return bundle.generate({
|
88 | format: options.format,
|
89 | exports: options.exports,
|
90 | paths: options.paths,
|
91 | moduleId: options.moduleId,
|
92 | name: options.moduleName,
|
93 | globals: options.globals,
|
94 | indent: options.indent,
|
95 | strict: options.useStrict,
|
96 | banner: options.banner,
|
97 | footer: options.footer,
|
98 | intro: options.intro,
|
99 | outro: options.outro,
|
100 | sourcemap: options.sourceMap,
|
101 | sourcemapFile: sourceMapFile
|
102 | });
|
103 | }).then(function(result) {
|
104 | var code = result.code;
|
105 |
|
106 | if (options.sourceMap === true) {
|
107 | var sourceMapOutPath = f.dest + '.map';
|
108 | grunt.file.write(sourceMapOutPath, result.map.toString());
|
109 | code += "\n//# sourceMappingURL=" + path.basename(sourceMapOutPath);
|
110 | } else if (options.sourceMap === "inline") {
|
111 | code += "\n//# sourceMappingURL=" + result.map.toUrl();
|
112 | }
|
113 |
|
114 | grunt.file.write(f.dest, code);
|
115 | });
|
116 | });
|
117 |
|
118 | Promise.all(promises)
|
119 | .then(function() {
|
120 | done();
|
121 | })
|
122 | .catch(function(error) {
|
123 | grunt.fail.warn(error);
|
124 | });
|
125 | });
|
126 |
|
127 | };
|