How to use vue development mode with Django - django

I don't know how to run development mode with Django. I'm running webpack and when I finish all the Vuejs work I just bundle all to a folder where Django serves It as a static file. I know taht I've to run webpack with mode development but that doens't work, It gives me a cannot found error.
I'd like to run Vuejs in development mode alongside with Django, how can I do that?
I'll share to you my project structure, my package.json and webpack config.
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
entry: './frontend/Vue/main.js',
output: {
filename: 'build.js',
path: path.resolve(__dirname, './static/js'),
publicPath: '/static/js'
},
module: {
rules: [
{
test: /\.(js)$/,
exclude: /(node_modules)/,
use: [
{ loader: 'babel-loader' }
]
},
{
test: /\.(css|scss)/,
use: [
'style-loader'
]
},
{
test: /\.vue$/,
exclude: /node_modules/,
loader: 'vue-loader'
}
]
},
devServer: {
// contentBase: ponerle la ruta del index de django
contentBase: path.join(__dirname, 'templates'),
watchContentBase: true,
historyApiFallback: true,
noInfo: true
},
plugins: [
new VueLoaderPlugin()
]
}
package.json
{
"name": "TuDistribuidora",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server --mode development --port 9000 --open",
"build": "webpack --progress"
},
"repository": {
"type": "git",
"url": "git+https://github.com/EmiBuffet/TuDistribuidora.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/EmiBuffet/TuDistribuidora/issues"
},
"homepage": "https://github.com/EmiBuffet/TuDistribuidora#readme",
"dependencies": {
"vue": "^2.6.11",
"vue-router": "^3.4.5",
"vuex": "^3.5.1"
},
"devDependencies": {
"#babel/core": "^7.11.1",
"babel-loader": "^8.1.0",
"babel-preset-es2015": "^6.24.1",
"node-sass": "^4.14.1",
"sass-loader": "^9.0.3",
"style-loader": "^1.2.1",
"vue-loader": "^15.9.3",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0",
"bulma": "^0.9.1"
}
}
project structure:

Using django-webpack-loader in your Django project in combination with webpack-bundle-tracker in your webpack build will provide the connection needed to alternate between production/dev Vue builds quickly. The bundle information will be recorded by webpack into a webpack-stats.json which will then be used by Django to fetch/load the correct bundles for a given view.
I have an article explaining step-by-step how to set up a project with the above techniques.
Good hacking!

Related

Using ts-node with ESM module: ipfs-http-client

I am trying to run some tests on files I created which use ipfs-http-client.
I write code with typescript and I am using ts-node to run files without actually doing the build (when i do tests). I am running this code in a commonJs folder. As ipfs packages is ESM only, it gives me an error each time i try to run this code
No "exports" main defined in ..... (points to the ipfs folder in node_modules)
To reproduce the issue I created a bare project which shows exactly what is happening.
I run npm init and npm i ipfs-http-client.
I have already installed ts-node.
the packages.json file looks like this
{
"name": "import_esm_in_commonjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ipfs-http-client": "^59.0.0"
}
}
and, for tsConfig file, I am just using the basic ts-node one:
{
"ts-node": {
"cwd": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS",
"projectSearchDir": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS"
},
"compilerOptions": {
"lib": [
"es2021"
],
"module": "commonjs",
"target": "es2021",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": [
"node"
],
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": true,
"declaration": false,
"noEmit": false,
"outDir": "./.ts-node"
}
}
I just created an index.ts file in the root of the project which looks like this:
import "ipfs-http-client"
console.log("file works");
when I run ts-node index.ts I receive the previously mentioned error:
No "exports" main defined in ..... (pointing to the ipfs folder in the node_modules)
Hope someone can help!
Actually I found how to do it.
In order to be able to import esm only modules in a typescripy project with "type": "commonJs"in the package.json like this:
import { create } from "ipfs-http-client";
console.log(create);
I did the following:
explicitly set tsconfig file for ts-node:
{
"ts-node": {
"cwd": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS",
"projectSearchDir": "/Users/WAW/Documents/Projects/_issues_stack_over_flow/import_ESM_in_commonJS"
},
"compilerOptions": {
"lib": [
"es2021"
],
"module": "commonjs",
"target": "es2021",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": [
"node"
],
"sourceMap": true,
"inlineSourceMap": false,
"inlineSources": true,
"declaration": false,
"noEmit": false,
"outDir": "./.ts-node"
}
}
set "module": "ES2022" and "modeResolution": "Node" in the tsconfig.json in the tsconfig.json
{
...
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "Node",
...
}
when calling ts-node use the --esm flag:
ts-node --esm name_of_the_file
and it works =)
ps: I also updated the repo

