1 | const path = require('path')
|
2 | const fs = require('fs-extra')
|
3 | const chalk = require('chalk')
|
4 | const stripIndent = require('common-tags/lib/stripIndent')
|
5 | const getImportant = require('../source-checker/get-important')
|
6 | const fsOpts = { encoding: 'utf8' }
|
7 | const minifyFiles = require('./minify-files')
|
8 | const babel = require('babel-core')
|
9 |
|
10 | module.exports = async ({ env, program, pkg }) => {
|
11 | try {
|
12 | const srcPath = path.join(program.cwd, './src')
|
13 | const distPath = path.join(program.cwd, './dist')
|
14 | const manifestPath = path.join(distPath, './manifest.json')
|
15 | const pkgPath = path.join(program.cwd, './package.json')
|
16 | let kitVersion, manifest
|
17 | try {
|
18 | kitVersion = fs.readJsonSync(pkgPath).version
|
19 | manifest = await fs.readJson(manifestPath, fsOpts)
|
20 | } catch (err) {
|
21 | console.log(chalk.red('⚠️ You may be in the wrong directory or haven\'t run \'blink build-manifest\''))
|
22 | await Promise.reject(err)
|
23 | }
|
24 | const blinkFileName = `blink-${manifest.general.adFormat}.js`
|
25 | const creativeFileName = `${manifest.general.bridgeId}.js`
|
26 | let [sentry, createjs, jquery, blink, adFormat, creative, components, apl, clips] = await Promise.all([
|
27 | fs.readFile(path.join(__dirname, '../../node_modules/@sentry/browser/build/bundle.min.js'), fsOpts),
|
28 | fs.readFile(path.join(srcPath, 'createjs-2015.11.26.min.js'), fsOpts),
|
29 | fs.readFile(path.join(srcPath, 'jquery.min.js'), fsOpts),
|
30 | (kitVersion && +kitVersion.split('.')[0] < 4) ? '' : fs.readFile(path.join(srcPath, 'blink.js'), fsOpts),
|
31 | fs.readFile(path.join(srcPath, blinkFileName), fsOpts),
|
32 | fs.readFile(path.join(srcPath, creativeFileName), fsOpts).then((creative) => {
|
33 | return creative.replace(/new createjs.LoadQueue\(true\)/, 'new createjs.LoadQueue(true, $b.baseUrl)')
|
34 | }),
|
35 | getImportant('get-components.js', path.join(srcPath, creativeFileName)),
|
36 | env.withoutApl ? '' : fs.readFile(path.join(srcPath, 'apl.js'), fsOpts).catch(() => ''),
|
37 | !fs.existsSync(path.join(srcPath, 'clips.js')) ? '' : fs.readFile(path.join(srcPath, 'clips.js'), fsOpts)
|
38 | ])
|
39 | const babelPath = path.resolve(__dirname, '../../node_modules', 'babel-preset-env')
|
40 | creative = babel.transform(creative, { presets: babelPath, sourceType: 'script' }).code
|
41 |
|
42 | const blinkOpts = {
|
43 | videos: manifest.videos,
|
44 | bridgeId: manifest.general.bridgeId,
|
45 | adParametersDefaults: {
|
46 | pxp: manifest.pxp,
|
47 | prp: manifest.prp,
|
48 | spr: manifest.spr
|
49 | }
|
50 | }
|
51 | let getBlink
|
52 |
|
53 | if (kitVersion && +kitVersion.split('.')[0] < 4) {
|
54 | getBlink = `require('blink-${manifest.general.adFormat}')`
|
55 | } else {
|
56 | getBlink = `window.Blink${manifest.general.adFormat.replace(/^\w/, a => a.toUpperCase())}`
|
57 | }
|
58 | const files = {}
|
59 | files['blink.js'] = blink
|
60 | files[blinkFileName] = adFormat
|
61 | files[creativeFileName] = creative
|
62 | files['clips.js'] = clips
|
63 | files['get-vpaid-ad.js'] = `window.getVPAIDAd = function () {
|
64 | return new (${getBlink})(
|
65 | ${JSON.stringify(blinkOpts, null, 2)}
|
66 | )
|
67 | }`
|
68 | const minifyOpts = {
|
69 | warnings: false,
|
70 | compress: {
|
71 | drop_console: !env.withConsoleLogs,
|
72 | drop_debugger: !env.withDebuggerStatements
|
73 | },
|
74 | mangle: false,
|
75 | output: {
|
76 | beautify: false,
|
77 | quote_style: 1
|
78 | }
|
79 | }
|
80 | const componentTags = components.components
|
81 | .map((comp) => {
|
82 | comp.className = comp.className.toLowerCase().replace('blink.', '') + '-component'
|
83 | return comp
|
84 | })
|
85 | .reduce((tags, comp) => {
|
86 | const tag = `scope.setTag('${comp.className}', '${fs.readJsonSync(path.join(env.comp, comp.folder, '/package.json'), fsOpts).version}');`
|
87 | if (!tags.includes(tag)) tags.push(tag)
|
88 | return tags
|
89 | }, [])
|
90 | .join('')
|
91 | const sentryConfig = stripIndent`
|
92 | window.Sentry.init({
|
93 | dsn: 'https://f4891be3e8a7407bb4189c35c0054db6@sentry.io/224228',
|
94 | release: '${pkg.release}',
|
95 | environment: 'production',
|
96 | sampleRate: 0.1,
|
97 | blacklistUrls: [/localhost/, /127\\.0\\.0\\.1/]
|
98 | });
|
99 | window.Sentry.configureScope(function (scope) {
|
100 | scope.setTag('blink-cli', '${pkg.version}')
|
101 | ${componentTags}
|
102 | scope.setTag('starter-kit', '${kitVersion}')
|
103 | scope.addEventProcessor(function (event) {
|
104 | if (/trident/i.test(event.request.headers['User-Agent'])) return null
|
105 | if (window.$b && window.$b.adParameters && window.$b.adParameters.debug) {
|
106 | event.environment = 'test'
|
107 | }
|
108 | return event
|
109 | })
|
110 | })
|
111 | `
|
112 | const development = [sentry, sentryConfig, createjs, jquery].concat(Object.values(files)).join(';\n')
|
113 | const pDevelopment = fs.writeFile(path.join(distPath, apl ? 'ad_base.uncompressed.js' : 'ad.uncompressed.js'), development, fsOpts).then(() => {
|
114 | if (!program.silent) {
|
115 | console.log(`✅ Compiled ${apl ? 'ad_base.uncompressed.js' : 'ad.uncompressed.js'}`)
|
116 | }
|
117 | })
|
118 | const filesMinified = await minifyFiles(files, minifyOpts, program.silent)
|
119 | const sentryConfigMinified = await minifyFiles({ 'sentry-config.js': sentryConfig }, minifyOpts, program.silent)
|
120 | const production = [sentry, sentryConfigMinified, createjs, jquery, filesMinified].join(';\n')
|
121 | const pProducation = fs.writeFile(path.join(distPath, apl ? 'ad_base.js' : 'ad.js'), production, fsOpts).then(() => {
|
122 | if (!program.silent) {
|
123 | console.log(`✅ Minified ${apl ? 'ad_base.js' : 'ad.js'}`)
|
124 | }
|
125 | })
|
126 | let pAplDev, pAplProd
|
127 | if (apl) {
|
128 | const aplFiles = {}
|
129 | aplFiles['apl.js'] = apl
|
130 | aplFiles['get-vpaid-ad-apl.js'] = `window.getVPAIDAd = function () {
|
131 | return new (window.BlinkAPL.default)({"_debug": false})
|
132 | }`
|
133 | const aplDev = [apl, aplFiles['get-vpaid-ad-apl.js'].replace('false', 'true')].join(';\n')
|
134 | pAplDev = fs.writeFile(path.join(distPath, 'ad.uncompressed.js'), aplDev, fsOpts).then(() => {
|
135 | if (!program.silent) {
|
136 | console.log('✅ Compiled ad.uncompressed.js')
|
137 | }
|
138 | })
|
139 | const aplProd = await minifyFiles(aplFiles, minifyOpts, program.silent)
|
140 | pAplProd = fs.writeFile(path.join(distPath, 'ad.js'), aplProd, fsOpts).then(() => {
|
141 | if (!program.silent) {
|
142 | console.log('✅ Minified ad.js')
|
143 | }
|
144 | })
|
145 | }
|
146 | await Promise.all([pDevelopment, pProducation, pAplDev, pAplProd])
|
147 | return { development, production }
|
148 | } catch (err) {
|
149 | if (!program.silent) console.log(chalk.red('⚠️ the vpaid ad did NOT build'))
|
150 | return Promise.reject(err)
|
151 | }
|
152 | }
|