diff --git a/README.md b/README.md index a37835d..78c7afc 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ You can continue to load other resources such as CSS files as before using the ` NPM.JS dependencies ------------------- -1. Add package.json files into one or more of your apps. All package.json files will be merged. +1. Add package.json or package.json5 files into one or more of your apps. All package files will be merged. -2. Import in your JS files from any of the npm modules specified in your package.json files. +2. Import in your JS files from any of the npm modules specified in your package files. 3. Run `./manage.py transpile`. diff --git a/npm_mjs/management/commands/create_package_json.py b/npm_mjs/management/commands/create_package_json.py index 8974477..128eeb2 100644 --- a/npm_mjs/management/commands/create_package_json.py +++ b/npm_mjs/management/commands/create_package_json.py @@ -1,9 +1,9 @@ import json import os +import pyjson5 from django.apps import apps as django_apps from django.core.management.base import BaseCommand -from json_minify import json_minify from npm_mjs.paths import TRANSPILE_CACHE_PATH @@ -34,11 +34,15 @@ def handle(self, *args, **options): package = {} configs = django_apps.get_app_configs() for config in configs: - app_package_path = os.path.join(config.path, "package.json") - try: - with open(app_package_path) as data_file: - data = json.loads(json_minify(data_file.read())) - except OSError: + json5_package_path = os.path.join(config.path, "package.json5") + json_package_path = os.path.join(config.path, "package.json") + if os.path.isfile(json5_package_path): + with open(json5_package_path) as data_file: + data = pyjson5.decode(data_file.read()) + elif os.path.isfile(json_package_path): + with open(json_package_path) as data_file: + data = json.loads(data_file.read()) + else: continue deep_merge_dicts(package, data) if not os.path.exists(TRANSPILE_CACHE_PATH): diff --git a/npm_mjs/management/commands/npm_install.py b/npm_mjs/management/commands/npm_install.py index 73e388b..33ea902 100644 --- a/npm_mjs/management/commands/npm_install.py +++ b/npm_mjs/management/commands/npm_install.py @@ -16,7 +16,7 @@ from npm_mjs.tools import set_last_run -def install_npm(force, stdout): +def install_npm(force, stdout, post_npm_signal=True): change_times = [0] for path in SETTINGS_PATHS: change_times.append(os.path.getmtime(path)) @@ -59,7 +59,8 @@ def install_npm(force, stdout): if node_version > 16: os.environ["NODE_OPTIONS"] = "--openssl-legacy-provider" call(["npm", "install"], cwd=TRANSPILE_CACHE_PATH) - signals.post_npm_install.send(sender=None) + if post_npm_signal: + signals.post_npm_install.send(sender=None) npm_install = True return npm_install @@ -76,9 +77,13 @@ def add_arguments(self, parser): help="Force npm install even if no change is detected.", ) + parser.add_argument( + "--nosignal", + action="store_false", + dest="post_npm_signal", + default=True, + help="Send a signal after finishing npm install.", + ) + def handle(self, *args, **options): - if options["force"]: - force = True - else: - force = False - install_npm(force, self.stdout) + install_npm(options["force"], self.stdout, options["post_npm_signal"]) diff --git a/npm_mjs/management/commands/webpack.config.template.js b/npm_mjs/management/commands/webpack.config.template.js index debee02..53e43f7 100644 --- a/npm_mjs/management/commands/webpack.config.template.js +++ b/npm_mjs/management/commands/webpack.config.template.js @@ -9,23 +9,34 @@ const baseRule = { loader: "babel-loader", options: { presets: ["@babel/preset-env"], - plugins: ["@babel/plugin-syntax-dynamic-import", "@babel/plugin-proposal-optional-chaining"] + plugins: [ + "@babel/plugin-syntax-dynamic-import", + "@babel/plugin-proposal-optional-chaining" + ] } } } const predefinedVariables = { - "transpile_VERSION": transpile.VERSION + transpile_VERSION: transpile.VERSION } if (settings.DEBUG) { baseRule.exclude = /node_modules/ - predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url)` -} else if (settings.STATICFILES_STORAGE !== "npm_mjs.storage.ManifestStaticFilesStorage") { - predefinedVariables.staticUrl = `(url => ${JSON.stringify(settings.STATIC_URL)} + url + "?v=" + ${transpile.VERSION})` + predefinedVariables.staticUrl = `(url => ${JSON.stringify( + settings.STATIC_URL + )} + url)` +} else if ( + settings.STATICFILES_STORAGE !== + "npm_mjs.storage.ManifestStaticFilesStorage" +) { + predefinedVariables.staticUrl = `(url => ${JSON.stringify( + settings.STATIC_URL + )} + url + "?v=" + ${transpile.VERSION})` } -module.exports = { // eslint-disable-line no-undef +module.exports = { + // eslint-disable-line no-undef mode: settings.DEBUG ? "development" : "production", module: { rules: [baseRule] @@ -35,8 +46,6 @@ module.exports = { // eslint-disable-line no-undef chunkFilename: transpile.VERSION + "-[id].js", publicPath: transpile.BASE_URL }, - plugins: [ - new webpack.DefinePlugin(predefinedVariables) - ], + plugins: [new webpack.DefinePlugin(predefinedVariables)], entry: transpile.ENTRIES } diff --git a/npm_mjs/package.json b/npm_mjs/package.json deleted file mode 100644 index 4570ab4..0000000 --- a/npm_mjs/package.json +++ /dev/null @@ -1,18 +0,0 @@ -// Django-npm-mjs will combine this file with package.json files in other installed -// apps before executing npm commands. Different from a regular package.json, comments -// are allowed in this file. -{ - "description": "Install dependencies for ES6 transpilation", - "private": true, - "dependencies": { - "@babel/preset-env": "^7.19.1", - "@babel/core": "7.19.1", - "@babel/cli": "7.18.10", - "@babel/plugin-syntax-dynamic-import": "7.8.3", - "@babel/plugin-proposal-optional-chaining": "^7.12.1", - "@babel/plugin-transform-template-literals": "^7.18.6", - "babel-loader": "8.2.5", - "webpack": "5.74.0", - "webpack-cli": "4.10.0" - } -} diff --git a/npm_mjs/package.json5 b/npm_mjs/package.json5 new file mode 100644 index 0000000..097246f --- /dev/null +++ b/npm_mjs/package.json5 @@ -0,0 +1,18 @@ +// Django-npm-mjs will combine this file with package.json files in other installed +// apps before executing npm commands. Different from a regular package.json, comments +// are allowed in this file. +{ + description: "Install dependencies for ES6 transpilation", + private: true, + dependencies: { + "@babel/preset-env": "^7.23.2", + "@babel/core": "7.23.2", + "@babel/cli": "7.23.0", + "@babel/plugin-syntax-dynamic-import": "7.8.3", + "@babel/plugin-transform-optional-chaining": "^7.23.0", + "@babel/plugin-transform-template-literals": "^7.18.6", + "babel-loader": "9.1.3", + webpack: "5.89.0", + "webpack-cli": "5.1.4", + }, +} diff --git a/requirements.txt b/requirements.txt index acad291..d7d7276 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ Django >= 3.2 -JSON_minify == 0.3.0 +pyjson5 == 1.6.4