|
| 1 | +import * as rimraf from 'rimraf'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as webpack from 'webpack'; |
| 4 | + |
| 5 | +import { BuildTaskOptions } from '../commands/eject'; |
| 6 | +import { NgCliWebpackConfig } from '../models/webpack-config'; |
| 7 | +import { CliConfig } from '../models/config'; |
| 8 | +import { AotPlugin } from '@ngtools/webpack'; |
| 9 | + |
| 10 | +const autoprefixer = require('autoprefixer'); |
| 11 | +const ExtractTextPlugin = require('extract-text-webpack-plugin'); |
| 12 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 13 | +const SilentError = require('silent-error'); |
| 14 | +const Task = require('../ember-cli/lib/models/task'); |
| 15 | + |
| 16 | +const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin; |
| 17 | +const LoaderOptionsPlugin = webpack.LoaderOptionsPlugin; |
| 18 | + |
| 19 | + |
| 20 | +const header = `const path = require('path');`; |
| 21 | +const plugins = ` |
| 22 | +const { LoaderOptionsPlugin, NoEmitOnErrorsPlugin, ProgressPlugin } = require('webpack'); |
| 23 | +const { CommonsChunkPlugin } = require('webpack').optimize; |
| 24 | +const { BaseHrefWebpackPlugin, GlobCopyWebpackPlugin } = require('@angular/cli/plugins/webpack'); |
| 25 | +const { AotPlugin } = require('@ngtools/webpack'); |
| 26 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 27 | +const ExtractTextPlugin = require('extract-text-webpack-plugin'); |
| 28 | +const autoprefixer = require('autoprefixer'); |
| 29 | +`; |
| 30 | +const localVariables = `const nodeModules = path.join(process.cwd(), 'node_modules');`; |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +class JsonWebpackSerializer { |
| 35 | + constructor(private _root: string) {} |
| 36 | + |
| 37 | + private _escape(str: string) { |
| 38 | + return '\uFF01' + str + '\uFF01'; |
| 39 | + } |
| 40 | + |
| 41 | + private _serializeRegExp(re: RegExp) { |
| 42 | + return this._escape(re.toString()); |
| 43 | + } |
| 44 | + |
| 45 | + private _serializeFunction(fn: Function) { |
| 46 | + return this._escape(fn.toString()); |
| 47 | + } |
| 48 | + |
| 49 | + private _commonsChunkPluginSerialize(value: any): any { |
| 50 | + let minChunks = value.minChunks; |
| 51 | + switch(typeof minChunks) { |
| 52 | + case 'function': |
| 53 | + minChunks = this._serializeFunction(value.minChunks); |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + name: value.chunkNames, |
| 59 | + filename: value.filenameTemplate, |
| 60 | + minChunks, |
| 61 | + chunks: value.selectedChunks, |
| 62 | + async: value.async, |
| 63 | + minSize: value.minSize |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + private _extractTextPluginSerialize(value: any): any { |
| 68 | + return { |
| 69 | + filename: value.filename, |
| 70 | + disable: value.options.disable |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + private _aotPluginSerialize(value: AotPlugin): any { |
| 75 | + return Object.assign({}, value.options, { |
| 76 | + tsConfigPath: path.relative(this._root, value.options.tsConfigPath), |
| 77 | + mainPath: path.relative(value.basePath, value.options.mainPath), |
| 78 | + hostOverrideFileSystem: undefined, |
| 79 | + exclude: value.options.exclude.map(p => { |
| 80 | + return p.startsWith('/') ? path.relative(value.basePath, p) : p; |
| 81 | + }) |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + private _htmlWebpackPlugin(value: any) { |
| 86 | + return Object.assign({}, value.options, { |
| 87 | + template: './' + path.relative(this._root, value.options.template), |
| 88 | + filename: './' + path.relative(this._root, value.options.filename) |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private _loaderOptionsPlugin(plugin: any) { |
| 93 | + return Object.assign({}, plugin.options, { |
| 94 | + test: plugin.options.test instanceof RegExp |
| 95 | + ? this._serializeRegExp(plugin.options.test) |
| 96 | + : undefined, |
| 97 | + options: Object.assign({}, plugin.options.options, { |
| 98 | + context: '', |
| 99 | + postcss: plugin.options.options.postcss.map(x => { |
| 100 | + if (x && x.toString() == autoprefixer()) { |
| 101 | + return this._escape('autoprefixer()'); |
| 102 | + } else { |
| 103 | + if (typeof x == 'function') { |
| 104 | + return this._serializeFunction(x); |
| 105 | + } else { |
| 106 | + return x; |
| 107 | + } |
| 108 | + } |
| 109 | + }) |
| 110 | + }) |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private _pluginsReplacer(plugins: any[]) { |
| 115 | + return plugins.map(plugin => { |
| 116 | + let args = plugin.options || undefined; |
| 117 | + |
| 118 | + switch (plugin.constructor) { |
| 119 | + case webpack.optimize.CommonsChunkPlugin: |
| 120 | + args = this._commonsChunkPluginSerialize(plugin); |
| 121 | + break; |
| 122 | + case ExtractTextPlugin: |
| 123 | + args = this._extractTextPluginSerialize(plugin); |
| 124 | + break; |
| 125 | + case AotPlugin: |
| 126 | + args = this._aotPluginSerialize(plugin); |
| 127 | + break; |
| 128 | + case HtmlWebpackPlugin: |
| 129 | + args = this._htmlWebpackPlugin(plugin); |
| 130 | + break; |
| 131 | + case LoaderOptionsPlugin: |
| 132 | + args = this._loaderOptionsPlugin(plugin); |
| 133 | + break; |
| 134 | + default: |
| 135 | + } |
| 136 | + |
| 137 | + const argsSerialized = JSON.stringify(args, (k, v) => this._replacer(k, v), 2) || ''; |
| 138 | + return `\uFF02${plugin.constructor.name}(${argsSerialized})\uFF02`; |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + private _resolveReplacer(value: any) { |
| 143 | + return Object.assign({}, value, { |
| 144 | + modules: value.modules.map(x => './' + path.relative(this._root, x)) |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + private _outputReplacer(value: any) { |
| 149 | + return Object.assign({}, value, { |
| 150 | + path: './' + path.relative(this._root, value.path) |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + private _entryReplacer(value: any) { |
| 155 | + const newValue = Object.assign({}, value); |
| 156 | + for (const key of Object.keys(newValue)) { |
| 157 | + const v = newValue[key]; |
| 158 | + newValue[key] = v.map(x => './' + path.relative(this._root, x)); |
| 159 | + } |
| 160 | + return newValue; |
| 161 | + } |
| 162 | + |
| 163 | + private _loaderReplacer(loader: any) { |
| 164 | + if (typeof loader == 'string') { |
| 165 | + if (loader.match(/\/node_modules\/extract-text-webpack-plugin\//)) { |
| 166 | + return 'extract-text-webpack-plugin'; |
| 167 | + } else if (loader.match(/@ngtools\/webpack\/src\/index.ts/)) { |
| 168 | + // return '@ngtools/webpack'; |
| 169 | + } |
| 170 | + } else { |
| 171 | + if (loader.loader) { |
| 172 | + loader.loader = this._loaderReplacer(loader.loader); |
| 173 | + } |
| 174 | + } |
| 175 | + return loader; |
| 176 | + } |
| 177 | + |
| 178 | + private _ruleReplacer(value: any) { |
| 179 | + if (value.loaders) { |
| 180 | + value.loaders = value.loaders.map(loader => this._loaderReplacer(loader)); |
| 181 | + } |
| 182 | + if (value.loader) { |
| 183 | + value.loader = this._loaderReplacer(value.loader); |
| 184 | + } |
| 185 | + |
| 186 | + const replaceExcludeInclude = (v: any) => { |
| 187 | + if (typeof v == 'object') { |
| 188 | + if (v.constructor == RegExp) { |
| 189 | + return this._serializeRegExp(v); |
| 190 | + } |
| 191 | + return v; |
| 192 | + } else if (typeof v == 'string') { |
| 193 | + return './' + path.relative(this._root, v); |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + if (value.exclude) { |
| 198 | + value.exclude = Array.isArray(value.exclude) |
| 199 | + ? value.exclude.map(x => replaceExcludeInclude(x)) |
| 200 | + : replaceExcludeInclude(value.exclude); |
| 201 | + } |
| 202 | + if (value.include) { |
| 203 | + value.include = Array.isArray(value.include) |
| 204 | + ? value.include.map(x => replaceExcludeInclude(x)) |
| 205 | + : replaceExcludeInclude(value.include); |
| 206 | + } |
| 207 | + |
| 208 | + return value; |
| 209 | + } |
| 210 | + |
| 211 | + private _moduleReplacer(value: any) { |
| 212 | + return Object.assign({}, value, { |
| 213 | + rules: value.rules && value.rules.map(x => this._ruleReplacer(x)) |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + private _replacer(key: string, value: any) { |
| 218 | + if (value === undefined) { |
| 219 | + return value; |
| 220 | + } |
| 221 | + if (value === null) { |
| 222 | + return null; |
| 223 | + } |
| 224 | + if (value.constructor === RegExp) { |
| 225 | + return this._serializeRegExp(value); |
| 226 | + } |
| 227 | + |
| 228 | + return value; |
| 229 | + } |
| 230 | + |
| 231 | + serialize(config: any): string { |
| 232 | + // config = Object.assign({}, config); |
| 233 | + config['plugins'] = this._pluginsReplacer(config['plugins']); |
| 234 | + config['resolve'] = this._resolveReplacer(config['resolve']); |
| 235 | + config['resolveLoader'] = this._resolveReplacer(config['resolveLoader']); |
| 236 | + config['entry'] = this._entryReplacer(config['entry']); |
| 237 | + config['output'] = this._outputReplacer(config['output']); |
| 238 | + config['module'] = this._moduleReplacer(config['module']); |
| 239 | + config['context'] = undefined; |
| 240 | + |
| 241 | + return JSON.stringify(config, (k, v) => this._replacer(k, v), 2) |
| 242 | + .replace(/"\uFF01(.*?)\uFF01"/g, (_, v) => { |
| 243 | + return JSON.parse(`"${v}"`); |
| 244 | + }) |
| 245 | + .replace(/(\s*)(.*?)"\uFF02(.*?)\uFF02"(,?).*/g, (_, indent, key, value, comma) => { |
| 246 | + const ctor = JSON.parse(`"${value}"`).split(/\n+/g).join(indent); |
| 247 | + return `${indent}${key}new ${ctor}${comma}`; |
| 248 | + }) |
| 249 | + .replace(/"\uFF01(.*?)\uFF01"/g, (_, v) => { |
| 250 | + return JSON.parse(`"${v}"`); |
| 251 | + }); |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | + |
| 256 | +export default Task.extend({ |
| 257 | + run: function (runTaskOptions: BuildTaskOptions) { |
| 258 | + const project = this.cliProject; |
| 259 | + const outputPath = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir; |
| 260 | + |
| 261 | + if (project.root === outputPath) { |
| 262 | + throw new SilentError ('Output path MUST not be project root directory!'); |
| 263 | +
8210
} |
| 264 | + rimraf.sync(path.resolve(project.root, outputPath)); |
| 265 | + |
| 266 | + const webpackConfig = new NgCliWebpackConfig(runTaskOptions).config; |
| 267 | + const serializer = new JsonWebpackSerializer(process.cwd()); |
| 268 | + console.log(header + '\n\n' |
| 269 | + + plugins |
| 270 | + + localVariables |
| 271 | + + '\n\n' |
| 272 | + + 'module.exports = ' + serializer.serialize(webpackConfig) + ';\n'); |
| 273 | + |
| 274 | + return Promise.resolve(); |
| 275 | + } |
| 276 | +}); |
0 commit comments