Dependencies aren't being included on the build output of electron forge

My electron app project uses create-react-app and my main process needed the electron-devtools-installer module. I'm using electron-forge for building the app. Now after running npm run make to package and built the app, I'm getting this error message
Error: Cannot find module 'electron-devtools-installer'
after opening the app. I'm not getting this error when I simply run electron ., thus I suspect the module is not being included on the packaging.
Now the said module is for development only, I could simply write a logic to ignore it when in production mode but what happens when I really need a 3rd party modules in my main process in the production, so how do I include the dependencies modules in the packaging?
I moved the electron-devtools-installer mode in dependencies key in package.json but it still doesn't work.
My package.json contains:
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"homepage": ".",
"description": "some description",
"main": "main/init.js",
"author": "anonymouse",
"license": "MIT",
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"electron-devtools-installer": "^3.2.0",
"electron-squirrel-startup": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "electron-forge start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "concurrently -k \"react-scripts start\" \"npm:electron\"",
"electron": "wait-on tcp:3000 && electron .",
"package": "react-scripts build && electron-forge package",
"make": "react-scripts build && electron-forge make",
"clean:winbuild": "rmdir build /s /q & rmdir out /s /q"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#electron-forge/cli": "^6.0.0-beta.67",
"#electron-forge/maker-deb": "^6.0.0-beta.67",
"#electron-forge/maker-rpm": "^6.0.0-beta.67",
"#electron-forge/maker-squirrel": "^6.0.0-beta.67",
"#electron-forge/maker-zip": "^6.0.0-beta.67",
"concurrently": "^7.4.0",
"dotenv": "^16.0.3",
"electron": "^21.1.1",
"eslint": "^8.25.0",
"eslint-config-google": "^0.14.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.3.0",
"eslint-plugin-promise": "^6.1.0",
"eslint-plugin-react": "^7.31.10",
"wait-on": "^6.0.1"
},
"config": {
"forge": "./forge.config.js"
}
}
My forge.config.js:
module.exports = {
packagerConfig: {
ignore: [
/coverage.*/,
/node_modules.*/,
/public.*/,
/src.*/,
/\.env|\.gitignore|.*bak|eslint.*|package-lock.*|(?<!build.*)\.txt|(?<!build.*)\.md/
]
},
makers: [
{
name: '#electron-forge/maker-squirrel',
config: {
name: 'bkkgapp'
}
},
{
name: '#electron-forge/maker-zip'
}
]
}

Command line way to generate HTML coverageReport in Jest

In Jest, is there a way from the command line to generate an HTML coverage report if it is not defined in the jest.config.js files?
I only want to generate the HTML report some of the time rather than every time I run Jest. The only way I've been able to generate the HTML report was by changing the config manually.
By default the collectCoverage option for jest is set to false. The easiest way to get an HTML coverage report is to configure jest in either package.json or jest.config.js. You will also need to set the coverageDirectory in the config as well.
There are several different configuration options for coverage reporting. See here for all config settings: https://jestjs.io/docs/en/configuration.html#collectcoverage-boolean
Here is an example of how to configure jest in package.json with a few options.
{
"name": "appname",
"version": "1.0.0",
"description": "description",
"main": "index.js",
"scripts": {
"test": "jest",
"postinstall": "jspm install"
},
"jest": {
"scriptPreprocessor": "./preprocessor.js",
"testPathIgnorePatterns": [
"/node_modules/",
],
"unmockedModulePathPatterns": [
"./node_modules/react"
],
"collectCoverage": true,
"coverageDirectory": "path/to/coverage/reports",
},
"author": "author",
"license": "ISC",
"dependencies": {
"del": "^1.1.1",
"gulp": "^3.8.11",
"gulp-filter": "^2.0.2",
"gulp-load-plugins": "^0.10.0",
"gulp-react": "^3.0.1",
"gulp-shell": "^0.4.1",
"harmonize": "^1.4.1",
"jest-cli": "^0.4.1",
"jspm": "^0.15.5",
"react": "^0.13.2",
"react-tools": "^0.13.2",
"run-sequence": "^1.1.0"
},
"devDependencies": {
"browser-sync": "^2.7.1",
"gulp": "^3.8.11"
},
"jspm": {
"directories": {},
"dependencies": {
"react": "npm:react#^0.13.2"
},
"devDependencies": {
"babel": "npm:babel-core#^5.1.13",
"babel-runtime": "npm:babel-runtime#^5.1.13",
"core-js": "npm:core-js#^0.9.4"
}
}
}
Now when you run your test with jest --coverage the HTML reports will be generated in the specified directory.
Have you tried to pass in a JSON config via the command line?
npx jest ./src/**/*.js --coverage --config='{ "coverageReporters": ["html"] }'
Here is a portion from my test scripts in package.json that may help you generate coverage reports:
"test": "react-scripts test --env=jsdom",
"test:coverage": "react-scripts test --coverage"
My setup uses jest#23.6.0 with enzyme#3.8.0 and jest-enzyme#7.0.1

