UNPKG

5.2 kBJavaScriptView Raw
1const path = require('path')
2const fs = require('fs-extra')
3const chalk = require('chalk')
4const stripIndent = require('common-tags/lib/stripIndent')
5const fsOpts = { encoding: 'utf8' }
6const minifyFiles = require('./minify-files')
7const babel = require('babel-core')
8const getImportant = require('../source-checker/get-important')
9
10module.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 creativeFileName = `${manifest.general.bridgeId}.js`
25 let [sentry, createjs, jquery, banners, creative, components, apl, clips] = await Promise.all([
26 fs.readFile(path.join(__dirname, '../../node_modules/@sentry/browser/build/bundle.min.js'), fsOpts),
27 fs.readFile(path.join(srcPath, 'createjs-2015.11.26.min.js'), fsOpts),
28 fs.readFile(path.join(srcPath, 'jquery.min.js'), fsOpts),
29 fs.readFile(path.join(srcPath, 'mraid-banner.js'), fsOpts),
30 fs.readFile(path.join(srcPath, creativeFileName), fsOpts).then((creative) => {
31 return creative.replace(/new createjs.LoadQueue\(true\)/, 'new createjs.LoadQueue(true, $b.baseUrl)')
32 }),
33 getImportant('get-components.js', path.join(srcPath, creativeFileName)),
34 '',
35 !fs.existsSync(path.join(srcPath, 'clips.js')) ? '' : fs.readFile(path.join(srcPath, 'clips.js'), fsOpts)
36 ])
37 const babelPath = path.resolve(__dirname, '../../node_modules', 'babel-preset-env')
38 creative = babel.transform(creative, { presets: babelPath, sourceType: 'script' }).code
39 const files = {}
40 const bannerOpts = {
41 videos: manifest.videos,
42 general: manifest.general,
43 tvCustomEvents: manifest.tvCustomEvents,
44 adParameters: {
45 geoInfo: manifest.geoInfo
46 }
47 }
48 files['mraid-banner.js'] = banners
49 files[creativeFileName] = creative
50 files['clips.js'] = clips
51 files['get-mraid-ad.js'] = `(function() {
52 new window.MraidBanner(${JSON.stringify(bannerOpts)})
53 })()`
54 const minifyOpts = {
55 warnings: false,
56 compress: {
57 drop_console: !env.withConsoleLogs,
58 drop_debugger: !env.withDebuggerStatements
59 },
60 mangle: false,
61 output: {
62 beautify: false,
63 quote_style: 1
64 }
65 }
66 const componentTags = components.components
67 .map((comp) => {
68 comp.className = comp.className.toLowerCase().replace('blink.', '') + '-component'
69 return comp
70 })
71 .reduce((tags, comp) => {
72 const tag = `scope.setTag('${comp.className}', '${fs.readJsonSync(path.join(env.comp, comp.folder, '/package.json'), fsOpts).version}');`
73 if (!tags.includes(tag)) tags.push(tag)
74 return tags
75 }, [])
76 .join('')
77 const sentryConfig = stripIndent`
78 window.Sentry.init({
79 dsn: 'https://f4891be3e8a7407bb4189c35c0054db6@sentry.io/224228',
80 release: '${pkg.release}',
81 environment: 'production',
82 sampleRate: 0.1,
83 blacklistUrls: [/localhost/, /127\\.0\\.0\\.1/]
84 });
85 window.Sentry.configureScope(function (scope) {
86 scope.setTag('blink-cli', '${pkg.version}')
87 ${componentTags}
88 scope.setTag('starter-kit', '${kitVersion}')
89 scope.addEventProcessor(function (event) {
90 if (/trident/i.test(event.request.headers['User-Agent'])) return null
91 if (window.$b && window.$b.adParameters && window.$b.adParameters.debug) {
92 event.environment = 'test'
93 }
94 return event
95 })
96 })
97 `
98 const development = [sentry, sentryConfig, createjs, jquery].concat(Object.values(files)).join(';\n')
99 const pDevelopment = fs.writeFile(path.join(distPath, apl ? 'ad_base.uncompressed.js' : 'ad.uncompressed.js'), development, fsOpts).then(() => {
100 if (!program.silent) {
101 console.log(`✅ Compiled ${apl ? 'ad_base.uncompressed.js' : 'ad.uncompressed.js'}`)
102 }
103 })
104 const filesMinified = await minifyFiles(files, minifyOpts, program.silent)
105 const sentryConfigMinified = await minifyFiles({ 'sentry-config.js': sentryConfig }, minifyOpts, program.silent)
106 const production = [sentry, sentryConfigMinified, createjs, jquery, filesMinified].join(';\n')
107 const pProducation = fs.writeFile(path.join(distPath, apl ? 'ad_base.js' : 'ad.js'), production, fsOpts).then(() => {
108 if (!program.silent) {
109 console.log(`✅ Minified ${apl ? 'ad_base.js' : 'ad.js'}`)
110 }
111 })
112 let pAplDev, pAplProd
113 await Promise.all([pDevelopment, pProducation, pAplDev, pAplProd])
114 return { development, production }
115 } catch (err) {
116 if (!program.silent) console.log(chalk.red('⚠️ the ad did NOT build'))
117 return Promise.reject(err)
118 }
119}