Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: packagerChrome.py

Issue 29549786: Issue 5535 - Replace our module system with webpack (Closed)
Patch Set: Addressed Sebastian's feedback Created Sept. 23, 2017, 8:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packagerChrome.py
diff --git a/packagerChrome.py b/packagerChrome.py
index d75203d17b9e63262e69f44e5112cfc729005d8d..cb7734e0222828b2e2712f7315899f2e50699e68 100644
--- a/packagerChrome.py
+++ b/packagerChrome.py
@@ -7,11 +7,14 @@ import io
import json
import os
import re
+import shutil
from StringIO import StringIO
import struct
+import subprocess
import sys
-import collections
+import tempfile
+from ensure_dependencies import read_deps
from packager import (readMetadata, getDefaultFileName, getBuildVersion,
getTemplate, Files)
@@ -139,48 +142,75 @@ def createManifest(params, files):
return manifest.encode('utf-8')
-def convertJS(params, files):
- output_files = collections.OrderedDict()
- args = {}
-
- for item in params['metadata'].items('convert_js'):
- name, value = item
- filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups()
- if arg is None:
- output_files[filename] = (value.split(), item.source)
- else:
- args.setdefault(filename, {})[arg] = value
-
- template = getTemplate('modules.js.tmpl')
-
- for filename, (input_files, origin) in output_files.iteritems():
- if '/' in filename and not files.isIncluded(filename):
- continue
-
- current_args = args.get(filename, {})
- current_args['autoload'] = [module for module in
- current_args.get('autoload', '').split(',')
- if module != '']
-
- base_dir = os.path.dirname(origin)
- modules = []
-
- for input_filename in input_files:
- module_name = os.path.splitext(os.path.basename(input_filename))[0]
- prefix = os.path.basename(os.path.dirname(input_filename))
- if prefix != 'lib':
- module_name = '{}_{}'.format(prefix, module_name)
- with open(os.path.join(base_dir, input_filename), 'r') as file:
- modules.append((module_name, file.read().decode('utf-8')))
- files.pop(input_filename, None)
+def create_bundles(params, files):
+ base_extension_path = params['baseDir']
+ info_templates = {
+ 'chrome': 'chromeInfo.js.tmpl',
+ 'edge': 'edgeInfo.js.tmpl',
+ 'gecko-webext': 'geckoInfo.js.tmpl'
+ }
+ info_module = None
+
+ # Historically we didn't use relative paths when requiring modules, so in
+ # order for webpack to know where to find them we need to pass in a list
+ # of resolve paths - "./lib" and the "lib" directory of all immediate
+ # dependencies (except adblockplus).
+ # Going forward we should always use relative paths when requiring modules
+ # and once we do that everywhere we can remove this logic.
+ resolve_paths = [os.path.join(base_extension_path, 'lib')]
+ deps = read_deps(base_extension_path)
+ if deps:
+ for dep in deps:
+ if not dep.startswith('_') and dep != 'adblockplus':
+ dep_lib_path = os.path.join(base_extension_path, dep, 'lib')
+ if os.path.exists(dep_lib_path):
+ resolve_paths.append(dep_lib_path)
+ resolve_paths = ' '.join(resolve_paths)
+
+ temp_dir = tempfile.mkdtemp()
+ try:
+ info_module = os.path.join(temp_dir, 'info.js')
+ template = getTemplate(info_templates[params['type']])
+ with open(info_module, 'w') as info_file:
+ info_file.write(
+ template.render(
+ basename=params['metadata'].get('general', 'basename'),
+ version=params['metadata'].get('general', 'version')
+ ).encode('utf-8')
+ )
- files[filename] = template.render(
- args=current_args,
- basename=params['metadata'].get('general', 'basename'),
- modules=modules,
- type=params['type'],
- version=params['metadata'].get('general', 'version')
- ).encode('utf-8')
+ for item in params['metadata'].items('bundles'):
+ name, value = item
+ base_item_path = os.path.dirname(item.source)
+
+ bundle_file = os.path.relpath(os.path.join(base_item_path, name),
+ base_extension_path)
+ entry_files = [
+ os.path.join(
+ '.',
+ os.path.relpath(os.path.join(base_item_path, module_path),
+ base_extension_path)
+ )
+ for module_path in value.split()
+ ]
+ subprocess.check_call(
+ ['npm', 'run-script', 'webpack', '--silent'],
+ cwd=os.path.dirname(__file__),
+ env={
+ 'EXTENSION_PATH': base_extension_path,
+ 'ENTRY_POINTS': ' '.join(entry_files),
+ 'OUTPUT_PATH': temp_dir,
+ 'BUNDLE_NAME': bundle_file,
+ 'RESOLVE_PATHS': resolve_paths,
+ 'INFO_PATH': info_module,
+ 'PATH': os.environ['PATH']
+ }
+ )
+ for file_name in [bundle_file, bundle_file + '.map']:
+ with open(os.path.join(temp_dir, file_name), 'r') as f:
+ files[file_name] = f.read()
+ finally:
+ shutil.rmtree(temp_dir)
def toJson(data):
@@ -389,8 +419,8 @@ def createBuild(baseDir, type='chrome', outFile=None, buildNum=None, releaseBuil
files.readMappedFiles(mapped)
files.read(baseDir, skip=[opt for opt, _ in mapped])
- if metadata.has_section('convert_js'):
- convertJS(params, files)
+ if metadata.has_section('bundles'):
+ create_bundles(params, files)
if metadata.has_section('preprocess'):
files.preprocess(
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld