UNPKG

3.75 kBPlain TextView Raw
1import * as _grunt from 'grunt';
2
3import * as _ from 'lodash';
4import * as TerserPlugin from 'terser-webpack-plugin';
5import { Plugin } from 'webpack';
6import * as browserConfig from './build/browser';
7import * as moduleConfig from './build/module';
8import * as serverConfig from './build/server';
9
10const serverConfigs = {
11 browser: browserConfig,
12 module: moduleConfig,
13 server: serverConfig,
14};
15
16_.forEach(serverConfigs, config => {
17 config.optimization = {
18 minimizer: [
19 new TerserPlugin({
20 cache: true,
21 parallel: true,
22 sourceMap: true,
23 terserOptions: {
24 output: {
25 beautify: true,
26 ascii_only: true,
27 },
28 compress: {
29 warnings: true,
30 sequences: false,
31 unused: false, // We need this off for OMeta
32 },
33 mangle: false,
34 },
35 }),
36 ],
37 };
38});
39
40export = (grunt: typeof _grunt) => {
41 grunt.initConfig({
42 clean: {
43 default: {
44 src: [`<%= grunt.option('target') %>`],
45 options: {
46 force: true,
47 },
48 },
49 },
50
51 checkDependencies: {
52 this: {
53 options: {
54 packageManager: 'npm',
55 // TODO: Enable when grunt-check-dependencies works correctly with deduped packages.
56 // onlySpecified: true
57 },
58 },
59 },
60
61 concat: _.mapValues(serverConfigs, (config, task) => {
62 const defines = (config.plugins as Array<
63 Plugin & { definitions?: {} }
64 >).find(plugin => plugin.definitions != null)!.definitions;
65 return {
66 options: {
67 banner: `
68 /*! Build: ${task} - <%= grunt.option('version') %>
69 Defines: ${JSON.stringify(defines, null, '\t')}
70 */
71 `,
72 },
73 src: ['out/pine.js'],
74 dest: 'out/pine.js',
75 };
76 }),
77
78 copy: {
79 default: {
80 files: [
81 {
82 expand: true,
83 cwd: 'src',
84 src: ['**'],
85 dest: `<%= grunt.option('target') %>`,
86 filter: (filename: string) =>
87 filename.endsWith('.d.ts') || !filename.endsWith('.ts'),
88 },
89 ],
90 },
91 },
92
93 gitinfo: {
94 commands: {
95 describe: ['describe', '--tags', '--always', '--long', '--dirty'],
96 },
97 },
98
99 rename: (() => {
100 const renames: _.Dictionary<{ src: string; dest: string }> = {};
101 _.forEach(serverConfigs, (_config, task) => {
102 renames[task] = {
103 src: 'out/pine.js',
104 dest: `out/pine-${task}-<%= grunt.option('version') %>.js`,
105 };
106 renames[`${task}.map`] = {
107 src: 'out/pine.js.map',
108 dest: `out/pine-${task}-<%= grunt.option('version') %>.js.map`,
109 };
110 });
111 return renames;
112 })(),
113
114 replace: {
115 'pine.js': {
116 src: 'out/pine.js',
117 overwrite: true,
118 replacements: [
119 {
120 from: /nodeRequire/g,
121 to: 'require',
122 },
123 ],
124 },
125 ..._.mapValues(serverConfigs, (_config, task) => {
126 return {
127 src: 'out/pine.js',
128 overwrite: true,
129 replacements: [
130 {
131 from: /sourceMappingURL=pine.js.map/g,
132 to: `sourceMappingURL=pine-${task}-<%= grunt.option('version') %>.js.map`,
133 },
134 ],
135 };
136 }),
137 },
138
139 webpack: serverConfigs,
140
141 ts: {
142 default: {
143 tsconfig: true,
144 options: {
145 additionalFlags: `--outDir <%= grunt.option('target') %> --resolveJsonModule`,
146 },
147 },
148 },
149 });
150
151 require('load-grunt-tasks')(grunt);
152
153 if (!grunt.option('target')) {
154 grunt.option('target', 'out/');
155 }
156
157 grunt.registerTask('version', function() {
158 this.requires('gitinfo:describe');
159 grunt.option('version', grunt.config.get('gitinfo.describe'));
160 });
161
162 for (const task of Object.keys(serverConfigs)) {
163 grunt.registerTask(task, [
164 'checkDependencies',
165 'webpack:' + task,
166 'gitinfo:describe',
167 'version',
168 'replace:pine.js',
169 `replace:${task}`,
170 `concat:${task}`,
171 `rename:${task}`,
172 `rename:${task}.map`,
173 ]);
174 }
175
176 grunt.registerTask('build', ['clean', 'checkDependencies', 'ts', 'copy']);
177};
178
\No newline at end of file