 Issue 29549786:
  Issue 5535 - Replace our module system with webpack  (Closed)
    
  
    Issue 29549786:
  Issue 5535 - Replace our module system with webpack  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public | 
| 2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 
| 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 
| 4 | 4 | 
| 5 import errno | 5 import errno | 
| 6 import io | 6 import io | 
| 7 import json | 7 import json | 
| 8 import os | 8 import os | 
| 9 import re | 9 import re | 
| 10 import shutil | |
| 10 from StringIO import StringIO | 11 from StringIO import StringIO | 
| 11 import struct | 12 import struct | 
| 13 import subprocess | |
| 12 import sys | 14 import sys | 
| 13 import collections | 15 import tempfile | 
| 14 | 16 | 
| 15 from packager import (readMetadata, getDefaultFileName, getBuildVersion, | 17 from packager import (readMetadata, getDefaultFileName, getBuildVersion, | 
| 16 getTemplate, Files) | 18 getTemplate, Files) | 
| 17 | 19 | 
| 18 defaultLocale = 'en_US' | 20 defaultLocale = 'en_US' | 
| 19 | 21 | 
| 20 | 22 | 
| 21 def getIgnoredFiles(params): | 23 def getIgnoredFiles(params): | 
| 22 return {'store.description'} | 24 return {'store.description'} | 
| 23 | 25 | 
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 # Normalize JSON structure | 134 # Normalize JSON structure | 
| 133 licenseComment = re.compile(r'/\*.*?\*/', re.S) | 135 licenseComment = re.compile(r'/\*.*?\*/', re.S) | 
| 134 data = json.loads(re.sub(licenseComment, '', manifest, 1)) | 136 data = json.loads(re.sub(licenseComment, '', manifest, 1)) | 
| 135 if '_dummy' in data: | 137 if '_dummy' in data: | 
| 136 del data['_dummy'] | 138 del data['_dummy'] | 
| 137 manifest = json.dumps(data, sort_keys=True, indent=2) | 139 manifest = json.dumps(data, sort_keys=True, indent=2) | 
| 138 | 140 | 
| 139 return manifest.encode('utf-8') | 141 return manifest.encode('utf-8') | 
| 140 | 142 | 
| 141 | 143 | 
| 142 def convertJS(params, files): | 144 def create_bundles(params, files): | 
| 143 output_files = collections.OrderedDict() | 145 base_extension_path = params['baseDir'] | 
| 144 args = {} | 146 info_templates = { | 
| 147 'chrome': 'chromeInfo.js.tmpl', | |
| 148 'edge': 'edgeInfo.js.tmpl', | |
| 149 'gecko-webext': 'geckoInfo.js.tmpl' | |
| 150 } | |
| 151 info_module = None | |
| 145 | 152 | 
| 146 for item in params['metadata'].items('convert_js'): | 153 # Once we use relative paths when requiring modules we can remove these, | 
| 147 name, value = item | 154 # but in the mean time Webpack needs to know where to look. | 
| 148 filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups() | 155 resolve_paths = ' '.join(['./lib', | 
| 149 if arg is None: | 156 './adblockpluscore/lib', | 
| 150 output_files[filename] = (value.split(), item.source) | 157 './adblockplusui/lib']) | 
| 151 else: | 158 try: | 
| 152 args.setdefault(filename, {})[arg] = value | 159 temp_dir = tempfile.mkdtemp() | 
| 160 template = getTemplate(info_templates[params['type']]) | |
| 161 with tempfile.NamedTemporaryFile(delete=False) as info_file: | |
| 
tlucas
2017/09/22 10:27:07
You are creating two separate temporary instances
 
kzar
2017/09/22 14:01:51
Good idea, we might as well have everything delete
 | |