Jest test are failing

Below is issue am getting when I try to run the jest command.
jest --config jest.config.json
FAIL test\Home.spec.js
? Test suite failed to run
SyntaxError: Unexpected reserved word at transformAndBuildScript(node_modules\jest\node_modules\jest-cli\node_modules\jest-runtime\build\transform.js:320:12)
Below is my configuration. Package.json with webpack, jest versions
{
"name": "testjest",
"version": "1.0.0",
"description": "Test React and Redux using Jest",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "webpack-dev-server --content-base --inline --hot",
"test": "jest --config jest.config.json"
},
"dependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-redux": "^5.0.1",
"redux": "^3.6.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^21.2.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"core-js": "^2.4.0",
"enzyme": "^2.7.0",
"jest": "^18.1.0",
"json-stable-stringify": "^1.0.1",
"react-addons-test-utils": "^15.4.1",
"react-test-renderer": "^15.4.1",
"redux-mock-store": "^1.2.1",
"regenerator-runtime": "^0.11.0",
"regenerator-transform": "^0.10.0",
"identity-obj-proxy": "3.0.0",
"webpack": "^1.14.0",
"webpack-dev-server": "^1.16.2"
}
}
Here is my jest json config file.
{
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": ["node_modules", "bower_components", "shared"],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
"testPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/legacy_notes_and_workbook/",
"<rootDir>/src/stylesheets/"
],
"transform": {},
"transformIgnorePatterns": [ "<roodDir>/node_modules/(?!)" , "node_modules/(?!(jest-)?react-native|react-navigation)" ],
"testRegex": "(/__tests__/.*|\\.?(test|spec|Test|Spec))\\.(ts|tsx|js|jsx)$"
}
Node versions installed on my machine are:
node -v
v4.2.2
npm -v
1.4.28
Please let me know if anybody has any solution on above issue. Thanks in advance.

Nested addon CSS not merged into vendor.css

I have an addon, my-addon-parent, that depends on a nested addon, my-addon-nested, that specified via the the dependencies property in my-addon-parent's package.json.
When I run the dummy app for my-addon-parent, the nested addon's CSS is imported correctly (I use the included hook to specify the import) and added to the vendor.css file.
However, when including my-addon-parent in an Ember app, the nested addons CSS is not added to the vendor.css file. Is there a hook/setting that I am missing? Below are the package.json for the app and the respective index.js files for the parent and nested addons.
Ember App package.json
{
"name": "my-app",
"version": "0.0.0",
"description": "My App.",
"private": true,
"directories": {
"doc": "doc",
"test": "tests"
},
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember test"
},
"repository": "",
"engines": {
"node": ">= 0.10.0"
},
"author": "",
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.0",
"connect-restreamer": "^1.0.1",
"ember-cli": "0.2.0",
"ember-cli-app-version": "0.3.2",
"ember-cli-babel": "^4.0.0",
"ember-cli-content-security-policy": "0.3.0",
"ember-cli-dependency-checker": "0.0.8",
"ember-cli-htmlbars": "0.7.4",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-qunit": "0.3.9",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.15",
"ember-export-application-global": "^1.0.2",
"express": "^4.12.3",
"glob": "^4.5.3",
"morgan": "^1.5.2",
"my-addon-parent": "some/location"
}
}
my-addon-parent index.js
/* jshint node: true */
'use strict';
module.exports = {
name: 'my-addon-parent',
included: function (app) {
this._super.included(app);
app.import('vendor/my-addon-parent/my-addon-parent.css');
}
};
my-addon-nested index.js
/* jshint node: true */
'use strict';
module.exports = {
name: 'my-addon-nested',
included: function (app) {
this._super.included(app);
app.import('vendor/my-addon-nested/my-addon-nested.css');
}
};
This is currently not possible. Details here.