简体   繁体   English

browser-sync:脚本不会从父目录加载

[英]browser-sync : scripts won't load from parent dir

I'm trying to get browser-sync to work in my project instead of using live-server 我正在尝试让浏览器同步在我的项目中工作而不是使用live-server

The app structure looks like this: 应用程序结构如下所示:

personal-app
├── node_modules
└── src
    ├── app
    │   └── app.ts
    └── index.html  

in index.html the scripts are loaded from node_modules dir index.html中 ,脚本从node_modules dir加载

<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>

when I use live-server , it loads the scripts and works fine, but since I moved to browser-sync, the scripts don't load. 当我使用live-server时 ,它会加载脚本并且工作正常,但是由于我转移到浏览器同步,脚本不会加载。 here is the code in gulpfile.js : 这是gulpfile.js中的代码:

gulp.task('serve', function() {
    browserSync.init({
        server: {
            baseDir: "./src"
        }
    });
    gulp.watch(sassfiles, ['sass']);
    gulp.watch(htmlfiles).on('change', browserSync.reload);
});

when I give the src path as base dir baseDir: "./src" the site works but the scripts don't load 当我将src路径作为基本目录baseDir: "./src"该站点可以工作,但脚本不加载

在此输入图像描述

but when I change it to baseDir: "./" 但是当我把它baseDir: "./"

The site gives me Cannot GET / , however when I add "/src" to the url in chrome like so " http://localhost:3000/src ", it works. 该网站给了我不能GET / ,但是当我将“/ src”添加到chrome中的url时,如“ http:// localhost:3000 / src ”,它可以工作。

so the problem here is that browser-sync won't load the scripts from the parent folder of its baseDir . 所以这里的问题是浏览器同步不会从其baseDir的父文件夹加载脚本。

I need a way to configure browser-sync to launch from its baseDir : "./" but load the main page index.html from sub folder. 我需要一种方法来配置浏览器同步以从其baseDir : "./"启动baseDir : "./"但是从子文件夹加载主页index.html

is this possible with browser-sync? 浏览器同步可能吗?

From browser-sync documentation 来自浏览器同步文档

baseDir can take multiple paths. baseDir可以采用多个路径。 I added the src folder where index.html exists. 我添加了index.html存在的src文件夹。

gulp.task('serve', function() {
    browserSync.init({
        server: {
            baseDir: ["./", "./src"]   //added multiple directories 
        }
    });
    gulp.watch(sassfiles, ['sass']);
    gulp.watch(htmlfiles).on('change', browserSync.reload);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM