sass import node_modules script - npm

I'm new to gulp, susy, sass, and have been looking for a solution to my following problem but haven't been successful in finding one.
#import 'susy';
#import 'breakpoint-sass';
does not work, but
#import "../node_modules/modularscale-sass/stylesheets/_modular-scale.scss";
#import "../node_modules/breakpoint-sass/stylesheets/_breakpoint.scss";
works. But it's not the right way isn't it?
I've also tried, sudo npm install breakpoint-sass --save-dev but still didn't work.
My project looks like
.
├── assets
│   ├── atoms
│   │   └── _test.scss
│   └── style.scss
├── dist
│   └── style.css
├── gulpfile.js
└── package.json
and this is my gulp file:
const gulp = require('gulp'),
browserSync = require('browser-sync'),
reload = browserSync.reload,
autoprefixer = require('autoprefixer'),
sass = require('gulp-sass')
imagemin = require('gulp-imagemin'),
uglify = require('gulp-uglify'),
plumber = require('gulp-plumber');
gulp.task('sass', function() {
return gulp.src('./assets/*.scss')
.pipe(sass({
outputStyle: 'compressed',
includePaths: ['node_modules/susy/sass']
}).on('error', sass.logError))
.pipe(gulp.dest('./dist'));
});
and this is my package.json:
{
"private": true,
"engines": {
"node": ">=4"
},
"devDependencies": {
"autoprefixer": "^6.3.6",
"breakpoint-sass": "^2.7.0",
"browser-sync": "^2.2.1",
"gulp": "^3.9.1",
"gulp-imagemin": "^3.0.1",
"gulp-plumber": "^1.0.1",
"gulp-sass": "^2.1.1",
"gulp-uglify": "^1.1.0",
"modularscale-sass": "^2.1.1",
"susy": "^2.2.12"
}
}
I hope you could help me out.
Thanks for your time

You just need to add the paths to the includePaths parameter in your sass task.
gulp.task('sass', function() {
return gulp.src('./assets/*.scss')
.pipe(sass({
outputStyle: 'compressed',
includePaths: [
'node_modules/susy/sass',
'node_modules/breakpoint-sass/stylesheets'
]
}).on('error', sass.logError))
.pipe(gulp.dest('./dist'));
});
In fact you had that already for susy just not breakpoint-sass. You can't import breakpoint-sass either, it has to be just breakpoint:
#import 'susy';
#import 'breakpoint';
That's because you want to import the file called node_modules/breakpoint-sass/stylesheets/_breakpoint.scss. Your import would try and find node_modules/breakpoint-sass/stylesheets/_breakpoint-sass.scss which doesn't exist.
For reference, the steps for installing and using a SASS library are:
Install the library with npm.
Find the library in node_modules/. Expand it, and find the file you want to #import. In this case it's _breakpoint.scss.
Add the path to the directory the file from step 3 lives in to includePaths.
Add the import to your own SASS source.
Or you can just keep the full #import in your code if you want. Or just include node_modules and then import e.g. susy/sass/susy and breakpoint-sass/stylesheets/breakpoint. It's up to you really.

Related

When using npm workspaces, how to force a package to be installed in the relative node_modules?

Here are some related questions:
When using yarn workspaces, how to force a package to be installed in the relative node_modules?
NPM 7 Workspaces - Multiple node_modules?
Should I have to use no-hoist for all packages in a monorepo with react-native-web?
I'm using npm workspaces to organise multiple packages. The issue is that my main package don't have its dependency's(which is also one of the workspaces) source code in local node_modules. I know the dependency is installed in root node_modules, the thing is that I need to visit it by relative path from the main package.
Here is the project structure after running npm install in root dir:
root
├── package.json -> { "workspaces": ["packages/*"] }
├── node_modules
│ ├── dependency-A
│ ├── dependency-B
└── packages
├── main-package
├── dependency-A
└── dependency-B
package.json in root dir:
{
"workspaces": [
"packages/main-package",
"packages/dependency-A",
"packages/dependency-B"
]
}
package.json in "packages/main-package":
{
"dependencies": {
"dependency-A": "0.1.0",
"dependency-B": "0.1.0"
}
}
webpack.config.js in "packages/main-package":
{
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: "node_modules/dependency-A/media",
to: "static/dependency-A-media",
},
],
}),
new CopyWebpackPlugin({
patterns: [
{
context: "node_modules/dependency-B/dist",
from: "research-data.json",
},
],
}),
]
}
When I run webapck in main-package, the error message is:
ERROR in unable to locate '/Users/trumangao/myApp/packages/main-package/node_modules/dependency-A/media' glob
ERROR in unable to locate '/Users/trumangao/myApp/packages/main-package/node_modules/dependency-B/dist/research-data.json' glob
I'm wondering what is the best practice to resolve such question? The option "nohoist" of Yarn inspired me but I can't find it in npm. I also tried to run install in package dir but it will break their symlink. How could I install dependencies of each package in their local node_modules while maintain their links like lerna#4?
Tried versions:
node.js#16.13.0
npm#8.1.0
&
node.js#18.14.0
npm#9.3.1
Hope I've made myself plain with my poor English LoL, thanks a lot.

