1
- import { platformSupport } from '../../src/features.js'
2
- import { readFileSync } from 'fs'
3
- import { cwd } from '../../../scripts/script-utils.js'
4
- import { join } from 'path'
5
- import * as esbuild from 'esbuild'
6
- const ROOT = join ( cwd ( import . meta. url ) , '..' , '..' )
7
- const DEBUG = false
1
+ import { platformSupport } from '../../src/features.js' ;
2
+ import { readFileSync } from 'fs' ;
3
+ import { cwd } from '../../../scripts/script-utils.js' ;
4
+ import { join } from 'path' ;
5
+ import * as esbuild from 'esbuild' ;
6
+ const ROOT = join ( cwd ( import . meta. url ) , '..' , '..' ) ;
7
+ const DEBUG = false ;
8
8
9
9
const contentScopePath = 'src/content-scope-features.js' ;
10
10
const contentScopeName = 'contentScopeFeatures' ;
11
11
12
- const prefixMessage = '/*! © DuckDuckGo ContentScopeScripts protections https://github.com/duckduckgo/content-scope-scripts/ */'
12
+ const prefixMessage = '/*! © DuckDuckGo ContentScopeScripts protections https://github.com/duckduckgo/content-scope-scripts/ */' ;
13
13
14
14
/**
15
15
* @param {object } params
@@ -19,23 +19,18 @@ const prefixMessage = '/*! © DuckDuckGo ContentScopeScripts protections https:/
19
19
* @param {string } [params.name]
20
20
* @return {Promise<string> }
21
21
*/
22
- export async function bundle ( params ) {
23
- const {
24
- scriptPath,
25
- platform,
26
- name,
27
- featureNames,
28
- } = params
29
-
30
- const extensions = [ 'firefox' , 'chrome' , 'chrome-mv3' ]
31
- const isExtension = extensions . includes ( platform )
32
- let trackerLookup = '$TRACKER_LOOKUP$'
22
+ export async function bundle ( params ) {
23
+ const { scriptPath, platform, name, featureNames } = params ;
24
+
25
+ const extensions = [ 'firefox' , 'chrome' , 'chrome-mv3' ] ;
26
+ const isExtension = extensions . includes ( platform ) ;
27
+ let trackerLookup = '$TRACKER_LOOKUP$' ;
33
28
if ( ! isExtension ) {
34
- const trackerLookupData = readFileSync ( '../build/tracker-lookup.json' , 'utf8' )
35
- trackerLookup = trackerLookupData
29
+ const trackerLookupData = readFileSync ( '../build/tracker-lookup.json' , 'utf8' ) ;
30
+ trackerLookup = trackerLookupData ;
36
31
}
37
- const suffixMessage = platform === 'firefox' ? `/*# sourceURL=duckduckgo-privacy-protection.js?scope=${ name } */` : ''
38
- const loadFeaturesPlugin = loadFeatures ( platform , featureNames )
32
+ const suffixMessage = platform === 'firefox' ? `/*# sourceURL=duckduckgo-privacy-protection.js?scope=${ name } */` : '' ;
33
+ const loadFeaturesPlugin = loadFeatures ( platform , featureNames ) ;
39
34
// The code is using a global, that we define here which means once tree shaken we get a browser specific output.
40
35
41
36
/** @type {import("esbuild").BuildOptions } */
@@ -50,34 +45,34 @@ export async function bundle (params) {
50
45
globalName : name ,
51
46
loader : {
52
47
'.css' : 'text' ,
53
- '.svg' : 'text'
48
+ '.svg' : 'text' ,
54
49
} ,
55
50
define : {
56
51
'import.meta.env' : 'development' ,
57
52
'import.meta.injectName' : JSON . stringify ( platform ) ,
58
- 'import.meta.trackerLookup' : trackerLookup
53
+ 'import.meta.trackerLookup' : trackerLookup ,
59
54
} ,
60
55
plugins : [ loadFeaturesPlugin , contentFeaturesAsString ( platform ) ] ,
61
56
footer : {
62
- js : suffixMessage
57
+ js : suffixMessage ,
63
58
} ,
64
59
banner : {
65
- js : prefixMessage
66
- }
67
- }
60
+ js : prefixMessage ,
61
+ } ,
62
+ } ;
68
63
69
- const result = await esbuild . build ( buildOptions )
64
+ const result = await esbuild . build ( buildOptions ) ;
70
65
71
66
if ( result . metafile && DEBUG ) {
72
- console . log ( await esbuild . analyzeMetafile ( result . metafile ) )
67
+ console . log ( await esbuild . analyzeMetafile ( result . metafile ) ) ;
73
68
}
74
69
75
70
if ( result . errors . length === 0 && result . outputFiles ) {
76
- return result . outputFiles [ 0 ] . text || ''
71
+ return result . outputFiles [ 0 ] . text || '' ;
77
72
} else {
78
- console . log ( result . errors )
79
- console . log ( result . warnings )
80
- throw new Error ( 'could not continue' )
73
+ console . log ( result . errors ) ;
74
+ console . log ( result . warnings ) ;
75
+ throw new Error ( 'could not continue' ) ;
81
76
}
82
77
}
83
78
@@ -88,78 +83,76 @@ export async function bundle (params) {
88
83
* @param {string[] } featureNames
89
84
* @returns {import("esbuild").Plugin }
90
85
*/
91
- function loadFeatures ( platform , featureNames = platformSupport [ platform ] ) {
92
- const pluginId = 'ddg:platformFeatures'
86
+ function loadFeatures ( platform , featureNames = platformSupport [ platform ] ) {
87
+ const pluginId = 'ddg:platformFeatures' ;
93
88
return {
94
89
name : 'ddg:platformFeatures' ,
95
- setup ( build ) {
90
+ setup ( build ) {
96
91
build . onResolve ( { filter : new RegExp ( pluginId ) } , ( args ) => {
97
92
return {
98
93
path : args . path ,
99
- namespace : pluginId
100
- }
101
- } )
94
+ namespace : pluginId ,
95
+ } ;
96
+ } ) ;
102
97
build . onLoad ( { filter : / .* / , namespace : pluginId } , ( ) => {
103
98
// convert a list of feature names to
104
99
const imports = featureNames . map ( ( featureName ) => {
105
- const fileName = getFileName ( featureName )
106
- const path = `./src/features/${ fileName } .js`
107
- const ident = `ddg_feature_${ featureName } `
100
+ const fileName = getFileName ( featureName ) ;
101
+ const path = `./src/features/${ fileName } .js` ;
102
+ const ident = `ddg_feature_${ featureName } ` ;
108
103
return {
109
104
ident,
110
- importPath : path
111
- }
112
- } )
105
+ importPath : path ,
106
+ } ;
107
+ } ) ;
113
108
114
- const importString = imports . map ( imp => `import ${ imp . ident } from ${ JSON . stringify ( imp . importPath ) } ` )
115
- . join ( ';\n' )
109
+ const importString = imports . map ( ( imp ) => `import ${ imp . ident } from ${ JSON . stringify ( imp . importPath ) } ` ) . join ( ';\n' ) ;
116
110
117
- const exportsString = imports . map ( imp => `${ imp . ident } ` )
118
- . join ( ',\n ' )
111
+ const exportsString = imports . map ( ( imp ) => `${ imp . ident } ` ) . join ( ',\n ' ) ;
119
112
120
- const exportString = `export default {\n ${ exportsString } \n}`
113
+ const exportString = `export default {\n ${ exportsString } \n}` ;
121
114
122
115
return {
123
116
loader : 'js' ,
124
117
resolveDir : ROOT ,
125
- contents : [ importString , exportString ] . join ( '\n' )
126
- }
127
- } )
128
- }
129
- }
118
+ contents : [ importString , exportString ] . join ( '\n' ) ,
119
+ } ;
120
+ } ) ;
121
+ } ,
122
+ } ;
130
123
}
131
124
132
- function contentFeaturesAsString ( platform ) {
133
- const pluginId = 'ddg:contentScopeFeatures'
125
+ function contentFeaturesAsString ( platform ) {
126
+ const pluginId = 'ddg:contentScopeFeatures' ;
134
127
return {
135
128
/**
136
129
* Load all platform features based on current
137
130
*/
138
131
name : pluginId ,
139
- setup ( build ) {
132
+ setup ( build ) {
140
133
build . onResolve ( { filter : new RegExp ( pluginId ) } , ( args ) => {
141
134
return {
142
135
path : args . path ,
143
- namespace : pluginId
144
- }
145
- } )
136
+ namespace : pluginId ,
137
+ } ;
138
+ } ) ;
146
139
build . onLoad ( { filter : / .* / , namespace : pluginId } , async ( ) => {
147
140
const result = await bundle ( {
148
141
scriptPath : contentScopePath ,
149
142
name : contentScopeName ,
150
- platform
151
- } )
143
+ platform,
144
+ } ) ;
152
145
153
- const encodedString = result . replace ( / \r \n / g, '\n' )
146
+ const encodedString = result . replace ( / \r \n / g, '\n' ) ;
154
147
155
148
return {
156
149
loader : 'text' ,
157
150
resolveDir : ROOT ,
158
- contents : encodedString
159
- }
160
- } )
161
- }
162
- }
151
+ contents : encodedString ,
152
+ } ;
153
+ } ) ;
154
+ } ,
155
+ } ;
163
156
}
164
157
165
158
/**
@@ -170,4 +163,4 @@ function contentFeaturesAsString (platform) {
170
163
*/
171
164
function getFileName ( featureName ) {
172
165
return featureName . replace ( / ( [ a - z A - Z ] ) (? = [ A - Z 0 - 9 ] ) / g, '$1-' ) . toLowerCase ( ) ;
173
- }
166
+ }
0 commit comments