diff --git a/CHANGES.rst b/CHANGES.rst index 512e30c..dfe6df7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,14 @@ Changelog ========= +1.0.1 (Unreleased) +------------------ + +- Update the export_target production and usage of working_dir to be + inline with what is expected by ``calmjs-3.1.0``. [ + `#2 `_ + ] + 1.0.0 (2018-01-12) ------------------ diff --git a/src/calmjs/webpack/cli.py b/src/calmjs/webpack/cli.py index e660853..2522850 100644 --- a/src/calmjs/webpack/cli.py +++ b/src/calmjs/webpack/cli.py @@ -4,6 +4,8 @@ """ import logging +from os.path import join +from os.path import realpath from calmjs.toolchain import Spec from calmjs.toolchain import spec_update_sourcepath_filter_loaderplugins @@ -11,6 +13,7 @@ from calmjs.toolchain import BUILD_DIR from calmjs.toolchain import CALMJS_MODULE_REGISTRY_NAMES from calmjs.toolchain import CALMJS_LOADERPLUGIN_REGISTRY_NAME +from calmjs.toolchain import WORKING_DIR from calmjs.toolchain import EXPORT_TARGET from calmjs.toolchain import GENERATE_SOURCE_MAP @@ -194,13 +197,20 @@ def create_spec( working_dir = working_dir if working_dir else default_toolchain.join_cwd() + # Normalize export_target and raw webpack.output.library + # Note that webpack_output_library is not directly assigned at this + # moment. if export_target is None: # Take the final package name for now... if package_names: - export_target = package_names[-1] + '.js' + computed_output_library = package_names[-1] else: - export_target = 'calmjs.webpack.export.js' - logger.debug("'export_target' autoconfig to '%s'", export_target) + computed_output_library = 'calmjs.webpack.export' + export_target = realpath( + join(working_dir, computed_output_library + '.js')) + logger.info("'export_target' is now set to '%s'", export_target) + else: + computed_output_library = export_target[:-3] spec = Spec() @@ -227,6 +237,7 @@ def create_spec( ) spec[BUILD_DIR] = build_dir + spec[WORKING_DIR] = working_dir spec[CALMJS_MODULE_REGISTRY_NAMES] = source_registries spec[EXPORT_TARGET] = export_target spec[SOURCE_PACKAGE_NAMES] = package_names @@ -283,7 +294,7 @@ def create_spec( # currently calmjs_compat mode assumes that it must be "umd". if webpack_output_library is True: if webpack_entry_point == DEFAULT_BOOTSTRAP_EXPORT: - spec[WEBPACK_OUTPUT_LIBRARY] = export_target[:-3] + spec[WEBPACK_OUTPUT_LIBRARY] = computed_output_library logger.info( "calmjs_compat is disabled; webpack.output.library " "automatically set to '%s', derived from input package " @@ -292,6 +303,7 @@ def create_spec( spec[WEBPACK_OUTPUT_LIBRARY] ) else: + # set to entry_point as that is defined. spec[WEBPACK_OUTPUT_LIBRARY] = webpack_entry_point logger.info( "calmjs_compat is disabled; webpack.output.library " diff --git a/src/calmjs/webpack/runtime.py b/src/calmjs/webpack/runtime.py index 3303f30..bb84226 100644 --- a/src/calmjs/webpack/runtime.py +++ b/src/calmjs/webpack/runtime.py @@ -44,8 +44,10 @@ def init_argparser_working_dir(self, argparser): super(WebpackRuntime, self).init_argparser_working_dir( argparser, explanation=( - 'for this tool it will be used as the base directory to ' - 'find source files declared for bundling; ' + 'for this tool it will be used as the base directory for ' + 'locating the node_modules for the declared bundled source ' + 'files, and as the base directory for export_target and ' + 'build_dir paths; ' ), ) diff --git a/src/calmjs/webpack/tests/test_cli.py b/src/calmjs/webpack/tests/test_cli.py index 6d7f77e..4c48f0e 100644 --- a/src/calmjs/webpack/tests/test_cli.py +++ b/src/calmjs/webpack/tests/test_cli.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- import unittest +import os +from os.path import join from calmjs.toolchain import Spec from calmjs.utils import pretty_logging @@ -8,6 +10,8 @@ from calmjs.webpack.cli import compile_all from calmjs.testing.mocks import StringIO +from calmjs.testing.utils import remember_cwd +from calmjs.testing.utils import mkdtemp class CliTestCase(unittest.TestCase): @@ -16,6 +20,11 @@ class CliTestCase(unittest.TestCase): in the toolchain and/or the integration tests. """ + def setUp(self): + remember_cwd(self) + self.cwd = mkdtemp(self) + os.chdir(self.cwd) + def test_create_spec_empty(self): with pretty_logging(stream=StringIO()) as stream: spec = create_spec([]) @@ -27,7 +36,9 @@ def test_create_spec_empty(self): "'__calmjs__'", stream.getvalue() ) self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.export.js') + self.assertEqual(spec['working_dir'], self.cwd) + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.export.js')) self.assertEqual(spec['calmjs_module_registry_names'], []) self.assertIn('webpack_externals', spec) self.assertEqual(spec['webpack_output_library'], '__calmjs__') @@ -74,7 +85,8 @@ def test_create_spec_with_calmjs_webpack(self): with pretty_logging(stream=StringIO()) as stream: spec = create_spec(['calmjs.webpack']) self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.js')) self.assertEqual(spec.get('webpack_entry_point'), '__calmjs__') self.assertEqual( spec['calmjs_module_registry_names'], ['calmjs.module']) @@ -87,11 +99,38 @@ def test_create_spec_with_calmjs_webpack(self): "sourcepaths", log, ) + def test_create_spec_with_calmjs_webpack_manual_working_dir(self): + working_dir = mkdtemp(self) + with pretty_logging(stream=StringIO()) as stream: + spec = create_spec( + ['calmjs.webpack'], + working_dir=working_dir + ) + + self.assertTrue(isinstance(spec, Spec)) + self.assertEqual(spec['export_target'], join( + working_dir, 'calmjs.webpack.js')) + log = stream.getvalue() + self.assertIn("'export_target' is now set to", log) + + def test_create_spec_with_calmjs_webpack_manual_export_target(self): + with pretty_logging(stream=StringIO()) as stream: + spec = create_spec( + ['calmjs.webpack'], + export_target='somewhere.js', + ) + + self.assertTrue(isinstance(spec, Spec)) + self.assertEqual(spec['export_target'], 'somewhere.js') + log = stream.getvalue() + self.assertNotIn("'export_target' autoconfig to", log) + def test_create_spec_with_calmjs_webpack_entry_point_only(self): with pretty_logging(stream=StringIO()) as stream: spec = create_spec(['calmjs.webpack'], webpack_entry_point='entry') self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.js')) self.assertEqual( spec['calmjs_module_registry_names'], ['calmjs.module']) self.assertEqual( @@ -120,7 +159,8 @@ def test_create_spec_with_calmjs_webpack_no_bootstrap(self): with pretty_logging(stream=StringIO()) as stream: spec = create_spec(['calmjs.webpack'], calmjs_compat=False) self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.js')) self.assertEqual( spec['calmjs_module_registry_names'], ['calmjs.module']) self.assertEqual( @@ -140,7 +180,8 @@ def test_create_spec_with_calmjs_webpack_no_registry(self): spec = create_spec( ['calmjs.webpack'], source_registry_method='none') self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.js')) self.assertEqual( spec['calmjs_module_registry_names'], []) self.assertEqual( @@ -157,7 +198,8 @@ def test_create_spec_with_calmjs_webpack_manual_source(self): spec = create_spec( ['calmjs.webpack'], source_registries=['calmjs.module.tests']) self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.js')) self.assertEqual( spec['calmjs_module_registry_names'], ['calmjs.module.tests']) self.assertEqual( @@ -222,6 +264,28 @@ def test_create_spec_with_calmjs_webpack_output_library_disable(self): self.assertEqual(spec['webpack_entry_point'], 'custom_entry') self.assertNotIn('webpack_output_library', spec) + def test_create_spec_with_calmjs_webpack_output_library_enabled(self): + with pretty_logging(stream=StringIO()) as stream: + spec = create_spec( + ['calmjs.webpack'], calmjs_compat=False, + webpack_output_library=True, + ) + log = stream.getvalue() + self.assertNotIn( + "webpack_entry_point is ignored; set calmjs_compat to false " + "to enable manual webpack.entry specification", log + ) + self.assertNotIn( + "webpack.output.library is disabled; it will be unset.", log) + self.assertIn( + "calmjs_compat is disabled; webpack.output.library " + "automatically set to 'calmjs.webpack', derived from input " + "package names and export filename", log + ) + # note that this is probably an invalid value. + self.assertEqual(spec['webpack_entry_point'], '__calmjs__') + self.assertEqual(spec['webpack_output_library'], 'calmjs.webpack') + def test_toolchain_empty(self): # dict works well enough as a null toolchain with pretty_logging(stream=StringIO()) as stream: @@ -230,4 +294,5 @@ def test_toolchain_empty(self): self.assertNotIn('packages []', stream.getvalue()) self.assertIn('no packages specified', stream.getvalue()) self.assertTrue(isinstance(spec, Spec)) - self.assertEqual(spec['export_target'], 'calmjs.webpack.export.js') + self.assertEqual(spec['export_target'], join( + self.cwd, 'calmjs.webpack.export.js')) diff --git a/src/calmjs/webpack/tests/test_integration.py b/src/calmjs/webpack/tests/test_integration.py index 5564699..2a8ea74 100644 --- a/src/calmjs/webpack/tests/test_integration.py +++ b/src/calmjs/webpack/tests/test_integration.py @@ -503,7 +503,8 @@ def test_cli_create_spec(self): with pretty_logging(stream=StringIO()): spec = cli.create_spec( ['site'], source_registries=(self.registry_name,)) - self.assertEqual(spec['export_target'], 'site.js') + self.assertEqual( + spec['export_target'], join(self._env_root, 'site.js')) def test_cli_compile_all_site(self): # create a new working directory to install our current site @@ -763,8 +764,10 @@ def test_runtime_cli_help_text(self): 'defaults to last ${package_name}.js ', out) self.assertIn( '--working-dir the working directory; ' - 'for this tool it will be used as the base directory to ' - 'find source files declared for bundling; ', out) + 'for this tool it will be used as the base directory for ' + 'locating the node_modules for the declared bundled source ' + 'files, and as the base directory for export_target and ' + 'build_dir paths; ', out) self.assertIn('default is current working directory', out) def test_runtime_cli_compile_explicit_missing(self):