| 162 info_file.write( | |
| 163 template.render( | |
| 164 basename=params['metadata'].get('general', 'basename'), | |
| 165 version=params['metadata'].get('general', 'version') | |
| 166 ).encode('utf-8') | |
| 167 ) | |
| 168 info_module = info_file.name | |
| 153 | 169 | 
| 154 template = getTemplate('modules.js.tmpl') | 170 for item in params['metadata'].items('bundles'): | 
| 171 name, value = item | |
| 172 base_item_path = os.path.dirname(item.source) | |
| 155 | 173 | 
| 156 for filename, (input_files, origin) in output_files.iteritems(): | 174 bundle_file = os.path.relpath(os.path.join(base_item_path, name), | 
| 157 if '/' in filename and not files.isIncluded(filename): | 175 base_extension_path) | 
| 158 continue | 176 entry_files = [ | 
| 159 | 177 os.path.join( | 
| 160 current_args = args.get(filename, {}) | 178 '.', | 
| 161 current_args['autoload'] = [module for module in | 179 os.path.relpath(os.path.join(base_item_path, module_path), | 
| 162 current_args.get('autoload', '').split(',') | 180 base_extension_path) | 
| 163 if module != ''] | 181 ) | 
| 164 | 182 for module_path in value.split() | 
| 165 base_dir = os.path.dirname(origin) | 183 ] | 
| 166 modules = [] | 184 subprocess.check_call( | 
| 167 | 185 ['npm', 'run-script', 'webpack'], | 
| 168 for input_filename in input_files: | 186 cwd=os.path.dirname(__file__), | 
| 169 module_name = os.path.splitext(os.path.basename(input_filename))[0] | 187 env={ | 
| 170 prefix = os.path.basename(os.path.dirname(input_filename)) | 188 'EXTENSION_PATH': base_extension_path, | 
| 171 if prefix != 'lib': | 189 'ENTRY_POINTS': ' '.join(entry_files), | 
| 172 module_name = '{}_{}'.format(prefix, module_name) | 190 'OUTPUT_PATH': temp_dir, | 
| 173 with open(os.path.join(base_dir, input_filename), 'r') as file: | 191 'BUNDLE_NAME': bundle_file, | 
| 174 modules.append((module_name, file.read().decode('utf-8'))) | 192 'RESOLVE_PATHS': resolve_paths, | 
| 175 files.pop(input_filename, None) | 193 'INFO_PATH': info_module, | 
| 176 | 194 'PATH': os.environ['PATH'] | 
| 177 files[filename] = template.render( | 195 } | 
| 178 args=current_args, | 196 ) | 
| 179 basename=params['metadata'].get('general', 'basename'), | 197 for file_name in [bundle_file, bundle_file + '.map']: | 
| 180 modules=modules, | 198 with open(os.path.join(temp_dir, file_name), 'r') as f: | 
| 181 type=params['type'], | 199 files[file_name] = f.read() | 
| 182 version=params['metadata'].get('general', 'version') | 200 finally: | 
| 183 ).encode('utf-8') | 201 if info_module: | 
| 202 os.remove(info_module) | |
| 203 shutil.rmtree(temp_dir) | |
| 184 | 204 | 
| 185 | 205 | 
| 186 def toJson(data): | 206 def toJson(data): | 
| 187 return json.dumps( | 207 return json.dumps( | 
| 188 data, ensure_ascii=False, sort_keys=True, | 208 data, ensure_ascii=False, sort_keys=True, | 
| 189 indent=2, separators=(',', ': ') | 209 indent=2, separators=(',', ': ') | 
| 190 ).encode('utf-8') + '\n' | 210 ).encode('utf-8') + '\n' | 
| 191 | 211 | 
| 192 | 212 | 
| 193 def import_string_webext(data, key, source): | 213 def import_string_webext(data, key, source): | 
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 'metadata': metadata, | 402 'metadata': metadata, | 
| 383 } | 403 } | 
| 384 | 404 | 
| 385 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] | 405 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] | 
| 386 files = Files(getPackageFiles(params), getIgnoredFiles(params), | 406 files = Files(getPackageFiles(params), getIgnoredFiles(params), | 
| 387 process=lambda path, data: processFile(path, data, params)) | 407 process=lambda path, data: processFile(path, data, params)) | 
| 388 | 408 | 
| 389 files.readMappedFiles(mapped) | 409 files.readMappedFiles(mapped) | 
| 390 files.read(baseDir, skip=[opt for opt, _ in mapped]) | 410 files.read(baseDir, skip=[opt for opt, _ in mapped]) | 
| 391 | 411 | 
| 392 if metadata.has_section('convert_js'): | 412 if metadata.has_section('bundles'): | 
| 393 convertJS(params, files) | 413 create_bundles(params, files) | 
| 394 | 414 | 
| 395 if metadata.has_section('preprocess'): | 415 if metadata.has_section('preprocess'): | 
| 396 files.preprocess( | 416 files.preprocess( | 
| 397 [f for f, _ in metadata.items('preprocess')], | 417 [f for f, _ in metadata.items('preprocess')], | 
| 398 {'needsExt': True} | 418 {'needsExt': True} | 
| 399 ) | 419 ) | 
| 400 | 420 | 
| 401 if metadata.has_section('import_locales'): | 421 if metadata.has_section('import_locales'): | 
| 402 import_locales(params, files) | 422 import_locales(params, files) | 
| 403 | 423 | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 416 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 436 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 
| 417 ) | 437 ) | 
| 418 | 438 | 
| 419 zipdata = files.zipToString() | 439 zipdata = files.zipToString() | 
| 420 signature = None | 440 signature = None | 
| 421 pubkey = None | 441 pubkey = None | 
| 422 if keyFile != None: | 442 if keyFile != None: | 
| 423 signature = signBinary(zipdata, keyFile) | 443 signature = signBinary(zipdata, keyFile) | 
| 424 pubkey = getPublicKey(keyFile) | 444 pubkey = getPublicKey(keyFile) | 
| 425 writePackage(outFile, pubkey, signature, zipdata) | 445 writePackage(outFile, pubkey, signature, zipdata) | 
| OLD | NEW |