ESLint Vue multiword components

Is there a way to stop getting error from ESLint for single word view name in Vue3?
Every time I run ESLint, I get following message:
1:1 error Component name "About" should always be multi-word vue/multi-word-component-names
I currently have this setup:
file structure:
├── index.html
├── node_modules
├── npm
├── package.json
├── package-lock.json
├── public
│   └── favicon.ico
├── README.md
├── src
│   ├── App.vue
│   ├── assets
│   │   └── logo.svg
│   ├── components
│   │   └── Menu.vue
│   ├── env.d.ts
│   ├── main.ts
│   ├── router
│   │   └── index.ts
│   └── views
│   ├── About.vue
│   └── Home.vue
├── tsconfig.json
└── vite.config.ts
.eslintrc:
{
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended",
"#vue/typescript/recommended"
],
"parserOptions": {
"ecmaVersion": 2021
},
"rules": {}
}
package.json
{
...
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview",
"lint": "eslint --ext .ts,vue --ignore-path .gitignore ."
},
...
}
Option 1: Disable globally
To disable the rule in all files (even those in src/components):
// <projectRoot>/.eslintrc.js
module.exports = {
⋮
rules: {
'vue/multi-word-component-names': 0,
},
}
Option 2: overrides in ESLint config for src/views/
To disable the rule only for src/views/**/*.vue, specify an overrides config:
// <projectRoot>/.eslintrc.js
module.exports = {
⋮
overrides: [
{
files: ['src/views/**/*.vue'],
rules: {
'vue/multi-word-component-names': 0,
},
},
],
}
Note: If using VS Code with the ESLint Extension, restarting the ESLint Server (through Command Palette's >ESLint: Restart ESLint Server command) or restarting the IDE might be needed to reload the configuration.
Option 3: Directory-level config for src/views/
It's also possible to disable the rule for src/views/**/*.vue with an .eslintrc.js file in that directory:
// <projectRoot>/src/views/.eslintrc.js
module.exports = {
rules: {
'vue/multi-word-component-names': 0,
},
}
For those still having this issue, add the following under rules in the .eslintrc.js file
rules: {
...
'vue/multi-word-component-names': 0,
}
There's a simple solution. You need define your component name with more than one word as it states. Should be PascalCase as below;
eg: AboutPage.vue
Find a vue.config.js file in the project root directory, create one in the root directory if you don’t have one, write the code marked below, save it, and recompile it. The project will run normally
Change your component name from About to AboutView
This prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.
Bad
<!-- in pre-compiled templates -->
<Item />
<!-- in in-DOM templates -->
<item></item>
Good
<!-- in pre-compiled templates -->
<TodoItem />
<!-- in in-DOM templates -->
<todo-item></todo-item>

Go to definition not working on my project (vue & sass file) [visual-studio-code]

I am disappointed on two points by developing a Nuxt project on vscode.
On vscode my jsconfig.js is the default one :
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./*"],
"#/*": ["./*"],
"~~/*": ["./*"],
"##/*": ["./*"]
}
},
"exclude": ["node_modules", ".nuxt", "dist"]
}
It's working on vue file for autocompletion to import some components for example (with ctrl+space)
But impossible to go to definition next with cmd+click. I do not understand why and this is really annoying.
I can't post image (need 10 reputation), but here is my import on vue file (with no definition found for ...)
import PldFooter from '#/components/Footer';
Other point, I use sass files on assests folder. Compilation working well but I cannot access by cmd+click to the file from node_modules. Here is an example of import :
#import "~bulma/sass/base/helpers.sass";
==> No definition found for helpers.sass
Thank you for your help,
Ben.
Have you opened multiple folders (projects) in a window?
I'm not sure whether your issue the same as me. I got an issue "Go to Definition not working" in Visual Studio Code when I opened multiples folders (projects) and I resolved.
I have used the plugin Vetur to support the .vue file.
There are 2 ways which work well:
Open only one project in a window
You can open multiple projects in a window but the project you want to "Go to Definition" works well which must be the first project in the folder tree in the EXPLORER tab.
It seems the plugin Vetur picks up the first project in multiple projects to be the root folder.
My file tsconfig.json
{
...
"compilerOptions": {
"baseUrl": "./",
"paths": {
"#/*": ["src/*"],
}
},
"exclude": ["node_modules", "dist"]
...
}
Reference:
https://github.com/vuejs/vetur/issues/423#issuecomment-405415204
I apologize if my answer which cannot help you.
According to the Vetur setup guide:
If you are using Webpack's alias or TypeScript's path mapping to resolve components, you need to update Vetur's tsconfig.json or jsconfig.json
For example:
└── src
├── components
│ ├── a.vue
│ └── b.vue
├── containers
│ └── index.vue
├── index.js
└── jsconfig.json
jsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"components/*": [
"src/components/*"
]
}
}
}
index.vue
import a from 'components/a.vue'
import b from 'components/b.vue'
It solved the problem in my case.

Vue CLI Child compilation failed after first build unless I clear .cache folder

The title of the question says it all really.
I'm using Vue CLI and the project compiles fine the first time. But if I try to build it again it fails with this error:
Error: Child compilation failed:
Entry module not found: Error: Can't resolve 'C:\Users\Oliver\Workspace\academy-residences\public\index.html' in 'C:\Users\Oliver\Workspace\academy-residences':
Error: Can't resolve 'C:\Users\Oliver\Workspace\academy-residences\public\index.html' in 'C:\Users\Oliver\Workspace\academy-residences'
- compiler.js:79 childCompiler.runAsChild
[academy-residences]/[html-webpack-plugin]/lib/compiler.js:79:16
- Compiler.js:300 compile
[academy-residences]/[webpack]/lib/Compiler.js:300:11
- Compiler.js:556 hooks.afterCompile.callAsync.err
[academy-residences]/[webpack]/lib/Compiler.js:556:14
- Hook.js:154 AsyncSeriesHook.lazyCompileHook
[academy-residences]/[tapable]/lib/Hook.js:154:20
- Compiler.js:553 compilation.seal.err
[academy-residences]/[webpack]/lib/Compiler.js:553:30
- Hook.js:154 AsyncSeriesHook.lazyCompileHook
[academy-residences]/[tapable]/lib/Hook.js:154:20
- Compilation.js:1323 hooks.optimizeAssets.callAsync.err
[academy-residences]/[webpack]/lib/Compilation.js:1323:35
- Hook.js:154 AsyncSeriesHook.lazyCompileHook
[academy-residences]/[tapable]/lib/Hook.js:154:20
- Compilation.js:1314 hooks.optimizeChunkAssets.callAsync.err
[academy-residences]/[webpack]/lib/Compilation.js:1314:32
- Hook.js:154 AsyncSeriesHook.lazyCompileHook
[academy-residences]/[tapable]/lib/Hook.js:154:20
- Compilation.js:1309 hooks.additionalAssets.callAsync.err
[academy-residences]/[webpack]/lib/Compilation.js:1309:36
[copy-webpack-plugin] unable to locate 'C:\Users\Oliver\Workspace\academy-residences\public' at 'C:\Users\Oliver\Workspace\academy-residences\public'
ERROR Build failed with errors.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! academy-residences#0.1.0 watch: `vue-cli-service build --watch`
npm ERR! Exit status 1
npm ERR!
However, if I delete the .cache folder in node_modules then it builds correctly again!
My package.json looks like this:
{
"name": "academy-residences",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"watch": "vue-cli-service build --watch",
"test:unit": "vue-cli-service test:unit",
"heroku-postbuild": "vue-cli-service build"
},
"dependencies": {
"axios": "^0.18.0",
"element-ui": "^2.4.5",
"moment": "^2.24.0",
"vue": "^2.5.21",
"vue-router": "^3.0.1",
"vuex": "^3.0.1"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.3.0",
"#vue/cli-plugin-unit-jest": "^3.3.0",
"#vue/cli-service": "^3.3.0",
"#vue/test-utils": "^1.0.0-beta.20",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"node-sass": "^4.9.2",
"sass-loader": "^7.0.3",
"vue-cli-plugin-element": "^1.0.1",
"vue-template-compiler": "^2.5.21"
}
}
and here is my vue.config
module.exports = {
outputDir: './public',
publicPath: process.env.NODE_ENV === 'production' ? '' : '',
configureWebpack: {
resolve: {
alias: {
'#': __dirname + '/ui/src'
}
},
entry: {
app: './ui/src/main.js'
}
}
};
package.json sits in the root folder of the project with all the vue source code in ui
root/
ui/
src/
node_modules/
package.json
This is a recent problem. The other day everything was working fine. The only things that I think could have caused this is deleting #vue/cli and reinstalling it.
This is a recent problem. The other day everything was working fine.
The only things that I think could have caused this is deleting
#vue/cli and reinstalling it.
Well, you're right about this. Until mid-2018 I used to have a folder structure like
project
├── config
├── src // components, router, etc
├── static // images, robots.txt manifest, vendor css and whatnot
├── dist // not under version control, generated at deploy time
└── index.html // entry point
When building the project, the dist folder was wiped clean, then the chunks coming from src where compiled into it, then the static files under static copied over to dist (This one might happen before compiling the src files, whatever).
My config looked like:
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
]),
Where config.build.assetsSubDirectory was dist.
Fast forward to current #vue/cli (as of March 2019), the template now uses webpack#4 instead of webpack#3, and some defaults have changed. Specifically, they expect the structure to be:
project
├── config
├── src
├── public // <── WHAIT!!! NO LONGER CALLED "static"
| └── index.html // <── NO LONGER IN ROOT
└── dist
So in your config, declaring
module.exports = {
outputDir: './public',
...
}
Means you're wiping public at buildtime, then webpack cannot copy your public assets from its default asset source to the output folder, which became the same.
Now, if you inspect what is webpack actually doing (from your project root folder)
vuw inspect > output.js
or
$(npm bin)/vue-cli-service > output.js
You will see output.js has a block in the likes of:
/* config.plugin('copy') */
new CopyWebpackPlugin(
[
{
from: '/home/user/project/public',
to: '/home/user/project/dist',
toType: 'dir',
ignore: [
'.DS_Store'
]
}
]
),
If I changed my vue.config.js to have, as you have
outputDir: "./public",
The latter command would output a section saying:
/* config.plugin('copy') */
new CopyWebpackPlugin(
[
{
from: '/home/user/project/public',
to: '/home/user/project/public',
toType: 'dir',
ignore: [
'.DS_Store'
]
}
]
),
Which obviously make no sense whatsoever
TL/DR;
Don't use public as output folder, or find a way to change the static assets folder, for which I believe you have to manually configure CopyWebpackPlugin in your vue config file.

Visual Code Studio - Typescript 2.0 does not resolve/compile path based modules (shows red underline)

I am using VSC 1.4 for my angular 2 project. I have a very simple structure as shown below and trying to setup path mapping based module resolution as mentioned here : https://github.com/Microsoft/TypeScript/issues/5039
projectRoot
├── src
│ ├── models
│ │ ├── modelA.ts
│ ├── app
│ └── modelB.ts (import {modelA} from 'src/models/modelA';)
│
└── tsconfig.json
And In my tsconfig.json I have the baseURL set to "."
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"noEmitHelpers": true,
"baseUrl": "."
}
But the vsc still shows a red line under the import file saying
[ts] cannot find module 'src/models/modelA'
In my package.json I have typescript 2.
"ts-helpers": "^1.1.1",
"ts-loader": "^0.8.2",
"ts-node": "^1.2.2",
"tslint": "^3.14.0",
"tslint-loader": "^2.1.5",
"typedoc": "^0.4.4",
"typescript": "^2.0.0",
"typings": "^1.3.2",
Can anyone tell me if I am missing something or something is not setup properly?
BTW I am using webpack. My webpack can compile it and works as expected but it is annoying to see the red line and it does not hepl in intelli-sense feature of VSCode.