UNPKG

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